Question

Something is either messed up in my operator overload <<, covertopostfix function, or my main output....

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

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

expreesion.h


class expression {
public:
bool last;
expression();
friend std::istream& operator >> (std::istream&, expression&);
friend std::ostream& operator<<(std::ostream&, expression&);
void convertToPostFix();
bool isOperator(char currentChar);
queue<char> obj;
private:
std::string ifix, pfix;
  
bool const precedence(char, char);
};

expression.cpp which contains the convert to post fix function




#include
#include"expression.h"
#include
#include
#include
#include

using namespace std;


void expression::convertToPostFix()
{
{

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

}

Homework Answers

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Using the following code perform ALL of the tasks below in C++: ------------------------------------------------------------------------------------------------------------------------------------------- Implementation: Overload input...
Using the following code perform ALL of the tasks below in C++: ------------------------------------------------------------------------------------------------------------------------------------------- Implementation: Overload input operator>> a bigint in the following manner: Read in any number of digits [0-9] until a semi colon ";" is encountered. The number may span over multiple lines. You can assume the input is valid. Overload the operator+ so that it adds two bigint together. Overload the subscript operator[]. It should return the i-th digit, where i is the 10^i position. So the first...
How do I fix my error of binding on line 74? #include <iostream> #include <fstream> #include...
How do I fix my error of binding on line 74? #include <iostream> #include <fstream> #include <cctype> #include <cstring> #include <iomanip> using namespace std; #include "AvlTree.h" class WordCount { public:     char *word;     int *lines;     int count;     int size;     bool operator<(const WordCount &rhs) const {return strcmp(word, rhs.word) < 0;}     bool operator!= (const WordCount &rhs) const {return strcmp(word, rhs.word) != 0;}     WordCount():lines(NULL), count(0), size(0) {word = new char[1]; word[0] = '\0';}     friend ostream& operator<<...
Please Use C++ I tried to calculate complex number by using *= and operator /= but...
Please Use C++ I tried to calculate complex number by using *= and operator /= but I got an incorrect result compared with the result of complex number calculator For example, When I calculate ( (c5 *= c4) *= c4) by using my operator function, the result was 1.08288e+06+1.11262e+07i on output, However, when using a complex calculator, the result was = −253987.448 − 355181.112i, so I got the wrong answer There is my code below. It compiles well, but my...
Data Structure in C++ I keep getting the same warning, and I cant seem to fix...
Data Structure in C++ I keep getting the same warning, and I cant seem to fix it.. Can you explain to me what I am doing wrong? Warning: dlist.cc: In function 'std::ostream& operator<<(std::ostream&, dlist&)': dlist.cc:66:10: error: invalid initialization of reference of type 'std::ostream& {aka std::basic_ostream&}' from expression of type 'dlist::node*' dlist.cc: In function 'dlist operator+(dlist&, dlist&)': dlist.cc:93:8: error: invalid operands of types 'dlist::node*' and 'dlist::node*' to binary 'operator+' dlist.cc:97:8: error: could not convert 'result' from 'int' to 'dlist' My code:...
I'm having a warning in my visual studio 2019, Can anyone please solve this warning. Severity  ...
I'm having a warning in my visual studio 2019, Can anyone please solve this warning. Severity   Code   Description   Project   File   Line   Suppression State Warning   C6385   Reading invalid data from 'DynamicStack': the readable size is '(unsigned int)*28+4' bytes, but '56' bytes may be read.   Here is the C++ code were I'm having the warning. // Sstack.cpp #include "SStack.h" // Constructor SStack::SStack(int cap) : Capacity(cap), used(0) {    DynamicStack = new string[Capacity]; } // Copy Constructor SStack::SStack(const SStack& s) : Capacity(s.Capacity), used(s.used)...
Getting the following errors: Error 1 error C2436: '{ctor}' : member function or nested class in...
Getting the following errors: Error 1 error C2436: '{ctor}' : member function or nested class in constructor initializer list on line 565 Error 2 error C2436: '{ctor}' : member function or nested class in constructor initializer list on line 761 I need this code to COMPILE and RUN, but I cannot get rid of this error. Please Help!! #include #include #include #include using namespace std; enum contactGroupType {// used in extPersonType FAMILY, FRIEND, BUSINESS, UNFILLED }; class addressType { private:...
Data Structures using C++ Consider the following class #ifndef LINKEDQUEUETYPE_H #define LINKEDQUEUETYPE_H #include <iostream> #include <new>...
Data Structures using C++ Consider the following class #ifndef LINKEDQUEUETYPE_H #define LINKEDQUEUETYPE_H #include <iostream> #include <new>    #include <cstdlib> #include "QueueADT.h" using namespace std; // Definition of the node template <class ItemType> struct NodeType {        ItemType info;        NodeType<ItemType> *next; }; template <class ItemType> class LinkedQueueType: public QueueADT<ItemType> { public:        // Constructor        LinkedQueueType();           // Default constructor.           // Post: An empty queue has been created. queueFront = NULL;           //       queueBack = NULL;...
I'm currently stuck on Level 3 for the following assignment. When passing my program through testing...
I'm currently stuck on Level 3 for the following assignment. When passing my program through testing associated with the assignment it is failing one part of testing.   Below is the test that fails: Failed test 4: differences in output arguments: -c input data: a b c -c expected stdout: b observed stdout: a b expected stderr: observed stderr: ./test: invalid option -- 'c' Unsure where I have gone wrong. MUST BE WRITTEN IN C++ Task Level 1: Basic operation Complete...
Can someone please edit my code so that it satisfies the assignments' requirements? I pasted the...
Can someone please edit my code so that it satisfies the assignments' requirements? I pasted the codes below. Requirement: Goals for This Project:  Using class to model Abstract Data Type  OOP-Data Encapsulation You are asked to write an app to keep track of a relatively small music library. The app should load song information from a data file once the app is started. It should allow user to view, add, remove, and search for songs. The app should...
Need to get the following output by Editing ChekingAccount.h ,ChekingAccount.cpp CheckingAccount Derived class CheckingAccount that inherits...
Need to get the following output by Editing ChekingAccount.h ,ChekingAccount.cpp CheckingAccount Derived class CheckingAccount that inherits from base class Account and include an additional data member of type double that represents the fee charged per transaction (transactionFee). Write Checking- Account’s constructor that receives the initial balance, as well as a parameter indicating a transaction fee amount. If transaction fee is less than zero, the transactionFee will be set to zero. Write the chargeFee member function that updates the balance by...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT