-
Notifications
You must be signed in to change notification settings - Fork 45
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Caroline Nardi - Calculator - Octos #31
base: master
Are you sure you want to change the base?
Conversation
CalculatorWhat We're Looking For
|
def parse(input) | ||
# Removes white space, converts string into an array split by the possible operators (but retaining the operators). | ||
# Removes empty strings from the array. I'm actually not sure where these are coming from, but I noticed them when the user enters a negative number. | ||
parsed_input = input.gsub(/\s+/,"").split(/(\+)|(-)|(\*)|(\/)|(\^)|(%)|(add)|(subtract)|(multiply)|(divide)|(exponent)|(modulo)/).reject { |x| x.empty? } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice regular expression to split the input string.
|
||
# Informs user of the limits of the calculator, the possible operators, and prompts user for their calculation. | ||
puts "Welcome to my (very limited) calculator. This calculator can perform basic arithmetic operations between two numbers." | ||
puts "\nThe accepted operators are: +, -, *, /, ^, or %, or the words add, subtract, multiply, divide, exponent, or modulo." |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would suggest an example of valid input. At first I was entering just the operator (-,+,/,*, etc).
puts "Hmm. It doesn't look like the operation you've entered is valid for this calculator." | ||
# If the array length is less than 3, the user likely didn't enter enough information, | ||
# or didn't enter it in a manner that the parser could handle. Returns nil. | ||
elsif (parsed_array[0] =~ /^[-+]?\d+(\.\d+)?$/).nil? || (parsed_array[2] =~ /^[-+]?\d+(\.\d+)?$/).nil? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice regular expression, I would suggest to use /^[-+]?\d*(\.\d+)?$/
instead of what you have there. Yours requires at least 1 digit to the left of the decimal so .25 wouldn't work.
parsed_input[3] = parsed_input[2..3].join | ||
parsed_input.slice!(2) | ||
end | ||
return parsed_input |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be indented
Calculator
Congratulations! You're submitting your assignment.
Comprehension Questions