Cod sursa(job #2791693)

Utilizator Ana_22Ana Petcu Ana_22 Data 30 octombrie 2021 22:32:21
Problema Bool Scor 100
Compilator c-64 Status done
Runda Arhiva de probleme Marime 2.55 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;
  if( ch[indexx] == '(' ) {
    indexx++;//trec peste paranteza deschisa
    nr = sau();//incep o noua expresie
    indexx++;//trec peste paranteza inchisa
  }
  else {
    nr = val[ch[indexx]];
    indexx++;
  }
  return nr;
}

int opus() {
  int rez, g;
  g = 0;
  while( ch[indexx] == '!' ) {
    indexx++;
    g = 1 - g;
  }
  rez = factor();
  if( g == 1 )
    rez = 1 - rez;
  return rez;
}

int si() {
  int rez;
  rez = opus();
  while( ch[indexx] == '&' ) {
    indexx++;
    rez &= opus();
  }
  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;
}