puts "Enter a value: "
num1 = gets.chomp()
puts "Enter b value: "
num2 = gets.chomp()
sum = num1.to_f + num2.to_f #Addition
puts "sum = #{sum}"
subtract = num1.to_f - num2.to_f #substraction
puts "subtract = #{subtract}"
multiply = num1.to_f * num2.to_f #multiplication
puts "multiply = #{multiply}"
divison = num1.to_f / num2.to_f #division
puts "divison = #{divison}"
With the above program i need to add option in all the operation. When i select 1 it should add the numbers.
Hi, I saw some basic problems in your code:
Use STDIN.gets()
instead of gets()
gets()
first checks the program arguments and read that as a file, it’s not what you want. For example:
Don’t use chomp() and then .to_f()
:
The .to_f()
or .to_i()
method converts string to integer. You can just run STDIN.gets.to_f
, and it will work fine.
Don’t use Multiple same method calls:
Every method call has some cost added to it, in terms of CPU time. So why not just use num = STDIN.gets.to_f
instead of using .to_f()
everywhere? .to_f converts string to float, and it wastes CPU time. Here we just used some memory to store it.
So your code now looks like this:
puts "Enter a value: "
num1 = STDIN.gets.to_f
puts "Enter b value: "
num2 = STDIN.gets.to_f
sum = num1 + num2 #Addition
puts "sum = #{sum}"
subtract = num1 - num2 #substraction
puts "subtract = #{subtract}"
multiply = num1 * num2 #multiplication
puts "multiply = #{multiply}"
divison = num1 / num2 #division
puts "divison = #{divison}"
Now coming to the original question.
How can you add 1 for addition, 2 for subtraction, 3 for multiplication and 4 for division? Well, that’s simple.
- Just ask the user to enter a number within 4
- clamp it to 4 so if the user enters anything above 4, like 66 it becomes 4. Also make sure the user can’t enter < 1, so -100 becomes 1, too.
- Store the operations in an array, get items from the array (user_input - 1).
For example:
OPERATIONS = %w(+ - * /)
print "Enter a value: "
num1 = STDIN.gets.to_f
print "Enter b value: "
num2 = STDIN.gets.to_f
print "Enter operation (1 +, 2 -, 3 *, 4 /): "
op = STDIN.gets.to_i.clamp(1, 4)
# Operation
operation = OPERATIONS[op - 1]
# Output as a floating point number
out_f = num1.method(operation).call(num2)
# Output as an integer
out_i = out_f.to_i
# Don't show 50.0 for example, when it's 50. Show 50.12 when it's 50.12.
result = out_i == out_f ? out_i : out_f.round(4)
# Format string
puts "#{num1} #{operation} #{num2} = #{result}"
Now it works like this:
$ ruby p.rb
Enter a value: 30
Enter b value: 50
Enter operation (1 +, 2 -, 3 *, 4 /): 3
30.0 * 50.0 = 1500
$ ruby p.rb
Enter a value: 30.23
Enter b value: 50
Enter operation (1 +, 2 -, 3 *, 4 /): 1
30.23 + 50.0 = 80.23
If you directly want to use the operation, entered by the user, just use this:
# Operation
operation = OPERATIONS.include?(op) ? op : ?+
Replace the Operation
section with this code. So if the user enters invalid operation, it will just use +.
And also, replace op = STDIN.gets.strip
. Here strip()
strips any \t
, \n
, \s
, \r
, \u0000
(unicode), \x00
(hex) and \00
(octal). So it deletes multiple copies of them as well, both from the beginning and the end of the string!
So it will work like this:
$ ruby p.rb
Enter a value: 3
Enter b value: 5
Enter operation (1 +, 2 -, 3 *, 4 /): *
3.0 * 5.0 = 15
$ ruby p.rb
Enter a value: -2000
Enter b value: -1000
Enter operation (1 +, 2 -, 3 *, 4 /): +
-2000.0 + -1000.0 = -3000
thank you for correcting my code and also answering my question. I understood the code.