Question
Trying to get my convert to post fix program to work and getting a really strange break error. It seems to be happening in my
Trying to get my convert to post fix program to work and getting a really strange break error. It seems to be happening in my expression.cpp file. Maybe you can see the issues? Please write comments on what you changed
expression.h file:
#ifndef EXPRESSION__H #define EXPRESSION__H #include #include
class expression { public: bool last; expression(); friend std::istream& operator>>(std::istream&, expression&); friend std::ostream& operator<<(std::ostream&, expression&); private: std::string ifix, pfix; void convertToPostFix(); bool precedence(char, char) const; };
#endif
expression.cpp file
#include #include"expression.h" #include #include #include using namespace std;
expression::expression() { // ifix = pfix = ""; ifix = ""; pfix = ""; last = false; }
bool expression::precedence(char s, char c) const{ if (s == '(' || s == '$') return false; if(s=='*'|| s =='/') return true; return (c ='+' || c=='-'); }
void expression::convertToPostFix() { stack s;
s.push('$'); pfix = "";
for (size_t i = 0; i < ifix.size() - 1; ++i) { switch (ifix[i]){ case'(': s.push(ifix[i]); break; case ')': while (s.top() != '(') { pfix += s.top(); s.pop(); } s.pop(); break; case'+': case '-': case '*': case '/' : while (precedence(s.top(), ifix[i])) { pfix += s.top(); s.pop(); } s.push(ifix[i]); break; default: pfix += ifix[i]; }
} while (s.top() != '$') pfix += s.top(); }
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; }
in2postfix.cpp file
#include "expression.h" //#include "stack.h" //#include "queue.h" #include //use these two until queue and stack are done #include #include #include #include
using namespace std;
int main(){//(int argc, char *argv[]) { ifstream fin; ofstream fout; expression exp; queue q; fin.open("input.txt"); fout.open("output.txt"); // fin.open(argv[1]);//put filenames for visual, leave arg for turn in //fout.open(argv[2]);
while (!exp.last) { fin >> exp; q.push(exp); } fin.close(); while (!q.empty()) { exp = q.front(); fout << exp; q.pop(); } fout.close(); return 0; }
QNodeType.h
#ifndef QNodeType__H #define QNodeType__H
#include
template struct QNodeType { T item; QNodeType* next; };
#endif
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started