Creating a program that converts infix to postfix when reading from an input fil
ID: 3802332 • 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. Can you see the issue the code, I imagine it is the expression.cpp file during the convertoPostFix function.
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
Use following code for Infix to Postfix conversion. After taking each infix expression from file, omit only d last ‘)’ character and then send the string for conversion. It will help.
#include<iostream>
#include<cstring>
#include<stack>
using namespace std;
// get weight of operators as per precedence
// higher weight given to operators with higher precedence
// for non operators, return 0
int getWeight(char ch) {
switch (ch) {
case '/':
case '*': return 2;
case '+':
case '-': return 1;
default : return 0;
}
}
// convert infix expression to postfix using a stack
void infix2postfix(char infix[], char postfix[], int size) {
stack<char> s;
int weight;
int i = 0;
int k = 0;
char ch;
// iterate over the infix expression
while (i < size) {
ch = infix[i];
if (ch == '(') {
// simply push the opening parenthesis
s.push(ch);
i++;
continue;
}
if (ch == ')') {
// if we see a closing parenthesis,
// pop of all the elements and append it to
// the postfix expression till we encounter
// a opening parenthesis
while (!s.empty() && s.top() != '(') {
postfix[k++] = s.top();
s.pop();
}
// pop off the opening parenthesis also
if (!s.empty()) {
s.pop();
}
i++;
continue;
}
weight = getWeight(ch);
if (weight == 0) {
// we saw an operand
// simply append it to postfix expression
postfix[k++] = ch;
}
else {
// we saw an operator
if (s.empty()) {
// simply push the operator onto stack if
// stack is empty
s.push(ch);
}
else {
// pop of all the operators from the stack and
// append it to the postfix expression till we
// see an operator with a lower precedence that
// the current operator
while (!s.empty() && s.top() != '(' &&
weight <= getWeight(s.top())) {
postfix[k++] = s.top();
s.pop();
}
// push the current operator onto stack
s.push(ch);
}
}
i++;
}
// pop of the remaining operators present in the stack
// and append it to postfix expression
while (!s.empty()) {
postfix[k++] = s.top();
s.pop();
}
postfix[k] = 0; // null terminate the postfix expression
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.