Simple Calculator(lintcode 478)
Description
Given two integers a and b, an operator, choices:
+, -, *, /
Calculate a < operator > b.
Notice:
Use switch grammar to solve it.
Example
For a = 1, b = 2, operator = +, return 3.
For a = 10, b = 20, operator = *, return 200.
For a = 3, b = 2, operator = /, return 1. (not 1.5)
For a = 10, b = 11, operator = -, return -1.
Interface
public class Calculator {
/**
* @param a, b: Two integers.
* @param operator: A character, +, -, *, /.
*/
public int calculate(int a, char operator, int b) {
/* your code */
}
}
Idea
Nothing important.
Solution
public class Calculator {
/**
* @param a, b: Two integers.
* @param operator: A character, +, -, *, /.
*/
public int calculate(int a, char operator, int b) {
/* your code */
switch (operator) {
case '+': return a + b;
case '-': return a - b;
case '*': return a * b;
case '/': return a / b;
}
return 0;
}
}