Processing expressions of either infix or postfix
Hey, I'm working on a program for class and I have part one of the assignment written but I'm not quite understanding this expression of infix or postfix.
This is the link to the assignment 9.
part 1 is completed but part 2 I just dont understand.
If anyone could help explain and help me out
http://faculty.cs.niu.edu/~byrnes/cs...ms/241pgm9.htm
Re: Processing expressions of either infix or postfix
Did you read the link you posted. Its all explained there or theres the RPN wiki
Re: Processing expressions of either infix or postfix
Yes i did read it, but how am i suppose to write the assign9.cpp ? I don't want the code but like the logic on what i need would help.
Re: Processing expressions of either infix or postfix
Quote:
Originally Posted by
youngyou4
Yes i did read it, but how am i suppose to write the assign9.cpp ? I don't want the code but like the logic on what i need would help.
The logic is all there in the link. What is there that you don't understand?
The goal of a computer programming assignment is to see if you can translate algorithmic, logical steps into a program. Any other help beyond showing you the algorithm/logic is asking us to write the code for you.
Regards,
Paul McKenzie
Re: Processing expressions of either infix or postfix
Quote:
One implementation might store the data in a linked list, while another might store the data in a dynamically allocated array. For this assignment, we will use a dynamically allocated array.
Every time I read an assignment that asks for a student to use a dynamically allocated array (especially when pointing out a standard container works just as well), a puppy dies.
Any ways, there is not much we can do for you if you don't understand what to do after having read about RPN. We can help with the code, the code logic, or just getting you started, but you need to input the actual logic behind what you want to do.
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.
Re: Processing expressions of either infix or postfix
Quote:
Originally Posted by
youngyou4
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.
Code:
#include <stack>
int main()
{
std::stack<int> IntStack; // an empty stack of integers
}
Regards,
Paul McKenzie
Re: Processing expressions of either infix or postfix
you can make an istringstream object from the string then parse from it two ints and a char. push the ints on the stack. switch on the char to do the right thing in the appropriate case and then return the result.