# Still need to know how to make a calculator

[b][red]This message was edited by IndrekSnt at 2003-4-30 5:24:48[/red][/b][hr]
[b][red]This message was edited by IndrekSnt at 2003-4-30 5:22:57[/red][/b][hr]
I wanted to make a calculator which loads a string from a textbox. And when i click on a button called "calculate" then it calculates all the stuff that is in the textbox(like a scientific calculator does). I tried a simplier way but it gave me wrong answers. What should i code to make such calculator? Please help me! Thanks for any help

• I had to make one in Java before in some data structures class
I don't know if it is the best way but it works

First you have to make 2 stacks
one is an operator stack and the other is an Number stack

Obviously your operators are '(','+', and so on

now push your first number on the Number stack
push your operator on the operator stack
push your next number on the number stack
now compare the next operator with the top one on the stack
if the operator on the stack has a greater precedence
pop the 2 numbers off and pop the opeator now do the calculation... then push the answer on the number stack

if the operator on the stack has a lower precedence then push the operator on the operator stack and continue with the algorithm until = has been reached .... = has the lowest precedence

It has been a couple of years since I did this I believe its close to what you want. If you need further help hit me up.

~Chris

• : I had to make one in Java before in some data structures class
: I don't know if it is the best way but it works
:
: First you have to make 2 stacks
: one is an operator stack and the other is an Number stack
:
: Obviously your operators are '(','+', and so on
:
: now push your first number on the Number stack
: push your operator on the operator stack
: push your next number on the number stack
: now compare the next operator with the top one on the stack
: if the operator on the stack has a greater precedence
: pop the 2 numbers off and pop the opeator now do the calculation... then push the answer on the number stack
:
: if the operator on the stack has a lower precedence then push the operator on the operator stack and continue with the algorithm until = has been reached .... = has the lowest precedence
:
: It has been a couple of years since I did this I believe its close to what you want. If you need further help hit me up.
:
: ~Chris
:
:
:
:
Yes, this really is what i wanted, thanks!
Now the only thing that matters me is that i must get the characters and numbers separately. Maybe that will be something long and hard to understand...
• : Yes, this really is what i wanted, thanks!
: Now the only thing that matters me is that i must get the characters and numbers separately. Maybe that will be something long and hard to understand...
:

I am confused on what you mean by getting the characters and numbers separately....
• : : Yes, this really is what i wanted, thanks!
: : Now the only thing that matters me is that i must get the characters and numbers separately. Maybe that will be something long and hard to understand...
: :
:
: I am confused on what you mean by getting the characters and numbers separately....
:
It doesn't matter anymore, i made the calculator successful in a little easier way. It works great and thanks to everyone who helped me!