i have a code let you calculate a fully parenthesized arithmetic expression only (with bracket)

my question is : how can I calculate it without bracket

for example:

((12-2)*(9/3))

if we put it as privious way we shall get 30

but if I put it as

12-2*9/3

it will gives different answer

how can i solve this problem

my code :

// written by C++++ from www.dreamincode.net #include <cctype> #include <cstdlib> #include <cstring> #include <iostream> #include <stack> using namespace std; double read_and_evaluate(istream& ins); void evaluate_stack_tops(stack<double>& numbers, stack<char>& operations); int main( ) { double answer; cout << "Type a fully parenthesized arithmetic expression:" << endl; answer = read_and_evaluate(cin); cout << "That evaluates to " << answer << endl; return EXIT_SUCCESS; } double read_and_evaluate(istream& ins) { const char DECIMAL = '.'; const char RIGHT_PARENTHESIS = ')'; stack<double> numbers; stack<char> operations; double number; char symbol; while (ins && ins.peek( ) != '\n') { if (isdigit(ins.peek( )) || (ins.peek( ) == DECIMAL)) { ins >> number; numbers.push(number); } else if (strchr("+-*/", ins.peek( )) != NULL) { ins >> symbol; operations.push(symbol); } else if (ins.peek( ) == RIGHT_PARENTHESIS) { ins.ignore( ); evaluate_stack_tops(numbers, operations); } else ins.ignore( ); } return numbers.top( ); } void evaluate_stack_tops(stack<double>& numbers, stack<char>& operations) { double operand1, operand2; operand2 = numbers.top( ); numbers.pop( ); operand1 = numbers.top( ); numbers.pop( ); switch (operations.top( )) { case '+': numbers.push(operand1 + operand2); break; case '-': numbers.push(operand1 - operand2); break; case '*': numbers.push(operand1 * operand2); break; case '/': numbers.push(operand1 / operand2); break; } operations.pop( ); }

Edit: Please use when writing your code. Thanks!

This post has been edited by **Martyr2**: 04 November 2007 - 04:30 PM