Cod sursa(job #159244)

Utilizator city_guy91alex isip city_guy91 Data 14 martie 2008 00:11:56
Problema Bool Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 3.5 kb
#include <iostream>   
#include <fstream>   
#include <cstring>   
#include <stack>   
  
using namespace std;   
  
typedef unsigned int uint;   
  
const int priority[6] = {   
    -666,   
    3,   
    1,   
    2,   
    -4,   
    -5   
};   
  
enum Token {   
    NOT = -1,   
    OR = -2,   
    AND = -3,   
    OB = -4,   
    CB = -5,   
    TRUE = 30,   
    FALSE = 31   
};   
  
char exp[11024];   
int tl[11024];   
bool vars[32];   
int pos;   
  
bool boolExpression();   
  
bool factor() {   
    bool a;   
  
    if (tl[pos] > 0) {   
        a = vars[tl[pos]];   
        ++pos;   
    } else if (NOT == tl[pos]) {   
        ++pos;   
        a = !factor();   
    } else if (OB == tl[pos]) {   
        ++pos;   
        a = boolExpression();   
        ++pos;   
    }   
  
    return a;   
}   
  
bool boolTerm() {   
//  cout << "{BT(" << pos << ") ";   
    bool a = factor();   
    while (AND == tl[pos]) {   
        ++pos;   
        bool b = factor();   
        a = a && b;   
    }   
//  cout << "BT(" << pos << ")}";   
    return a;   
}   
  
bool boolExpression() {   
//  cout << "{BE(" << pos << ") ";   
    bool a = boolTerm();   
    while (OR == tl[pos]) {   
        ++pos;   
        bool b = boolTerm();   
        a = a || b;   
    }   
//  cout << "BE(" << pos << ")}";   
    return a;   
}   
  
int main(int argc, char *argv[]) {   
    ifstream fin("bool.in");       
    fin.getline(exp, 11000);   
  
    pos = 0;   
    for (uint i (0); i < strlen(exp); ++i) {   
        if (exp[i] == ' ')   
            continue;   
  
        switch (exp[i]) {   
            case 'T':   
                if ('R' == exp[i+1]) {   
                    tl[pos++] = TRUE;   
                    i += 3;   
                } else goto letter;   
                break;   
            case 'F':   
                if ('A' == exp[i+1]) {   
                    tl[pos++] = FALSE;   
                    i += 4;   
                } else goto letter;   
                break;   
            case 'N':   
                if ('O' == exp[i+1]) {   
                    tl[pos++] = NOT;   
                    i += 2;   
                } else goto letter;   
                break;   
            case 'O':   
                if ('R' == exp[i+1]) {   
                    tl[pos++] = OR;   
                    i += 1;   
                } else goto letter;   
                break;   
            case 'A':   
                if ('N' == exp[i+1]) {   
                    tl[pos++] = AND;   
                    i += 2;   
                } else goto letter;   
                break;   
            case '(':   
                tl[pos++] = OB;   
                break;   
            case ')':   
                tl[pos++] = CB;   
                break;   
            default:   
letter:   
                tl[pos++] = exp[i] - 'A' + 1;   
        }   
    }   
    tl[pos] = -888;   
  
    /*for (int i(0); tl[i] != -888; ++i)  
        cout << tl[i] << " ";  
    cout << endl;*/  
  
    for (int i(0); i < 32; ++i)   
        vars[i] = false;   
    vars[TRUE] = true;   
  
    int N;   
    fin >> N;   
    char c;   
    ofstream fout("bool.out");   
    while (N--) {   
        fin >> c;   
        vars[c - 'A' + 1] = !vars[c - 'A' + 1];   
        pos = 0;   
//      for (int i(0); i < 26; ++i)   
//          cout << vars[i];   
//      cout << endl;   
        //fout << boolExpression();   
        fout << boolExpression();   
    }   
    fout << endl;   
    fout.close();   
    fin.close();   
  
    return 0;   
}