Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › (CODE) Need Ruby help {Simple calculator}
New Posts  All Forums:Forum Nav:

# (CODE) Need Ruby help {Simple calculator}

Hey Im kinda new in ruby but I need help from someone who knows ruby well.

So basically I'm trying to create a basic calculator that would do addition, subtraction, multiplication and division. But I'm required to create it in this format ( see below) Obviously with user input

Enter 1st number: 12345
Enter 2nd number: 67890
Enter operation: +

12345
+67890
80235
Enter 1st number: 324
Enter 2nd number: 111
Enter operation: -

324
-111
----
213
Enter 1st number: 325
Enter 2nd number: 4405
Enter operation: *

325
*4405
1625
0
1300
1300
1431625
Enter 1st number: 1234
Enter 2nd number: 4
Enter operation: *

1234
*4
----
4936

So that is how it should look at the end, but the problem is the I can do the calculator but cannot figure out how to do it in this layout, could anyone give me so tips or links to sites ?
 The Dark Phantom (19 items)
 The Dark Phantom (19 items)
I think this is what you're attempting to do. I'd encourage you to extend this to do more things.
Code:
``````#the difference between puts and print
#is that puts adds a newline (an 'enter') to to whatever
#you output while print doesn't

#chomp removes the newline at the end
#to_f converts the input string to a floating point number

print "enter the first number: "
num1 = gets.chomp.to_f

print "enter the second number: "
num2 = gets.chomp.to_f

oper = gets.chomp

#case looks at the variable oper
#it starts at the top when and check each
#one for a match. If it doesn't find a match,
#it does the final 'else' match at the bottom
#the 'end' tells it that it's reached the end
#of the case statement
case oper
when "-"
puts "  #{op1}\n- #{op2}\n-----------\n  #{(op1 - op2)}"
when "*"
puts "  #{op1}\nX #{op2}\n-----------\n  #{(op1 * op2)}"
when "+"
puts "  #{op1}\n+ #{op2}\n-----------\n  #{(op1 + op2)}"
when "/"
puts "  #{op1}\n% #{op2}\n-----------\n  #{(op1 \ op2)}"
else
puts "I don't under stand that command."
end

#to explain that puts command
#begin the string and put 2 spaces at the front (to align everything)
#the '#{}' command evaluates what's inside and inserts
#it into the string at that point.

#the '\n' inserts a newline or 'enter'
#we add a space after the operator to align everything
#we insert the operator and change to a new line
#we insert a bunch of hyphens on that line and go to the next line

