如果给你一个只包含+,-,*,/,(,),及非负整数数字和空格组成的字符串表达式,你能编程计算它的值吗?
输入格式
第一行为一个N,表示测试样例数。 以下N行每个样例为一个字符串表达式,每个表达式至少一个字符,可能包含空格,但长度不超过50.
输出格式
对每一测试样例,输出它的值。
样例输入
3 (2+3/3)+1 2+1/2-5 10*(2-2 )
样例输出
4 -3 0
#include<iostream>
#include<string>
#include<stack>
using namespace std;
//进行计算,a@b,其中@是操作符,由c表示,可以是加减乘除,a和b分别是操作数
int calculate(int a,int b,int c) {
switch(c) {
case 0:
return a+b;
case 1:
return a-b;
case 2:
return a*b;
case 3:
return a/b;
}
}
//进行字符确认,看其是何种操作符号 ,注意针对空格的处理
int certify(char c) {
switch(c) {
case ' ':
return 7;
case '=':
return 6;
case ')':
return 5;
case '(':
return 4;
case '/':
return 3;
case '*':
return 2;
case '-':
return 1;
case '+':
return 0;
default :
return 8;
}
}
//在确认了字符不是空格或者操作符号的时候就知道它是数字了,所以进行转换
int gdata(char c) {
switch(c) {