Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Broken Calculator: Help with Calculations
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
GobinG




PostPosted: Fri Jan 20, 2012 7:14 pm   Post subject: Broken Calculator: Help with Calculations

What is it you are trying to achieve?
I am trying to make the game Broken Calculator: http://www.mathsisfun.com/games/broken-calculator.html I am trying to get the calculator to do problems such as 2 * 3 +2, but I cant seem to.


What is the problem you are having?
With the code I have the calculator should be able to do these problems, but no, I don't know why


Describe what you have tried to solve this problem
I tried messing with the order of the code, I know the calculations code is correct, but there is something wrong with the order, I think. Below there is the code for the calculations.


Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)
<Answer Here>

Turing:


<            %Calculations
            if op = "+" then
                answer := strint (num1) + strint (num2)
            elsif op = "*" then
                answer := strint (num1) * strint (num2)
            end if

            if op2 = "+" then
                answer := answer + strint (num2)
            elsif op2 = "*" then
                answer := answer * strint (num2)
            end if>



Please specify what version of Turing you are using
<4.1.1>



BrokenCalc.t
 Description:
Here's the program

Download
 Filename:  BrokenCalc.t
 Filesize:  19.61 KB
 Downloaded:  87 Time(s)

Sponsor
Sponsor
Sponsor
sponsor
Dreadnought




PostPosted: Sat Jan 21, 2012 12:20 am   Post subject: Re: Broken Calculator: Help with Calculations

The problem is basically that you take input for calculation in an odd way. (I find it odd anyway)

Let's think about a real calculator and try to model its behavior.

First, note that calculators are old, so they shouldn't be too complicated. This means we want a general model with as few special cases.

now lets think of computation. We can type 1+2+3+4+5+6+7+8+9 into a calculator and it will add up these integers one at a time. We might be tempted to think that this is going on.
code:
1 + 2  (the first two numbers are used as arguments for the first computation)
=> 3
3 + 3  (the result of the previous computation is used as the first argument)
=> 6
6 + 4  (again, the result of the previous computation is used as the first argument)
=> 10
...    (and repeat up to 9)

But, there is something that doesn't quite feel right about this method. Notice how the first computation is special. In the first computation we wait until the user has entered two values before performing the calculation, but for all other computations we use the result of the previous equation along with the next input from the user. This clashes with our first idea that thing should be general.

Is there way of making the first computation fit the general model that all the future computations follow. That is " result (operator) input = 'new result' "
Well, that would mean that when we do "1 + 2" the value 1 is the value of the result of some previous computation. What could it be?

It's probably pretty obvious that this previous computation is "0 + 1" (or "1 * 1" I guess but I'll explain why this is not the case later). Since there was no computation before entering 1, 0 and + must be initial values. This means that hitting AC will set the result of the previous computation to 0 and the current operation to +.

As I mentioned, the initial values could have been 1 and * but consider the functionality of the = button. In our model, it should be obvious that hitting = will accomplish the last computation the user entered and display the result. So if we hit AC then immediately hit =, we get 0. This shows that the initial values must be 0. and +.

What does all this mean to you? Well, your calculator uses 3 loops to take input from the player before doing any computation, then they repeat (ex: lines 190 - 250 in your code). From what I described here, you can get away with just one loop taking all input from the player. Here's the general pattern in pseudo-code (imaginary code).
code:
variables : result (int), input (int), operator (math function)
result, input = 0, operator = (+)

-- the player inputs 1
input = 1
-- the payer hits +
result = result (operator) input = 0 + 1 = 1
input = 0, operator = (+)
-- the player inputs 2
input = 2
-- the player hits *
result = result (operator) input = 1 + 2 = 3
input = 0, operator = (*)
-- the player enters 3
input = 3
-- the player hits =
result = result (operator) input = 3 * 3 = 9
etc.


Note that, in this model, the = button has a condition in its functionality. If no number has been input then no calculation should be done. Otherwise hitting equal could multiply or divide by 0. Since you're using strings for number input you can simply check for an empty string and choose whether or not to perform calculation. This means that the initial value of the input might be an empty string in your case.

Hope this helps.
GobinGiri




PostPosted: Sat Jan 21, 2012 3:19 pm   Post subject: RE:Broken Calculator: Help with Calculations

Thanks, I'm gonna use what you said, it's probably much more efficient.
Alex C.




PostPosted: Sat Jan 21, 2012 3:25 pm   Post subject: RE:Broken Calculator: Help with Calculations

wait, one user is now 2?
.... O_O *ouch brain hurts*

on an ACTUAL non spam topic, GobinGiri (or GobinG) are you going to be using a GUI (mouse like that online game) for your game or keyboard input
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 4 Posts ]
Jump to:   


Style:  
Search: