Write a C program for the question below Please implement an

Write a C++ program for the question below. Please implement and use the codes that are given below.

You are going to exercise your knowledge of stacks.
First, look up and familiarize yourself with the STL stack class. You will use this class for this
assignment.


1 - Infix to Postfix
Implement the Infix to Postfix algorithm discussed in class:
string infixToPostfix(string exp)
This will take an infix expression as an argument, and return the corresponding postfix expression.
Operands will be single upper-case letter, and operators will be *, / +, -. You may assume the input
expression is correct. Your algorithm should skip over any blank spaces it finds.


2 - Postfix Evaluation
Implement the Postfix Evaluation algorithm discussed in class.
double evaluatePostfix(string exp)
This will take a postfix expression of the form generated in part 1, and evaluate it as a double value.
See below for the values of the operands.


3 - PostfixToPrefix
You will implement an algorithm to convert from postfix to prefix.
string postfixToPrefix(string exp)


The postfix to prefix conversion algorithm is as follows:

Create a stacks, S, of strings

Scan the postfix expression from left to right
(skip over whitespace)
If the character (ch) is an operand:
S.push(ch)
If the character (ch) is an operator,
x = s.pop();
y = s.pop();
S.push(ch + y + x) (string concatenation)
At the end, the resulting prefix string will be the only element in the
stack.

Main Program
Repeatedly:
1. Read an infix expression from a file (a single line), consisting of upper case letters and
operators +, -, *, /, and possible blank spaces. Assume the expression is correct.
2. Invoke the infixToPostfix function to create the corresponding postfix expression.
3. Invoke the postfixToPrefix function, passing the postfix expression generated in step 2, to
create the corresponding prefix expression.
4. Invoke the evaluatePostfix function, passing the postfix expression generated in step 2, to
evaluate the expression. Assume the following operand values (this may be hard-coded):
A: 2.0 B: 3.0 C: 4.0 D: 5.0 E: 6.0

Use the following file of infix expressions for your run:

***NOTE: This file should be opened as a .txt file.***

A + B * C
( A + B ) * C
A *( B + C * D )+ E
A * ( ( E / B ) + C )
( A – B ) / C * ( D + E )


Output should look like this:

CODES:

.....................................

LinkedStack.cpp Codes:

......................................

#include         // For assert

#include \"LinkedStack.h\" // Header file

LinkedStack::LinkedStack() : topPtr(nullptr)

{

} // end default constructor

LinkedStack::LinkedStack(const LinkedStack& aStack)

{

   // Point to nodes in original chain

   Node* origChainPtr = aStack.topPtr;

  

   if (origChainPtr == nullptr)

      topPtr = nullptr; // Original stack is empty

   else

   {

      // Copy first node

      topPtr = new Node();

      topPtr->setItem(origChainPtr->getItem());

     

      // Point to first node in new chain

      Node* newChainPtr = topPtr;

     

      // Advance original-chain pointer

      origChainPtr = origChainPtr->getNext();

     

      // Copy remaining nodes

      while (origChainPtr != nullptr)

      {

         // Get next item from original chain

         ItemType nextItem = origChainPtr->getItem();

        

         // Create a new node containing the next item

         Node* newNodePtr = new Node(nextItem);

        

         // Link new node to end of new chain

         newChainPtr->setNext(newNodePtr);

        

         // Advance pointer to new last node

         newChainPtr = newChainPtr->getNext();

        

         // Advance original-chain pointer

         origChainPtr = origChainPtr->getNext();

      } // end while

     

      newChainPtr->setNext(nullptr);           // Flag end of chain

   } // end if

} // end copy constructor

LinkedStack::~LinkedStack()

{

                // Pop until stack is empty

                while (!isEmpty())

                                pop();

} // end destructor

bool LinkedStack::push(const ItemType& newItem)

{

                Node* newNodePtr = new Node(newItem, topPtr);                                                        

                topPtr = newNodePtr;

    newNodePtr = nullptr;

     

                return true;

} // end push

bool LinkedStack::pop()

{

                bool result = false;

                if (!isEmpty())

                {

      // Stack is not empty; delete top

                                Node* nodeToDeletePtr = topPtr;

                                topPtr = topPtr->getNext();

                               

                                // Return deleted node to system

      nodeToDeletePtr->setNext(nullptr);

                                delete nodeToDeletePtr;

      nodeToDeletePtr = nullptr;

           

      result = true;

                } // end if

  

                return result;    

} // end pop

ItemType LinkedStack::peek() const

{

                assert(!isEmpty()); // Enforce precondition during debugging

  

                // Stack is not empty; return top

                return topPtr->getItem();

} // end getTop

bool LinkedStack::isEmpty() const

{

   return topPtr == nullptr;

} // end isEmpty

................................................................

LinkedStack.h Codes:

................................................................

#ifndef LINKED_STACK_

#define LINKED_STACK_

#include \"Node.h\"

class LinkedStack

{

private:

                Node* topPtr; // Pointer to first node in the chain;

                  // this node contains the stack\'s top

public:

                LinkedStack();                   

                LinkedStack(const LinkedStack& aStack); // Copy constructor

                ~LinkedStack();

                bool isEmpty() const;

                bool push(const ItemType& newEntry);

                bool pop();

                ItemType peek() const;

};

#endif

.......................................................

LinkedStackTest.cpp Codes:

.......................................................

#include \"LinkedStack.h\"

#include

using namespace std;

int main()

