zoukankan      html  css  js  c++  java
  • POJ3337 Expression Evaluator[模拟运算符]

    Expression Evaluator
    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 1824   Accepted: 570

    Description

    This problem is about evaluating some C-style expressions. The expressions to be evaluated will contain only simple integer variables and a limited set of operators; there will be no constants in the expressions. There are 26 variables in the program, named by lower case letters a through z. Before evaluation, the initial values of these variables are a = 1, b = 2, ..., z = 26.

    The operators allowed are addition and subtraction (binary + and -), with their known meaning. So, the expression a + c - d + b has the value 2 (1 + 3 - 4 + 2). Additionally, ++ and –- operators are allowed in the input expression too, which are unary operators, and may come before or after variables. If the ++ operator comes before a variable, then that variable's value is increased (by one) before the variable's value is used in calculating the value of the whole expression. Thus the value of ++ c - b is 2. When ++ comes after a variable, that variable is increased (by one) after its value is used to calculate the value of the whole expression. So, the value of the c ++ - b is 1, though c is incremented after the value for entire expression is computed; its value will be 4 too. The -- operator behaves the same way, except that it decreases the value of its operand.

    More formally, an expression is evaluated in the following manner:

    • Identify every variable that are preceded by ++. Write an assignment statement for incrementing the value of each of them, and omit the ++ from before that variable in the expression.
    • Do similarly for the variables with ++ after them.
    • At this point, there is no ++ operator in the expression. Write a statement evaluating the remaining expression after the statements determined in step 1, and before those determined in step 2.
    • Execute the statements determined in step 1, then those written in step 3, and finally the one written in step 2.

    This way, evaluating ++ a + b ++ is the same as computing a = a + 1, result = a + b, and b = b + 1.

    Input

    The first line of the input contains a single integer T which is the number of test cases, followed by T lines each containing the input expression for a test case. Ignore blanks in the input expression. Be sure that no ambiguity is in the input expressions (like a+++b). Similarly, ++ or -- operators do not appear both before and after one single variable (like ++a++). You may safely assume each variable appears only once in an expression.

    Output

    For each test case, write each expression as it appears in the input (exactly), then write the value of the complete expression. After this, on separate lines, write the value of each variable after evaluating the expression (write them in sorted order of the variable names). Write only the values of the variables that are used in the expressions. To find out about the output format, follow the style used in the sample output below.

    Sample Input

    2
    a+b
    c+f--+--a
    

    Sample Output

    Expression: a+b
    value = 3
    a = 1
    b = 2
    Expression: c+f--+--a
    value = 9
    a = 0
    c = 3
    f = 5
    

    Source

     
     
     
     
    模拟运算符
     
    code:
      1 #include<iostream>
      2 #include<cstdio>
      3 #include<cstring>
      4 using namespace std;
      5 
      6 int data[26];
      7 char opr[10010];
      8 char opr1[10010];
      9 bool vst[26];
     10 
     11 void init()
     12 {
     13     int i;
     14     for(i=0;i<26;i++)
     15         data[i]=i+1;
     16     memset(vst,false,sizeof(vst));
     17 }
     18 
     19 int main()
     20 {
     21     int ans;
     22     int t;
     23     int i,j;
     24     scanf("%d\n",&t);
     25     while(t--)
     26     {
     27         init();
     28         ans=0;
     29         bool flag=false;
     30         gets(opr1);
     31         int len=strlen(opr1);
     32         for(i=0,j=0;i<len;i++)
     33         {
     34             if(opr1[i]!=' ')
     35             {                
     36                 opr[j]=opr1[i];
     37                 j++;
     38             }
     39         }
     40         for(i=0;i<j;i++)
     41         {
     42             if(flag==false)
     43             {
     44                 if(opr[i]>='a'&&opr[i]<='z')
     45                 {
     46                     if(i-2>=0&&opr[i-2]=='+'&&opr[i-1]=='+')
     47                     {
     48                         data[opr[i]-'a']++;
     49                         ans=data[opr[i]-'a'];
     50                         vst[opr[i]-'a']=true;
     51                     }
     52                     else if(i-2>=0&&opr[i-2]=='-'&&opr[i-1]=='-')
     53                     {
     54                         data[opr[i]-'a']--;
     55                         ans=data[opr[i]-'a'];
     56                         vst[opr[i]-'a']=true;
     57                     }
     58                     else if(opr[i+2]=='+'&&opr[i+1]=='+')
     59                     {
     60                         ans=data[opr[i]-'a']++;
     61                         vst[opr[i]-'a']=true;
     62                     }
     63                     else if(opr[i+2]=='-'&&opr[i+1]=='-')
     64                     {
     65                         ans=data[opr[i]-'a']--;
     66                         vst[opr[i]-'a']=true;
     67                     }
     68                     else if(opr[i-1]=='-')
     69                     {
     70                         ans=-data[opr[i]-'a'];
     71                         vst[opr[i]-'a']=true;
     72                     }
     73                     else
     74                     {
     75                         ans=data[opr[i]-'a'];
     76                         vst[opr[i]-'a']=true;
     77                     }
     78                     flag=true;
     79                 }
     80             }
     81             else
     82             {
     83                 if(opr[i]>='a'&&opr[i]<='z')
     84                 {
     85                     if(i-4>=0&&opr[i-2]=='+'&&opr[i-1]=='+'&&opr[i-3]=='-')
     86                     {
     87                         ans=ans-(++data[opr[i]-'a']);
     88                         vst[opr[i]-'a']=true;
     89                     }
     90                     else if(i-4>=0&&opr[i-2]=='-'&&opr[i-1]=='-'&&opr[i-3]=='+')
     91                     {
     92                         ans=ans+(--data[opr[i]-'a']);
     93                         vst[opr[i]-'a']=true;
     94                     }
     95                     else if(opr[i+1]=='+'&&opr[i+2]=='+')
     96                     {
     97                         if(opr[i-1]=='-')
     98                         {
     99                             ans=ans-data[opr[i]-'a'];
    100                             data[opr[i]-'a']++;
    101                             vst[opr[i]-'a']=true;
    102                         }
    103                         if(opr[i-1]=='+')
    104                         {
    105                             ans=ans+data[opr[i]-'a'];
    106                             data[opr[i]-'a']++;
    107                             vst[opr[i]-'a']=true;
    108                         }
    109                     }
    110                     else if(opr[i+1]=='-'&&opr[i+2]=='-')
    111                     {
    112                         if(opr[i-1]=='+')
    113                         {
    114                             ans=ans+data[opr[i]-'a'];
    115                             data[opr[i]-'a']--;
    116                             vst[opr[i]-'a']=true;
    117                         }
    118                         if(opr[i-1]=='-')
    119                         {
    120                             ans=ans-data[opr[i]-'a'];
    121                             data[opr[i]-'a']--;
    122                             vst[opr[i]-'a']=true;
    123                         }
    124                     }
    125                     else if(opr[i-1]=='-')
    126                     {
    127                         ans=ans-data[opr[i]-'a'];
    128                         vst[opr[i]-'a']=true;
    129                     }
    130                     else if(opr[i-1]=='+')
    131                     {
    132                         ans=ans+data[opr[i]-'a'];
    133                         vst[opr[i]-'a']=true;
    134                     }
    135                 }
    136             }
    137         }
    138         printf("Expression: %s\n",opr1);
    139         printf("value = %d\n",ans);
    140         for(i=0;i<26;i++)
    141         {
    142             if(vst[i])
    143                 printf("%c = %d\n",'a'+i,data[i]);
    144         }
    145     }
    146     return 0;
    147 }
    148 /*
    149 10
    150 --a+b--+c--+--d
    151 Expression: --a+b--+c--+--d
    152 value = 9
    153 a = 0
    154 b = 1
    155 c = 2
    156 d = 3
    157 
    158 */
  • 相关阅读:
    ASP.NET Core 集成 WebSocket
    如何在CentOS7上安装桌面环境?
    Visual Studio 2017 远程调试(Remote Debugger)应用
    cmd sc命令进行服务操作
    EntityFrameworkCode 操作MySql 相关问题
    Windows下安装PHP开发环境
    未在本地计算机上注册“microsoft.ACE.oledb.12.0”提供程序
    System.Runtime.InteropServices.COMException (0x800A03EC): 无法访问文件
    c# WinFo判断当前程序是否已经启动或存在的几种方式
    MVC自定义视图引擎地址
  • 原文地址:https://www.cnblogs.com/XBWer/p/2687474.html
Copyright © 2011-2022 走看看