This is an assignment for a data structures class that I need a little help getting started with. We are given all of the code that I provided below and we are asked to modify it so that the program for evaluating arithmetic expressions will also allow the unary operation '-'. For example, for the expression "-1" the result is -1; for the expression "-(1+3)" the result is -4; etc.
I am not asking for the answer, I just need some direction in how to get started. Any help is much appreciated. Thank you.
package cs272; import java.util.Scanner; import java.util.regex.Pattern; import edu.colorado.collections.LinkedStack; public class Evaluation { /** * @param args */ private static boolean TRACE = false; public static final Pattern CHARACTER = Pattern.compile("\\S.*?"); public static final Pattern UNSIGNED_DOUBLE = Pattern.compile("((\\d+\\.?\\d*)|(\\.\\d+))([Ee][-+]?\\d+)?.*?"); public static void printStack(LinkedStack<String> st) { LinkedStack<String> pr = st; while (!pr.isEmpty()) { System.out.print(pr.pop() +" "); } } public static LinkedStack<String> reverseStack(LinkedStack<String> st) { LinkedStack<String> rev = new LinkedStack<String>(); while (!st.isEmpty()) { rev.push(st.pop()); } return rev; } public static double calculate(LinkedStack<Double> operands, Character operation) { double op1 = operands.pop(); double op2 = operands.pop(); double result = 0; if (TRACE) System.out.println("op1 "+op1 +" op2 "+op2 +" " + operation); switch(operation) { case '+': result = op2 + op1; break; case '-': result = op2 - op1; break; case '*': result = op2 * op1; break; case '/': result = op2 / op1; break; } return result; } public static double evaluation(LinkedStack<String> st) { LinkedStack<Double> operands = new LinkedStack<Double>(); String next; while (!st.isEmpty()) { next = st.pop(); if (TRACE) System.out.println("Processing "+next); switch (next.charAt(0)) { case '+': case '-': case '*': case '/': operands.push(calculate(operands, next.charAt(0))); break; default: operands.push(Double.valueOf(next)); break; } } return operands.pop(); } public static LinkedStack<String> infix2postfix(String s) { LinkedStack<String> results = new LinkedStack<String>(); LinkedStack<Character> operations = new LinkedStack<Character>(); Scanner input = new Scanner(s); String next; Character first; while (input.hasNext()) { if (input.hasNext(UNSIGNED_DOUBLE)) { next = input.findInLine(UNSIGNED_DOUBLE); results.push(next); if (TRACE) System.out.println("Processing "+next); } else { next = input.findInLine(CHARACTER); if (TRACE) System.out.println("Processing "+next+" size of operations stack "+operations.size()); first = next.charAt(0); switch (first) { case '+': // Addition case '-': // Substraction // System.out.println(" ... size of operations stack before do-while "+operations.size()); while (!operations.isEmpty() && !operations.peek().equals('(')) { Character op = operations.pop(); String sop = Character.toString(op); results.push((String) sop); } // System.out.println(" ... size of operations stack after do-while "+operations.size()); operations.push(first); break; case '*': // Multiplication case '/': // Division while (!operations.isEmpty() && !(operations.peek().equals('(') || operations.peek().equals('+') || operations.peek().equals('-') ) ) { Character op = operations.pop(); String sop = Character.toString(op); results.push(sop); } operations.push(first); break; case ')': // Right parenthesis while (!operations.isEmpty() && !operations.peek().equals('(')) { Character op = operations.pop(); String sop = Character.toString(op); results.push(sop); } if (operations.isEmpty()) throw new IllegalArgumentException("Invalid Expression"); operations.pop(); break; case '(': // Left parenthesis operations.push(first); break; default : // Illegal character throw new IllegalArgumentException("Illegal character"); } } // System.out.println(next); } while (!operations.isEmpty()) { Character op = operations.pop(); String sop = Character.toString(op); results.push(sop); } return results; } // evaluate a fully parenthesized arithmetic expression public static void main(String[] args) { // TODO Auto-generated method stub String exp = args[0]; LinkedStack<String> rev; System.out.println("Expression: "+exp); LinkedStack<String> postfix = infix2postfix(exp); rev = reverseStack(postfix); printStack(rev.clone()); System.out.println("\nEvaluation: "+evaluation(rev)); } }