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

Show the contents of the two stacks as you trace the algorithm evaluateInfix, gi

ID: 3678194 • Letter: S

Question

Show the contents of the two stacks as you trace the algorithm evaluateInfix, given in the photo below, write a java class that will evaluate each of the following infix expressions. Assume that a = 2, b = 3, c = 4, d = 5, e= 6, and f = 7.

a.) (a + b) / (c -d) -5

dspects of its logic from the previous gu Algorithm evaluateInfixCinfix) 5.21 The algorithm. The alg Evaluates an inf amew empty shack operatorstack valueStacka new empty stack nextCharacter next nonblank character ofinfix switch (nextCharacter) le Cinfix has characters left to process) chatera valueStack.push Covalue of the variable nextCharacter) break case nariable case A' operatorStack.push(nextCharacter) break case '+' : case ,-' : case '*, : case '/' : while (loperatorStack.isEmptyO and precedence ofnextChar acter ence of nextCharacter precedence ofoperatorst precedence Execute operator at top ofoperatorStack topoperator = operatorStack-pop() operandTwo valueStack.pop O operandOne valueStack.pop O result - the result of the operation in topOperator and its operandOne and operandTwo operands valueStack.push(result) operatorStack.push (nextCharacter) break case ,(' : operatorStack.push(nextCharacter) break topoperator operatorStack, pop() while (topoperator !'(') ession is valid operandTwo = valueStack, pop() operandOne - valueStack.popO result = the result of the operation in topoper ator and its operands operandOne and operandTwo valueStack.push(result) topoperator = operato rStack. pop() break

Explanation / Answer

package assignment;

import java.util.Stack;

public class InfixExpressionEval {
   double a = 2, b = 3, c = 4, d = 5, e= 6, f = 7;
  
   public static void main(String[] theArgs) {
      
       String expr = " (a + b) / (c -d) -5 ";
       InfixExpressionEval infixeval = new InfixExpressionEval();
       System.out.println("Expr: "+expr+" Value: "+infixeval.evaluateInfix(expr));
      
   }
  
   public double evaluateInfix(String expr) {
       char topOperator = ' ';
       double operandOne = 0;
       double operandTwo = 0;
       double result = 0;
       //change datatye from integer to float/double based on your requirement
      
       Stack<Character> operatorStack = new Stack<Character>();
       Stack<Double> valueStack = new Stack<Double>();
       int count = 0;
       char nextCharacter = ' ';
       while(count < expr.length()) {
           topOperator = ' ';
            operandOne = 0;
           operandTwo = 0;
           result = 0;
           nextCharacter = expr.charAt(count);
           count++;
           switch(nextCharacter) {
               case 'a':
                   valueStack.push(a);
                   break;
               case 'b':
                   valueStack.push(b);
                   break;
               case 'c':
                   valueStack.push(c);
                   break;
               case 'd':
                   valueStack.push(d);
                   break;
               case 'e':
                   valueStack.push(e);
                   break;
               case 'f':
                   valueStack.push(f);
                   break;
               case '^':
                   //this is in algorithm
                   operatorStack.push(nextCharacter);
                   break;
                  
               case '+':
               case '-':
               case '*':
               case '/':
               System.out.println(nextCharacter+" "+operatorStack.toString());
                   while(!operatorStack.isEmpty() && isLowerOrEqualPrecedence(nextCharacter, operatorStack.peek())) {
                           topOperator = operatorStack.pop();
                           operandTwo = valueStack.pop();
                           operandOne = valueStack.pop();
                           result = result(topOperator, operandOne, operandTwo);
                           valueStack.push(result);
                   }
                   operatorStack.push(nextCharacter);
                   //System.out.println(operatorStack.toString());
                   break;
              
               case '(':
                   operatorStack.push(nextCharacter);
                   break;
               case ')':
                   topOperator = operatorStack.pop();
                  
                   while(topOperator != '(') {
                       operandTwo = valueStack.pop();
                       operandOne = valueStack.pop();
                       result = result(topOperator, operandOne, operandTwo);
                       valueStack.push(result);
                       topOperator = operatorStack.pop();
                       if(topOperator == '(')
                           break;
                      
                   }
                   //System.out.println(valueStack.toString());
                   break;
               default:
                   if(Character.isDigit(nextCharacter))
                       valueStack.push((double)Character.digit(nextCharacter, 10));
                   break;
           }
       }
      
       System.out.println(valueStack.toString());
       System.out.println(operatorStack.toString());
       while(!operatorStack.isEmpty() && valueStack.size() > 1) {
           topOperator = operatorStack.pop();
           operandTwo = valueStack.pop();
           operandOne = valueStack.pop();
           result = result(topOperator, operandOne, operandTwo);
           valueStack.push(result);
       }
      
       return valueStack.peek();
   }
  
   public boolean isLowerOrEqualPrecedence(char oper1, char oper2) {
       //exponentiation has higher precedence than multiplication and division
       if(oper2 == '^')
           return true;
       else if(oper2 == '/' && oper1 != '^')
           return true;
       else if(oper2 == '*' && (oper1 == '*' || oper1 == '+' || oper1 == '-'))
           return true;
       else if(oper2 == '+' && (oper1 == '+' || oper1 == '-'))
           return true;
       else if(oper2 == '-' && (oper1 == '-'))
           return true;
      
       return false;
   }
  
   public double result(char operator, double operand1, double operand2) {
       if(operator == '+')
           return operand1+operand2;
       if(operator == '-')
           return operand1-operand2;
       if(operator == '*')
           return operand1*operand2;
       if(operator == '/')
           return operand1/operand2;
       if(operator == '^')
           return Math.pow(operand1,operand2);
       return 0;
   }
}

--output--

+ [(]
/ []
- [/, (]
- [/]
[-5.0, 5.0]
[-]
Expr: (a + b) / (c -d) -5 Value: -10.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