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

#include <iostream> #include <string> #include \"myStack.h\" #include <stdio.h>

ID: 3750525 • Letter: #

Question

#include <iostream>

#include <string>

#include "myStack.h"

#include <stdio.h>

#include <ctype.h>

#include <climits>

using namespace std;

int main()

{

cout << "***************************************************************** " << endl;

cout << "*************** postfix mini calculator ************************* " << endl;

cout << "***************************************************************** " << endl;

//Todo: Create your stack

char inputItem;

bool flag = true;

while (true)

{

cout << "Enter your input items. Then enter 'c' at the end of your input line!" << endl;

cout << "Enter 'H' to halt the program at any time!" << endl;

while (flag)

{

cin >> inputItem;

if (inputItem == 'H')

{

cout << "***************************************************************** " << endl;

cout << "***************************************************************** " << endl;

cout << "***************************************************************** " << endl;

return 0;

}

else if (inputItem == 'c')

{

//Todo:

//1) Check if the input line is in the correct format.

//2) If true, compute the results based on the algorithm and print for the user.

//3) Otherwise, print a meaningful error message and pop all items of the stack.

flag = false;

}

else if (isspace(inputItem))

{

continue;

}

else if ((inputItem == '+') || (inputItem == '/') || (inputItem == '-') || (inputItem == '*'))

{

//Follow the steps mentioned in the algorithm for operators.

}

else

{

int num = inputItem - '0';

if ((num >= 0) && (num <= 9))

{

//Follow the steps mentioned in the algorithm for integer values.

}

else

{

cout << "Invalid input line!!!" << endl;

flag = false;

//Pop all input items

while ((getchar() != ' '));

}

}

}

flag = true;

}

//myStack.h

#ifndef STACK_H

#define STACK_H

#define MAX 1000

template <class userType>

class myStack

{

int CurrentSize; //An indicator that contains the current size of the stack

public:

userType holder[MAX]; //Maximum size of the stack

myStack();

bool push(userType x);

userType pop();

bool isEmpty();

};

template <class userType>

myStack::myStack()

{

//Todo: initialize the value of the currentSize.

}

template <class userType>

bool myStack::push(userType x)

{

if (isEmpty)

{

//Todo

//Push the item x to myStack.

//Update the value of the currentSize.

return true;

}

else

{

//Todo:

//print a meaningful error message

return false;

}

}

template <class userType>

userType myStack::pop()

{

//Todo:

//Pop the currentSize element of the stack (e.g. x).

//update the value of the currentSize.

//return x.

}

template <class userType>

bool myStack::isEmpty()

{

//Check if the size of the stack is still less than the MAX value.

}

#endif // STACK_H

#pragma once

1.In this question, you are going to implement a stack data structure in part 1 and then use it to create postfix mini-calculator in section 2 1.1. Use the myStack.h to implement a stack template. It should be able to work for arbitrary data types (char, int, double, etc.) 1.2. Postfix calculato: Complete the provided main script to implement a mini postfix calculator for integer numbers. We use infix notation in regular calculations like 2+4 3, while its postfix notation is 2 4 3* +. Use your stack data structure from part 1.1 and the following algorithm to to create the postfix calculator: » Read a line from terminal as the user input » If the input is a valid integer, you push that integer onto the stack. If the input is a valid operator (+, *, /, -), pop two integers off the stack, perform the requested operation, and push the result back onto the stack If there were less than 2 integers remained in the stack, print a meaningful error message If the user enters "c", you need to print the final result. If the user input line was not in a correct format, you should print a meaningful message If the user enters "H", you halt the program without performing any additional computations For any other input characters, print a meaningful error message » » . 2. Use the myQueue.cpp and myQueue.h to implement a queue data structure for integer data type: To keep track of the front and rear of the queue, you can use two integer indicators. Be careful about the "empty" and "full" state. You can use one of the three implementations that distinguish these two states » » » Complete the main script of this question

Explanation / Answer

Here is the working code for the postfix evaluation code for an expression,

Please comment below for any further queries regarding the answer or for modifying the code.

Thank you.

#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#include <stack>
#include <string.h>

using namespace std;

bool is_Operator(char symbl)
{
if (symbl=='+' || symbl=='-' || symbl=='*' || symbl=='/')
return true;
else
return false;
}


int performOperation(int op1, int op2, char op)
{
int answr;
switch(option){
case '+':
answr = op2 + op1;
break;
case '-':
answr = op2 - op1;
break;
case '*':
answr = op2 * op1;
break;
case '/':
answr = op2 / op1;
break;
}
return answr;
}


int main()
{
char expr[1000], buff[15];
int i,op1, op2, length, j, x;
stack<int> s;
printf("Enter a Postfix Expression: ( e.g. 23 34 * ) ");
gets(expr);
length = strlen(expr);
j = 0;
for(i=0; i<length;i++){

if(expr[i]>='0' && expr[i]<='9'){
buff[j++] = expr[i];
}
else if(expr[i]==' '){
if(j>0){
buff[j] = '';
x = atoi(buff);
s.push(x);
j = 0;
}
}

else if(is_Operator(expr[i])){
op1 = s.top();
s.pop();
op2 = s.top();
s.pop();
s.push(performOperation(op1, op2, expr[i]));
}
}

printf("Answer is %d ", s.top());

return 0;
}