Skip to content

Instantly share code, notes, and snippets.

@luizfonseca
Last active July 11, 2017 16:05
Show Gist options
  • Save luizfonseca/50672533a94be757db465bf89c9fcce4 to your computer and use it in GitHub Desktop.
Save luizfonseca/50672533a94be757db465bf89c9fcce4 to your computer and use it in GitHub Desktop.
[Reboot] Calculator

Pseudo code steps

1 - print "Welcome to the calculator"?

2 - Get first number from the user

3 - Get second number from the user

4 - Ask which operation the user wants to perform

5 - Show the results

# Calculate based on user input
def calculate(number1, number2, operation)
result = case operation
when "-" then number1 - number2
when "+" then number1 + number2
when "*" then number1 * number2
when "/" then number1 / number2
else
result = "not valid"
end
return result
end
require_relative "calculator.rb"
other_operation = "y"
until other_operation == "n"
puts "This is a calculator. Input 2 numbers"
puts "1) Insert the first number"
number1 = gets.chomp.to_f
puts "2) Insert the second number"
number2 = gets.chomp.to_f
# # Assert + - / *
puts "3) What kind of operation? \nChoose one: + - / *"
operation = gets.chomp # Getting input from the user
# Showing what the user inputed
result = calculate(number1, number2, operation)
puts "Oh, the result is #{result}"
puts "Want to do another one? Type (n/y)"
other_operation = gets.chomp
# You can use REGEX! We are expecting N OR Y (the PIPE | is for saying OR)
#unless other_operation == "y" or other_operation == "n"
# ANY LETTER BUT N OR Y we break
unless other_operation =~ /^n|y$/
puts "This is not an option"
break
end
end
# if condition == "-"
# elsif condition == "+"
# elsif condition == "*"
# elsif condition == "/"
# else
# end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment