Evaluate_Arithmetic Expression
Evaluate_Arithmetic Expression
}
}
return (Integer) stack.pop();
}
Result of evaluation = 6
Result of evaluation = 22
EXAMPLE: (prefix notation) –
import java.util.Stack;
public class PrefixEvaluationSingleDigitOperand {
public static Double evaluate(double a, double b, char operator){
switch (operator) {
case '+':
return a + b;
case '-':
return b - a;
case '*':
return a * b;
case '/':
if (a == 0)
throw new
UnsupportedOperationException("Cannot divide by zero");
return b / a;
}
return 0.0;
}
public static Double convert(String expression) {
Output:
Prefix Expression: -/*2*5+3652
Evaluation: 16.0
if (charsExp[i] == '(') {
charsExp[i] = ')';
i++;
}
else if (charsExp[i] == ')') {
charsExp[i] = '(';
i++;
}
}
for (int i = 0; i <charsExp.length ; i++) {
char c = charsExp[i];