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

am working on the code based on the given instruction below (it is about stackin

ID: 3782080 • Letter: A

Question

am working on the code based on the given instruction below (it is about stacking, infix to postfix calculation). Because I never studied this section before, I am not sure if I am on the right track. This is JAVA.

Given instruction:

=======================

My code: I already have errors in the following code which I don't know how to fix and not finished yet. Please revise my code or rewrite it please.

Thank you in advance.

import java.util.*;

public class Evaluation {
   private String curCharacter;
   Stack stack = new Stack();
  
   public Evaluation(String moreTokens){
       curCharacter = moreTokens;
       String outputString ="";
       for(int i = 0; i            if(!symbolLow(curCharacter) || !symbolHigh(curCharacter)){
               stack.push(curCharacter);
           }else if(curCharacter == "("){
               stack.push(curCharacter);
           }else if(curCharacter == ")"){
               while (!(stack.pop()=="(")){
                   String value2 = stack.pop();
                   String value1 = stack.pop();
                   stack.push(newValue(value2, curCharacter, value1));
               }
           }else{
               while(true){
                   if(stack.isEmpty() || !stack.peek()=="(" || ){
                       stack.push(curCharacter);
                   }
               }
           }
       }
   }
  
   public boolean symbolLow(String low){
       switch(low){
           case "+":
           case "-":
               return true;
           default:
               return false;
       }
   }
   public boolean symbolHigh(String high){
       switch(high){
           case "*":
           case "/":
               return true;
           default:
               return false;
       }
   }
   public newValue(String value2, String operator, String value1){
       switch(operator){
           case "+":
               return value2+value1;
           case "-":
               return value2-value1;
           case "*":
               return value2*value1;
           case "/":
               return value2/value1;
       }
   }
}

while (moreTokens) if token is not an operator (in other words it is a number) operand Stack. push (token) else if token is operator stack .push (symbol) This creates a false bottom to the stack. It must be matched by else if token is Process all the operators until a matching is found. while (op operator Stack.pop is not value2 operands tack. pop Note pop the values off valuel operands tack. pop in reverse order This accounts for the fact subtraction and division are not commutative. new value valuel op value2 Perform the popped operator on these two values operand Stack.push (newValue) store the value back on the stack. token is else Check the precedence of the previous operator. If the stack is empty or the operator is a or the precedence of the previous operator is higher then the current operator is pushed and the inner loop is exited. while true if 1 the stack is empty OR 2 the top of the stack is a OR 3 the precedence of the operator at the top of the stack is less than this operator's precedence) operator Stack.push (token)

Explanation / Answer

// It was difficult to work on your code so I rewrote it. Here is my code.

import java.util.Stack;

public class Evaluation
{
public static int evaluate(String expression)
{
char[] tokens = expression.toCharArray();

// Stack for numbers
Stack<Integer> operandStack = new Stack<Integer>();

// Stack for Operators
Stack<Character> operatorStack = new Stack<Character>();

//using for is more natural here
for (int i = 0; i < tokens.length; i++)
{
if (tokens[i] == ' ')
continue;
  
// Token is a number, push it to stack
if (tokens[i] >= '0' && tokens[i] <= '9')
{
   // There can be multi digit number. Create number as sring
StringBuffer numberString = new StringBuffer();

while (i < tokens.length && tokens[i] >= '0' && tokens[i] <= '9')
{
   numberString.append(tokens[i++]);
}
// Converting numberString to integer and adding to array
operandStack.push(Integer.parseInt(numberString.toString()));
}

// Encounter open bracket, push it to operator stack
else if (tokens[i] == '(')
operatorStack.push(tokens[i]);

// Encounter closing bracket, evaluate and push result back in operand stack
else if (tokens[i] == ')')
{
while (operatorStack.peek() != '(')
operandStack.push(evaluateOpeartion(operatorStack.pop(), operandStack.pop(), operandStack.pop()));
operatorStack.pop();
}

// Encounter a operator
else if (tokens[i] == '+' || tokens[i] == '-' ||
tokens[i] == '*' || tokens[i] == '/')
{
while (!operatorStack.empty() &&
   isHigherPrecedence(tokens[i], operatorStack.peek()))
{
operandStack.push(evaluateOpeartion(operatorStack.pop(), operandStack.pop(), operandStack.pop()));
}

// Push current token to 'ops'.
operatorStack.push(tokens[i]);
}
}

while (!operatorStack.empty())
operandStack.push(evaluateOpeartion(operatorStack.pop(), operandStack.pop(), operandStack.pop()));

// operandStack now contain result
return operandStack.pop();
}

// check for precendence order
public static boolean isHigherPrecedence(char op1, char op2)
{
if (op2 == '(' || op2 == ')')
return false;
if ((op1 == '*' || op1 == '/') && (op2 == '+' || op2 == '-'))
return false;
else
return true;
}

// Evaluate result of operator on operand b and a
public static int evaluateOpeartion(char op, int b, int a)
{
switch (op)
{
   case '+':
   return a + b;
   case '-':
   return a - b;
   case '*':
   return a * b;
   case '/':
   if (b == 0)
   throw new
   UnsupportedOperationException("Cannot divide by zero");
   return a / b;
}
return 0;
}

// Test result
public static void main(String[] args)
{
System.out.println(Evaluation.evaluate("900 / 10 * 2 + 6"));
System.out.println(Evaluation.evaluate("12 / ( 1 + 11 ) * 2"));
}
}

/*

sample run

javac Evaluation.java

java Evaluation

186
2

*/

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote