#include <stdio.h>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <stack>
#include <queue>
char s[2];
using namespace std;
int main(){
ios::sync_with_stdio(false);
stack<double> sta;
double sum=0;
double a;
char f;
while(~scanf("%lf",&a)){
sta.push(a);
f=getchar();
if(a==0&&f=='\n') break;
// return 0;
//else if(f=='\n')
//goto here;
while(~scanf("%s %lf",s,&a)){
if(s[0]=='*') sta.top()*=a;
else if(s[0]=='/') sta.top()/=a;
else if(s[0]=='-') sta.push(-a);
else sta.push(a);
f=getchar();
if(f=='\n') break;
}
//here:
while(!sta.empty()){
sum+=sta.top();
sta.pop();
}
printf("%.2lf\n",sum);
sum=0;
}
return 0;
}
D - 简单计算器 HDU - 1237
读入一个只包含 +, -, *, / 的非负整数计算表达式,计算该表达式的值。
Input
测试输入包含若干测试用例,每个测试用例占一行,每行不超过200个字符,整数和运算符之间用一个空格分隔。没有非法表达式。当一行中只有0时输入结束,相应的结果不要输出。
Output
对每个测试用例输出1行,即该表达式的值,精确到小数点后2位。
Sample Input
1 + 2 4 + 2 * 5 - 7 / 11
0
Sample Output
3.00 13.36
本文介绍了一个简单的计算器程序设计,使用C++实现对非负整数的四则运算表达式进行解析和计算。通过栈来处理运算符和操作数,支持加、减、乘、除等基本运算。
679

被折叠的 条评论
为什么被折叠?



