|
-
November 22nd, 2010, 06:38 PM
#6
Re: Processing expressions of either infix or postfix
int evalExp( const string & )
This function evaluates only postfix expressions and returns the result of the expression. The argument to this function is a string that contains a postfix expression. In order for this function to be implemented correctly, you will need to create a local stack that can hold integers.
In order to evaluate the postfix expression you will process it from left to right, character by character, until you reach the end.
If the current postfix character is a digit, push its integer value onto the integer stack.
Otherwise, if the current postfix character is an operator, pop the top two elements from the stack, apply the operator to the two values from the stack, and then push the result onto the integer stack. Do not forget that the first item to come off the stack will be the right operand, and then it will be followed by the left.
Once the entire postfix expression has been processed, there should be one value left on the stack. It's the final result of the expression. This should be popped off the stack and returned to the calling routine.
Code:
int evalExp (const string &)
I dont know where to even start from here. I understand i have to create a empty stack but i dont even know where to begin.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|