Menu

(Solved) : Part 1 Evaluate Expression Modify Code Add Operators Exponent Modulus Example 3 2 9 3 2 1 Q38174741 . . .

Part 1: (Evaluate expression)

Modify code below to add operators ^ forexponent and % for modulus.

For example, 3 ^ 2 is 9 and 3% 2 is 1.

The ^ operator has the highest precedence andthe % operator has the same precedence as the* and / operators. Your programshould prompt the user to enter an expression.

  • Sample output:

package Collection_Ch20;

import java.util.Stack;

public class EvaluateExpression {
   public static void main(String[] args) {
       // Check number of argumentspassed
       if (args.length != 1) {
          System.out.println(“Usage: java EvaluateExpression”expression””);
          System.exit(1);
       }

       try {
          System.out.println(evaluateExpression(args[0]));
       }
       catch (Exception ex) {
          System.out.println(“Wrong expression: ” + args[0]);
       }
   }

/** Evaluate an expression */
   public static int evaluateExpression(Stringexpression) {
       Stack<Integer> operandStack =new Stack<>();
       Stack<Character>operatorStack = new Stack<>();

       // Insert blanks around (, ), +,-, /, and *
       expression =insertBlanks(expression);

       // Extract operands andoperators
       String[] tokens =expression.split(” “);

       // Phase 1: Scan tokens
       for (String token: tokens) {
           if(token.length() == 0) // Blank space
              continue; // Back to the while loop to extractthe next token
           else if(token.charAt(0) == ‘+’ || token.charAt(0) == ‘-‘) {
              // Process all +, -, *, / in the top of theoperator stack
              while (!operatorStack.isEmpty() &&
                  (operatorStack.peek() == ‘+’|| operatorStack.peek() == ‘-‘ ||
                  operatorStack.peek() == ‘*’|| operatorStack.peek() == ‘/’ )) {
                 processAnOperator(operandStack, operatorStack);
              }
              // Push the + or – operator into the operatorstack
              operatorStack.push(token.charAt(0));
           }
           else if(token.charAt(0) == ‘*’ || token.charAt(0) == ‘/’ ) {
              // Process all *, / in the top of the operatorstack
              while (!operatorStack.isEmpty() &&
                  (operatorStack.peek() == ‘*’|| operatorStack.peek() == ‘/’)) {
                 processAnOperator(operandStack, operatorStack);
              }
              // Push the * or / or % operator into theoperator stack
              operatorStack.push(token.charAt(0));
           }
           else if(token.trim().charAt(0) == ‘(‘) {
              operatorStack.push(‘(‘); // Push ‘(‘ tostack
           }
           else if(token.trim().charAt(0) == ‘)’) {
              // Process all the operators in the stack untilseeing ‘(‘
              while (operatorStack.peek() != ‘(‘) {
                 processAnOperator(operandStack, operatorStack);
              }
              operatorStack.pop(); // Pop the ‘(‘ symbol fromthe stack
           }
           else { // Anoperand scanned
              // Push an operand to the stack
              operandStack.push(new Integer(token));
           }
       }     
       // Phase 2: Process all theremaining operators in the stack
       while (!operatorStack.isEmpty()){
          processAnOperator(operandStack, operatorStack);
       }
       // Return the result
       return operandStack.pop();
   }
             
   /** Process one operator: Take an operator fromoperatorStack and
   / * apply it on the operands in the operandStack*/
   public static voidprocessAnOperator(Stack<Integer> operandStack,Stack<Character> operatorStack) {
       char op =operatorStack.pop();
       int op1 = operandStack.pop();
       int op2 = operandStack.pop();
       if (op == ‘+’)
          operandStack.push(op2 + op1);
       else if (op == ‘-‘)
          operandStack.push(op2 – op1);
       else if (op == ‘*’)
          operandStack.push(op2 * op1);
       else if (op == ‘/’)
          operandStack.push(op2 / op1);
       }

   public static String insertBlanks(String s) {
       String result = “”;

       for (int i = 0; i <s.length(); i++) {
           if (s.charAt(i)== ‘(‘ || s.charAt(i) == ‘)’ ||
              s.charAt(i) == ‘+’ || s.charAt(i) == ‘-‘||
              s.charAt(i) == ‘*’ || s.charAt(i) == ‘/’ )
              result += ” ” + s.charAt(i) + ” “;
           else
              result += s.charAt(i);
       }
       return result;
   }
}

Expert Answer


Answer to Part 1 Evaluate Expression Modify Code Add Operators Exponent Modulus Example 3 2 9 3 2 1 Q38174741 . . .

OR