# Thread: Create Calculation

1. ## Create Calculation

I'm trying to create a calculator that uses a text user interface. I have the interface writtern the way I want for now. Now I need help on writting the calculation code.

Would main() contain something like the code below?
Code:
```char operation;
int quit = 0;
while(!quit)
{
while(operation != '=')
{
getNumber();
getOperation();
}
performCalculations();
}```
Thank you

2. More easy would be to let the user enter a string with an expression and analyse this expression. In your code for example, what if the user enters more than one number or more than one operator at a time?

Also you should initialise operation before using it.

3. One good way to make a command line calulator is to take in expressions in postfix notation ( because paranthesis are not require so it's a little simpler ).

Then all you have to do is make a stack.

and to evaluate the expression just ( in psuedocode),
Code:
```while(true){
if(x == aNumber) stack.push(x)
else if(x == anOperator) {
y = stack.pop();
z = stack.pop();
result = (x=='*')?y * z:
(x=='/')?y / z:
(x=='+')?y + z:
(x=='-')?y - z: y % z;
stack.push(result);
}
}```
this will work for expressions of any size in postfiz notation.

If you want to use infix notation ( from the users point-of-view) you can convert the equation before you do this calulation.

4. The above code was meant to see if I had the right idea and I want a text interface for my calculator similar to the Windows calculator.

5. >>I want a text interface for my calculator similar to the Windows calculator.
Eh? The Windows calculator (calc.exe) is GUI based

6. Yes, I know thats GUI based. if text isn't the right term than how about ASCII?

[]
[7][8][9][+]
[4][5][6][-]
[1][2][3][/]
[C][9][0][*]

Popular pages Recent additions