I have written a calculater that gets an expression from the user and change it to polish form and then calculates the result it can calculate cos,sin,tan cot,+,-,*,/
the first part is very similar to a program i saw in web(function ReversePolish) it must be able to get () . it does work properly if you put everything in paranthesis however once you omit the paranthesis it does'nt work right. for example:
it can calculate: (1+(2*3))
but when you write it like this: 1+2*3 it gives wrong answer.
I will send my code
can you plz help me? I have short time

Recommended Answers

All 3 Replies

i think use more codes but o don,t know that code but i have calculator to y dont u check this

i think use more codes but o don,t know that code but i have calculator to y dont u check this

where can i che it? and i want to use this code in another program which will draw the table in a way users ask and each part of the table is supose to work as a variable and this calculator is to be used on that. can you help me??
thank you for your respond.

Since you are using C, your file should be named "calculator.c", not "calculator.cpp".

The problem is that you haven't paid any attention to operator precedence. Line 37 tests all the operators as if they had the same precedence.

The whole point of RPN is that it makes precedence moot -- that is, operators are applied as they appear. In standard notation, however, it makes a great deal of difference.

You are not saving yourself any trouble to convert to RPN. You will still have to code against precedence properly when lexing the standard math program.

So your choices are:
1. Always use parentheses in the standard formulations
2. Write something that handles precedence (in which case you might as well forget the RPN stuff and just calculate the answer as you go). I recommend you google through "recursive descent" a little. This will be a bit of a learning curve. The wikipedia has some stuff which would be a good starting point for you.

Good luck.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.