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

Program below is supposed to be outputting like this: InFix is: A+B-C Post fix i

ID: 3804092 • Letter: P

Question

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..
expression.cpp which contains the convert to post fix function




#include<iostream>
#include"expression.h"
#include<string>
#include<fstream>
#include<cstring>
#include <sstream>

using namespace std;


void expression::convertToPostFix()
{
{

stack<char> 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 = "";
pfix = "";
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;

}

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

Explanation / Answer

#include<iostream>
#include"expression.h"
#include<string>
#include<fstream>
#include<cstring>
#include <sstream>

using namespace std;


void expression::convertToPostFix()
{
{

stack<char> 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 = "";
pfix = "";
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;
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote