uva327Evaluating Simple C Expressions

题目大概意思就是计算一个表达式的值(不知道这道题和书什么关系。。顺序扫一遍就好了)

题目:

 Evaluating Simple C Expressions 

The task in this problem is to evaluate a sequence of simple C expressions, buy you need not know C to solve the problem! Each of the expressions will appear on a line by itself and will contain no more than 110 characters. 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 which may appear in our simple expressions, namely those with the names a through z (lower-case letters only). At the beginning of evaluation of each expression, these 26 variables will have the integer values 1 through 26, respectively (that is, a = 1, b = 2, ..., n = 14, o = 15, ..., z = 26). Each variable will appear at most once in an expression, and many variables may not be used at all.

The operators that may appear in expressions include the binary (two-operand) + and -, with the usual interpretation. Thus the expression a + c - d + b has the value 2 (computed as 1 + 3 - 4 + 2). The only other operators that may appear in expressions are ++ and --. These are unary (one-operand) operators, and may appear before or after any variable. When the ++ operator appears before a variable, that variable's value is incremented (by one) before the variable's value is used in determining the value of the entire expression. Thus the value of the expression ++c - b is 2, with c being incremented to 4 prior to evaluating the entire expression. When the ++ operator appears after a variable, that variable is incremented (again, by one) after its value is used to determine the value of the entire expression. Thus the value of the expression c++ - b is 1, but c is incremented after the complete expression is evaluated; its value will still be 4. The -- operator can also be used before or after a variable to decrement (by one) the variable; its placement before or after the variable has the same significance as for the ++ operator. Thus the expression --c + b-- has the value 4, with variables c and b having the values 2 and 1 following the evaluation of the expression.

Here's another, more algorithmic, approach to explaining the ++ and -- operators. We'll consider only the ++ operator, for brevity:

  1. Identify each variable that has a ++ operator before it. Write a simple assignment statement that increments the value of each such variable, and remove the ++ operator from before that variable in the expression.
  2. In a similar manner, identify each variable that has a ++ operator after it. Write a simple assignment statement that increments the value of each of these, and remove the ++ operator from after that variable in the expression.
  3. Now the expression has no ++ operators before or after any variables. Write the statement that evaluates the remaining expression after those statements written in step 1, and before those written in step 2.
  4. Execute the statements generated in step 1, then those generated in step 3, and finally the one generated in step 2, in that order.

Using this approach, evaluating the expression ++a + b++ is equivalent to computing

  • a = a + 1 (from step 1 of the algorithm)
  • expression = a + b (from step 3)
  • b = b + 1 (from step 2)

where expression would receive the value of the complete expression.

Input and Output

Your program is to read expressions, one per line, until the end of the file is reached. Display each expression exactly as it was read, then display the value of the entire expression, and on separate lines, the value of each variable after the expression was evaluated. Do not display the value of variables that were not used in the expression. The samples shown below illustrate the desired exact output format.

Blanks are to be ignored in evaluating expressions, and you are assured that ambiguous expressions like a+++b (ambiguous because it could be treated as a++ + b or a + ++b) will not appear in the input. Likewise, ++ or -- operators will never appear both before and after a single variable. Thus expressions like ++a++ will not be in the input data.

Sample Input

a + b
b - z
a+b--+c++
c+f--+--a
   f-- + c-- + d-++e

Sample Output

Expression: a + b
    value = 3
    a = 1
    b = 2
Expression: b - z
    value = -24
    b = 2
    z = 26
Expression: a+b--+c++
    value = 6
    a = 1
    b = 1
    c = 4
Expression: c+f--+--a
    value = 9
    a = 0
    c = 3
    f = 5
Expression:    f-- + c-- + d-++e
    value = 7
    c = 2
    d = 4
    e = 6
    f = 5


