From d68a9a2f4198e23ab8c5ad0cb49b3b4c82b83c94 Mon Sep 17 00:00:00 2001 From: Brittany Jones Date: Wed, 7 Feb 2018 10:22:23 -0800 Subject: [PATCH 1/2] I made a calculator --- Calculator.md | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 Calculator.md diff --git a/Calculator.md b/Calculator.md new file mode 100644 index 0000000..926ecad --- /dev/null +++ b/Calculator.md @@ -0,0 +1,63 @@ +puts "Lets Do Math!" + +puts "Enter the the first number:" +a = gets.chomp + +puts "Please enter the operator:" +b = gets.chomp + +puts "Give me another number:" +c = gets.chomp + +# To validate if user inputs are valid numbers? +def valid_num(str) + str == "0" || str.to_i != 0 +end + +# To validate if the user input numbers include decimal points or not +def dec_num(str) + str.include?(".") +end + +# change data types of numbers (integer ) +def num_type(x) + if dec_num(x) + x.to_i + else + x.to_i + end +end + +def domath(x, y, z) +# x = x.to_i; z = z.to_i + if valid_num(x) && valid_num(z) + x = num_type(x) + z = num_type(z) + +ans = case y + when '+', 'add' + x + z + when '-', 'subtract' + x - z + when '*', 'multiply' + x * z + when '/', 'divide' + # The program handles divide when attempting to divide by zero + if z != 0 + x / z + else puts "Please enter other than '0'" + end + when '%' , 'modulo' + x % z + when '**' , 'exponents' + x ** z + else + puts "Enter a valid operator" + end + # p ans + puts "#{x} #{y} #{z} = #{ans}" + else puts "Enter valid numbers" + end +end + +domath(a,b,c) From fe3f379ae3256e3593f983eec0968ffb8d9791b2 Mon Sep 17 00:00:00 2001 From: Brittany Jones Date: Wed, 7 Feb 2018 22:51:51 -0800 Subject: [PATCH 2/2] I did my best on calculator --- Calculator.md | 122 ++++++++++++++++++++++++++++++++------------------ 1 file changed, 79 insertions(+), 43 deletions(-) diff --git a/Calculator.md b/Calculator.md index 926ecad..13ce848 100644 --- a/Calculator.md +++ b/Calculator.md @@ -1,63 +1,99 @@ -puts "Lets Do Math!" +# Brittany Jones +# Ada Calculator +# Last Edited 2/7/18 +# The program should ask the user for an operation (string or numeric symbol) and two numbers. -puts "Enter the the first number:" -a = gets.chomp +#Primary Requirements +#The program should use the input operation and two numbers to provide the result of applying the operation to the two numbers. +#The program should have support for these four operations: addition, subtraction, multiplication, and division. +#The program should accept both the name (add) and the symbol (+) for each possible operation. -puts "Please enter the operator:" -b = gets.chomp -puts "Give me another number:" -c = gets.chomp +# Prompt user. +puts "Let us do math!" -# To validate if user inputs are valid numbers? -def valid_num(str) - str == "0" || str.to_i != 0 +# Get first number from user. +puts "\nEnter the the first number:" +a = gets.chomp +while true + if a.to_f.to_s == a || a.to_i.to_s == a + break + else + print "Please enter a valid number:" + a = gets.chomp + end end -# To validate if the user input numbers include decimal points or not -def dec_num(str) - str.include?(".") +# Get the operation they want to use. +puts "Now enter your favorite operator:" +op = gets.chomp + +# Get another number from the user. +puts "Give me another number:" +b = gets.chomp +while true + if b.to_f.to_s == b || b.to_i.to_s == b + break + else + print "Please enter a valid number:" + b = gets.chomp + end +end +# While loop, if input goes from integer to string and it is still == to c, then it is a number. +# To validate if user inputs are valid numbers? (Not sure if this part is needed anymore.) +def valid_num(num) + num == "0" || num.to_i != 0 end -# change data types of numbers (integer ) -def num_type(x) - if dec_num(x) - x.to_i +def dec_num(num) + num.include?(".") +end +##### NOT SURE IF THIS SECTION IS NEEDED AT ALL, ESPECIALLY IF I HAVE CONVERTED THE USER INPUT INTO FLOAT/Integer.##### +# change data types of number to float to account for decimal numbers. +def num_class(i) + if dec_num (i) + i.to_f else - x.to_i + i.to_i end end +####### End of some unworthy code ###### -def domath(x, y, z) -# x = x.to_i; z = z.to_i - if valid_num(x) && valid_num(z) - x = num_type(x) - z = num_type(z) - -ans = case y - when '+', 'add' - x + z - when '-', 'subtract' - x - z - when '*', 'multiply' - x * z +def domath(a, op, b) + if valid_num(a) && valid_num(b) + a = num_class(a) + b = num_class(b) +#### End of unworthy code which depend upon the previously written uworthy code#### +# Case Loop +ans_wer = case op + when '+' , 'add' + a + b + when '-' , 'subtract' + a - b + when ' * ' , 'multiply' + a * b when '/', 'divide' - # The program handles divide when attempting to divide by zero - if z != 0 - x / z - else puts "Please enter other than '0'" +# What to do when user tries to use division by 0 + if b != 0 + a / b + else puts "Please enter a number other than '0' when using division" end +#Add support for the modulo operator (10 % 3 = 1). when '%' , 'modulo' - x % z - when '**' , 'exponents' - x ** z + a % b +#Add support for computing exponents (2^4 = 2 * 2 * 2 * 2 = 16) + when ' ** ' , 'exponents' + a ** b else - puts "Enter a valid operator" +# If they enter some kind of operator that isnt supported. + puts "Sorry, this calculator only does calculations with +, -, %, /, * , and exponents. Please enter your favorite operator:" + op = gets.chomp end - # p ans - puts "#{x} #{y} #{z} = #{ans}" - else puts "Enter valid numbers" +# print it out when def method domath + puts "\nLet there be math!" + puts "#{a} #{op} #{b} = #{ans_wer}" end end -domath(a,b,c) +#Print out the formula in addition to the result, i.e. 2 + 2 = 4 +domath(a, op ,b)