/**************************************************************
作者:陈新
邮箱:[email protected]
用途:hdu1237
时间:2014.3.4 23:17
编译环境 : g++
数据:10308543 2014-03-15 20:45:36 Accepted 1237 140MS 424K 1948 B G++ 超级旅行者
*************************************************************/ #include <iostream>
#include <stack>
#include <iomanip> using namespace std; typedef struct{
stack<double> nums;
char op;
}EXPRESSION; bool endingCheck();
double caculate(double firstNum,double secondNum,char op);
void caculateExpression(EXPRESSION * pExpr); int main(){
EXPRESSION expr;
double firstNum = ;
while(cin >> firstNum){
if(endingCheck())
{
if(firstNum != ){
cout << fixed << setprecision() << firstNum << endl;
continue;
}
else{
break;
}
}
else{
expr.nums.push(firstNum);
} char op;
while(op = cin.get()){
double thirdNum;
cin >> thirdNum;
if(op == '+' || op == '-'){
caculateExpression(&expr);
expr.nums.push(thirdNum);
expr.op = op;
}
else{
double secondNum = expr.nums.top();
expr.nums.pop();
expr.nums.push(caculate(secondNum,thirdNum,op));
}
if(endingCheck()){
caculateExpression(&expr);
break;
}
} cout << fixed << setprecision() << expr.nums.top() << endl;
expr.nums.pop();
}
return ;
} bool endingCheck(){
return cin.get() == '\n';
} double caculate(double firstNum,double secondNum,char op){
if(op == '+'){
return firstNum + secondNum;
}
else if(op == '-'){
return firstNum - secondNum;
}
else if(op == '*'){
return firstNum * secondNum;
}
else if(op == '/'){
return firstNum / secondNum;
}
} void caculateExpression(EXPRESSION * pExpr){
if(pExpr ->nums.size() == ){
return;
}
else{
double secondNum = pExpr ->nums.top();
pExpr ->nums.pop();
double firstNum = pExpr ->nums.top();
pExpr ->nums.pop();
pExpr ->nums.push(caculate(firstNum,secondNum,pExpr ->op));
}
}
04-17 17:28