You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
privatevoidrpnToResult()
{
// stack is used to hold operands and each calculationStack<Double> calcStack = newStack<Double>();
// RPN is processed, ultimately calcStack has final resultfor (Stringtoken : this.reverse_polish)
{
// If the token is an operator, calculateif (isOperator(token))
{
// Pop the two top entriesdoublee1 = calcStack.pop();
doublee2 = calcStack.pop();
// Calculate intermediate resultsswitch (token) {
case"+":
result = e2 + e1;
break;
case"-":
result = e2 - e1;
break;
case"*":
result = e2 * e1;
break;
case"/":
result = e2 / e1;
break;
case"%":
result = e2 % e1;
break;
default:
break;
}
// Push intermediate result back onto the stackcalcStack.push( result );
}
// else the token is a number push it onto the stackelse
{
calcStack.push(Double.valueOf(token));
}
}
// Pop final result and set as final result for expressionthis.result = calcStack.pop();
}
Add unbalanced parenthesis check and in original FRQ, or other error checks. FYI, usually when structuring code with error checking it can greatly impact code structure.
Build in Power of operator ^:2 ^ 1 = 2, 2 ^ 2 = 4, 2 ^ 3 = 83. Build an API to receive an expression and respond with a result. This is a good opportunity to respond with an error if you built in parenthesis or other error checking.
privatefinalMap<String, Integer> OPERATORS = newHashMap<>();
{
// Map<"token", precedence>OPERATORS.put("*", 3);
OPERATORS.put("/", 3);
OPERATORS.put("%", 3);
OPERATORS.put("+", 4);
OPERATORS.put("-", 4);
OPERATORS.put("^", 4);
}
privatevoidtokensToReversePolishNotation () {
// codes are hidedcase "+": case "-": case "*": case "/": case "%": case "^": // codes are hided } private void rpnToResult() { //codes hided case "^": //pow() java use for power rule https://www.javatpoint.com/java-math-pow-method
result = Math.pow(e2,e1);
break;
default:
break;
}
//codes hided
}
api
@RestController@RequestMapping("/api/calculator")
publicclassCalculatorApiController {
@GetMapping("{expression}")
publicResponseEntity<String> getExpression(@PathVariableStringexpression) throwsJsonMappingException, JsonProcessingException {
// Backend Year ObjectCalculatortest = newCalculator(expression);
returnResponseEntity.ok(test.toString()); // JSON response, see ExceptionHandlerAdvice for throws
}
}
String str
for(int i=0; i<str.length(); i++)
str.substring(i,i+1) // means get every character in String
01234
"Tianbin".substring(0,1) = "T"
.substring(start , end) [start, end) from start to end-1
Array arr
for(int i=0; i<arr.length; i++)
arr[i] // means get every element in the arr
ArrayList list
for(int i=0; i<list.size(); i++)
list.get(i) // means get every item in the ArrayList
The text was updated successfully, but these errors were encountered:
Finish rpnToResult for Calculator
Add unbalanced parenthesis check and in original FRQ, or other error checks. FYI, usually when structuring code with error checking it can greatly impact code structure.
Build in Power of operator ^:2 ^ 1 = 2, 2 ^ 2 = 4, 2 ^ 3 = 83. Build an API to receive an expression and respond with a result. This is a good opportunity to respond with an error if you built in parenthesis or other error checking.
api
Tester Method Result Output
API test result in Postman
FRQ3 Collageboard
note, different between Array, Array list, list
The text was updated successfully, but these errors were encountered: