• nyist OJ 35 (表达式求值)


    题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=35

    直接上代码:

    View Code
      1 #include <cstdio>
      2 #include <cstring>
      3 #include <cstdlib>
      4 using namespace std;
      5 
      6 const int N = 1001;
      7 char stack[N];
      8 char op[N];
      9 char s[N];
     10 int top;
     11 int top_op;
     12 int compare(char a);
     13 void translate();
     14 void calculator();
     15 
     16 int main()
     17 {
     18     int t;
     19     scanf("%d", &t);
     20     while(t--)
     21     {
     22           translate();
     23           calculator(); 
     24     }
     25     return 0;    
     26 }
     27 
     28 int compare(char a)
     29 {
     30     switch(a)
     31     {
     32              case '+':
     33              case '-':
     34                   return 1;
     35              case '*':
     36              case '/':
     37                   return 2;
     38              case '(':
     39                   return 0;
     40              default:
     41                      return -1;         
     42     }    
     43 }
     44 
     45 void translate()
     46 {
     47      scanf("%s", s);
     48      int len = strlen(s) - 1;
     49      top = -1;
     50      top_op = -1;
     51      for(int i = 0; i < len; i++)
     52      {
     53            if('0' <= s[i] && s[i] <= '9' || s[i] == '.')
     54            {
     55                   top++;
     56                   stack[top] = s[i];       
     57            }  
     58            else if(s[i] == '(')
     59            {
     60                   top_op++;
     61                   op[top_op] = s[i];     
     62            }
     63            else if(s[i] == ')')
     64            {
     65                   while(op[top_op] != '(')
     66                   {
     67                         top++;
     68                         stack[top] = ' ';
     69                         top++;
     70                         stack[top] = op[top_op];
     71                         top_op--;                 
     72                   }   
     73                   top_op--;
     74            }
     75            else if(s[i] == '+' || s[i] == '-' || s[i] == '*' || s[i] == '/')
     76            {
     77                  top++;
     78                  stack[top] = ' ';
     79                  while(compare(s[i]) <= compare(op[top_op]))
     80                  {
     81                          top++;
     82                          stack[top] = op[top_op];
     83                          top++;
     84                          stack[top] = ' ';
     85                          top_op--;                    
     86                  }     
     87                  top_op++;
     88                  op[top_op] = s[i];
     89            }
     90      }     
     91      while(top_op != -1)
     92      {
     93            top++;
     94            stack[top] = ' ';
     95            top++;
     96            stack[top] = op[top_op];
     97            top_op--;             
     98      }
     99      top++;
    100      stack[top] = ' ';
    101      top++;
    102      stack[top] = '=';
    103      top++;
    104      stack[top] = '\0';
    105 }
    106 
    107 void calculator()
    108 {
    109      double d[N];
    110      double r1, r2;
    111      char a[100];
    112      char ch;
    113      int k;
    114      int top_d = -1;
    115      for(int i = 0; i < top; i++)
    116      {
    117           k = 0;
    118           ch = stack[i];
    119           while(stack[i] != ' ')
    120           {
    121                  if(stack[i] == '=')
    122                      break;
    123                  a[k++] = stack[i];
    124                  i++;      
    125           }          
    126           if('0' <= ch && ch <= '9')
    127           {
    128                  a[k] = '\0';
    129                  top_d++;
    130                  d[top_d] = atof(a);       
    131           } 
    132           else
    133           {
    134                  switch(ch)
    135                  {
    136                        case '+':
    137                               r2 = d[top_d];
    138                               top_d--;
    139                               r1 = d[top_d];
    140                               d[top_d] = r1 + r2;
    141                               break;
    142                        case '-':
    143                               r2 = d[top_d];
    144                               top_d--;
    145                               r1 = d[top_d];
    146                               d[top_d] = r1 - r2;
    147                               break;
    148                        case '*':
    149                               r2 = d[top_d];
    150                               top_d--;
    151                               r1 = d[top_d];
    152                               d[top_d] = r1 * r2;
    153                               break;
    154                        case '/':
    155                               r2 = d[top_d];
    156                               top_d--;
    157                               r1 = d[top_d];
    158                               d[top_d] = r1 / r2;
    159                               break;
    160                        default:
    161                                break;        
    162                  }    
    163           }
    164      } 
    165      printf("%.2lf\n", d[0]);       
    166 }
  • 相关阅读:
    poj3277 City Horizon
    60.左值右值以及类型判断
    59.C++与正则表达式
    57.C++处理转义字符
    56.lambda表达式与绑定以及伪函数和绑定
    55.函数模板指针匹配(模板自动匹配*多的)
    54.函数模板默认参数
    53.伪函数与函数绑定器
    52.模板的重载
    51.模板与引用
  • 原文地址:https://www.cnblogs.com/10jschen/p/2631172.html
Copyright © 2020-2023  润新知