Cod sursa(job #1342739)

Utilizator constantinsegarceanuConstantin Segarceanu constantinsegarceanu Data 14 februarie 2015 14:34:58
Problema Convertor Scor 100
Compilator cpp Status done
Runda rosedu_cdl_2015 Marime 2.05 kb
#include <fstream>
#include <string>

using namespace std;

ifstream f("convertor.in");
ofstream g("convertor.out");
//ofstream h("log.txt");

int state = 1;

void property_automata(char c)
{
    switch(state)
    {
    case 1:
        if(c == '"'){
            state = 2;
        }
        break;
    case 2:
        if(c == '"')
            state = 3;
        else
            g << c;
        break;
    case 3:
        if(c == ','){
            state = 1;
            g << ',';
        }
        else if(c == '}'){
            state = 4;
            g << ',';
        }
        break;
    }
}

void value_automata(char c)
{
    switch(state)
    {
    case 1:
        if(c == ':')
            state = 2;
        else if(c == ']')
            state = 6;
        break;
    case 2:
        if(c == '"')
            state = 3;
        else if(c > 47 && c < 58){
            g << c;
            state = 5;
        }
        break;
    case 3:
        if(c == '"')
            state = 4;
        else
            g << c;
        break;
    case 4:
        if(c == ','){
            state = 1;
            g << ',';
        }
        else if(c == '}'){
            state = 1;
            g << ',';
            g << '\n';
        }
        break;
    case 5:
        if(c > 47 && c < 58)
        {
            g << c;
        }
        else
            if(c == ','){
                state = 1;
                g << ',';
            }
            else if(c == '}'){
                state = 1;
                g << ',';
                g << '\n';
            }
        break;
    }
}

int main()
{
    string n;
    while(getline(f, n) && state != 4)
    {
        for(int i = 0; i < n.length(); i++)
            property_automata(n[i]);
    }
    g << "\n";
    f.clear();
    f.seekg(0,ios::beg);
    state = 1;
    while(getline(f, n) && state != 6)
    {
        for(int i = 0; i < n.length(); i++)
            value_automata(n[i]);
    }
    f.close();
    g.close();
    return 0;
}