• <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(逆波蘭表達式求值)

            /*************************************/
            /*支持數字(而非表達式)前面有正負號   */
            /*支持浮點數的+,-,*,/,^ 運算      */
            /*************************************/
            #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標記當前是否可以輸入數字, hasDot標記是否已經輸入小數點
             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()])))){ //如果^是右結合的話就用這個
                  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

            導航

            統計

            常用鏈接

            留言簿

            文章分類

            文章檔案

            搜索

            最新評論

            久久国产精品一国产精品金尊| 久久高潮一级毛片免费| 99久久精品国产一区二区 | 久久亚洲2019中文字幕| 久久强奷乱码老熟女| 精品久久久久久无码专区 | 亚洲一区二区三区日本久久九| 久久天天躁狠狠躁夜夜网站| 色婷婷综合久久久中文字幕| 无码人妻久久一区二区三区免费| 久久99精品综合国产首页| 久久精品亚洲男人的天堂| 日产精品久久久久久久| 日韩AV毛片精品久久久| 无码伊人66久久大杳蕉网站谷歌 | 久久香蕉综合色一综合色88| 久久亚洲国产精品成人AV秋霞 | 亚洲va国产va天堂va久久| 久久青草国产精品一区| 久久精品免费一区二区| 亚洲欧美日韩久久精品第一区| 人人狠狠综合久久亚洲| 91麻精品国产91久久久久| 99久久精品国产麻豆| 国产成人99久久亚洲综合精品 | 一97日本道伊人久久综合影院| 精品久久久久成人码免费动漫| 久久不见久久见免费视频7| 久久久久亚洲精品天堂久久久久久 | 欧美激情精品久久久久| 99精品久久精品| 久久福利青草精品资源站免费| 亚洲精品乱码久久久久久蜜桃不卡 | 国内精品伊人久久久久777| 无码任你躁久久久久久久| 久久久噜噜噜久久中文字幕色伊伊| 99久久久精品免费观看国产| 久久国产色AV免费看| 成人国内精品久久久久一区| 国产精品视频久久久| 久久99国产精品一区二区|