#we add 2 spaces and then insert the result of
#performing the requested operation on the numbers
#finally, puts adds it's own newline at the end```
```

Edited by hajile - 10/19/13 at 8:20pm
Thank you very much for this, I loved your explanation but if you have some time i'm getting this error :
Code:
``````syntax error, unexpected \$undefined, expecting ')'
puts "  #{op1}\n% #{op2}\n-----------\n  #{(op1 \ op2)}"
```
```

Rep for you
 The Dark Phantom (19 items)
 The Dark Phantom (19 items)
Never mind i spotted the mistake "\" instead of "/"
 The Dark Phantom (19 items)
 The Dark Phantom (19 items)
I really sorry to ask you more but

how would i go about creating the multiplication layout like this so that it look like you would do it by hand

Enter 1st number: 325
Enter 2nd number: 4405
Enter operation: *

325
*4405
1625
0
1300
1300
1431625

Also how would I go about trying to create a CYCLE so that i can use this over and over again as soon as the last problem was solved
 The Dark Phantom (19 items)
 The Dark Phantom (19 items)
To do the cycling, you need to use a loop

The while loop checks if the test is true. If the statement is true, the loop works one time and then checks again.
Code:
``````#the when statement
when <test_here> do
#do whatever here
end

#the if statement executes the 'then' part
#if true otherwise it executes the 'else' part
if <test_here> then
#do this stuff if test is true
else
#do this if test is NOT true
end```
```
Code:
``````#the statement 'true' forces the loop
#to repeat forever. We add an if statement
#to make up for that
while true do
print "enter your operator (type 'exit' to quit program): "
oper = gets.chomp

#check if the input is 'exit', if so
#the 'break' command forces you to quit
#the current loop no matter what. In our
#case, it causes the program to exit
if oper == "exit" then
break
end

case oper
#this is just like before
when "+"
puts "  #{op1}\n+ #{op2}\n-----------\n  #{(op1 + op2)}"
else
puts "I don't under stand that command."
end
#end the while loop here
end```
```

Moving on to your multiplication question. Let's break it down into smaller pieces.

Our first problem is aligning the numbers correctly when one number is bigger than the other. Normally, the larger number is placed on top for multiplication. Remember, ALL input is actually characters (which are then converted to numbers). We'll need to change 'gets.chomp.to_f' to 'gets.chomp' for now because we need to work with strings (you'll see why in a minute). All strings have a length function available to them which tells how many characters they have. This is done by using:
Code:
``````my_string = "test string "
#notice that it counts the space at the end
puts my_string.length

#you could also do this without making a variable first
"another string".length```
```
To find which is longer, we just need to compare lengths, but there's a problem. The numbers can have a minus sign in front which makes the real number smaller. If we had '-123' and '2345' the lengths would be the same, but the length of the numbers would be different. We need to check this too.
Code:
``````#I'm assuming we're using num1 and num2 as before
#but we haven't converted them from strings to numbers yet
#that is, we used
num1 = gets.chomp
#rather than
num1 = gets.chomp.to_f```
```

Sorry, I've got to go before I complete this. I'll edit with the rest later. In short, you need to put the longer string on top (you also need to check for the decimal point and adjust alignment accordingly). The total length of both strings should give you how long the final number will be. This number (also adjusted for decimal place) will allow you to calculate how many spaces each number needs to be from the right (there's a class in ruby that can do this for you, but that kind of defeats the purpose of this exercise).

To get your calculations, loop over each character in the lower number. For each character (remember, they're numbers), convert it to a string and multiply by the top number (make sure to remove the decimal point from the string and then convert this string to an integer (use var.to_i) otherwise you'll get the wrong numbers here. Convert it to a string again and pad it over based on which position it is in and then repeat. For the final answer, I'd just output the results of multiplying them like you did before.
Thank you very much hajile, you helped me a lot, but im still stuck on the multiplication i have tried it out but it never seems to work . Could you please try and finish this, thanks a lot....

This is what i have right now:
Code:
``````print "enter the first number: "
num1 = gets.chomp.to_i

print "enter the second number: "
num2 = gets.chomp.to_i

operation = gets.chomp

case operation
when "-"
puts " #{num1}\n-#{num2}\n------\n#{num1 - num2}"
when "+"
puts " #{num1}\n+#{num2}\n------\n#{num1 + num2}"
when "*"
if num1 == num2
then print " #{num1}\n*#{num2}\n------\n#{num1 * num2}"
end
if num1 != num2
then if num1 > num2
print " #{num2}\n*#{num1}\n------\n"

end
if num1 != num2
then if num1 < num2
print "#{num1}\n*#{num2}\n------\n"
end

#puts " #{num1}\n*#{num2}\n------\n#{num1 * num2}"

else
puts "I don't under stand that command."
end
end
end```
```
 The Dark Phantom (19 items)
 The Dark Phantom (19 items)
Oh and the exact spacing im meant to have is like this
Code:
``````Enter 1st number: 325
Enter 2nd number: 4405
Enter operation: *

325
*4405
-----
1625
0
1300
1300
-------
1431625```
```
 The Dark Phantom (19 items)
 The Dark Phantom (19 items)
Oh as well I'm not required to do the loop/cycle so the it does all the calculations at once without re running
 The Dark Phantom (19 items)
 The Dark Phantom (19 items)
Quote:
Originally Posted by Scout Lukas

Oh as well I'm not required to do the loop/cycle so the it does all the calculations at once without re running
This reply indicates that this is a homework assignment. I have no problem offering solutions to problems of personal interest, but the point of homework is for you to learn how to solve your own problems and I won't give you those answers.
New Posts  All Forums:Forum Nav:
Return Home
Back to Forum: Coding and Programming
• (CODE) Need Ruby help {Simple calculator}
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › (CODE) Need Ruby help {Simple calculator}