UVA397 LA5314 POJ1559 ZOJ1725 Equation Elation【表達式求值】

The author of an elementary school algebra text book has approached you to write a program to solve simple algebra equations. The author wants to use a program to avoid human errors in preparing the solutions manual. The text book author will provide a text file of the simple problems for your problem to solve. All of the problems will be in the form of an algebraic equality. The specific syntax of the problems will be an algebraic statement consisting of integer constants and the four basic arithmetic operators, an equal sign, and a variable. For example:
12 - 4 * 3 = x
    For the solutions manual the problem is not just to be solved, but solved one step at a time. For the above input line, the corresponding output would be:
12 - 4 * 3 = x
12 - 12 = x
0 = x
    The simple problems your program is to solve are limited to integer values with multiplication, division, addition and subtraction operations. Note that, as in the above example, the computation must follow the standard order of precedence for arithmetic operations. All multiplications and divisions are performed, from left to right, before any additions and subtractions, and then all additions and subtractions are performed from left to right. You may assume that all divisions will result in integer values.
Input
The input file will consist of several equations to be solved. Each equation will contain from 1 to 20 operations with 2 to 21 integer operands (there will, of course, always be one more operand than operators). Integer operators in the input may have a leading sign (i.e. may be preceded by a unary operator). Spaces in the input line are optional; that is, spaces may be present between operators and operands, or they may not. The variable names will consist of 1 to 8 alphabetic characters.
Output
Output for a problem should begin with the problem to be solved, then followed by one line of output after each operation. The spacing between the numbers and operations in the output is not critical. Having the correct answers and all the correct steps in the output is important.
    A typical input file will consist of multiple algebraic problems, each on a separate line. The output for each input problem should be separated by a single blank line in the output. The end of the file marks the end of the input.
Sample Input
3 * 4 + 4 - 1 / 1 = xyzzy
12 + 2 * 12 / 2 - 1 = y
2 * -3 + -6 - +4 = r
2*-3±6-+4=r
Sample Output
3 * 4 + 4 - 1 / 1 = xyzzy
12 + 4 - 1 / 1 = xyzzy
12 + 4 - 1 = xyzzy
16 - 1 = xyzzy
15 = xyzzy

12 + 2 * 12 / 2 - 1 = y
12 + 24 / 2 - 1 = y
12 + 12 - 1 = y
24 - 1 = y
23 = y

2 * -3 + -6 - 4 = r
-6 + -6 - 4 = r
-12 - 4 = r
-16 = r

2 * -3 + -6 - 4 = r
-6 + -6 - 4 = r
-12 - 4 = r
-16 = r

問題鏈接UVA397 LA5314 POJ1559 ZOJ1725 Equation Elation
問題簡述:(略)
問題分析
    表達式計算問題,本來涉及到詞法分析、語法分析和表達式求值的問題。表達式求值多用堆棧來實現。本題的表達式比較簡單,只有優先級問題,沒有括號,可以用簡單的程序處理來實現。
程序說明:(略)
參考鏈接:(略)
題記:(略)

AC的C++語言程序如下:

/* UVA397 LA5314 POJ1559 ZOJ1725 Equation Elation */

#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

const int N = 20;
int a[N + 1];       // 操作數 operands
char ops[N];        // 操作符 operations
char var[N + 1];        // 變量名

bool isop(char c)
{
    return c == '+' || c == '-' || c == '*' || c == '/';
}

int main()
{
    string s;
    int caseno = 0;

    while(getline(cin, s)) {
        if(s == "") continue;
        if(caseno++) putchar('\n');

        // 詞法分析與語法分析,結果放入操作數和操作符數組中
        int state = 0, v = 0, sign = 1, cnt = 0;
        for(int i = 0; s[i]; i++) {
            if(state == 0) {
                if(s[i] == '+' || s[i] == '-' || isdigit(s[i])) {
                    state = 1;
                    if(s[i] == '+') sign = 1;
                    else if(s[i] == '-') sign = -1;
                    else v = s[i] - '0';
                }
            } else if(state == 1) {
                if(s[i] == ' ' || s[i] == '=' || isop(s[i])) {
                    a[cnt] = v * sign;
                    sign = 1;
                    v = 0;
                    if(isop(s[i])) {
                        state = 0;
                        ops[cnt++] = s[i];
                    } else if(s[i] == '=') {
                        state = 3;
                        ops[cnt++] = s[i];
                    } else
                        state = 2;
                } else
                    v = v * 10 + s[i] - '0';
            } else if(state == 2) {
                ops[cnt++] = s[i];
                if(isop(s[i])) state = 0;
                else if(s[i] == '=') state = 3;
            } else if(state == 3) {
                if(s[i] != ' ') {
                    strcpy(var, &s[i]);
                    int len = strlen(var) - 1;
                    while(var[len] == ' ') {
                        var[len] = '\0';
                        len--;
                    }
                    break;
                }
            }
        }

        // 計算方程式
        for(int i = 0; i < cnt; i++) {
            for(int j = i; j < cnt - 1; j++)
                printf("%d %c ", a[j], ops[j]);
            printf("%d = %s\n", a[cnt - 1], var);

            int k = i;      // 最前面的乘(*)或除(/),也可能沒有
            for(int j = cnt - 1; j >= i; j--)
                if(ops[j] == '*' || ops[j] == '/') k = j;
            if(ops[k] == '+') a[k + 1] = a[k] + a[k + 1];
            else if(ops[k] == '-') a[k + 1] = a[k] - a[k + 1];
            else if(ops[k] == '*') a[k + 1] = a[k] * a[k + 1];
            else if(ops[k] == '/') a[k + 1] = a[k] / a[k + 1];

            for(int j = k; j >= i; j--) {
                a[j] = a[j - 1];
                ops[j] = ops[j - 1];
            }
        }
    }

    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章