Hello! I have been given the assignment to use two ( 2 ) stacks to evaluate an infix expression. I cannot switch to postfix or prefix, then solve. It has to be a direct computation. I have been working on this for around 5 hours and cannot figure out why my code isn't working. When I run the program and type in an expression.... nothing happens. No errors, just no output. Any help pushing me in the right direction would be greatly appreciated. THANKS!
import java.util.Scanner; import java.util.Stack; import java.util.regex.Pattern; public class InfixEvaluation { public static double evalz(String expression) { Scanner console = new Scanner(expression); Stack<Character> opStack = new Stack<Character>(); Stack<Double> valStack = new Stack<Double>(); String operator; char topOperator; char first; double operandTwo; double operandOne; double result; double next; while(console.hasNext( )) { } if (console.hasNextDouble()) { next = console.nextDouble(); valStack.push(next); } else { operator = console.next(); first = operator.charAt(0); switch (first) { case '^': opStack.push(first); break; case '+': case '-': case '*': case '/': while (!opStack.isEmpty() && getPriority(first) <= getPriority(opStack.peek())) { // Execute operator at top of opStack topOperator = opStack.pop(); operandTwo = valStack.pop(); operandOne = valStack.pop(); if (topOperator == '*') result = operandOne * operandTwo; else if (topOperator == '+') result = operandOne + operandTwo; else if (topOperator == '-') result = operandOne - operandTwo; else throw new InfixException("Illegal symbol: " + topOperator); valStack.push(result); } opStack.push(first); break; case '(': opStack.push(first); break; case ')': // Stack is not empty if infix expression is valid topOperator = opStack.pop(); while (topOperator != '(') { operandTwo = valStack.pop(); operandOne = valStack.pop(); if (topOperator == '*') result = operandOne * operandTwo; else if (topOperator == '+') result = operandOne + operandTwo; else if (topOperator == '-') result = operandOne - operandTwo; else throw new InfixException("Illegal symbol: " + topOperator); valStack.push(result); topOperator = opStack.pop(); } break; default: break; } } while(!opStack.isEmpty()){ topOperator = opStack.pop(); operandTwo = valStack.pop(); operandOne = valStack.pop(); if (topOperator == '*') result = operandOne * operandTwo; else if (topOperator == '+') result = operandOne + operandTwo; else if (topOperator == '-') result = operandOne - operandTwo; else throw new InfixException("Illegal symbol: " + topOperator); valStack.push(result); } double answer = valStack.peek(); return answer; } public static int getPriority(char first) { if (first == '*' || first == '/') return 1; else if (first == '+' || first == '-') return 2; else if (first == '=') return 3; else return Integer.MIN_VALUE; } public static final Pattern CHARACTER = Pattern.compile("\\S.*?"); public static final Pattern UNSIGNED_DOUBLE = Pattern.compile("((\\d+\\.?\\d*)|(\\.\\d+))([Ee][-+]?\\d+)?.*?"); }
And here is the MAIN:
MAIN:
import java.util.Scanner; public class InfixConsole { public static void main(String[] args) { Scanner console = new Scanner(System.in); String line = null; // string to be evaluated double answer; // result of evaluation // Get next expression to be processed. System.out.print("Enter an infix expression to be evaluated: "); line = console.nextLine(); answer = InfixEvaluation.evalz(line); // Output result. System.out.println(); System.out.println("Result = " + answer); } }
I also have a small error exception class.. if I need to put that too please let me know. Thanks!