Cod sursa(job #941701)

Utilizator andreiagAndrei Galusca andreiag Data 19 aprilie 2013 14:57:06
Problema Bool Scor 70
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.95 kb
#include <iostream>
#include <fstream>
#include <map>
#include <vector>
#include <set>
#include <stack>
#include <string>
#include <cctype>

using namespace std;

stack<char> operations;
stack<bool> factors;
map<char, bool> dict;
// NOT > AND > OR;

bool high_prec(char a, char b)
{
    return (a == '!' && (b == '&' || b == '|')) || (a == '&' || b == '|');
}

string process_line(string &line)
{
    string ret;
    operations.push('#');
    int s = line.size();
    int  i = 0;
    while (i < s) {
        char c = line[i];
        if (c == ' ') i++;
        else if (c == '(') {operations.push(c); i++;} //open parentheses
        else if (c == ')') {char d = operations.top();
                            while(d != '('){ret += d; operations.pop(); d = operations.top();}
                            operations.pop(); i++;} //close parentheses
        else if (i == s - 1 || !isalpha(line[i+1])) {ret += c; i++;} //single letter, so symbol
        else if (c == 'T') {ret += '1'; i += 4;}
        else if (c == 'F') {ret += '0'; i += 5;}
        else {int k; switch (c) {
                        case 'A' : {c = '&'; k = 3;} break;
                        case 'O' : {c = '|'; k = 2;} break;
                        case 'N' : {c = '!'; k = 3;} break;}
              if (operations.top() == '#') {operations.push(c); i += k;}
              else {char d = operations.top();
                    while (d != '(' && d != '#'  && !high_prec(c,d)) {
                           ret += d; operations.pop(); d = operations.top();}
                    operations.push(c); i += k;
            }
        }
    }
    char d = operations.top();
    while(d != '#') {
        ret += d;
        operations.pop();
        d = operations.top();
    }
    return ret;
}

bool eval_str(string &RPN)
{
    bool tmp;
    int i = 0;
    int s = RPN.size();
    while(i < s) {
        char c = RPN[i];
        if(isalnum(c)) factors.push(dict[c]);
        else switch (c) {
                case '!':   factors.top() = !factors.top(); break;
                case '&':   tmp = factors.top(); factors.pop();
                            factors.top() = factors.top() && tmp; break;
                case '|':   tmp = factors.top(); factors.pop();
                            factors.top() = factors.top() || tmp; break;
            }
        i++;
    }

    return factors.top();
}



int main()
{

    ifstream fin ("bool.in");
    ofstream fout("bool.out");

    string line, change;
    int N;
    getline(fin, line);
    fin >> N;
    getline(fin, change);
    getline(fin, change);

    dict['0'] = 0;
    dict['1'] = 1;

    for(int i = 0; i < 26; i++)
        dict[i+65] = false;

    string RPN = process_line(line);

    int i = 0;
    int s = change.size();
    while(i < s) {
        char c = change[i];
        dict[c] = !dict[c];
        fout << eval_str(RPN);
        i++;
    }
    fout << endl;

    return 0;
}