Cod sursa(job #2792214)

Utilizator andreic06Andrei Calota andreic06 Data 1 noiembrie 2021 09:56:26
Problema Bool Scor 20
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.34 kb
#include <iostream>
#include <fstream>
#include <string>
#include <ctype.h>

using namespace std;
const int LEN_MAX = 1e3;
const int NMAX = 1e2;
const int SIGMA = 26;
const bool TRUE = 1;
const bool FALSE = 0;

char input_exp[LEN_MAX];
bool value[SIGMA]; int n;

bool solve_not ( char* &str );
bool solve_term ( char* &str );
bool solve_exp ( char* &str );
bool solve_exp_final ( char* &str );

bool get_alpha ( char* &str ) {

   bool result;
   if ( *str == 'F' && *(str + 1) == 'A' && *(str + 2) == 'L' ) {
     result = 0;
     str += 5;
   }
   else if ( *str == 'T' && *(str + 1) == 'R' ){
     result = 1;
     str += 4;
   }
   else {
     result = value[*str - 'A'];
     str ++;
   }
   return result;
}


bool solve_term ( char* &str ) {
    bool result;
    if ( *str == '(' ) {
      str ++;
      result = solve_exp ( str );
      str ++;
    }
    else {
      result = get_alpha ( str );
      while ( *str == 'O' && *(str + 1) == 'R' ) {
        str += 2;
        result |= solve_exp_final ( str );
      }
    }

    ///cout << "TERM = " << result << "\n";
    return result;
}

bool solve_exp ( char* &str ) {
    bool result;
    if ( *str == 'N' && *(str + 1) == 'O' && *(str + 2) == 'T' )
      result = solve_exp_final ( str );
    else {
      result = solve_term ( str );
      while ( *str == 'A' && *(str + 1) == 'N' && *(str + 2) == 'D' ) {
         str += 3;
         result &= solve_exp_final ( str );
      }
    }

    ///cout << "EXP = " << result << "\n";
    return result;
}

bool solve_exp_final ( char* &str ) {
    bool sign = true;
    while ( *str == 'N' && *(str + 1) == 'O' && *(str + 2) == 'T' ) {
       sign = 1 - sign;
       str += 3;
    }
    if ( sign == true )
      return solve_exp ( str );
    return 1 - solve_exp ( str );
}

void erase_space () {

}
ifstream fin ( "bool.in" );
ofstream fout ( "bool.out" );
int main()
{
   int size_of = 0;
   char ch; ch = fin.get ();
   while ( ch != '\n' && ch != EOF ) {
      if ( !isspace ( ch ) )
        input_exp[size_of ++] = ch;
      ch = fin.get ();
   }

   fin >> n;
   for ( int i = 1; i <= n; i ++ ) {
      char letter; fin >> letter;
      value[letter - 'A'] = 1 - value[letter - 'A'];

      char *p = input_exp;
      fout << solve_exp_final ( p );
   }
    return 0;
}