Cod sursa(job #2791276)

Utilizator Ana_22Ana Petcu Ana_22 Data 30 octombrie 2021 12:15:08
Problema Bool Scor 80
Compilator c-64 Status done
Runda Arhiva de probleme Marime 2.5 kb
#include <stdio.h>
#include <stdlib.h>
#define LMAX 100000

unsigned char ch[LMAX];
int val[128];
int indexx;

int sau();

int factor() {
  int nr, g;
  if( ch[indexx] == '(' ) {
    indexx++;//trec peste paranteza deschisa
    nr = sau();//incep o noua expresie
    indexx++;//trec peste paranteza inchisa
  }
  else {
    g = 0;
    if( ch[indexx] == '!' ) {
      g = 1;
      indexx++;
    }
    nr = val[ch[indexx]];
    if( g == 1 )
      nr = 1 - nr;
    indexx++;
  }
  return nr;
}

int si() {
  int rez;
  rez = factor();
  while( ch[indexx] == '&' ) {
    indexx++;
    rez &= factor();
  }
  return rez;
}

int sau() {
  int rez;
  rez = si();
  while( ch[indexx] == '|' ) {
    indexx++;
    rez |= si();
  }
  return rez;
}

int main() {
    FILE *fin, *fout;
    int i, j, n;
    unsigned char o;
    fin = fopen( "bool.in", "r" );
    fout = fopen( "bool.out", "w" );
    indexx = 0;
    ch[indexx] = fgetc( fin );
    while( ch[indexx] != '\n' && ch[indexx] != EOF )
      ch[++indexx] = fgetc( fin );
    i = 0;
    while( i < indexx ) {
      if( ch[i] == ' ' ) {
        for( j = i + 1; j <= indexx; j++ )
          ch[j-1] = ch[j];
        indexx--;
        i--;
      }
      else if( ch[i] == 'A' && ch[i+1] == 'N' && ch[i+2] == 'D' ) {
        ch[i] = '&';
        for( j = i + 3; j <= indexx; j++ )
          ch[j-2] = ch[j];
        indexx -= 2;
      }
      else if( ch[i] == 'N' && ch[i+1] == 'O' && ch[i+2] == 'T' ) {
        ch[i] = '!';
        for( j = i + 3; j <= indexx; j++ )
          ch[j-2] = ch[j];
        indexx -= 2;
      }
      else if( ch[i] == 'O' && ch[i+1] == 'R' ) {
        ch[i] = '|';
        for( j = i + 2; j <= indexx; j++ )
          ch[j-1] = ch[j];
        indexx--;
      }
      else if( ch[i] == 'T' && ch[i+1] == 'R' ) {
        ch[i] = 'a';//val[a] va fi mereu 1, val[b], 0
        for( j = i + 4; j <= indexx; j++ )
          ch[j-3] = ch[j];
        indexx -= 3;
      }
      else if( ch[i] == 'F' && ch[i+1] == 'A' && ch[i+2] == 'L' ) {
        ch[i] = 'b';//val[b] va fi mereu 0
        for( j = i + 5; j <= indexx; j++ )
          ch[j-4] = ch[j];
        indexx -= 4;
      }
      i++;
    }
    val['a'] = 1;
    val['b'] = 0;
    fscanf( fin, "%d", &n );
    fgetc( fin );
    for( i = 0; i < n; i++ ) {
      o = fgetc( fin );
      val[o] = 1 - val[o];
      indexx = 0;
      fprintf( fout, "%d", sau() );
    }
    fclose( fin );
    fclose( fout );
    return 0;
}