1157:表达式的值
Time Limit:1000MS Memory Limit:65536K
Total Submit:94 Accepted:27
Description
如果给你一个只包含+,-,*,/,(,),及非负整数数字和空格组成的字符串表达式,你能编程计算它的值吗?不忙回答,,试试就知道了。
Input
第一行为一个N,表示测试样例数。
以下N行每个样例为一个字符串表达式,每个表达式至少一个字符,可能包含空格,但长度不超过50.
Output
对每一测试样例,输出它的值。
Sample Input
3
(2+3/3)+1
2+1/2-5
10*(2-2 )
Sample Output
4
-3
0
Hint
题目输入的表达式都是合法的.
在测试2中,我们认为1/2=0,所以最后答案为-3.
Source
SWUST
solution——
#include<cstdio>//不用iostream是为了节约时间,避免G++的cin,cout类多余时间消耗
#include<cstring>
#include<stack>
using namespace std;
int calculate(int c, int a, int b)//计算函数
{
if(c == 1)
return a + b;
else if(c == 2)
return a - b;
else if(c == 3)
return a * b;
else
return a / b;
}
{
if(c == 1)
return a + b;
else if(c == 2)
return a - b;
else if(c == 3)
return a * b;
else
return a / b;
}
int main()
{
int n;
char str[55];
stack<int> num, stkch;//运算符栈没用字符存储,也用数字量化,配合计算函数
char ch[] = {'(', '+', '-', '*', '/', ')'};//运算符优先操作
scanf("%d", &n);
getchar();
while(n--)
{
gets(str);
num.push(0);
for(int i = 0; i < strlen(str); i++)
if(str[i] >= '0' && str[i] <= '9')
num.top() = num.top() * 10 + (str[i] - '0');/*数字入栈,不过由于这题不是高精度计算 ,所以用这种方式进行多位数读取*/
else//符号入栈判断
{
if(str[i] == ' ')//空格忽略
continue;
int k;
for(int j = 0; j < 6; j++)//判断优先
if(ch[j] == str[i])
{
k = j;
break;
}
if(!k)//前括号直接进站
{
stkch.push(k);
continue;
}
if(k != 5)//运算符入栈
{
while(!stkch.empty() && k < stkch.top())//空栈判断及优先运算
{
int b = num.top();
num.pop();
int a = num.top();
num.pop();
num.push(calculate(stkch.top(), a, b));
stkch.pop();
}
stkch.push(k);
num.push(0);
}
else//读取后括号进行括号处理
{
while(1)
{
if(stkch.top() == 0 || num.size() < 2)
{
stkch.pop();
break;
}
int b = num.top();
num.pop();
int a = num.top();
num.pop();
num.push(calculate(stkch.top(), a, b));
stkch.pop();
}
}
}
while(!stkch.empty() && num.size() > 1)//化为简单无括号型后的运算,可以说是后事处理
{
int b = num.top();
num.pop();
int a = num.top();
num.pop();
num.push(calculate(stkch.top(), a, b));
stkch.pop();
}
printf("%d/n", num.top());//最后栈顶即结果
num.pop();//数字出栈,归还空间
}
return 0;
}
{
int n;
char str[55];
stack<int> num, stkch;//运算符栈没用字符存储,也用数字量化,配合计算函数
char ch[] = {'(', '+', '-', '*', '/', ')'};//运算符优先操作
scanf("%d", &n);
getchar();
while(n--)
{
gets(str);
num.push(0);
for(int i = 0; i < strlen(str); i++)
if(str[i] >= '0' && str[i] <= '9')
num.top() = num.top() * 10 + (str[i] - '0');/*数字入栈,不过由于这题不是高精度计算 ,所以用这种方式进行多位数读取*/
else//符号入栈判断
{
if(str[i] == ' ')//空格忽略
continue;
int k;
for(int j = 0; j < 6; j++)//判断优先
if(ch[j] == str[i])
{
k = j;
break;
}
if(!k)//前括号直接进站
{
stkch.push(k);
continue;
}
if(k != 5)//运算符入栈
{
while(!stkch.empty() && k < stkch.top())//空栈判断及优先运算
{
int b = num.top();
num.pop();
int a = num.top();
num.pop();
num.push(calculate(stkch.top(), a, b));
stkch.pop();
}
stkch.push(k);
num.push(0);
}
else//读取后括号进行括号处理
{
while(1)
{
if(stkch.top() == 0 || num.size() < 2)
{
stkch.pop();
break;
}
int b = num.top();
num.pop();
int a = num.top();
num.pop();
num.push(calculate(stkch.top(), a, b));
stkch.pop();
}
}
}
while(!stkch.empty() && num.size() > 1)//化为简单无括号型后的运算,可以说是后事处理
{
int b = num.top();
num.pop();
int a = num.top();
num.pop();
num.push(calculate(stkch.top(), a, b));
stkch.pop();
}
printf("%d/n", num.top());//最后栈顶即结果
num.pop();//数字出栈,归还空间
}
return 0;
}