1 + 2 4 + 2 * 5 - 7 / 11 0 Sample Output3.00 13.36好久没写题了 写个水题练练手感
getchar()的用法要注意测试数据 0 + 1要注意一下
#include<stdio.h>#include<stack>#include<algorithm>using namespace std;int main(){ stack<double> s; int n; while(~scanf("%d",&n)) { char c; c = getchar(); if(c=='/n' && n==0) { break; } s.push(n); c = getchar(); double m; while(~scanf("%d",&n)) { if(c == '*') { m = s.top(); m *= n; s.pop(); s.push(m); } if(c == '/') { m = s.top(); m /= n; s.pop(); s.push(m); } if(c == '+') { s.push(n); } if(c == '-') { n = -n; s.push(n); } if(c=getchar() == '/n') { break; } c = getchar(); } double sum = 0; while(!s.empty()) { sum += s.top(); s.pop(); } printf("%.2lf/n",sum); }return 0;}
新闻热点
疑难解答