From 0ea222c168a87e69cc476901923c584e7c4694e5 Mon Sep 17 00:00:00 2001 From: Spatterjaaay Date: Thu, 9 Feb 2017 23:26:49 -0800 Subject: [PATCH 1/2] Create Calculator.rb --- Calculator.rb | 76 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 Calculator.rb diff --git a/Calculator.rb b/Calculator.rb new file mode 100644 index 0000000..e95613c --- /dev/null +++ b/Calculator.rb @@ -0,0 +1,76 @@ +# checks whether user wrote correct operation +def math() + operations = ["add", "+", "subtract", "-", "multiply", "*", "divide", "/", "raise", "^", "modulo", "%"] + symbol = gets.chomp + until operations.include?(symbol) + print "That's not a valid entry, please try again: " + symbol = gets.chomp + end + return symbol +end + +# beginning of the code +print "What kind of math would you like to perform? " +command = math() + +# checks whether user wrote a number +def read_number(prompt) + error_msg_1 = "Uh oh, invalid value. Please try again." + number = nil + while number == nil + print prompt + user_input = gets.chomp + if user_input.include?(".") + # Integer() will convert input to a number or it will throw an error + # that will be caught by the rescue clause if input is not a number + # rescue prints error message and continues execution of the code + number = Float(user_input) rescue puts(error_msg_1) + else + number = Integer(user_input) rescue puts(error_msg_1) + end + end + return number +end + +first_number = read_number("What is the first number? ") + +second_number = read_number("What is the second number? ") + +# performs calculation based on user's input +if command == "add" || command == "+" + result = first_number + second_number + command = "+" +elsif command == "substract" || command == "-" + result = first_number - second_number + command = "-" +elsif command == "multiply" || command == "*" + result = first_number * second_number + command = "*" +elsif command == "divide" || command == "/" + # catches the division by 0 that throws an error + # begin contains code that potentially may throw an error + begin + result = first_number / second_number + # rescue catches and handles error + # prints a message + rescue + puts("Oh no. We cannot divide by 0.") + # terminates the program + exit + end +elsif command == "raise" || command == "^" + result = first_number ** second_number + command = "^" +elsif command == "modulo" || command == "%" + command = "%" + begin + result = first_number % second_number + rescue + puts("Oh no. We cannot divide by 0.") + exit + end + command = "%" +end + +# prints the result based on the user input +puts "#{first_number} #{command} #{second_number} = #{result}" From 1ed443343b66749689aa6159e7ef9149feb4f687 Mon Sep 17 00:00:00 2001 From: Spatterjaaay Date: Thu, 9 Feb 2017 23:48:53 -0800 Subject: [PATCH 2/2] Changed if statements to case/when --- Calculator.rb | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/Calculator.rb b/Calculator.rb index e95613c..e66e7a8 100644 --- a/Calculator.rb +++ b/Calculator.rb @@ -37,16 +37,17 @@ def read_number(prompt) second_number = read_number("What is the second number? ") # performs calculation based on user's input -if command == "add" || command == "+" +case command +when "add", "+" result = first_number + second_number command = "+" -elsif command == "substract" || command == "-" +when "substract", "-" result = first_number - second_number command = "-" -elsif command == "multiply" || command == "*" +when "multiply", "*" result = first_number * second_number command = "*" -elsif command == "divide" || command == "/" +when "divide", "/" # catches the division by 0 that throws an error # begin contains code that potentially may throw an error begin @@ -58,10 +59,10 @@ def read_number(prompt) # terminates the program exit end -elsif command == "raise" || command == "^" +when "raise", "^" result = first_number ** second_number command = "^" -elsif command == "modulo" || command == "%" +when "modulo", "%" command = "%" begin result = first_number % second_number