I would like my function to stop reading in the file when it hits a ‘.’ so the output file would like
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 *
instead of
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 *
InFix is: garbagehere
Post fix is: garbagehere
The input file is:
A+B
-C;
A
+ C
;
x*(y+z)-(w+t);
A+B*(C+D)-E/F+G+H;
A*B.
garbage here.
the function that is converting it is:
I would like this to stop reading the file in when its a period ‘.’
#include
#include”expression.h”
#include
#include
#include
#include
using namespace std;
void expression::convertToPostFix() //(int argc, char *argv[])
{
{
queue obj;
char chr;
ifstream fin;
fin.open(“input.txt”);//(argv[1]);
while (!fin.eof()) {
fin >> chr;
if (!fin.eof()) {
if (chr != ‘ ‘)
{
obj.push(chr);
}
}
}
ofstream fout(“output.txt”); //(argv[2]);
stack stack;
stringstream postfix;
while (!obj.empty()) {
stack.push(‘(‘);
fout << “InFix is:t”;
while (1) {
if ((obj.front() != ‘;’) && (obj.front() != ‘.’))
{
const char current = obj.front();
cout << current;
fout << current;
obj.pop();
if (isspace(current)) {
}
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);
}
else if (‘)’ == current) {
while (!stack.empty() && ‘(‘ != stack.top()) {
postfix << ‘ ‘ << stack.top();
stack.pop();
}
stack.pop();
postfix << ‘ ‘;
}
}
else
{
obj.pop();
break;
}
}
while (!stack.empty() && ‘(‘ != stack.top()) {
postfix << ‘ ‘ << stack.top();
stack.pop();
}
stack.pop();
pfix = postfix.str();
postfix.str(“”);
cout << “nPost fix is:t” << pfix << endl << endl;
fout << “nPost fix is:t” << pfix << endl << endl;
}
}
}
bool const expression::precedence(char leftOperator, char rightOperator)
{
if (leftOperator == ‘*’ || leftOperator == ‘/’) {
return true;
}
else if (rightOperator == ‘*’ || rightOperator == ‘/’) {
return false;
}
return true;
}
expression::expression() {
ifix = “”;
pfix = “”;
last = 0;
}
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;
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;
}
}