POJ1539 UVA327 UVALive5435 Evaluating Simple C Expressions

Evaluating Simple C Expressions
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 651 Accepted: 273

Description

The task in this problem is to evaluate a sequence of simple C expressions, but 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 80 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, ...). 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 evaluation 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 value of the expression -- c + b -- has the value 4, with variables b and c having the values 1 and 2 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:

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 ++ from before that variable in the expression.
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.
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.
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

Your program is to read expressions, one per line, until a totally blank (or empty) line is read.
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.

Output

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 output format.

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

Source

North Central North America 1993

Regionals 1993 >> North America - North Central NA

问题链接POJ1539 UVA327 UVALive5435 Evaluating Simple C Expressions
问题简述:(略)
问题分析
    这个问题就是一个表达式求值问题。一般而言,对于表达式采用语法树来处理。然而,这里的题解采用文本处理的方式来解决,只是需要多遍扫描,这个题解做了三遍扫描。
    每一遍处理需要将已经处理的字符删除,相对比较费时间。比起用STL的容器存储来,应该还是快的。
程序说明
     程序中使用2个数组分别存储当前变量值,和运算符"++"和"--"运算之后的变量值。
参考链接:(略)
题记:(略)

AC的C++程序如下:

/* POJ1539 UVA327 UVALive5435 Evaluating Simple C Expressions */

#include <iostream>
#include <cstring>
#include <cctype>

using namespace std;

const int N = 26;
int cv[N], av[N];       // 变量当前值和变更后值
bool flag[N];

int main()
{
    string s;
    while(getline(cin, s)) {
        string expression = s;

        memset(flag, false, sizeof(flag));  // 初始化
        for(int i = 0; i < N; i++)
            cv[i] = av[i] = i + 1;

        int j = 0;
        for(int i = 0; s[i]; i++) {      // 去空格
            if(s[i] != ' ')
                s[j++] = s[i];
        }
        s[j] = '\0';

        j = 0;
        for(int i = 0; s[i]; i++) {     // 处理++和--
            if(s[i] == '+' && s[i + 1] == '+') {
                if(isalpha(s[i - 1]))
                    av[s[i - 1] - 'a']++;
                else if(isalpha(s[i + 2])) {
                    av[s[i + 2] - 'a']++;
                    cv[s[i + 2] - 'a']++;
                }
                i++;
            } else if(s[i] == '-' && s[i + 1] == '-') {
                if(isalpha(s[i - 1]))
                    av[s[i - 1] - 'a']--;
                else if(isalpha(s[i + 2])) {
                    av[s[i + 2] - 'a']--;
                    cv[s[i + 2] - 'a']--;
                }
                i++;
            } else
                s[j++] = s[i];
        }
        s[j] = '\0';

        int ans = 0;
        bool plus = true;
        for(int i = 0; s[i]; i++) {
            if(isalpha(s[i])) {
                if(plus)
                    ans += cv[s[i] - 'a'];
                else
                    ans -= cv[s[i] - 'a'];
                flag[s[i] - 'a'] = true;
            } else
                plus = (s[i] == '+');
        }

        cout << "Expression: " << expression << endl;
        cout << "    value = " << ans << endl;
        for (int i = 0; i < N; i++)
            if (flag[i])
                cout << "    " << char('a' + i) << " = " << av[i] << endl;
    }

    return 0;
}

posted on 2019-02-28 11:09  新海岛Blog  阅读(120)  评论(0)    收藏  举报

导航

// ... runAll: function() { this.resetPreCode(); hljs.initHighlightingOnLoad(); // 重新渲染,添加语法高亮 hljs.initLineNumbersOnLoad(); // 为代码加上行号 } // ...