I'm using a test file before I use the one my teacher gave me.
My test file looks like this
3
3+1
(3+1)
Now I'm trying to take the file, read it line by line and turn the infix equations into postfix equations. In other words, they're suppose to end up looking like this:
3
31+
31+
The assignment is to utilize a non-STL stack to make it so.
void read_and_evaluate(istream& ins)
{
constchar RIGHT_PARENTHESIS = ')';
constchar LEFT_PARENTHESIS = '(';
stack<char> operators;
stack<int> operands;
int operand = 0;
char symbol;
// Read through the file, line by line
do
{
ins.get();
do
{
if(ins.peek() == LEFT_PARENTHESIS)
{
//read left paren and push onto stack
ins>>symbol;
operators.push(symbol);
}
//next input is a number/other operand
elseif(isdigit(ins.peek()))
{
//read operand and write it to output
ins>>operand;
cout<<operand<<endl;
}
elseif (strchr("+-*/", ins.peek()))
{
cout<<operators.top();
operators.pop();
}
// Read next input and push it onto the stack
operators.push(ins.peek());
}while(ins.peek() == NULL);
}while(!ins.eof());
Here's the pseudocode I'm using from my textbook so you can see what I'm referencing:
1. Initialize a stack of characters to hold the operation symbols and parentheses
2. do
if(the next input is a left parenthesis)
Read the left parenthesis and push it onto the stack
else if( the next input is a number or other operand)
Read the operand and write it to the output
else if( the next input is one of the operation symbols)
{
do
Print the top operation and pop it
while none of these three conditions are true:
1. The stack becomes empty, or
2. The next symbol on the stack is a a left parenthesis, or
3. The next symbol on the stack is an operation with lower precedence
than the next input symbol
Read the next input symbol, and push this symbol onto the stack
}
else
{
Read and discard the next input symbol(which should be a right
parenthesis).
Print the top operation and pop it; Keep printing and popping until
the next symbol on the stack is a left parenthesis.(If no parenthesis
is encountered, then print an error message indicating unbalanced
parentheses, and halt.) Finally, pop the left parenthesis.
}
while(there is more of the expression to read);
3. Print and pop any remaining operations on the stack. (There should be no remaining left parentheses; if there are, the input expression did not have balanced parentheses.)
So far I'm only reading and outputting numbers but it doesn't output the operators. My output looks like this right now:
3
3
1
31
I'm trying to figure out why it doesn't want to output the operators at all. This is only my third semester programming, so I'm still pretty new at this. Is there something that I'm doing wrong or something that I haven't thought of yet?
I guess now I'm at a complete loss on this project then. I don't it would be so bad if the file we're suppose to read from didn't start with an empty line. This was the only way I could see it working unless there's something out there that I haven't found yet.
elseif (strchr("+-*/", input.peek()))
{
input.get(mathOperators);
do
{
cout<<operators.top();
operators.pop();
}while(!operators.empty() && operators.top() != LEFT_PARENTHESIS && (orderOperations(mathOperators) > orderOperations(operators.top())));
// Read next input and push it onto the stack
operators.push(mathOperators);
Ok so here's my problem and I've been beating my head against the wall for hours on this. When I come upon the line 5+7, my program will read the 5 straight out to the screen on the first go around. However when it comes to the + it does what it's suppose to at first but instead of ignoring the do/while loop it just goes into it. The stack is empty so it should ignore this on the first go around but it doesn't. I can't seem to figure out where I'm going wrong on this one. Can anyone help me please?
but instead of ignoring the do/while loop it just goes into it.
that is what a do { ... } while(...); loop is supposed to do: it checks the condition after the content is processed.
A while(...) { ... } loop checks before the processing is done
Ok so I've switched it to a while loop but it still processes it rather than skipping it. Maybe I've interpreted the conditions wrong.
1 2 3 4 5 6 7 8 9 10 11
elseif (strchr("+-*/", input.peek()))
{
input.get(mathOperators);
while((operators.empty() == false) && operators.top() != LEFT_PARENTHESIS && (orderOperations(mathOperators) > orderOperations(operators.top())));
{
cout<<operators.top();
operators.pop();
}
// Read next input and push it onto the stack
operators.push(mathOperators);
do
Print the top operation and pop it
while none of these three conditions are true:
1. The stack becomes empty, or
2. The next symbol on the stack is a a left parenthesis, or
3. The next symbol on the stack is an operation with lower precedence
than the next input symbol
Read the next input symbol, and push this symbol onto the stack