-
Notifications
You must be signed in to change notification settings - Fork 44
calculator #40
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
base: master
Are you sure you want to change the base?
calculator #40
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
|
||
# Program for calculator. | ||
puts " Welcome to the calculator " | ||
operators_array = %w[add + subtract - divide / multiply * % ^] | ||
puts operators_array | ||
|
||
# To verify whether the user enteres a valid operator | ||
puts "Which operation would you like to perform " | ||
operation = gets.chomp | ||
while !operators_array.include?(operation) | ||
print "Invalid input,Please try again\n" | ||
operation = gets.chomp | ||
end | ||
|
||
# To know when it needs to return an _integer_ versus a _float_. | ||
def convert_i_f(value) | ||
if (value =~ /\./) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This checks to see it if has ANY decimals in it. So the user could enter two of them, more detection of an invalid value would be better. For example 3...1 is accepted |
||
value = value.to_f | ||
elsif | ||
value = value.to_i | ||
end | ||
return value | ||
end | ||
|
||
# To check whether the user input is numeric for first number | ||
flag = true | ||
while flag | ||
puts "Enter the first number " | ||
num1 = gets.chomp | ||
if (num1 =~ /[a-zA-Z]/) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This only checks for letters (a-z), not punctuation for example. |
||
print "Invalid input; Please input again\n" | ||
else | ||
flag = false | ||
end | ||
end | ||
f_num = convert_i_f(num1) | ||
|
||
# To check whether the user input is numeric for second number | ||
flag = true | ||
flag = true | ||
while flag | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. notice how this section is a repeat of above... great candidate for a method! |
||
puts "Enter the second number " | ||
num2 = gets.chomp | ||
if (num2 =~ /[a-zA-Z]/) | ||
print "Invalid input; Please input again\n" | ||
else | ||
flag = false | ||
end | ||
end | ||
|
||
s_num = convert_i_f(num2) | ||
|
||
# To handle division by zero | ||
while (s_num == 0 ) && (operation == "division" || operation == "/") | ||
puts " Can't divide by Zero,Enter the input again " | ||
num2 = gets.chomp | ||
s_num = convert_i_f(num2) | ||
end | ||
|
||
# To perform the operations | ||
|
||
if operation == "add" || operation == "+" | ||
result = f_num + s_num | ||
elsif operation == "subtract" || operation == "-" | ||
result = f_num - s_num | ||
elsif operation == "multiply" || operation == "*" | ||
result = f_num * s_num | ||
elsif operation == "division" ||operation == "/" | ||
result = f_num/s_num | ||
elsif operation == "modulo" || operation == "%" | ||
result = f_num % s_num | ||
elsif operation == "^" | ||
result = f_num**s_num | ||
end | ||
puts "#{num1} #{operation} #{num2} = #{result}" |
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