VIC55

CLICK HERE TO DOWNLOAD THIS ANSWER INSTANTLY

Postfix Expression Calculator Write a program that converts an infix expression into an equivalent postfix expression. The rules to convert an infix expression into an equivalent postfix expression are as follows: Suppose infx represents the infix expression and pfx represents the postfix expression. The rules to convert infx into pfx are as follows: a. Initialize pfx to an empty expression and also initialize the stack. b. Get the next symbol, sym, from infx. b.1. If sym is an operand, append sym to pfx. b.2. If sym is (, push sym into the stack. b.3. If sym is ), pop and append all of the symbols from the stack until the most recent left parentheses. Pop and discard the left parentheses. b.4. If sym is an operator: b.4.1. Pop and append all of the operators from the stack to pfx that are above the most recent left parentheses and have precedence greater than or equal to sym. b.4.2. Push sym onto the stack. c. After processing infx, some operators might be left in the stack. Pop and append to pfx everything from the stack. In this program, you will consider the following (binary) arithmetic operators: +, -, *, and/. You may assume that the expressions you will process are error free. Design a class that stores the infix and postfix strings. Below you can find the class information. In main, test your program on the following expressions after reading them from a text file: a. A + B – C; b. (A + B) * C; c. (A + B) + (C – D); d. A + ((B + C) (E – F) – G) / (H – I) e. A + B * (C + D) – E /F G + H; f. 2+4-1; g. (6+3) *2; For each expression, your answer must be in the following form: Infix Expression: A + B – C; Postfix Expression: ABC- If the expression has digits, the program must also evaluate and print the numeric result. To achieve this, define the client function named evaluate Postfix; //The function below takes a string and checks whether this string has digits or not. If it has digits, the function evaluate the expression using a stack and returns the result of the expression. Otherwise the function returns -l (eg, for the expressions such as A B-C) int evaluatePostfix(char* exp); Sample Output: Infix Expression: A+B-C; Postfix Expression: AB+C- Infix Expression: (A+B)*C: Postfix Expression: AB+C* Infix Expression: (A+B) CC-D>: Postfix Expression: AB CD- Infix Expression: A+((B+C)*(E-F)-G/CH-I> Postfix Expression: ABC EF-“G-HI-/+ Infix Expression: A+B*(C+D)-E/F G+H; Postfix Expression : ABCD+*+EF/GM-H Infix Expression: 2+4-1; Postfix Expression: 24+1 Result of Postfix Expression:51 Infix Expression: (6+3)*2; Postfix Expression: 63+2* Result of Postfix Expression:18 Report: Report must include the detailed explanation of each function that you’ve defined or write its body for this question as well as the sample screenshots, // Header file: mystack.h #ifndef H StackType #define H StackType #include #include class stackType : public stackADTType> public: const stackType Type>& operator-(const stackType Type>&); 7 /overload the assignment operator. void initializeStack() //Function to initialize the stack to an empty state. // Post condition: StackTop = 0 bool is Emptystack() const; //Function to determine whether the stack is empty. //Postcondition: Returns true if the stack is empty, otherwise return false. bool isFullstack const; //Function to determine whether the stack is full. //Postcondition: Returns true if the stack is full, otherwise return false. void push(const Type& newItem) //Function to add newItem to the stack. // Precondition: The stack exists and is not full. //Postcondition: The stack is changed and newItem is added to the top of the stack. Type top const; //Function to return the top element of the stack. // Precondition: The stack exists and is not empty. // Postcondition: If the stack is empty, the program terdinates; otherwise, the top elentent of the stack is returned. void pop(); //Function to remove the top element of the stack. //Precondition. The stack exists and is not empty. V/Post condition: The stack is changed and the top element is removed from the stack stackType(int stackSize 100) StackType(int StackSize 100) V/ constructor // Create an array of the size stackSize to hold v/the stock elements. The default stack size is 100. 1/Postcondition: The variable list contains the base address of the array, stackTop – , and maxStackSize stackSize. stackType(const stackType Type> other stack): //copy constructor stackType(); //destructor //Remove all the elements from the stack. //Postcondition: The array (list) holding the stack elements is deleted. private: int maxStackSize; //variable to store the maximum stack size int stackTop: //variable to point to the top of the stack Type list; //pointer to the array that holds the stack elements void copyStack(const stackType< Type>& otherStack) Function to make a copy of otherStack. 7/Postcondition: A copy of otherStack is created and assigned to this stack. template void stackType Type:initializeStack() stackTop – B; //end initializeStack template bool stackType Type>: isEmptyStack() const return(stackTop }//end isEmptyStack 0); template bool stackTypeType> : isFullstack() const return(StackTop == maxstack size); ) //end isFullstack template void stackTypeType>push(const Type& newIte) if (isFullstack() listretto telplate push const Types if Stack list stackTo W I W Wadd newItem to the Wtop of the stack StackTop++ Wincrement stack Top cout << “Cannot add to a full stack.” }//end push endl; template stackType Type>::stackType( destructor delete[] list; //deallocate the memory occupied // by the array }//end destructor template void stackType Typer copyStack(const stackType Type>& otherStack) delete[] list; maxStackSize otherStack, naxStackSize; stackTop – otherStack.stacktop: list – new Type(maxStackSize]: //copy otherStack into this stack for (int j = 0; j < StackTop: j++ list[3] = other stack.list[il: } //end copyStack template stackType Type>:: stackType(const stackTypeType>& otherStack) list = NULL; copyStack(otherStack) }//end copy constructor template const stackTypeType>& stack Type Type:: operator (const stackType Type>& otherStack) if (this ! SotherStack) tavold self-copy copyStack(otherstack); return this: > Wend operators #endit Header file: stadKADT tindef H.SEALADT #define H St KADT te plate class stackADT public: virtual void initializestack() – Dj /Method to initialize the stack to an empty state. // Postcondition: Stack is empty virtual bool isEmptystack() const = 0; Function to determine whether the stack is empty //Postcondition: Returns true if the stack is empty. otherwise returns false. virtual bool isFullstack const = 0; // Function to determine whether the stack is full. // Postcondition: Returns true if the stack is full, otherwise returns false. virtual void push(const Type & newItem) = 0; // Function to add newItem to the stack. 7/Precondition: The stack exists and is not full. //Postcondition: The stack is changed and newItem is added to the top of the stack. virtual Type top() const = 0; //Function to return the top element of the stack. //Precondition: The stack exists and is not empty. //Postcondition: If the stack is empty, the program terminates; otherwise, the top element of the stack is returned. virtual void pop() = 0; //Function to remove the top element of the stack. //Precondition: The stack exists and is not empty. //Postcondition: The stack is changed and the top element is removed from the stack. #Endif