From ee93563380c55ce47921acb1f0144f9778dec622 Mon Sep 17 00:00:00 2001 From: tehut Date: Fri, 10 Feb 2017 07:30:51 -0800 Subject: [PATCH 1/2] Create Queues _Calculator_Tehut.rb --- Queues _Calculator_Tehut.rb | 111 ++++++++++++++++++++++++++++++++++++ 1 file changed, 111 insertions(+) create mode 100644 Queues _Calculator_Tehut.rb diff --git a/Queues _Calculator_Tehut.rb b/Queues _Calculator_Tehut.rb new file mode 100644 index 0000000..1f13720 --- /dev/null +++ b/Queues _Calculator_Tehut.rb @@ -0,0 +1,111 @@ + +puts " +▄▄· ▄▄▄· ▄▄▌ ▄▄· ▄• ▄▌▄▄▌ ▄▄▄· ▄▄▄▄▄ ▄▄▄ +▐█ ▌▪▐█ ▀█ ██• ▐█ ▌▪█▪██▌██• ▐█ ▀█ •██ ▪ ▀▄ █· +██ ▄▄▄█▀▀█ ██▪ ██ ▄▄█▌▐█▌██▪ ▄█▀▀█ ▐█.▪ ▄█▀▄ ▐▀▀▄ +▐███▌▐█ ▪▐▌▐█▌▐▌▐███▌▐█▄█▌▐█▌▐▌▐█ ▪▐▌ ▐█▌·▐█▌.▐▌▐█•█▌ +·▀▀▀ ▀ ▀ .▀▀▀ ·▀▀▀ ▀▀▀ .▀▀▀ ▀ ▀ ▀▀▀ ▀█▄▀▪.▀ ▀ +" +#define methods +def add(number1, number2) + solution = number1.to_f + number2.to_f + return solution +end + +def subtract(number1, number2) + solution = number1.to_f - number2.to_f + return solution +end + +def multiply(number1, number2) + solution = number1.to_f * number2.to_f + return solution +end + +def divide(number1, number2) + solution = number1.to_f / number2.to_f + return solution +end + +#retrieve user input and validate operation +have_operation = false +until have_operation + puts "Would you like to add, subtract, multiply, or divide?" + operation = gets.chomp.downcase + + #validate operation + method = "" + if + operation == "add" || operation == "addition" || operation == "+" + method = "add" + have_operation = true + elsif + operation == "subtract" || operation == "subtraction" || operation == "-" + method = "subtract" + have_operation = true + elsif + operation == "multiply" || operation == "multiplication" || operation == "*" + operation == "x" + method = "multiply" + have_operation = true + elsif + operation == "divide" || operation == "division" || + operation == "/" || operation == "÷" + method = "divide" + have_operation = true + else + have_operation = false + puts "hmm that's not right, lets try that again." + end + end + +#retrieve user input and validate values +tested_num_1 = "" +tested_num_2 = "" +have_values = false + +while !have_values + print "First number:" + num_1 = gets.chomp + tested_num_1 = num_1.match(/\d(?![a-zA-Z])/) + + print "Second Number:" + num_2 = gets.chomp + tested_num_2 = num_2.match(/\d(?![a-zA-Z])/) + + if tested_num_1 && tested_num_2 && + method == "add" || method == "subtract" || method == "multiply" || method == "divide" + num_1 = tested_num_1.to_s.to_f + num_2 = tested_num_2.to_s.to_f + have_values = true + if method == "divide" && num_2 == 0 + have_values = false + puts "You cannot divide by 0. Please enter your numbers again\n" + elsif method == "divide" && num_2 !=0 + num_1 = tested_num_1.to_s + num_2 = tested_num_2.to_s + have_values = true + end + + elsif !tested_num_1 + print "Your first entry must be a number. Enter both values again.\n" + elsif !tested_num_2 + print "Your second entry must be a number.Enter both values again.\n" + have_values = false + end +end + + + + +#do the math +puts "Hrmmmm, let me think" +if method == "add" + puts "The answer is: #{add(num_1, num_2)}" +elsif method == "subtract" + puts "The answer is: #{subtract(num_1, num_2)}" +elsif method == "multiply" + puts "The answer is: #{multiply(num_1, num_2)}" +elsif method == "divide" + puts "The answer is: #{divide(num_1, num_2)}" +end From 133636ab64207d396787c5b99fe008f9360029d6 Mon Sep 17 00:00:00 2001 From: tehut Date: Fri, 10 Feb 2017 07:37:06 -0800 Subject: [PATCH 2/2] Update Queues _Calculator_Tehut.rb --- Queues _Calculator_Tehut.rb | 54 ++++++++++++++++++------------------- 1 file changed, 27 insertions(+), 27 deletions(-) diff --git a/Queues _Calculator_Tehut.rb b/Queues _Calculator_Tehut.rb index 1f13720..f164d7c 100644 --- a/Queues _Calculator_Tehut.rb +++ b/Queues _Calculator_Tehut.rb @@ -8,7 +8,7 @@ " #define methods def add(number1, number2) - solution = number1.to_f + number2.to_f + solution = number1.to_f + number2.to_f return solution end @@ -32,32 +32,32 @@ def divide(number1, number2) until have_operation puts "Would you like to add, subtract, multiply, or divide?" operation = gets.chomp.downcase - + #validate operation method = "" - if - operation == "add" || operation == "addition" || operation == "+" - method = "add" - have_operation = true - elsif - operation == "subtract" || operation == "subtraction" || operation == "-" - method = "subtract" - have_operation = true - elsif - operation == "multiply" || operation == "multiplication" || operation == "*" - operation == "x" - method = "multiply" - have_operation = true - elsif - operation == "divide" || operation == "division" || - operation == "/" || operation == "÷" - method = "divide" - have_operation = true - else - have_operation = false - puts "hmm that's not right, lets try that again." - end + if + operation == "add" || operation == "addition" || operation == "+" + method = "add" + have_operation = true + elsif + operation == "subtract" || operation == "subtraction" || operation == "-" + method = "subtract" + have_operation = true + elsif + operation == "multiply" || operation == "multiplication" || operation == "*" + operation == "x" + method = "multiply" + have_operation = true + elsif + operation == "divide" || operation == "division" || + operation == "/" || operation == "÷" + method = "divide" + have_operation = true + else + have_operation = false + puts "hmm that's not right, lets try that again." end +end #retrieve user input and validate values tested_num_1 = "" @@ -68,11 +68,11 @@ def divide(number1, number2) print "First number:" num_1 = gets.chomp tested_num_1 = num_1.match(/\d(?![a-zA-Z])/) - + print "Second Number:" num_2 = gets.chomp tested_num_2 = num_2.match(/\d(?![a-zA-Z])/) - + if tested_num_1 && tested_num_2 && method == "add" || method == "subtract" || method == "multiply" || method == "divide" num_1 = tested_num_1.to_s.to_f @@ -86,7 +86,7 @@ def divide(number1, number2) num_2 = tested_num_2.to_s have_values = true end - + elsif !tested_num_1 print "Your first entry must be a number. Enter both values again.\n" elsif !tested_num_2