Cod sursa(job #60577)

Utilizator DastasIonescu Vlad Dastas Data 15 mai 2007 14:50:59
Problema Bool Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.08 kb
#include <cstdio>
#include <cstring>

#define maxL 1000
#define maxN 100
#define alfa 26

FILE *in = fopen("bool.in","r"), *out = fopen("bool.out","w");

char e[maxL];
int alf[alfa] = {0};
char var[maxN];
int n;
char RPN[maxL];
int state = 0; // 0 daca 1 == 1 si 0 == 0, 1 altcumva

int l;

void read()
{
    fgets(e, maxL, in);

    fscanf(in, "%d\n", &n);
    fgets(var, maxN, in);

    l = strlen(e) - 1;
    e[l] = '\0';
}

void norm()
{
    char t[maxL];
    int k = 0;

    for ( int i = 0; i < l; ++i )
        if ( e[i] == 'A' && e[i+1] == 'N' )
            t[k++] = '&', i += 2;
        else if ( e[i] == 'O' && e[i+1] == 'R' )
            t[k++] = '|', i += 2;
        else if ( e[i] == 'N' && e[i+1] == 'O' )
            t[k++] = '~', i += 2;
        else if ( e[i] == 'T' && e[i+1] == 'R' )
            t[k++] = '1', i += 3;
        else if ( e[i] == 'F' && e[i+1] == 'A' )
            t[k++] = '0', i += 4;
        else if ( e[i] != ' ' )
            t[k++] = e[i];

    t[k] = '\0';
    strcpy(e, t);
    l = strlen(e);
}

int isalpha(char c)
{
    return (c <= 'Z' && c >= 'A') || c == '1' || c == '0';
}

void conv()
{
    char st[maxL];
    int r = 0;
    int k = 1;
    st[0] = 'd';

    for ( int i = 0; i < l; i++ )
    {
        if ( isalpha(e[i]) )
            RPN[r++] = e[i];
        else if ( e[i] == '&' || e[i] == '|' )
        {
            while ( (st[k-1] == '&' || st[k-1] == '|' || st[k-1] == '~') && k > 0 )
                RPN[r++] = st[--k];
            st[k++] = e[i];
        }
        else if ( e[i] == '~' )
        {
            while ( st[k-1] == '~' && k > 0 )
                RPN[r++] = st[--k];
            st[k++] = e[i];
        }
        else if ( e[i] == '(' )
            st[k++] = '(';
        else
        {
            while ( st[k-1] != '(' )
                RPN[r++] = st[--k];
            --k;
        }
    }
    while ( k > 1 )
        RPN[r++] = st[--k];
//
//    printf("%s\n%s\n%d\n", RPN, st, k);
}

int main()
{
    read();
    norm();
   // conv();

	return 0;
}