代码:

  
  1 #include <iostream>
  2 #include <cstdio>
  3 #include <cstring>
  4 #include <cctype>
  5 using namespace std;
  6 char temp[200];
  7 char str[200];
  8 int ps=0;
  9 int single[200];
 10 int vis[200];
 11 int main()
 12 {
 13     //freopen("in.txt","r",stdin);
 14 
 15     while(gets(temp))
 16     {
 17         ps=0;
 18         for(int i=0;i<strlen(temp);i++)
 19         {
 20             if(temp[i]!=' ')str[ps++]=temp[i];
 21         }
 22 
 23         cout<<"Expression: "<<temp<<endl;
 24         int value = 0;
 25         for(int i=0;i<strlen(str); )
 26         {
 27             if(!i && isalpha(str[i])) {value += str[i]-'a'+1;single[str[i]-'a'] = str[i]-'a'+1;vis[str[i]-'a']=1; i++;}
 28                 if(str[i]==str[i+1]&& str[i+1]=='+')
 29                 {
 30                     if( i && isalpha(str[i-1]))
 31                     {
 32                         single[str[i-1]-'a'] ++;
 33                         i+=2;
 34                     }
 35                     else if( isalpha(str[i+2]))
 36                     {
 37                         value+= str[i+2]-'a'+2;
 38                         single[str[i+2]-'a'] = str[i+2]-'a'+2;
 39                         vis[str[i+2]-'a'] =1;
 40                          i+=3;
 41                     }
 42 
 43                 }
 44                 if(str[i]==str[i+1]&& str[i+1]=='-')
 45                 {
 46                     if( i && isalpha(str[i-1]))
 47                     {
 48                         single[str[i-1]-'a'] --;
 49                         i+=2;
 50                     }
 51                     else if( isalpha(str[i+2]))
 52                     {
 53                         value+= str[i+2]-'a';
 54                         single[str[i+2]-'a'] = str[i+2]-'a';
 55                         vis[str[i+2]-'a'] =1;
 56 
 57                         i+=3;
 58                     }
 59                 }
 60 
 61                 if(str[i]=='+')
 62                 {
 63                     int tt=i;
 64                     while(!isalpha(str[tt]))tt++;
 65 
 66                     if(tt-1==i)
 67                     {
 68                         value+=str[tt]-'a'+1;
 69                         single[str[tt]-'a'] = str[tt]-'a'+1;
 70                         vis[str[tt]-'a'] =1;
 71                         i=tt+1;
 72 
 73                     }
 74                     else
 75                     {
 76                         if(str[tt-1]=='-')
 77                         {
 78                             value += str[tt]-'a';
 79                             single[str[tt]-'a'] = str[tt]-'a' ;
 80                             vis[str[tt]-'a'] =1;
 81                             i=tt+1;
 82                         }
 83                     }
 84                 }
 85                 else if(str[i]=='-')
 86                 {
 87                      int tt=i;
 88                     while(!isalpha(str[tt]))tt++;
 89                     if(tt-1==i)
 90                     {
 91                         value-=str[tt]-'a'+1;
 92                         single[str[tt]-'a'] = str[tt]-'a'+1;
 93                         vis[str[tt]-'a'] =1;
 94                         i=tt+1;
 95                     }
 96                     else
 97                     {
 98                         if(str[tt-1]=='+')
 99                         {
100                             value -= str[tt]-'a'+2;
101                             single[str[tt]-'a'] = str[tt]-'a'+2 ;
102                            vis[str[tt]-'a'] =1;
103                             i=tt+1;
104                         }
105                     }
106 
107 
108                 }
109 
110 
111 
112 
113         }
114 
115 
116 
117         cout<<"    value = "<<value<<endl;
118         for(int i=0;i<26;i++)
119         {
120             if(vis[i])
121             {
122                 cout<<"    "<<char('a'+i)<<" = "<<single[i]<<endl;
123             }
124         }
125         value=0;
126         memset(temp,0,sizeof(temp));
127         memset(str,0,sizeof(str));
128         memset(single,0,sizeof(single));
129         memset(vis,0,sizeof(vis));
130     }
131     return 0;
132 }
原文地址:https://www.cnblogs.com/doubleshik/p/3472280.html