Cod sursa(job #1344324)

Utilizator MIonutMistreanu Ionut MIonut Data 16 februarie 2015 17:10:48
Problema Convertor Scor 90
Compilator cpp Status done
Runda rosedu_cdl_2015 Marime 2.16 kb
#include<stdio.h>
#include<fstream>
#include <string>
using namespace std;

FILE *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()
{
    int i;
    std::string line;
    std::ifstream f;
    f.open("convertor.in");
    g = fopen("convertor.out", "wt");

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

    f.seekg (0, f.beg);
    fprintf(g, "\n");

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

    fclose(g);
    f.close();

    return 0;
}