Cod sursa(job #1344252)

Utilizator MIonutMistreanu Ionut MIonut Data 16 februarie 2015 15:54:28
Problema Convertor Scor 90
Compilator c Status done
Runda rosedu_cdl_2015 Marime 2.09 kb
#include<stdio.h>

FILE *f, *g;
int stare = 1;

void automat_cheie(char c)
{
    switch(stare)
    {
    case 1:
        if (c == '"') stare = 2;
        break;

    case 2:
        if (c == '"')
        {
            stare = 3;
            fprintf(g, ",");
        }
        else
            fprintf(g, "%c", c);
        break;

    case 3:
        if (c == ',') stare = 4;
        else if(c == '}') stare = 5;
        break;

    case 4:
        if (c == '"') stare = 2;
        break;
    }
}

void automat_afis(char c)
{
    switch(stare)
    {
    case 1:
        if (c == '"') stare = 2;
        break;

    case 2:
        if (c == '"')  stare = 3;
        break;


    case 3:
        if (c == '"') stare = 4;
        else if(c >='0' && c<='9')
        {
            fprintf(g, "%c", c);
            stare = 5;
        }
        break;

    case 4:
        if (c == '"')
        {
            fprintf(g, ",");
            stare = 6;
        }
        else fprintf(g, "%c", c);
        break;

    case 5:
        if (c == '"')
        {
            fprintf(g, ",");
            stare = 2;
        }
        else if ( c == '}')
        {
            fprintf(g, ",");
            stare = 7;
        }
        else if(c >='0' && c<='9') fprintf(g, "%c", c);
        break;


    case 6:
        if (c == '"') stare = 2;
        else if ( c == '}') stare = 7;
        break;

    case 7:
        if (c == '{')
        {
            fprintf(g, "\n");
            stare = 1;
        }
        break;
    }

}

int main()
{
    char v[1026];
    int i;

    f = fopen("convertor.in", "rt");
    g = fopen("convertor.out", "wt");

    while(stare != 5 && fgets(v, 1024, f))
        for(i=0; v[i]!='\0' && stare != 5; ++i)
            automat_cheie(v[i]);

    fseek( f , 0, SEEK_SET);
    fprintf(g, "\n");

    if(stare == 5)
    {
        stare = 1;
        while(fgets(v, 1024, f))
            for(i=0; v[i]!='\0'; ++i)
                automat_afis(v[i]);
    }

    fclose(g);
    fclose(f);

    return 0;
}