From 60bf52afebbd8b889274c4db0effa1f01af4ecf4 Mon Sep 17 00:00:00 2001 From: Alexandria Brown Date: Wed, 7 Feb 2018 22:36:12 -0800 Subject: [PATCH 1/2] Ampers: Alex --- alex_calc.rb | 79 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 alex_calc.rb diff --git a/alex_calc.rb b/alex_calc.rb new file mode 100644 index 0000000..95e7e3b --- /dev/null +++ b/alex_calc.rb @@ -0,0 +1,79 @@ +### Calculator program ### + +operators_list = %w(add + subtract - multiply * divide /) + +# methods +def validate_operator(operator, operators_list) + until operators_list.include?(operator) + print "Please enter an operator word or symbol (i.e. add, +, *): " + operator = gets.chomp + end + return operator +end + +def validate_num(num) + while num =~ /[[:alpha:]]/ + print "Please enter a number (i.e. 1, 50, 100): " + num = gets.chomp + end + num = num.to_i + return num +end + +def addition(x, y) + add_answer = x + y + puts "#{x} + #{y} = #{add_answer}" + return add_answer +end + +def subtraction(x, y) + sub_answer = x - y + puts "#{x} - #{y} = #{sub_answer}" + return sub_answer +end + +def multiplication(x, y) + mult_answer = x * y + puts "#{x} * #{y} = #{mult_answer}" + return mult_answer +end + +def division(x, y) + div_answer = x / y + puts "#{x} / #{y} = #{div_answer}" + return div_answer +end + +def operation(operator, first_num, second_num) + case operator + when "add", "+" + return addition(first_num, second_num) + when "subtract", "-" + return subtraction(first_num, second_num) + when "multiply", "*" + return multiplication(first_num, second_num) + when "divide", "/" + if second_num == 0 + puts "Undefined: cannot divide by 0" + else + return division(first_num, second_num) + end + end +end + +# prompts and inputs +puts "This is a simple calculator program...\n" +print "\nEnter an operator: " +operator = gets.chomp +validate_operator(operator, operators_list) + +print "Enter a number: " +first_num = gets.chomp +first_num = validate_num(first_num) + +print "Enter another number: " +second_num = gets.chomp +second_num = validate_num(second_num) + +puts +operation(operator, first_num, second_num) From 83bae76ba6579726744d25fc7ade6cc5a0d6dd89 Mon Sep 17 00:00:00 2001 From: Alexandria Brown Date: Wed, 7 Feb 2018 22:52:42 -0800 Subject: [PATCH 2/2] Ampers: Alex --- alex_calc.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/alex_calc.rb b/alex_calc.rb index 95e7e3b..c79ae98 100644 --- a/alex_calc.rb +++ b/alex_calc.rb @@ -4,7 +4,7 @@ # methods def validate_operator(operator, operators_list) - until operators_list.include?(operator) + while !(operators_list.include?(operator)) print "Please enter an operator word or symbol (i.e. add, +, *): " operator = gets.chomp end @@ -65,7 +65,7 @@ def operation(operator, first_num, second_num) puts "This is a simple calculator program...\n" print "\nEnter an operator: " operator = gets.chomp -validate_operator(operator, operators_list) +operator = validate_operator(operator, operators_list) print "Enter a number: " first_num = gets.chomp