Cod sursa(job #1749224)

Utilizator delia_ioanaCeapa Delia Ioana delia_ioana Data 28 august 2016 08:49:06
Problema Bool Scor 30
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.88 kb
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
int modif, p = 0;
string S, M;
vector<bool> vals(26, false);
bool and_eval();
bool not_eval();
bool factor();
ifstream infile;
ofstream outfile;

string termen() {
    string sol;
    if (p == S.length())
        return sol;
    int save = p;
    if (S[p] == '(') {
        sol += S[p];
        return sol;
    }
    while (p < S.length() && S[p] >= 'A' && S[p] <= 'Z') {
        sol += S[p];
        ++ p;
    }

    p = save;
    return sol;
}

bool or_eval() {
    bool r = and_eval();
    while (termen() == "OR") {
        p += 3;
        r |= and_eval();
    }

    return r;
}

bool and_eval() {
    bool r = not_eval();
    while (termen() == "AND") {
        p += 4;
        r &= not_eval();
    }

    return r;
}

bool not_eval() {
    bool r, check = false;
    while (termen() == "NOT") {
        p += 4;
        check = true;
        r = !factor();
    }

    if (check == false)
        r = factor();

    return r;
}

bool factor() {
    bool r;
    if (termen() == "(") {
        p ++;
        r = or_eval();
        p ++;
    } else if (termen() == "TRUE") {
        r = true;
        p += 4;
    } else if (termen() == "FALSE") {
        r = false;
        p += 5;
    } else {
        int val = (int)S[p] - 65;
        r = vals[val];
        p ++;
    }

    while (p < S.length() && (S[p] == ' ' || S[p] == ')'))
        ++ p;

    return r;
}

int main() {
    infile.open("bool.in");
	outfile.open("bool.out");
    getline(infile, S);
    cout << S << endl;
    infile >> modif;
    cout << modif << endl;
    char ch;
    for (int i = 0; i < modif; i ++) {
        infile >> ch;
        M.push_back(ch);
    }
    cout << M << endl;

    for (int i = 0; i < modif; i ++) {
        p = 0;
        int val = (int)M[i] - 65;
        vals[val] = !vals[val];
        outfile << or_eval();
    }
    return 0;
}