Write a simple integer calculator that supports addition (+), subtraction (-), and multiplication (*). Specifically, you are required to write a function named evaluate
that accepts a string expression, evaluates it, and returns the result as an integer.
You can start with this template code and ensure your code can run correctly within this template:
def evaluate(expr):
# your code here
# Note that the following code is for local testing purposes only. You should leave this part of code unchanged and not submit it to the OJ system.
T = int(input())
for t in range(T):
result = evaluate(input())
print(result)
Please note that you only have to submit the definition of the evaluate
function to the OJ system. The rest of the code is for your local testing purposes only.
Input Specification
The first line of the input gives the number of test cases, , and then test cases follow.
For each test case, it will consist of one line of expression which may contains addition (+), subtraction (-), and multiplication (*).
Constraints
Output Specification
For each test case, output one line containing a number that represents the result of the given expression.
Sample Input
3
2+3
2*8-14
(4+3)*2
Sample Output
5
2
14
Hint
function
eval()
Comments