Please help with C++ program

Hi.I have to make a C++ program with the following
"Make a program that calculates an expression" for example (2+3*5+(2^4*3+(5/6)*7)-4)
The expression must have only "+" "-" "*" "/" "^" "(" ")"
It must contain structures
I have an idea but don't know how to implement it:
Include each parenthesis in a string and calculate it.
Show us what code you have already. I'd recommend having a loop in your main function that uses a switch to evaluate each character. Have a getnext() function to grab the next character. To follow PEMDAS, if the getnext function finds an operator with high precedence, evaluate you can recursively grab the next character and perform the operation on the return value. If this isn't entirely clear, here it is in rough C++:
1
2
3
4
5
int getnext(std::string expression, int index) {
    // grab a number before this
    if(expression[index] == '*')
	return number * getnext(expression, ++index);
}
Last edited on
well i have nothing since i dont know how to make it..i've searched the internet but all i found is Expression Evaluation..but not with this "^"...only with the regular signs..
Before you begin, write down the precedence rules on a piece of paper. An easy (but probably inefficient) algorithm would be to read in everything first, then finding the highest precedence operator, processing it and concatenating the original sequence, e.g.:

Input: 4+5+6^2*3
Step 1: Highest precedence: ^ --> 4+5+36*3
Step 2: Highest precedence: * --> 4+5+108
...

In case of parentheses, you can recursively call your processing function on the piece between ( ).

The most difficult part is finding a good way to make the input readable.

Topic archived. No new replies allowed.