{

    LinkedStack s;

   

    cout << \"[1] \" << s.isEmpty() << endl;

   

    s.push(1);

    s.push(2);

    s.push(3);

    s.push(4);

   

    cout << \"[4] \" << s.peek() << endl;

    cout << \"[0] \" << s.isEmpty() << endl;

   

    LinkedStack s2(s);

   

    cout << \"[4] \" << s2.peek() << endl;

    cout << \"[0] \" << s2.isEmpty() << endl;

   

    s.pop();

   

    cout << \"[3] \" << s.peek() << endl;

   

    s.push(5);

   

    cout << \"[5] \" << s.peek() << endl;

   

    s.pop();

    s.pop();

    s.pop();

    s.pop();

   

     cout << \"[1] \" << s.isEmpty() << endl;

    

     //s.peek();

}

............................................................

Node.cpp Codes:

............................................................

#include \"Node.h\"

Node::Node() : next(nullptr)

{

} // end default constructor

Node::Node(const ItemType& anItem) : item(anItem), next(nullptr)

{

} // end constructor

Node::Node(const ItemType& anItem, Node* nextNodePtr) :

                item(anItem), next(nextNodePtr)

{

} // end constructor

void Node::setItem(const ItemType& anItem)

{

   item = anItem;

} // end setItem

void Node::setNext(Node* nextNodePtr)

{

   next = nextNodePtr;

} // end setNext

ItemType Node::getItem() const

{

   return item;

} // end getItem

Node* Node::getNext() const

{

   return next;

} // end getNext

........................................................

Node.h Codes:

.........................................................

#ifndef NODE_

#define NODE_

#include

using namespace std;

typedef int ItemType;

class Node

{

private:

   ItemType item; // A data item

   Node*     next; // Pointer to next node

  

public:

   Node();

   Node(const ItemType& anItem);

   Node(const ItemType& anItem, Node* nextNodePtr);

   void setItem(const ItemType& anItem);

   void setNext(Node* nextNodePtr);

   ItemType getItem() const ;

   Node* getNext() const ;

}; // end Node

#endif

infix prefix postfix value 14.0 20.0 (A+B) C ABC AB+C etc...

Solution

main.cpp

#include <stack>
#include <iostream>
#include <fstream>
#include <string>
using namespace std;


string infixToPostfix(string exp);
string postfixToPrefix(string exp);
bool precedence(char a, char b);
double evaluatePostfix(string exp);
string removespaces(string a);

int main()
{
    string exp;
    ifstream file;
    file.open(\"input.txt\");

    cout << \"infix\\t\\t\\tprefix\\t\\tpostfix\\t\\tvalue\ \ \";

    while(getline(file, exp)) {
   cout << removespaces(exp)
         << (exp.length() > 14 ? \"\\t\\t\": \"\\t\\t\\t\") << postfixToPrefix(infixToPostfix(exp))
             << (postfixToPrefix(infixToPostfix(exp)).length() > 7 ? \"\\t\": \"\\t\\t\") <<infixToPostfix(exp)
             << (infixToPostfix(exp).length() > 7 ? \"\\t\": \"\\t\\t\")<<evaluatePostfix(infixToPostfix(exp)) << \'\ \';
   }

   file.close();
}

string removespaces(string a)
{ string b;
   for (int i= 0; i < a.length(); i++)
       if(a[i] != \' \') b += a[i];  
   return b;
}

string infixToPostfix(string exp)
{
   stack<char> hold;
   string postfix;
   for (int i = 0; i<exp.length(); i++)
   {
       if (exp[i] >= \'A\' && exp[i] <= \'Z\') postfix += exp[i];
       else if (exp[i] == \'(\') hold.push(exp[i]);
       else if (exp[i] == \')\') {while (hold.top() != \'(\') {postfix += hold.top(); hold.pop();} hold.pop();}
       else if (exp[i] != \' \' ) {
           while (!hold.empty() && !precedence(exp[i], hold.top()) && hold.top() != \'(\')
               {postfix += hold.top(); hold.pop();}
           hold.push(exp[i]);
       }
   }

   while (!hold.empty()){postfix += hold.top(); hold.pop();}

   return postfix;

}

string postfixToPrefix(string exp)
{
   stack<string> hold;
   string prefix;
   for (int i = 0; i<exp.length(); i++) {
       if (exp[i] >= \'A\' && exp[i] <= \'Z\') { hold.push(string(1, exp[i]));}
       else if (exp[i] != \' \' ) {string A = hold.top(); hold.pop(); string B = hold.top(); hold.pop(); hold.push(exp[i] + B + A);};   
   }
   return hold.top();
}

bool precedence(char a, char b) { return (b == \'*\' || b == \'/\') ? false : true; }


double evaluatePostfix(string exp) {
   stack<double> hold;
   double value;
   for (int i = 0; i<exp.length(); i++)
   {
       if (exp[i] >= \'A\' && exp[i] <= \'Z\') hold.push(exp[i]-\'A\'+2);
       else {
            double A = hold.top(); hold.pop(); double B = hold.top(); hold.pop();
            switch (exp[i])
           {
           case \'+\':
              hold.push(B+A);    
              break;
           case \'-\':
              hold.push(B-A);
              break;
           case \'*\':
              hold.push(B*A);
              break;
           case \'/\':
              hold.push(B/A);
              break;
           }
       };
   }
   return hold.top();
}

input.txt

A + B * C
( A + B ) * C
A * ( B + C * D ) + E
A * ( ( E / B ) + C )
( A - B ) / C * ( D + E)
( E - (C + D)) / A


Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site