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<<...
- implement the Stack ADT using the linked list approach. Use C++ program language #include "StackLinked.h"...
- implement the Stack ADT using the linked list approach. Use C++ program language #include "StackLinked.h" template StackLinked::StackLinked (int maxNumber) { } template StackLinked::StackLinked(const StackLinked& other) { } template StackLinked& StackLinked::operator=(const StackLinked& other) { } template StackLinked::~StackLinked() {    clear(); } template void StackLinked::push(const DataType& newDataItem) throw (logic_error) {    } template DataType StackLinked::pop() throw (logic_error) { } template void StackLinked::clear() {    StackNode* t;    while ( top != NULL)    {        t = top;       ...
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:...
Use the following algorithm: (1) Create three stacks of characters: stk1, stk2, stk3. (2) Read the...
Use the following algorithm: (1) Create three stacks of characters: stk1, stk2, stk3. (2) Read the input infix expression one character at a time and push every character read ( other than ')' ) onto stk1. Do not push the character read if it is ')'. (3) As long as stk1 is not empty, do the following:            Fetch the top character ( call it c ) of stk1 and pop stk1.            if c is an alphabetic character (a-z),...
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...
Data Structures using C++ Consider the classes QueueADT and ArrayQueueType: QueueADT: #ifndef QUEUEADT_H #define QUEUEADT_H template...
Data Structures using C++ Consider the classes QueueADT and ArrayQueueType: QueueADT: #ifndef QUEUEADT_H #define QUEUEADT_H template <class ItemType> class QueueADT { public:        // Action responsibilities        virtual void resetQueue() = 0;           // Reset the queue to an empty queue.           // Post: Queue is empty.        virtual void add(const ItemType& newItem) = 0;           // Function to add newItem to the queue.           // Pre: The queue exists and is not full.          ...
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;...
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...
Programming Exercise 2: implement the member function moveToNth(...) that removes the item marked by the cursor...
Programming Exercise 2: implement the member function moveToNth(...) that removes the item marked by the cursor and inserts it as the nth element of the list; test your implementation by turning the flag LAB3_TEST2 from 0 to 1 in config.h; - Programming Exercise 3: implement the ListArray member function find(...) that searches for the element given as a parameter; the search starts at the cursor and stops when it finds the element or at the end of the list; the...