Question: Something is either messed up in my operator overload < > (std::istream&, expression&); friend std::ostream& operator < > chr; if (!fin.eof()) { if (chr !=

Something is either messed up in my operator overload <<, covertopostfix function, or my main output. Cannot figure it out. please help. Please comment your changes too.

Program below is supposed to be outputting like this:

InFix is: A+B-C Post fix is: A B + C -

InFix is: A+C Post fix is: A C +

InFix is: x*(y+z)-(w+t) Post fix is: x y z + * w t + -

InFix is: A+B*(C+D)-E/F+G+H Post fix is: A B C D + * + E F / - G + H +

InFix is: A*B Post fix is: A B *

Howveer it is outputting as:

InFix is: A+B-C Post fix is:

InFix is: A+C Post fix is: A C +

InFix is: x*(y+z)-(w+t) Post fix is:

InFix is: A+B*(C+D)-E/F+G+H Post fix is:

InFix is: A*B Post fix is:

PLEASE LOOK AT THE FILES IM PUTTING BELOW. I Really need this to output the postfix functions aswell.. expreesion.h

class expression { public: bool last; expression(); friend std::istream& operator >> (std::istream&, expression&); friend std::ostream& operator<<(std::ostream&, expression&); void convertToPostFix(); bool isOperator(char currentChar); queue obj; private: std::string ifix, pfix; bool const precedence(char, char); };

expression.cpp which contains the convert to post fix function

#include #include"expression.h" #include #include #include #include

using namespace std;

void expression::convertToPostFix() { {

stack stack; // Push a left parenthesis ( onto the stack. stringstream postfix;

while (!obj.empty()) { stack.push('(');

while (1) { if ((obj.front() != ';') && (obj.front() != '.')) {

const char current = obj.front(); obj.pop(); if (isspace(current)) { } // If it's a digit or '.' or a letter ("variables"), add it to the output else if (isalnum(current)) { postfix << current; }

else if ('(' == current) { stack.push(current); }

else if (isOperator(current)) { char rightOperator = current; while (!stack.empty() && isOperator(stack.top()) && precedence(stack.top(), rightOperator)) { postfix << ' ' << stack.top(); stack.pop(); } postfix << ' '; stack.push(rightOperator); }

// We've hit a right parens else if (')' == current) { // While top of stack is not a left parens while (!stack.empty() && '(' != stack.top()) { postfix << ' ' << stack.top(); stack.pop(); } if (stack.empty()) { cout << ("missing left paren"); } // Discard the left paren stack.pop(); postfix << ' '; } else { cout << ("invalid input character"); } } else { obj.pop(); break; } }

// Started with a left paren, now close it: // While top of stack is not a left paren while (!stack.empty() && '(' != stack.top()) { postfix << ' ' << stack.top(); stack.pop(); } // Discard the left paren stack.pop();

pfix = postfix.str(); // delete postfix; postfix.str(""); }

}

bool const expression::precedence(char leftOperator, char rightOperator) {

if (leftOperator == '^') { return true; } else if (rightOperator == '^') { return false; } else if (leftOperator == '*' || leftOperator == '/' || leftOperator == '%') { return true; } else if (rightOperator == '*' || rightOperator == '/' || rightOperator == '%') { return false; }

return true;

}

expression::expression() {

ifix = "\0"; pfix = "\0"; last = 0;

char chr; ifstream fin; fin.open("input.txt");

while (!fin.eof()) { fin >> chr;

if (!fin.eof()) { if (chr != ' ') { obj.push(chr); } }

} }

std::istream& operator >> (std::istream& in, expression& exp) { char sym; exp.ifix = ""; do { in >> sym; exp.ifix += sym; } while (sym != '.' && sym != ';'); if (sym == '.') exp.last = true; exp.convertToPostFix(); return in; } std::ostream& operator<<(std::ostream& out, expression& exp) { out << "Infix: " << exp.ifix << std::endl; out << "Postfix: " << exp.pfix << std::endl; return out; }

bool expression::isOperator(char currentChar) {

switch (currentChar) { case '+': case '-': case '*': case '/': case '^': case '%': return true; default: return false; } }

MAIN.CPP WHERE THE OUTPUT IS!

#include "expression.h" #include "stack.h" #include "queue.h" #include #include #include #include

using namespace std; int main() {

ifstream fin; ofstream fout; fin.open("input.txt"); fout.open("output.txt"); expression exp; queue q;

while (!exp.last) { fin >> exp; q.push(exp); } fin.close(); while (!q.empty()) { exp = q.front(); fout << exp; cout << exp; q.pop(); }

system("pause"); return 0;

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!