Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Using the code below (assuming that all funtions and objects have been defined)

ID: 3761297 • Letter: U

Question

Using the code below (assuming that all funtions and objects have been defined) fill in the calculate postfix funtion.

#include <iostream>
#include <cmath>
#include <cstring>
#include "GenericList.cc"
#include "Queue.cc"
#include "Stack.cc"
using namespace std;

Queue<char> Convert(Queue<char>&);
double Calculate(Queue<char>&);
bool IsOperand(char);
int InfixPriority(char);
int StackPriority(char);

int main()
{
   char line[256];

   while (cin.getline(line, 256))
   {
       Queue<char> infix;
       Queue<char> postfix;

       for (int i=0; i<strlen(line); i++)
           infix.Enqueue(line[i]);
       cout << infix << endl;
       postfix = Convert(infix);
       cout << postfix << endl;
       cout << Calculate(postfix) << endl << endl;
   }

   return 0;
}

Queue<char> Convert(Queue<char>& infix)
{
    Queue<char> postfix;
    Stack<char> oper;
    while(!infix.IsEmpty())
    {

        char t= infix.Peek();
        infix.Dequeue();

        if(IsOperand(t))
        {
            postfix.Enqueue((t));
        }
        else if(oper.IsEmpty())
        {
            oper.Push(t);
        }
        else if(t=='(')
        {
            oper.Push(t);
        }
        else if(t==')')
        {
            while(oper.Peek()!= '(')
            {
                postfix.Enqueue(oper.Peek());
                oper.Pop();
            }
            oper.Pop();
        }
        else
        {
            while(!oper.IsEmpty() && oper.Peek()!= '(' && InfixPriority(t)<= StackPriority(oper.Peek()))
            {
                postfix.Enqueue(oper.Peek());
                oper.Pop();
            }
            oper.Push(t);
        }
    }
    while (!oper.IsEmpty())
    {
        postfix.Enqueue(oper.Peek());
        oper.Pop();
    }
    return postfix;
}

double Calculate(Queue<char>& postfix)
{
   Stack<double> S;
  
   while(!postfix.IsEmpty())
    {
      
    }
  
   return S.Pop();
}

bool IsOperand(char c)
{
   return (c >= '0' && c <= '9');
}

int InfixPriority(char c)
{
   switch (c)
   {
       case '(':
           return 4;
       case '^':
           return 3;
       case '*':
       case '/':
           return 2;
       case '+':
       case '-':
           return 1;
       default:
           return 0;
   }
}

int StackPriority(char c)
{
   switch (c)
   {
       case '^':
       case '*':
       case '/':
           return 2;
       case '+':
       case '-':
           return 1;
       default:
           return 0;
   }
}

okay so this code works it should print out e.g.

5+7

57+

12   i have it printing out 5+7 and 57+ but i cannot get it to calculate the expression.

ps this should work for amore complex single digit expressions such as 2^2^2

thank you

Explanation / Answer

# include <iostream>
# include <cstring>

using namespace std;

struct node
{
char data;
node *next;
};

node *top=NULL;
node *bottom=NULL;
node *entry;
node *endentry;
node *secondendentry;

void push(const char Element)
{
entry=new node;
if(bottom==NULL) {
entry->data=Element;
entry->next=NULL;
bottom=entry;
top=entry;
}
else
{
entry->data=Element;
entry->next=NULL;
top->next=entry;
top=entry;
}
}

const char pop( ) {
char Element=NULL;

if(bottom==NULL)
cout<<" Stack is empty. "<<endl;

else {
for (endentry=bottom; endentry->next!=NULL; endentry=endentry->next)
secondendentry=endentry;

if(top==bottom)
bottom=NULL;

Element=top->data;

delete top;

top=secondendentry;
top->next=NULL;
}

return Element ;
}

void infix_to_postfix(const char *Infix) {
char Infix_expression[100]={NULL};
char Postfix_expression[100]={NULL};

strcpy(Infix_expression,"(");
strcat(Infix_expression,Infix);
strcat(Infix_expression,")");

char Element[5]={NULL};
char Temp[5]={NULL};

for(int count=0;count<strlen(Infix_expression);count++) {
Element[0]=Infix_expression[count];

if(Element[0]=='(')
push(Element[0]);

else if(Element[0]==')') {
Element[0]=pop( );

while(Element[0]!='(')
{
strcat(Postfix_expression,Element);

Element[0]=pop( );
}
}

else if(Element[0]=='^' || Element[0]=='*' || Element[0]=='/'
|| Element[0]=='+' || Element[0]=='-')
{
if(Element[0]=='*' || Element[0]=='/')
{
Temp[0]=pop( );

while(Temp[0]=='^' || Temp[0]=='*' || Temp[0]=='/')
{
strcat(Postfix_expression,Temp);

Temp[0]=pop( );
}

push(Temp[0]);
}

else if(Element[0]=='+' || Element[0]=='-')
{
Temp[0]=pop( );

while(Temp[0]!='(')
{
strcat(Postfix_expression,Temp);

Temp[0]=pop( );
}

push(Temp[0]);
}

push(Element[0]);
}

else
strcat(Postfix_expression,Element);
}

cout<<" Postfix Expression : "<<Postfix_expression;
}

int main( ) {
char Infix_expression[100]={NULL};
cout<<" Enter the Infix Expression : ";
cin>>Infix_expression;
infix_to_postfix(Infix_expression);
return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at drjack9650@gmail.com
Chat Now And Get Quote