题目描述
读入一个只包含 +, -, *, / 的非负整数计算表达式,计算该表达式的值。
输入
测试输入包含若干测试用例,每个测试用例占一行,每行不超过200个字符,整数和运算符之间用一个空格分隔。没有非法表达式。当一行中只有0时输入结束,相应的结果不要输出。
输出
对每个测试用例输出1行,即该表达式的值,精确到小数点后2位。
样例输入
30 / 90 - 26 + 97 - 5 - 6 - 13 / 88 * 6 + 51 / 29 + 79 * 87 + 57 * 92
0
样例输出
12178.21
思路
这道题一开始我想着是中缀表达式转后缀表达式,然后通过后缀表达式计算,后来考虑到这道题里面只有+, -, ×, / 四种运算符,没有括号之类的。用后缀表达式的方法有点麻烦,所以参考了这篇博客,把每一次计算的结果都存入栈中,最后的结果相加,具体的思路如下:
1、遍历字符串的时候,用一个字符数组num存储数字,然后用atoi函数转换为数值,将该数值存放到栈中;
2、若遍历的是*,则栈顶元素出栈,与当前数字做相乘操作,结果进栈;
3、若遍历的是/,则栈顶元素出栈,与当前数字做相除操作,结果进栈;
4、若遍历的是-,则取负,结果进栈;
5、若遍历的是-,则结果进栈;
对于第一个数字,因为此时没有栈顶元素,所以直接让它进栈,相当于是第5步。
代码
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <string.h>
#include <stack>
#include <map>
using namespace std;
int main(){
char str[210],num[210];//str:输入,num:存放数字
map<char,int> mp;
mp['*']=0;
mp['/']=1;
mp['+']=2;
mp['-']=3;
int len;//字符串长度
stack<double> cal;
while(cin.getline(str,201)){
len=strlen(str);
if(len==1&&str[0]=='0') break;//输入为0时,结束
int flag=2,count=0;//flag判断加减乘除,初始化为2,因为第一个数直接存进去
for(int i=0;i<len;i++){
int flag2=0;//帮助判断是否是数字并存入num
if(str[i]==' ') continue;
if(str[i]>='0'&&str[i]<='9'){
num[count++]=str[i];
flag2=1;//若是数字就改变flag
}
if(flag2==0||i==len-1){
num[count]='\0';
double n=atoi(num);//转为数字
count=0;//count复原,用于下一个数字的存储
if(flag==0){//乘法,相乘后放入栈中
double m=cal.top();
n*=m;
cal.pop();
cal.push(n);
}
else if(flag==1){//除法,相除后放入栈中
double m=cal.top();
n=m/n;
cal.pop();
cal.push(n);
}
else if(flag==3){//减法,存为负数放入栈中
cal.push(-n);
}
else{//加法,直接放入栈中
cal.push(n);
}
flag=mp[str[i]];
}
}
double alcount=0;
while(cal.empty()==false){//栈内剩余元素相加
alcount+=cal.top();
cal.pop();
}
printf("%.2lf\n",alcount);
}
return 0;
}
问题
该代码在本地运行无错,但是在codeup的OJ上提示50%错误,还未找到错误原因,大家发现了可以指正一下。
后缀表达式计算
在学习《算法笔记》里面栈的章节的时候,书上给了这道题的后缀表达式方法的思路和代码,这里我把它贴出来。
两个步骤:
- 中缀表达式转后缀表达式;
- 计算后缀表达式
步骤1、中缀表达式转后缀表达式
步骤2、计算后缀表达式
附上代码
#include <stdio.h>
#include <cstdio>
#include <iostream>
#include <string>
#include <stack>
#include <map>
#include <queue>
using namespace std;
struct node{
double num;//操作数
char op;//操作符
bool flag;//true 表示操作数,false 表示操作符
};
string str;
stack<node> s;//操作符栈
queue<node> q;//后缀表达式序列
map<char,int> opp;//操作符优先级映射
void Change(){//中缀表达式转后缀表达式
double num;
node temp;
int len=str.length();
for(int i=0;i<len;){
if(str[i]>='0'&&str[i]<='9'){//如果是数字数
temp.flag=true;//标记为数字数
temp.num=str[i++]-'0';//记录这个操作数的第一个数位
while(i<len&&str[i]>='0'&&str[i]<='9'){
temp.num=temp.num*10+(str[i]-'0');//更新这个操作数,因为这个数有可能不止一位
i++;
}
q.push(temp);//将操作数压入后缀表达式的队列中
} else {//如果是操作符
temp.flag=false;//标记为操作符
//只要操作符栈的栈顶元素比该操作符优先级高
//就把操作符栈顶元素弹出到后缀表达式的队列中
//直到操作符栈顶元素优先级低于该操作符,将该操作符压入操作符栈
while(!s.empty()&&opp[str[i]]<=opp[s.top().op]){//中括号外的op是优先级的map,
q.push(s.top()); //里面的op是操作符栈s里的操作符
s.pop();
}
temp.op=str[i];//该操作符入栈
s.push(temp);
i++;
}
}
//如果操作符栈中还有操作符,就把它弹出到后缀表达式队列中
while(!s.empty()){
q.push(s.top());
s.pop();
}
}
double Cal(){//计算后缀表达式
double temp1,temp2;
node cur,temp;
while(!q.empty()){//后缀表达式队列非空
cur=q.front();//cur记录队首元素
q.pop();
if(cur.flag==true) s.push(cur);//如果是操作数,直接压入栈
else {//如果是操作符
temp2=s.top().num;//弹出第二操作数
s.pop();
temp1=s.top().num;//弹出第一操作数
s.pop();
temp.flag=true;//临时记录操作数
if(cur.op=='+') temp.num=temp1+temp2;//加法
else if(cur.op=='-') temp.num=temp1-temp2;//减法
else if(cur.op=='*') temp.num=temp1*temp2;//乘法
else temp.num=temp1/temp2;
s.push(temp);//将该操作数压入栈
}
}
return s.top().num;//栈顶元素就是后缀表达式的值
}
int main(){
opp['+']=opp['-']=1;//设定操作符优先级
opp['*']=opp['/']=2;
while(getline(cin,str),str!="0"){
for(string::iterator it=str.end();it!=str.begin();it--){
if(*it==' ') str.erase(it);//去掉表达式中的空格
}
while(!s.empty()) s.pop();//初始化栈
Change(); //将中缀表达式转换为后缀表达式
printf("%.2f\n",Cal());//计算后缀表达式
}
return 0;
}