Question: Help me change this code. I have to change this code with using template T And i have to also put menu on main(1.infix to

Help me change this code.

I have to change this code with using template T

And i have to also put menu on main(1.infix to postfix. 2.postfixevaluation 3.exit)(Whenever there is a user entry, there is a validation)

// CODE // Operators allowed : '+' , '-' , '*', '/', '(', ')' and '^' (for exponentiation) #include #include #include"IntStack.h" using namespace std;

class infixEval { private: string infix; string postfix;

// To verify whether a character is an operand(letter or numeric digit) or not bool is_operand(char c) { return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'); }

// To verify whether a character is operator or not bool is_operator(char c) { return (c == '+' || c == '-' || c == '*' || c == '/' || c == '^'); }

// To verify whether an operator is right associative or not int is_right_associative(char op) { return (op == '^'); }

// To get weight of an operator // The operator with higher weight will have higher precedence int operator_weight(char op) { switch(op) { case '+': case '-': return 1; case '*': case '/': return 2; case '^': return 3; } return -1; }

// To perform an operation and return output bool has_higher_precedence(char instack, char incoming) { int in_stack_weight = operator_weight(instack); int incoming_weight = operator_weight(incoming); // If operators have equal precedence, return true if they are left associative // return false, if right associative // if operator is left-associative, left one should be given priority if(in_stack_weight == incoming_weight) { if(is_right_associative(instack)) return false; else return true; } return (in_stack_weight > incoming_weight); }

int evaluate(char op, int o1, int o2) { switch(op) { case '+': return o1+o2; case '-': return o1-o2; case '*': return o1*o2; case '/': return o1/o2; case '^': return o1^o2; default: return 0; } } public: infixEval(string expression) { this->infix = expression; postfix = ""; // Initialize postfix as empty string. }

// To convert Infix expression to postfix string infix_to_postfix() { IntStack S;// Declaring a Stack

// scan the input from left to right, one character at a time for(int i = 0;i< infix.length();i++) { char c = infix[i]; // If character is operator // pop two elements from stack and // perform operation and push the result back. if(is_operator(c)) { while(!S.empty() && ((char)S.peek()) != '(' && has_higher_precedence(((char)S.peek()),c)) { postfix+= ((char)S.peek()); S.pop(); } S.push(c); // push the operator to the stack } else if(is_operand(c)) { // if character is an operand // add it directly to the postfix output postfix += c; } else if (c == '(') { S.push(c); } else if(c == ')') { while(!S.empty() && ((char)S.peek()) != '(') { postfix += (char)S.peek(); S.pop(); } S.pop(); } } while(!S.empty()) { postfix += (char)S.peek(); S.pop(); } return postfix; }

int postfix_eval() { if(postfix == "") return 0;

IntStack S; // stack for(int i = 0;i< postfix.length();i++) { char c = postfix[i];

if(is_operand(c)) S.push(c-48); //x-48 for removing the effect of ASCII else { int op2 = S.peek(); // get top value S.pop();// remove top value int op1 = S.peek(); // get top value

S.pop(); // remove top value int val = evaluate(c,op1,op2); S.push(val); } } return S.peek(); } }; int main() { string infix;

// read the infix notation cout<<"Enter Infix Expression "; getline(cin,infix);

infixEval iev(infix);

// compute the ppostfix notation string postfix = iev.infix_to_postfix();

// output the postfix notation cout<<"Postfix = "<

int value = iev.postfix_eval(); cout<

// IntStack.h

#pragma once #define CAPACITY 128 typedef int StackElement; class IntStack { private: StackElement stackArray[CAPACITY]; int stack_size; public: IntStack(); bool empty(); void push(StackElement item); StackElement pop(); StackElement peek(); };

// IntStack.cpp

#include #include "IntStack.h" #define CAPACITY 128 typedef int StackElement; using namespace std; /*Creating an operand Structure*/

IntStack::IntStack() { stack_size = 0; }

bool IntStack::empty() { if (stack_size == 0) return true; else return false; }

void IntStack::push(StackElement item) { if (stack_size == CAPACITY) cout << "Stack is full "; else { stackArray[stack_size] = item; stack_size++; } }

StackElement IntStack::pop() { stack_size--; if (stack_size >= 0) return stackArray[stack_size]; else return '\0'; }

// returns the top element with out removing it StackElement IntStack::peek() { if (stack_size > 0) return stackArray[stack_size-1]; else { return -1; } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!