Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Trying to get my convert to post fix program to work and getting a really strang

ID: 3864189 • Letter: T

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 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

Explanation / Answer

just Make the changes like below what iam doing and paste the code it works

#include
#include"expression.h"
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;
}