From 8618fb50dc6330a6b49a167f33dea00c20b55392 Mon Sep 17 00:00:00 2001 From: vertige Date: Fri, 11 Aug 2017 08:40:50 -0700 Subject: [PATCH 1/2] Create Jessica Owens -- Carets --- Jessica Owens -- Carets | 175 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 175 insertions(+) create mode 100644 Jessica Owens -- Carets diff --git a/Jessica Owens -- Carets b/Jessica Owens -- Carets new file mode 100644 index 0000000..d62fd21 --- /dev/null +++ b/Jessica Owens -- Carets @@ -0,0 +1,175 @@ +### CALCULATOR EXERCISE + +## DEFINE VARIABLES +addition = ["addition", "add", "+", "plus", "and", "ajoute"] +subtraction = ["subtract", "subtraction", "-", "minus", "less", "moins"] +multiplication = ["multiply", "multiplication", "times", "*", "fois"] +division = ["divide", "division", "/", "divided by", "per", "over", "divise"] +powerof = ["square", "power", "^", "power of" "to the power of", "squared", "carre", "to the"] +modulus = ["modulus", "modulo", "remainder", "remainder of", "rem", "%", "mod"] + +operators = addition + subtraction + multiplication + division + powerof + modulus +symbols = { + "+" => addition, + "-" => subtraction, + "*" => multiplication, + "/" => division, + "^" => powerof, + "%" => modulus +} + +$op_display = "- add (+) +- subtract (-) +- multiply (*) +- divide (/) +- square (^) +- modulus (%)" + +## DEFINE METHODS +# Checking for included operations +def check_ops(input, reqs) # where input is what we are testing & reqs is an array of values for which we are checking + check = false + until check + reqs.each do |req| + check = true if input == req + end + + if !check + puts "That is not a valid option. Please use one of the following:\n#{$op_display}" + print "What operator would you like to use? " + input = gets.chomp + end + end + + return input +end + +# Checking for negatives +def check_neg(input) + if input.strip[0].include? ("-") + return input.strip[1..-1] + else + return input.strip + end +end + + +# Checking for integer versus float, only works after #check_vals validation +def is_float(input) + if check_neg(input).include?(".") + return true + else + return false + end +end + +# Checking for numbers +def check_vals(input) + until !check_neg(input).gsub(".","").match?(/\D/) && input.strip.match?(/\d/) # [] Fix: Known issue- substitution allows for entries such as 3.0.5 to pass + print "That is not a valid number. Please select another number: " + input = gets.chomp + end + + return input +end + +# Perform calculations +def calculate(op,a,b) + case op + when "+" + return a + b + when "-" + return a - b + when "*" + return a * b + when "/" + return a / b + when "^" + return a ** b + when "%" + return a % b # may create terrible float if b is float + end +end + + +## EXECUTION +# Get User input +puts "Welcome to a simple calculator? You can use the following operators:\n#{$op_display}" + + +print "Please enter your first value: " +user_val1 = check_vals(gets.chomp) + +print "Choose your operator: " +# user_op = gets.chomp +user_op = check_ops(gets.chomp, operators) +disp_op = user_op # could be set to "", but just to be safe + +# Convert user_op to symbol +symbols.each do |key, value| + if value.include? user_op + disp_op = key + user_op = value # may not be necessary + end +end + +print "Please select your second value: " +user_val2 = check_vals(gets.chomp) + +# Check to see if they're trying to divide by zero +while (disp_op == "/" || disp_op == "%") && user_val2.to_f == 0 + print "You cannot divide by zero. Select another value to divide by: " + user_val2 = check_vals(gets.chomp) +end + +# Display equation +if disp_op == "^" # Check if operator is ^ for formatting + puts "This is your equation: #{user_val1.strip}#{disp_op}#{user_val2.strip}" +else + puts "This is your equation: #{user_val1.strip} #{disp_op} #{user_val2.strip}" +end + +# Display results +if is_float(user_val1) || is_float(user_val2) # Check for float or integer + puts "The result is: #{calculate(disp_op, user_val1.to_f, user_val2.to_f)}" +else + puts "The result is: #{calculate(disp_op, user_val1.to_i, user_val2.to_i)}" +end + + +## Plan for allowing parentheticals +# A. refactor to allow inline entry, split apart string into components (manage improper entry methods)... +# B. Allow for someone to enter () either together to indicate a group of entries will be processed together then passed through, and allow them to tell you when to exit such parentheticals (ie "exit") or to let them open a parens and check for a closed parens, validate entry and process, then return value for next part of calculation. +# [] Either way, would need to give explicit, simple human instructions for the users to understand capabilities. +# [] Formatting display of parentheticals similar to "^"... + + +### PROMPT +# Build a calculator command line interface (CLI) that allows a user to perform simple arithmetic. + +### Baseline +# The program should ask the user for an operation (string or numeric symbol) and two numbers. + +### Primary Requirements +# [X] The program should use the input operation and two numbers to provide the result of applying the operation to the two numbers. +# [X] The program should have support for these four operations: addition, subtraction, multiplication, and division. +# [X] The program should accept both the name (add) and the symbol (+) for each possible operation. + +### Test & Verify +# [X] 1. The program adds numbers with both add and + +# [X] 2. The program subtracts numbers with both subtract and - +# [X] 3. The program adds numbers with both multiply and * +# [X] 4. The program adds numbers with both divide and / +# [X] 5. The program handles divide when attempting to divide by zero. +# [X] 6. The program handles erroneous input. For example the user might enter clown when asked to enter a number. +# [X] 7. The program also needs to handle erroneous operators. + +### Optional Enhancements +# [X] Print out the formula in addition to the result, i.e. 2 + 2 = 4 +# [X] Add support for computing exponents (2^4 = 2 * 2 * 2 * 2 = 16). +# [X] Add support for the modulo operator (10 % 3 = 1). +# [n/a] Gracefully handle unexpected user input: +# [n/a] What happens if the user input is nil (i.e., the user just pressed enter)? +# [n/a, handled by 6] What happens if the user tries to add hotdog to elephant? +# [X] Make your program know when it needs to return an integer versus a float. +# [] Add support for parentheticals, i.e. 10 / (5 + 5) = 1. From 7ffd32e18069e7b535628f4d1f5670655e1c5fe6 Mon Sep 17 00:00:00 2001 From: vertige Date: Fri, 11 Aug 2017 13:46:44 -0700 Subject: [PATCH 2/2] Some slight refactoring With a bit more sleep and some nice insight from classmates today :) --- ...ns -- Carets => Jessica Owens -- Carets.rb | 73 ++++++------------- 1 file changed, 23 insertions(+), 50 deletions(-) rename Jessica Owens -- Carets => Jessica Owens -- Carets.rb (69%) diff --git a/Jessica Owens -- Carets b/Jessica Owens -- Carets.rb similarity index 69% rename from Jessica Owens -- Carets rename to Jessica Owens -- Carets.rb index d62fd21..ccba4e4 100644 --- a/Jessica Owens -- Carets +++ b/Jessica Owens -- Carets.rb @@ -1,21 +1,13 @@ ### CALCULATOR EXERCISE ## DEFINE VARIABLES -addition = ["addition", "add", "+", "plus", "and", "ajoute"] -subtraction = ["subtract", "subtraction", "-", "minus", "less", "moins"] -multiplication = ["multiply", "multiplication", "times", "*", "fois"] -division = ["divide", "division", "/", "divided by", "per", "over", "divise"] -powerof = ["square", "power", "^", "power of" "to the power of", "squared", "carre", "to the"] -modulus = ["modulus", "modulo", "remainder", "remainder of", "rem", "%", "mod"] - -operators = addition + subtraction + multiplication + division + powerof + modulus -symbols = { - "+" => addition, - "-" => subtraction, - "*" => multiplication, - "/" => division, - "^" => powerof, - "%" => modulus +operators = { + "+" => ["addition", "add", "+", "plus", "and", "ajoute"], + "-" => ["subtract", "subtraction", "-", "minus", "less", "moins"], + "*" => ["multiply", "multiplication", "times", "*", "fois"], + "/" => ["divide", "division", "/", "divided by", "per", "over", "divise"], + "^" => ["square", "power", "^", "power of", "to the power of", "squared", "carre", "to the"], + "%" => ["modulus", "modulo", "remainder", "remainder of", "rem", "%", "mod"] } $op_display = "- add (+) @@ -44,25 +36,6 @@ def check_ops(input, reqs) # where input is what we are testing & reqs is an arr return input end -# Checking for negatives -def check_neg(input) - if input.strip[0].include? ("-") - return input.strip[1..-1] - else - return input.strip - end -end - - -# Checking for integer versus float, only works after #check_vals validation -def is_float(input) - if check_neg(input).include?(".") - return true - else - return false - end -end - # Checking for numbers def check_vals(input) until !check_neg(input).gsub(".","").match?(/\D/) && input.strip.match?(/\d/) # [] Fix: Known issue- substitution allows for entries such as 3.0.5 to pass @@ -87,7 +60,7 @@ def calculate(op,a,b) when "^" return a ** b when "%" - return a % b # may create terrible float if b is float + return a % b end end @@ -98,42 +71,42 @@ def calculate(op,a,b) print "Please enter your first value: " -user_val1 = check_vals(gets.chomp) +val1 = check_vals(gets.chomp) print "Choose your operator: " # user_op = gets.chomp -user_op = check_ops(gets.chomp, operators) +user_op = check_ops(gets.chomp, operators.values.flatten) disp_op = user_op # could be set to "", but just to be safe # Convert user_op to symbol -symbols.each do |key, value| - if value.include? user_op - disp_op = key - user_op = value # may not be necessary - end +operators.each do |key, value| + disp_op = key if value.include? user_op end print "Please select your second value: " -user_val2 = check_vals(gets.chomp) +val2 = check_vals(gets.chomp) # Check to see if they're trying to divide by zero -while (disp_op == "/" || disp_op == "%") && user_val2.to_f == 0 +while (disp_op == "/" || disp_op == "%") && val2.to_f == 0 print "You cannot divide by zero. Select another value to divide by: " - user_val2 = check_vals(gets.chomp) + val2 = check_vals(gets.chomp) end # Display equation if disp_op == "^" # Check if operator is ^ for formatting - puts "This is your equation: #{user_val1.strip}#{disp_op}#{user_val2.strip}" + puts "This is your equation: #{val1.strip}#{disp_op}#{val2.strip}" else - puts "This is your equation: #{user_val1.strip} #{disp_op} #{user_val2.strip}" + puts "This is your equation: #{val1.strip} #{disp_op} #{val2.strip}" end # Display results -if is_float(user_val1) || is_float(user_val2) # Check for float or integer - puts "The result is: #{calculate(disp_op, user_val1.to_f, user_val2.to_f)}" +return_float = calculate(disp_op, val1.to_f, val2.to_f) +return_integer = calculate(disp_op, val1.to_i, val2.to_i) + +unless return_integer == return_float + puts "#{return_float}\n" else - puts "The result is: #{calculate(disp_op, user_val1.to_i, user_val2.to_i)}" + puts "#{return_integer}\n" end