標籤:style blog http color os io 資料 art
題目連結:http://acm.hdu.edu.cn/showproblem.php?pid=1237
題目大意:一個簡單的計算機,只要判斷好運算順序即可。先加減後乘除~建議一個比較簡單的想法,輸入一個數字,再輸入一個字元,接下去對字元進行判斷,分別將數字存入棧中,最後將棧的數進行整理。這樣做下去還可以,不過還是wa了幾次!
提供測試資料:0 + 1 估計大多數都是忘記考慮這種情況了~
1 #include <iostream> 2 #include <cstdio> 3 #include <stack> 4 #include <cstring> 5 using namespace std; 6 int main () 7 { 8 stack<double>s; 9 int n;10 double m;11 char ch,str;12 while (scanf("%d%c",&n,&ch)!=EOF)13 {14 if (n==0&&ch==‘\n‘)15 break;16 s.push(n);17 scanf("%c %d",&ch,&n);18 switch (ch)19 {20 case ‘+‘:21 {22 s.push((double)n);23 break;24 }25 case ‘-‘:26 {27 s.push(-(double)n);28 break;29 }30 case ‘*‘:31 {32 m=s.top();33 s.pop();34 m=m*(double)n;35 s.push(m);36 break;37 }38 case ‘/‘:39 {40 m=s.top();41 s.pop();42 m=m/(double)n;43 s.push(m);44 break;45 }46 }47 while (scanf("%c",&ch)!=EOF)48 {49 if (ch==‘\n‘)50 break;51 scanf("%c%d",&str,&n);52 switch (str)53 {54 case ‘+‘:55 {56 s.push((double)n);57 break;58 }59 case ‘-‘:60 {61 s.push(-(double)n);62 break;63 }64 case ‘*‘:65 {66 m=s.top();67 s.pop();68 m=m*(double)n;69 s.push(m);70 break;71 }72 case ‘/‘:73 {74 m=s.top();75 s.pop();76 m=m/(double)n;77 s.push(m);78 break;79 }80 }81 82 }83 double sum=0;84 while (!s.empty())85 {86 //cout<<s.top();87 sum+=s.top();88 s.pop();89 }90 printf ("%.2lf\n",sum);91 }92 }View Code