Cod sursa(job #1343281)

Utilizator constantinsegarceanuConstantin Segarceanu constantinsegarceanu Data 15 februarie 2015 09:56:49
Problema Convertor Scor 100
Compilator cpp Status done
Runda rosedu_cdl_2015 Marime 2.26 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 contents, n;
    f.seekg(0, std::ios::end);
    contents.resize(f.tellg());
    f.seekg(0, std::ios::beg);
    f.read(&contents[0], contents.size());
    //while(getline(f, n) && state != 4)
    {
        for(int i = 0; i < contents.length() && state != 4; i++)
            property_automata(contents[i]);
    }
    g << "\n";
    //f.clear();
    //f.seekg(0,ios::beg);
    state = 1;
    //while(getline(f, n) && state != 6)
    {
        for(int i = 0; i < contents.length() && state != 6; i++)
            value_automata(contents[i]);
    }
    f.close();
    g.close();
    return 0;
}