From 7430f00b11e082e5a2c05cb8c1603c44024f2f26 Mon Sep 17 00:00:00 2001 From: Angela Date: Wed, 5 Feb 2020 16:30:33 -0800 Subject: [PATCH 1/3] Create calculator.rb --- calculator.rb | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 calculator.rb diff --git a/calculator.rb b/calculator.rb new file mode 100644 index 0000000..72e188c --- /dev/null +++ b/calculator.rb @@ -0,0 +1,57 @@ +# CALCULATOR +# Program accepts user input to perform calculator evaluations. + +# TODO: +# -Write the actual evaluation function +# -refactor validate code + + +operators = { + :addition => ["+", "plus", "add"], + :subtraction => ["-", "minus", "subtract"], + :multiplication => ["*", "times","multiply"], + :division => ["/", "divide","split"], + :mod => ["%", "mod","modulo"], + :exponentialize => ["^", "to the power of","exponential"] +} + + +puts "CALCULATOR" +puts "**********\n\n" + +puts "Welcome to the CALCULATOR. Available operators include:\n\n" + +#pretty-print the list of valid operators +operators.each do |k,v| + puts "#{k}: #{v.join(", ")}" +end + + +equation = { + "operator" => nil, + "left number" => nil, + "right number" => nil, +} + +equation.each do |equation_piece, value| + print "Please give me the #{equation_piece.to_s} > " + input = gets.strip + + if equation_piece == "operator" + until operators.all? {|operation| operation.has} #TO-DO: figure out how to check input against all data in the operator array to find match + operators.each do |operator, value| + puts "\n" + puts input.class + puts operator.class + puts value + puts value.include? input + + end + + + end +end + +puts "\n\nEvaluating the following: #{equation["left number"]} #{equation["operator"]} #{equation["right number"]}" + + From fff8becb43420bde45aeae3a60cce24d46f1c103 Mon Sep 17 00:00:00 2001 From: Angela Date: Thu, 6 Feb 2020 13:15:56 -0800 Subject: [PATCH 2/3] Update calculator.rb --- calculator.rb | 71 +++++++++++++++++++++++++++++---------------------- 1 file changed, 41 insertions(+), 30 deletions(-) diff --git a/calculator.rb b/calculator.rb index 72e188c..f4f5988 100644 --- a/calculator.rb +++ b/calculator.rb @@ -1,31 +1,43 @@ # CALCULATOR # Program accepts user input to perform calculator evaluations. -# TODO: -# -Write the actual evaluation function -# -refactor validate code - - operators = { - :addition => ["+", "plus", "add"], - :subtraction => ["-", "minus", "subtract"], - :multiplication => ["*", "times","multiply"], - :division => ["/", "divide","split"], + :addition => ["+", "plus", "add", "addition"], + :subtraction => ["-", "minus", "subtract", "subtraction"], + :multiplication => ["*", "times","multiply", "multiplication"], + :division => ["/", "divide","split","division"], :mod => ["%", "mod","modulo"], - :exponentialize => ["^", "to the power of","exponential"] + :exponentialize => ["^", "to the power of","exponential","exponentialize"] } +def calculate(equation, operators) + case equation["operator"] + when *(operators[:addition]) + puts "Answer: #{equation["left number"].to_f.round(2) + equation["right number"].to_f.round(2)}" + when *(operators[:subtraction]) + puts "Answer: #{equation["left number"].to_f.round(2) - equation["right number"].to_f.round(2)}" + when *(operators[:multiplication]) + puts "Answer: #{equation["left number"].to_f.round(2) * equation["right number"].to_f.round(2)}" + when *(operators[:division]) + puts "Answer: #{equation["left number"].to_f.round(2) / equation["right number"].to_f.round(2)}" + when *(operators[:mod]) + puts "Answer: #{equation["left number"].to_f.round(2) % equation["right number"].to_f.round(2)}" + when *(operators[:exponentialize]) + puts "Answer: #{equation["left number"].to_f.round(2) ** equation["right number"].to_f.round(2)}" + else + puts "Nothing to evaluate." + end +end puts "CALCULATOR" puts "**********\n\n" - puts "Welcome to the CALCULATOR. Available operators include:\n\n" -#pretty-print the list of valid operators operators.each do |k,v| puts "#{k}: #{v.join(", ")}" end +puts "********\n\n" equation = { "operator" => nil, @@ -33,25 +45,24 @@ "right number" => nil, } -equation.each do |equation_piece, value| - print "Please give me the #{equation_piece.to_s} > " - input = gets.strip - - if equation_piece == "operator" - until operators.all? {|operation| operation.has} #TO-DO: figure out how to check input against all data in the operator array to find match - operators.each do |operator, value| - puts "\n" - puts input.class - puts operator.class - puts value - puts value.include? input - +equation.each do |segment, value| #populate each part of the equation with user input, validating as you go + print "Please give me the #{segment.to_s} > " + input = gets.strip.to_s + + if segment == "operator" + until operators.any? { |keys, values| values.include?(input)} + print "Invalid operator. Try again. > " + input = gets.strip.to_s + end + else + until input.to_f.to_s == input || input.to_f.round(2).to_s == input || '%.2f' % input.to_f.to_s == input + print "'#{input}' (#{input.class}) is not a valid number. Please try again. > " + input = gets.strip + end end + equation[segment] = input - - end end -puts "\n\nEvaluating the following: #{equation["left number"]} #{equation["operator"]} #{equation["right number"]}" - - +puts "\n\nEvaluating the following input: #{equation["left number"]} #{equation["operator"]} #{equation["right number"]}" +puts calculate(equation, operators) From ff6a1d144349c42a9c225e37158d32765ddfdd6c Mon Sep 17 00:00:00 2001 From: Angela Date: Thu, 6 Feb 2020 13:33:33 -0800 Subject: [PATCH 3/3] Update calculator.rb Fixed some wonky .to_s/.to_f/.to_i bugs --- calculator.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/calculator.rb b/calculator.rb index f4f5988..b7894f3 100644 --- a/calculator.rb +++ b/calculator.rb @@ -47,16 +47,16 @@ def calculate(equation, operators) equation.each do |segment, value| #populate each part of the equation with user input, validating as you go print "Please give me the #{segment.to_s} > " - input = gets.strip.to_s + input = gets.strip if segment == "operator" until operators.any? { |keys, values| values.include?(input)} print "Invalid operator. Try again. > " - input = gets.strip.to_s + input = gets.strip end else - until input.to_f.to_s == input || input.to_f.round(2).to_s == input || '%.2f' % input.to_f.to_s == input - print "'#{input}' (#{input.class}) is not a valid number. Please try again. > " + until input.to_f.to_s == input || input.to_i.to_s == input || '%.2f' % input.to_f.to_s == input + puts "Whoops, '#{input}' (#{input.class}) is not a valid number. Please try again." input = gets.strip end end