Cod sursa(job #130258)

Utilizator PavelRazvanPavel Razvan PavelRazvan Data 31 ianuarie 2008 18:12:54
Problema Bool Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 3.03 kb
    #include <stdio.h>  
    #include <string.h>  
    #define lmax 1011  
    #define nmax 101;  
    int poz, n, st[lmax], k, bal[40], sol;  
     
    //1..29 Literele  
    //30 TRUE 31 FALSE  
    //  -1 NOT -2 AND -3 OR -5 ( -4 )  
   int expresie ();  
     
   int  
   factor ()  
   {  
     int aux;  
     if (st[poz] > 0)  
       aux = bal[st[poz]],poz++;  
     else if (st[poz] == (-1))  
       poz++,aux = !(factor ());  
     else if (st[poz] == (-5))  
      poz++,aux = expresie (),poz++;  
     return (aux);  
   }  
     
   int  
   termen ()  
   {  
     int aux = factor ();  
     while (st[poz] == (-2))  
       {  
         poz++;  
         aux &= factor ();  
       }  
     return (aux);  
   }  
     
   int  
   expresie ()  
   {  
     int aux = termen ();  
    while (st[poz] == (-3))  
       {  
         poz++;  
         aux |= termen ();  
       }  
     return (aux);  
   }  
     
   int  
  main ()  
   {  
     FILE *fi = fopen ("bool.in", "r"), *fo = fopen ("bool.out", "w");  
     char s[lmax], c;  
     int i;  
     fgets (s, 1001, fi);  
     for (poz = 0, k = 0; poz < strlen (s) - 1; poz++)  
     
       {  
         switch (s[poz])  
     
       {  
       case 'F':  
        if (s[poz + 1] == 'A')  
     
           {  
             st[++k] = 31;  
             poz += 4;  
           }  
    
         else  
           st[++k] = s[poz] - 'A' + 1;  
         break;  
       case 'N':  
         if (s[poz + 1] == 'O')  
     
           {  
             st[++k] = (-1);  
             poz += 2;  
           }  
     
         else  
           st[++k] = s[poz] - 'A' + 1;  
         break;  
       case 'A':  
        if (s[poz + 1] == 'N')  
   
          {  
            st[++k] = (-2);  
            poz += 2;  
           }  
     
         else  
           st[++k] = s[poz] - 'A' + 1;  
         break;  
       case 'O':  
         if (s[poz + 1] == 'R')  
     
           {  
             st[++k] = (-3);  
            poz++;  
          }  
    
       else  
         st[++k] = s[poz] - 'A' + 1;  
        break;  
     case 'T':  
       if (s[poz + 1] == 'R')  
   
         {  
           st[++k] = 30;  
           poz += 3;  
         }  
   
        else  
       st[++k] = s[poz] - 'A' + 1;  
        break;  
      case '(':  
      st[++k] = (-5);  
        break;  
      case ')':  
       st[++k] = (-4);  
        break;  
     default:  
       if (s[poz] != ' ')  
          st[++k] = s[poz] - 'A' + 1;  
      }  
      }  
    for (i = 1; i <= 31; i++)  
      bal[i] = 0;  
   bal[30] = 1;  
    fscanf (fi, "%d\n", &n);  
    for (i = 1; i <= n; i++)  
    
      {  
      fscanf (fi, "%c", &c);  
        bal[c - 'A' + 1] ^= 1;  
        poz = 1;  
        sol = expresie ();  
        if (sol)  
      sol = 1;  
        fprintf (fo, "%d", sol);  
      }  
    fprintf (fo, "\n");  
    fclose (fi);  
    fclose (fo);  
    return (0);  
  }