2019 FRQ 3 PBL
Code + Extra
Calculator.java
- Converts the string expression to arraylist of tokens
- Determines if the character is an operator or a separator
- If no, appends character
- If yes and length is greater than 0, adds the token
- Reverse Polish notation in which operators follow the numbers
- rpnToResult: if character is operator, number is added to stack and if it's not, two numbers are removed and result is calculated with operand. Result is then added to stack
- Push things to front of the stack
- Code pushes all the numbers first and then the operator second (case does that)
public ArrayList<String> getDelimitersList(String[] tokens){
ArrayList<String> delims = new ArrayList<String>(); // store delimiters in tokens
for(String token: tokens){
if(token.equals(this.openDel) || token.equals(this.closeDel)){ // check if delimeter
delims.add(token);
}
}
return delims;
}
public boolean isBalanced(ArrayList<String> delimiters){
int openCount = 0;
int closeCount = 0;
for(String delim: delimiters){
if(delim.equals(this.openDel)){
openCount++;
}
else{
closeCount++;
if(openCount >= closeCount){
continue;
}
else{
return false;
}
}
}
if(openCount == closeCount){
return true;
}
return false;
}