Cod sursa(job #1344337)

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

std::ofstream g;
int stare = 1;

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

    case 2:
        if (c == '"')
        {
            stare = 3;
            g<<',';
        }
        else
            g<<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')
        {
            g<<c;
            stare = 5;
        }
        break;

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

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


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

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

}

int main()
{
    int i;
    std::string line;
    std::ifstream f;

    f.open("convertor.in");
    g.open("convertor.out");

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

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

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

    f.close();
    g.close();

    return 0;
}