• HDU 1237(表达式求值)


    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1237

    这道题主要思路是这样的:设置两个栈,一个用来保存后缀式的栈,一个用来暂时保存运算符的栈,将中序表达式一个一个字符地读入,遇到数字字符就直接压入后缀式栈,遇到运算符时就先暂时保存到运算符栈中,等到下次读到字符时将运算符栈中的运算符与之比较优先级,若运算符栈里的运算符的优先级高于这次读到的运算符就将运算符栈中的运算符进栈,否则将这个运算符压入运算符栈。

    代码如下:

      1 #include <cstdio>
      2 #include <cstring>
      3 #include <cstdlib>
      4 using namespace std;
      5 
      6 const int N = 202;
      7 char stack[N];
      8 char op[N];
      9 char s[N];
     10 int top;
     11 int top_op;
     12 
     13 int compare(char a);
     14 void translate();
     15 void calculator();
     16 
     17 int main()
     18 {
     19   while(gets(s) && strcmp(s, "0"))
     20   {
     21     translate();
     22     calculator(); 
     23   }
     24   return 0; 
     25 }
     26 
     27 int compare(char a)
     28 {
     29   switch(a)
     30   {
     31     case '+':
     32     case '-':
     33       return 1;
     34     case '*':
     35     case '/':
     36       return 2;
     37     default:
     38       return 0; 
     39   } 
     40 }
     41 
     42 void translate()
     43 {
     44   int len = strlen(s);
     45   top = -1;
     46   top_op = -1;
     47   for(int i = 0; i < len; i++)
     48   {
     49     if('0' <= s[i] && s[i] <= '9')
     50     {
     51       top++;
     52       stack[top] = s[i]; 
     53     } 
     54     else if(s[i] == ' ')
     55     {
     56       ; 
     57     }
     58     else if(s[i] == '+' || s[i] == '-' || s[i] == '*' || s[i] == '/')
     59     {
     60       top++;
     61       stack[top] = ' ';
     62       if(top_op != -1)
     63       {
     64         while(compare(s[i]) <= compare(op[top_op]))
     65         {
     66           top++; 
     67           stack[top] = op[top_op];
     68           top++;
     69           stack[top] = ' ';
     70           top_op--; 
     71         } 
     72       } 
     73       top_op++;
     74       op[top_op] = s[i];
     75     }
     76 
     77   } 
     78   while(top_op != -1)
     79   {
     80     top++;
     81     stack[top] = ' ';
     82     top++;
     83     stack[top] = op[top_op];
     84     top_op--; 
     85   } 
     86   top++;
     87   stack[top] = '\0';
     88 }
     89 
     90 void calculator()
     91 {
     92   double d[N];
     93   int top_d = -1;
     94   double r1, r2;
     95   int k;
     96   char ch;
     97   char a[100];
     98   for(int i = 0; i < top; i++)
     99   {
    100     k = 0;
    101     ch = stack[i];
    102     while(stack[i] != ' ')
    103     {
    104       if(stack[i] == '\0')
    105         break;
    106       a[k++] = stack[i]; 
    107       i++; 
    108     } 
    109     if('0' <= ch && ch <= '9')
    110     {
    111       a[k] = '\0';
    112       top_d++;
    113       d[top_d] = atof(a); 
    114     }
    115     else
    116     {
    117       switch(ch)
    118       {
    119         case '+':
    120           r2 = d[top_d];
    121           top_d--; 
    122           r1 = d[top_d];
    123           d[top_d] = r1 + r2;
    124           break;
    125         case '-':
    126           r2 = d[top_d];
    127           top_d--;
    128           r1 = d[top_d];
    129           d[top_d] = r1 - r2;
    130           break;
    131         case '*':
    132           r2 = d[top_d];
    133           top_d--;
    134           r1 = d[top_d];
    135           d[top_d] = r1 * r2;
    136           break; 
    137         case '/':
    138           r2 = d[top_d];
    139           top_d--;
    140           r1 = d[top_d];
    141           d[top_d] = r1 / r2;
    142           break;
    143        } 
    144     } 
    145   } 
    146   printf("%.2lf\n", d[0]); 
    147 }
  • 相关阅读:
    struts2 标签给iterator添加自然序号
    theirtts2
    zhihutheirTTS1
    theirs《A 2019 Guide to Speech Synthesis with Deep Learning》
    theirmvsnetv00000
    theirmvsnetv2
    theirMeshCNN/
    their MVF-Net: Multi-View 3D Face Morphable Model Regression(2019 CVPR)
    their DenseFusion6dof
    C++35
  • 原文地址:https://www.cnblogs.com/10jschen/p/2631171.html
Copyright © 2020-2023  润新知