Originally Posted by
Adak
I STRONGLY recommend that you goto Wikipedia's website and read up on Polish Notation. You need to know EXACTLY what it is that you're trying to work with - is it:
*Polish Notation
*Reverse Polish Notation
or
Postfix Notation
You surely will want to see some examples of each of these, and decide what type of notation you want to work with, before engaging a lot of time into the project.
If you want to parse it all out, a good start would be:
1) Scan the digits and locate the innermost bracketed items.
2) Follow the rules of mathematical precedence and solve that one item.
3) Re-write the original equation, substituting your result for the innermost bracketed item.
4) Loop back to number 1 above, and repeat until it's reduced fully to a single value.
There are several sites that explain the above much better than I can. If you have trouble with your CODE or algorithm, please post your code and explain what the problem is and what output you're currently getting from your program that's not correct.
You need to be quite specific in order to get valuable help.
Good luck, this is an ambitious project.
algorithm:
Code:
*open a file
*read the file character by character
*for( i=0;i<x;i++)//assuming that x is the number of times you operate inside the parenthesis, for example, ((2+3)5)+5, this requires three times in parsing to get the result
*follow step 1 in reading characters if it is digit, place it on a stack, if it is an operand solve the number of digits inside the parenthesis
if the operand is addition or so, follow step number 2
**if it is addition, add the two numbers on a stack
**same through for subtract, multiply and divide and also solving for exponents
*the result will be the first number to be place on a stack for another parsing until the parsing is finished
*display the result
pseudo code
Code:
main()
{
in(filename)
f1=open(filename, read)
in(saved filename)
f2=open(filename, write)
if(filename!=NULL)
{
show original content
}
else
{
error
}
int x
for(i=0;i<x;i++)
{
isdigit
placing stack
isoperand
do step number 2
}
return result
}
but im find hard to convert my pseudo code to my C++ code, on my algorithm translate the code is my problem..how to convert my algorithm/pseudo code to C++ code?
NOTE: stack.h is include in the program