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;
}
}
}
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
*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.