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

Creating a program that converts infix to postfix when reading from an input fil

ID: 3801775 • Letter: C

Question

Creating a program that converts infix to postfix when reading from an input file (until it hits a '.')and outputting to a file. It is reading in my input well except that it keeps throwing a ) after each input read in. Also it is not outputting my postfix expressions correctly, it seems to be adding them together and not even converting them correctly.

My input file:

A+B
-C;
A
+ C
;

x*(y+z)-(w+t);
A+B*(C+D)-E/F+G+H;

A*B.

Garbage here.

My output.txt results

Infix: A+B-C;)
Postfix: (A+B-C;)
Infix: A+C;)
Postfix: (A+B-C;)(A+C;)
Infix: x*(y+z)-(w+t);)
Postfix: (A+B-C;)(A+C;)(x(*y+z)(-w+t);)
Infix: A+B*(C+D)-E/F+G+H;)
Postfix: (A+B-C;)(A+C;)(x(*y+z)(-w+t);)(A+B(*C+D)-E/F+G+H;)
Infix: A*B.)
Postfix: (A+B-C;)(A+C;)(x(*y+z)(-w+t);)(A+B(*C+D)-E/F+G+H;)(A*B.)

EXPRESSION.H:

#ifndef EXPRESSION__H
#define EXPRESSION__H
#include<iostream>
#include<string>
#include<cstring>
#include <stdexcept>
#include <cctype>
#include <sstream>
#include <stack>
#include <queue>
using namespace std;

class expression {
public:
bool last;
expression();
friend std::istream& operator>>(std::istream&, expression&);
friend std::ostream& operator<<(std::ostream&, expression&);
void convertToPostFix();

private:
std::string ifix, pfix;
//convert here move
bool const precedence(char, char);
};

#endif

EXPRESSION.CPP:

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

using namespace std;

expression::expression() {

stack stack;
stack.push('(');
ifix.push_back(')');


for (size_t i = 0; i < ifix.size(); ++i) {

  
if (isdigit(ifix[i]) || isspace(ifix[i])) {
pfix.push_back(ifix[i]);
}

  
else if (ifix[i] == '(') {
stack.push(ifix[i]);
}

else if ((ifix[i]))
{
char operator1 = ifix[i];
if ((stack.top())) {
while (!stack.empty() && precedence(operator1, stack.top())) {
pfix.push_back(stack.top());
stack.pop();
}
}
  
stack.push(operator1);
}
else if (ifix[i] == ')') {
  
while (stack.top() != '(') {
  
pfix.push_back(stack.top());
stack.pop();
}
stack.pop();
}
}

while (!stack.empty()) {
pfix.push_back(stack.top());
stack.pop();
}
}

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

in2post.cpp: (where im reading in/out)

#include "expression.h"
//#include "stack.h"
//#include "queue.h"
#include <stack>//use these two until queue and stack are done
#include <queue>
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>

using namespace std;

int main(){
  
ifstream fin;
ofstream fout;
expression exp;
queue q;

fin.open("input.txt");
fout.open("output.txt");

while (!exp.last) {

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

return 0;
}

Explanation / Answer

Answer:

#include<iostream.h>
#include<string.h>
#include<stdlib.h>
#include<ctype.h>

class expression
{
private:
char expression_infix[100];
char value[200];
int upper;
int r;
char expression_postfix[100];
public:
void interchange();
int read(char);
int check(char);
int rank(char);
};

int expression::read(char c)
{
if(c==’+’ || c==’-‘)
return 1;
else if(c==’*’ || c==’/’)
return 3;
else if(c==’^’)
return 6;
else if(isalpha(c)!=0)
return 7;
else if(c=='(‘)
return 9;
else if(c==’)’)
return 0;
else
{
cout<<"Invalid expression ::input error ";
exit(0);
}
}

int expression::check(char c)
{
if(c==’+’ || c==’-‘)
return 2;
else if(c==’*’ || c==’/’)
return 4;
else if(c==’^’)
return 5;
else if(isalpha(c)!=0)
return 8;
else if(c=='(‘)
return 0;
else
{
cout<<"Invalid expression ::stack error ";
exit(0);
}
}

int expression::rank(char c)
{
if(c==’+’ || c==’-‘)
return -1;
else if(c==’*’ || c==’/’)
return -1;
else if(c==’^’)
return -1;
else if(isalpha(c)!=0)
return 1;
else
{
cout<<"Invalid expression ::in rank ";
exit(0);
}
}

void expression::interchange()
{
cout<<"Enter an expression_infix expression :: ";
cin>>expression_infix;
int l=strlen(expression_infix);

expression_infix[l]=’)’;
expression_infix[l+1]=";


upper=1;
value[upper]='(‘;

r=0;
int x=-1;

int i=0;
char later=expression_infix[i];

while(later!=")
{

while( read(later) < check(value[upper]) )
{
   if(upper<1)
   {
    cout<<"invalid expression ::value error ";
    exit(0);
   }

   expression_postfix[++x]=value[upper];
   upper–;

   r=r+rank(expression_postfix[x]);

   if(r<1)
   {
    cout<<"Invalid expression ::r<1 ";
    exit(0);
   }
}

if(read( later ) != check( value[upper]))
   value[++upper]=later;
else
   upper–;

i++;
later=expression_infix[i];
}
expression_postfix[++x]=";

if(r!=1 || upper!=0)
{
cout<<"Invalid expression ::error in rank or value ";
exit(0);
}

cout<<" The corresponding expression_postfix expression is :: ";
cout<<expression_postfix<<endl;
}
int main()
{
expression val;
val.interchange();
return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote