diff --git a/Project_2A.java b/Project_2A.java index dc06294..1bd0be1 100644 --- a/Project_2A.java +++ b/Project_2A.java @@ -52,7 +52,7 @@ else if(tokenArray[i] == '(') { //check for closing parenthesis else if(tokenArray[i] == ')') { - while(operators.peek() != ")") { + while(operators.peek() != "(") { /**Performsoperation in the parenthesis in order * until it reaches the open parenthesis * pushes the outcome into the numbers stack after each operation @@ -62,21 +62,21 @@ else if(tokenArray[i] == ')') { nums.push(equation(left, right, operators.pop())); }//end while }//end branch for closing parenthesis - else if(isOperator(tokenArray[i])) { - /**while loop to find multipart operators - * and convert them to a string - */ - string newOp = ""; - while(isOperator(tokenArray[i])) { + else { + String newOp = ""; + while(!isNumber(tokenArray[i])&& tokenArray[i]!=' ') { newOp = newOp + tokenArray[i]; - i++; - + i++; }//end while loop for op building /** i has to be decrimented, since it is incremented in the while loop * that way the main for loop doesnt skip any tokens in the equation */ i--; - + if(isOperator(newOp)) { + /**while loop to find multipart operators + * and convert them to a string + */ + //while loop until you reach open parenthesis /**use while loop to check if current operator * has the same or higher precendence to one @@ -85,29 +85,32 @@ else if(isOperator(tokenArray[i])) { * * added !.operators.isEmpty(), as empty stack causes error for comparison */ - while(!operators.isEmpty() && (precedence(operators.peek()) > precedence(tokenArray[i]))) { + while(!operators.isEmpty() && (precedence(operators.peek()) > precedence(newOp))) { Integer right = nums.pop(); Integer left = nums.pop(); nums.push(equation(left, right, operators.pop())); }//end while loop //once token is either first token or highest precedence adds to stack - operators.push(tokenArray[i]); - }//end else if branch for operator - }//end for + operators.push(newOp); + }//end nested if branch + }//end else /** once the while expression is either added to stacks or is * put through needed operations solves remaining operations in * the operators stack, until empty */ - while (!operators.isEmpty()) { - Integer right = nums.pop(); - Integer left = nums.pop(); - nums.push(equation(left, right, operators.pop())); + while (!operators.isEmpty()) { + Integer right = nums.pop(); + Integer left = nums.pop(); + nums.push(equation(left, right, operators.pop())); + + + }//end while - return nums.pop(); - } - }//end evaluate method + }//end for + return nums.pop(); + }//end method /* *Precedence method. This returns a value based on the operator @@ -124,10 +127,10 @@ public int precedence(String operator) { if (operator.equals("==") || operator.equals("!=")) { return 3; } if (operator.equals("&&")) { return 2; } if (operator.equals("||")) { return 1; } - throw new IllegalArgumentException(String.format("Operator %s is not supported.", operator)) } // Time complexity: O(n) + throw new IllegalArgumentException(String.format("Operator %s is not supported.", operator)); // Time complexity: O(n) }//End Precedence method - public int equation(Integer left, Integer right, String s) { + int equation(Integer left, Integer right, String s) { /** * FIX ME */ @@ -135,7 +138,7 @@ public int equation(Integer left, Integer right, String s) { //method to check if character is a number //simple method to see if char is between 1-9 and thus a number - public boolean isNumber(char c) { + boolean isNumber(char c) { if(c >= '0' && c <='9') return true; else @@ -145,8 +148,10 @@ public boolean isNumber(char c) { *Method to check if String s is equal to any of the allowed operators for this project. *Returns: True if String s is equal to one of the operators, otherwise returns false bvecause String s isnt an operator */ - public boolean isOperator(String s) { - if (s.equals("^" || "*" || "/" || "+" || "-" || ">" || ">=" || "<" || "<=" || "==" || "!=" || "&&" || "||"){ + boolean isOperator(String s) { + if (s.equals("^") || s.equals("*") || s.equals("/") || s.equals("+") || + s.equals("-") || s.equals(">") || s.equals(">=") || s.equals("<") || + s.equals("<=") || s.equals("==") || s.equals("!=") || s.equals("&&") || s.equals("||")){ return true; }else {return false;} }