hdu 1237 簡單計算機

來源:互聯網
上載者:User

標籤: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

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.