• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            hdu3596(逆波蘭表達(dá)式求值)

            /*************************************/
            /*支持?jǐn)?shù)字(而非表達(dá)式)前面有正負(fù)號   */
            /*支持浮點數(shù)的+,-,*,/,^ 運算      */
            /*************************************/
            #include <stdio.h>
            #include <memory>
            #include <iostream>
            #include <algorithm>
            #include <cstring>
            #include <vector>
            #include <map>
            #include <cmath>
            #include <set>
            #include <queue>
            #include <time.h>
            #include <limits>
            #include <stack>
            using namespace std;
            #define N 10005
            #define eps 1e-9
            #define vType double
            struct node{
             vType val;
             char op;
             node(vType _val=0, char _op = ' '):val(_val), op(_op){}
            }nodes[N];
            char str[N];
            inline bool isDigit(char ch){
             return ch >= '0' && ch <= '9';
            }
            map<char, int> ms;
            void init(){
             ms['('] = 0;
             ms['-'] = ms['+'] = 1;
             ms['*'] = ms['/'] = 2;
             ms['^'] = 3;
            }
            vType calPoland(node* nodes, int k){
             int i, j;
             vType a, b;
             stack<vType> s;
             for(i = 0; i < k; i++){
              if(nodes[i].op == ' '){
               s.push(nodes[i].val);
              }else{
               a = s.top();
               s.pop();
               b = s.top();
               s.pop();
               switch(nodes[i].op){
                case '+':
                 s.push(b + a);
                 break;
                case '-':
                 s.push(b - a);
                 break;
                case '*':
                 s.push(b * a);
                 break;
                case '/':
                 if(fabs(a) < eps) throw true;
                 s.push(b / a);
                 break;
                case '^':
                 s.push(pow(b, a));
                 break;
               }
              }
             }
             return s.top();
            }
            vType poland(char* str){
             int i, k, sign;
             bool inNum, hasDot;  //inNum標(biāo)記當(dāng)前是否可以輸入數(shù)字, hasDot標(biāo)記是否已經(jīng)輸入小數(shù)點
             stack<char> oper;
             for(i = k = 0, sign = 1, inNum = true; str[i]; i++){
              if(isDigit(str[i]) || str[i] == '.'){
               if(inNum){
                vType val;
                double w = 1;
                if(str[i] == '.'){
                 hasDot = true;
                 val = 0;
                }
                else{
                 val = str[i] - '0';
                 hasDot = false;
                }
                i++;
                while(isDigit(str[i]) || str[i] == '.'){
                 if(str[i] == '.'){
                  if(hasDot) throw true;
                  hasDot = true;
                 }else{
                  if(hasDot){
                   w *= 0.1;
                   val += (str[i] - '0') * w;
                  }
                  else val = val * 10 + str[i] - '0';
                 }
                 i++;
                }
                i--;
                nodes[k++] = node(val * sign, ' ');
                sign = 1;
                inNum = false;
               }else throw true;
              }else{
               switch(str[i]){
                case '(':
                 oper.push(str[i]);
                 break;
                case ')':
                 while(!oper.empty() && oper.top() != '('){
                  nodes[k++] = node(0, oper.top());
                  oper.pop();
                 }
                 if(oper.empty()) throw true;  //沒有與')'匹配的'('
                 oper.pop();
                 break;
                case '+':
                case '-':
                case '*':
                case '/':
                case '^':
                 if(inNum){
                  if(str[i] != '+' && str[i] != '-') throw true;
                  while(str[i] == '+' || str[i] == '-'){
                   if(str[i] == '-') sign *= -1;
                   i++;
                  }
                  i--;
                 }else{
                  //while(!oper.empty() && ((str[i] != '^' && ms[str[i]] <= ms[oper.top()]) ||
                  // ((str[i] == '^' && ms[str[i]] < ms[oper.top()])))){ //如果^是右結(jié)合的話就用這個
                  while(!oper.empty() && ms[str[i]] <= ms[oper.top()]){
                    nodes[k++] = node(0, oper.top());
                    oper.pop();
                  }
                  oper.push(str[i]);
                  inNum = true;
                 }
                 break;
               }
              }
             }
             while(!oper.empty()){
              nodes[k++] = node(0, oper.top());
              oper.pop();
             }
             return calPoland(nodes, k);
            }
            void Cal(char* str){
             try{
              vType ans = poland(str);
              printf("%.8lf\n", ans);
             }
             catch(bool){
              printf("The teacher is so lazy!\n");
             }
            }
            int main(){
            #ifndef ONLINE_JUDGE
             //freopen("in.txt", "r", stdin);
             //freopen("out.txt", "w", stdout);
            #endif
             init();
             while(~scanf("%s", str)) Cal(str);
             return 0;
            }

            posted on 2011-01-16 19:58 tw 閱讀(715) 評論(0)  編輯 收藏 引用 所屬分類: HDU題解

            <2025年6月>
            25262728293031
            1234567
            891011121314
            15161718192021
            22232425262728
            293012345

            導(dǎo)航

            統(tǒng)計

            常用鏈接

            留言簿

            文章分類

            文章檔案

            搜索

            最新評論

            国产美女久久久| 久久毛片免费看一区二区三区| 色婷婷狠狠久久综合五月| 狠狠精品久久久无码中文字幕 | 精品国产福利久久久| 久久国产香蕉视频| 久久综合国产乱子伦精品免费| 久久久久久久尹人综合网亚洲| 久久亚洲精品无码观看不卡| 麻豆成人久久精品二区三区免费| 国内精品久久久久| 中文字幕人妻色偷偷久久| 国产精品伊人久久伊人电影| 亚洲精品美女久久777777| 精品久久久久久无码国产| 久久精品国产网红主播| 四虎影视久久久免费| 久久99精品久久久久久| 无码专区久久综合久中文字幕| 91精品国产高清久久久久久国产嫩草| 久久久久久久久波多野高潮| 精品久久久久久无码中文野结衣| 亚洲中文久久精品无码ww16| 一本大道久久香蕉成人网| 久久WWW免费人成—看片| 久久午夜电影网| 国产午夜福利精品久久2021| 午夜欧美精品久久久久久久| 久久人人爽人人人人爽AV| 久久受www免费人成_看片中文| 精品综合久久久久久88小说| 国产精久久一区二区三区| 99久久免费只有精品国产| 国产精品毛片久久久久久久| 国产精品久久久久…| 国产精品久久成人影院| 久久综合九色综合精品| 亚洲国产精品久久| 久久久久久久综合综合狠狠| 狠狠色丁香婷婷综合久久来来去| 久久国产成人午夜aⅴ影院 |