← 返回 apple 的题目列表Basic Calculator with Addition and Subtraction
类型:online_judge
Design and implement a simple calculator that supports only addition and subtraction operations. The input is a string format mathematical expression containing only integers with addition and subtraction operations. Assume the input is always a valid expression without any extra spaces or invalid characters. Implement the corresponding calculation functionality.
Example inputs:
"1+2"
"10-5+3"
"7-2+1"
The output should be the calculated result of the corresponding expression.
Requirements:
The time complexity should be O(n), where n is the length of the input string.
Provide at least five test cases to verify the program.
Example
Input
3+5