From 358dea14c3d0f4527bf720db48bda0ba085f0c40 Mon Sep 17 00:00:00 2001 From: ricecakemonster Date: Fri, 10 Feb 2017 09:01:58 -0800 Subject: [PATCH 1/3] Create calculator.rb --- calculator.rb | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 calculator.rb diff --git a/calculator.rb b/calculator.rb new file mode 100644 index 0000000..2aba23a --- /dev/null +++ b/calculator.rb @@ -0,0 +1,41 @@ +print "Enter an operation to use: " +operation = gets.chomp + until ["+","-","*","/","add", "subtract", "multiply", "divide"].include? (operation) + print "Re-Enter an operation to use: " + operation = gets.chomp + end + +print "Enter the first number: " +f_num = gets.chomp +until f_num.to_s == f_num.to_i.to_s + print "Re-Enter the first number: " + f_num = gets.chomp +end + +print "Enter the second number: " +s_num = gets.chomp +until s_num.to_s == s_num.to_i.to_s + print "Re-Enter the second number: " + s_num = gets.chomp +end + +f_num = f_num.to_f +s_num = s_num.to_f + +case operation +when "add", "+" + result = f_num + s_num +when "subtract", "-" + result = f_num - s_num +when "multiply", "*" + result = f_num * s_num +when "divide", "/" + until s_num != 0 + print "Enter the second number again: " + s_num = gets.chomp.to_i + end + result = f_num / s_num + +end + +puts result From 19ec0fd55f38ccc59ba47ea6741c9cd07ff78523 Mon Sep 17 00:00:00 2001 From: ricecakemonster Date: Fri, 10 Feb 2017 09:24:23 -0800 Subject: [PATCH 2/3] Update calculator.rb --- calculator.rb | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/calculator.rb b/calculator.rb index 2aba23a..13b772f 100644 --- a/calculator.rb +++ b/calculator.rb @@ -5,20 +5,26 @@ operation = gets.chomp end +def is_number?(num) + num == num.to_i.to_s || num == num.to_f.to_s +end + print "Enter the first number: " f_num = gets.chomp -until f_num.to_s == f_num.to_i.to_s +until is_number?(f_num) print "Re-Enter the first number: " f_num = gets.chomp end print "Enter the second number: " s_num = gets.chomp -until s_num.to_s == s_num.to_i.to_s +until is_number?(s_num) print "Re-Enter the second number: " s_num = gets.chomp end +puts f_num +puts s_num f_num = f_num.to_f s_num = s_num.to_f @@ -39,3 +45,4 @@ end puts result + From f00fd2265c39e8911bdc92a69590913e8ffbfbb3 Mon Sep 17 00:00:00 2001 From: ricecakemonster Date: Sat, 11 Feb 2017 16:27:48 -0800 Subject: [PATCH 3/3] Update calculator.rb --- calculator.rb | 98 ++++++++++++++++++++++++++++++--------------------- 1 file changed, 58 insertions(+), 40 deletions(-) diff --git a/calculator.rb b/calculator.rb index 13b772f..bd0952b 100644 --- a/calculator.rb +++ b/calculator.rb @@ -1,48 +1,66 @@ -print "Enter an operation to use: " -operation = gets.chomp - until ["+","-","*","/","add", "subtract", "multiply", "divide"].include? (operation) - print "Re-Enter an operation to use: " - operation = gets.chomp +puts "Let's calculate!" +yes_no = "y" +while yes_no == "y" + print "Enter an operation to use. (Enter N to quit): " + operation = gets.chomp + if operation.downcase == "n" + break + else + until ["+","-","*","/","^","mod","add", "subtract", "multiply", "divide","exponent", "modulo"].include? (operation) + print "Re-Enter an operation to use: " + operation = gets.chomp + end end -def is_number?(num) - num == num.to_i.to_s || num == num.to_f.to_s -end + print "Enter the first number: " + f_num = Float(gets.chomp) rescue nil + until f_num.is_a?(Float) + print "Re-Enter the first number: " + f_num = Float(gets.chomp) rescue nil + end -print "Enter the first number: " -f_num = gets.chomp -until is_number?(f_num) - print "Re-Enter the first number: " - f_num = gets.chomp -end + print "Enter the second number: " + s_num = Float(gets.chomp) rescue nil + until s_num.is_a?(Float) + print "Re-Enter the second number: " + s_num = Float(gets.chomp) rescue nil + end -print "Enter the second number: " -s_num = gets.chomp -until is_number?(s_num) - print "Re-Enter the second number: " - s_num = gets.chomp -end + case operation + when "add", "+" + result = f_num + s_num + when "subtract", "-" + result = f_num - s_num + when "multiply", "*" + result = f_num * s_num + when "divide", "/" + until s_num != 0 + print "Enter the second number again: " + s_num = gets.chomp.to_i + end + result = f_num / s_num + when "exponent", "^" + result = f_num ** s_num + when "modulo", "mod" + result = f_num % s_num + end -puts f_num -puts s_num -f_num = f_num.to_f -s_num = s_num.to_f - -case operation -when "add", "+" - result = f_num + s_num -when "subtract", "-" - result = f_num - s_num -when "multiply", "*" - result = f_num * s_num -when "divide", "/" - until s_num != 0 - print "Enter the second number again: " - s_num = gets.chomp.to_i - end - result = f_num / s_num + #returning integers instead of floats when it's applicable. + def is_integer?(num) + num % num.to_i == 0.0 rescue 0 + end -end + if is_integer?(result) + result = result.to_i + end + + if is_integer?(f_num) + f_num = f_num.to_i + end -puts result + if is_integer?(s_num) + s_num = s_num.to_i + end + puts "#{f_num} #{operation} #{s_num} = #{result}" +end