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

C++ Project Use the “static stack template class”, to build a “char” stack. Then

ID: 3768964 • Letter: C

Question

C++ Project

Use the “static stack template class”, to build a “char” stack. Then, write the code of the postfix expression calculator, following the algorithm given in class. We consider only one digit integers, and the division is integer division (meaning 3/2=1). The header of the function should look like:

int PostfixCalculator (string postfixExpression){ }

Use the following expression to test your code: 34*5*7+3-8+ 675*3+2/+

Use the same “static stack template class”, to build a “char” stack. Then, write the code of the infix-to-postfix converter, following the algorithm given in class. The header of the function should look like:

string InfixToPostfixConverter (string infixExpression){ }

Use the following expression to test your code: a+(b*c + d)/e (b*b – 4*a*c)/(2*a)

Explanation / Answer

public class ExpressionParser    {       // Associativity constants for operators       private static final int LEFT_ASSOC  = 0;       private static final int RIGHT_ASSOC = 1;           // Operators       private static final Map OPERATORS = new HashMap();       static        {           // Map           OPERATORS.put("+", new int[] { 0, LEFT_ASSOC });           OPERATORS.put("-", new int[] { 0, LEFT_ASSOC });           OPERATORS.put("*", new int[] { 5, LEFT_ASSOC });           OPERATORS.put("/", new int[] { 5, LEFT_ASSOC });               }           // Test if token is an operator       private static boolean isOperator(String token)        {           return OPERATORS.containsKey(token);       }           // Test associativity of operator token       private static boolean isAssociative(String token, int type)        {           if (!isOperator(token))            {               throw new IllegalArgumentException("Invalid token: " + token);           }                      if (OPERATORS.get(token)[1] == type) {               return true;           }           return false;       }           // Compare precedence of operators.           private static final int cmpPrecedence(String token1, String token2)        {           if (!isOperator(token1) || !isOperator(token2))            {               throw new IllegalArgumentException("Invalid tokens: " + token1                       + " " + token2);           }           return OPERATORS.get(token1)[0] - OPERATORS.get(token2)[0];       }           // Convert infix expression format into reverse Polish notation       public static String[] infixToRPN(String[] inputTokens)        {           ArrayList out = new ArrayList();           Stack stack = new Stack();                      // For each token           for (String token : inputTokens)            {               // If token is an operator               if (isOperator(token))                {                     // While stack not empty AND stack top element                    // is an operator                   while (!stack.empty() && isOperator(stack.peek()))                    {                                           if ((isAssociative(token, LEFT_ASSOC)         &&                             cmpPrecedence(token, stack.peek())
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