From 7b7d07b2d4e8ad52ad995686522aad6fdfd4b494 Mon Sep 17 00:00:00 2001 From: nkiruka Date: Fri, 11 Aug 2017 08:53:53 -0700 Subject: [PATCH 1/3] Create Pipes - - --- Pipes - - | 108 ++++++++++++++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 Pipes - - diff --git a/Pipes - - b/Pipes - - new file mode 100644 index 0000000..68a25fd --- /dev/null +++ b/Pipes - - @@ -0,0 +1,108 @@ +# Last edited 8/10/17 +# Create a calculator + +##Issues: user entry set to interger or in Method +##checking if num is an interger + +# Methods for computing math operations +def add(num_1, num_2) + result = num_1.to_i + num_2.to_i + return result +end + +def subtract(num_1, num_2) + result = num_1.to_i - num_2.to_i + return result +end + +def multiply(num_1, num_2) + result = num_1 & num_2 + return result +end + +def divide(num_1, num_2) + result = num_1.to_f/num_2.to_f + if num_2 == 0 + puts "Invalid number, re-enter number >0" + num_2 = gets.chomp + end + return result.round(6) +end + +def exponent(num_1, num_2) + result num_1 ** num_2 + return result +end + +# method checks if denominator is 0 +def modulo(num_1, num_2) + result num_1 % num_2 + return result +end + +# Check if number is an interger +#def num_check_1(num) +# while (num != '0') && (num.to_i.to_s != num.strip) +# puts " input is not a number. please try again" +# num_1 = gets.chomp +# end +# end + +# def num_check_2(num) +# begin +# num = Integer(num) +# rescue +# print "Please enter an integer number:" +# retry +# return num +# end +# end + +# Greeting, welcome message/instructions +puts "What your name?" +name = gets.chomp + +puts "Hello #{name}!! I am C3P0 your human calculator................\n +These are the operations that I can perform: add(+), subtract(-), multiply(*), +divide(/), exponent(^) and modulo(%)......\n +To exit the calculator enter \'quit'\ for operation...\n +Let's get started: enter two numbers\n\n" + +puts "First number" +num_1 = gets.chomp.to_i +# check_input(num_1) +puts "Second number" +num_2 = gets.chomp.to_i +# num_check_2(num_2) + +puts "Enter operation you want to perform...." +prompt = "> " +while operation = gets.chomp + case operation + when "add" , "+" + puts add(num_1, num_2) + break + when "subtract" , "-" + puts subtract(num_1, num_2) + break + when "divide" , "/" + puts divide(num_1, num_2) + break + when "multiply" , "*" + puts multiply(num_1, num_2) + break + when "exponent" , "^" + puts exponent(num_1, num_2) + break + when "modulo" , "%" + puts modulo(num_1, num_2) + break + when "quit" + break + else + puts "Invalid operation, please select again" + print prompt + end +end + +# output calculation From 5f43ee00a12cbd5d1ccf94067eb3e43096557df3 Mon Sep 17 00:00:00 2001 From: nkiruka Date: Fri, 11 Aug 2017 11:15:25 -0700 Subject: [PATCH 2/3] Update Pipes - - --- Pipes - - | 59 ++++++++++++++----------------- 1 file changed, 27 insertions(+), 32 deletions(-) diff --git a/Pipes - - b/Pipes - - index 68a25fd..e802fb2 100644 --- a/Pipes - - +++ b/Pipes - - @@ -1,4 +1,5 @@ -# Last edited 8/10/17 +# Nkiru Onwuneme +# Last edited 8/11/17 # Create a calculator ##Issues: user entry set to interger or in Method @@ -6,62 +7,57 @@ # Methods for computing math operations def add(num_1, num_2) - result = num_1.to_i + num_2.to_i + result = num_1 + num_2 return result end def subtract(num_1, num_2) - result = num_1.to_i - num_2.to_i + result = num_1 - num_2 return result end def multiply(num_1, num_2) - result = num_1 & num_2 + result = num_1 * num_2 return result end +# method to check if denominator is o def divide(num_1, num_2) - result = num_1.to_f/num_2.to_f if num_2 == 0 - puts "Invalid number, re-enter number >0" - num_2 = gets.chomp + puts "Invalid number, numerator indivisible by 0" + else + result = num_1.to_f/num_2.to_f end return result.round(6) end def exponent(num_1, num_2) - result num_1 ** num_2 + result = num_1 ** num_2 return result end -# method checks if denominator is 0 def modulo(num_1, num_2) - result num_1 % num_2 + result = num_1 % num_2 return result end -# Check if number is an interger -#def num_check_1(num) -# while (num != '0') && (num.to_i.to_s != num.strip) -# puts " input is not a number. please try again" -# num_1 = gets.chomp -# end -# end - -# def num_check_2(num) -# begin -# num = Integer(num) -# rescue -# print "Please enter an integer number:" -# retry -# return num -# end -# end +def num_check(num) + begin + num = gets.chomp + if num.include?(".") + return num = Float(num) + else + return num = Integer(num) + end + rescue + print "Please enter an integer number:" + retry + end +end # Greeting, welcome message/instructions puts "What your name?" name = gets.chomp - puts "Hello #{name}!! I am C3P0 your human calculator................\n These are the operations that I can perform: add(+), subtract(-), multiply(*), divide(/), exponent(^) and modulo(%)......\n @@ -69,11 +65,9 @@ To exit the calculator enter \'quit'\ for operation...\n Let's get started: enter two numbers\n\n" puts "First number" -num_1 = gets.chomp.to_i -# check_input(num_1) +num_1 = num_check(num_1) puts "Second number" -num_2 = gets.chomp.to_i -# num_check_2(num_2) +num_2 = num_check(num_2) puts "Enter operation you want to perform...." prompt = "> " @@ -106,3 +100,4 @@ while operation = gets.chomp end # output calculation + From 7ce78635c1101bbd88160dd8f86389330425646f Mon Sep 17 00:00:00 2001 From: nkiruka Date: Fri, 11 Aug 2017 11:20:55 -0700 Subject: [PATCH 3/3] Update Pipes - - --- Pipes - - | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Pipes - - b/Pipes - - index e802fb2..29def72 100644 --- a/Pipes - - +++ b/Pipes - - @@ -2,8 +2,8 @@ # Last edited 8/11/17 # Create a calculator -##Issues: user entry set to interger or in Method -##checking if num is an interger +##Issues: user entry set to interger or in Method - fixed +# consolidate methods # Methods for computing math operations def add(num_1, num_2)