Tsinsen A1112 代数表达式

http://www.tsinsen.com/A1112

分析:这题有水..随便乱糊弄就过了。较真的话“a+;”应该输出ERROR 3的,随便输出“OK”也过了。不过输出”ERROR 3”也过了,总的来说这题有水。

代码:

#include "bits/stdc++.h"
using namespace std;

char s[55];
int idx, size, Succ, L;

int GetOption();

int GetVal() {
    while (s[idx] == '(' && idx < size) ++idx;
    if (idx >= size) return 0;
    if (s[idx] == 'a' || s[idx] == 'b' || s[idx] == 'c') {
        idx = idx + 1;
        while (s[idx] == '(' && idx < size) ++idx;
        if (idx >= size) return 1;
        else return GetOption();
    }
    if (s[idx] == '+' || s[idx] == '-' || s[idx] == '*' || s[idx] == '/') {
        puts("ERROR 3"); Succ = 0; return 0;
    }
    puts("ERROR 1"); Succ = 0; return 0;
}

int GetOption() {
    while (s[idx] == '(' && idx < size)  ++idx;
    if (idx >= size) return 0;
    if (s[idx] == '+' || s[idx] == '-' || s[idx] == '*' || s[idx] == '/') {
        idx = idx + 1; return GetVal();
    }
    if (s[idx] == 'a' || s[idx] == 'b' || s[idx] == 'c') {
        puts("ERROR 3"); Succ = 0; return 0;
    }
    puts("ERROR 1"); Succ = 0; return 0;
}

int main() {
    scanf("%s", s);
    size = strlen(s);
    s[--size] = '\0';
    Succ = 1;
    for (int i = 0; i < size; ++i) {
        if (s[i] == '(') ++L;
        if (s[i] == ')') {
            Succ &= (L-- > 0);
            s[i] = '(';
        }
    }
    if (L != 0 || !Succ) puts("ERROR 2");
    else if (GetVal()) puts("OK");
    else if (Succ) puts("ERROR 3");
    return 0;
}

你可能感兴趣的:(ACM-01)