Cod sursa(job #1356026)

Utilizator RazzinnatorRazvan Brinzea Razzinnator Data 23 februarie 2015 09:30:09
Problema Convertor Scor 70
Compilator cpp Status done
Runda rosedu_cdl_2015 Marime 3.27 kb
#include <iostream>
#include <string>
#include <fstream>
#include <vector>

using namespace std;
ifstream f("convertor.in");
ofstream g("convertor.out");

int main()
{
    bool started_string = false;
    bool expecting_field = true;
    bool started_value = false;
    int value_count = 0;
    int current_values = 0;
    char c;
    string fields, values;

    f >> c;
    while( c != '}' )
    {
        if( expecting_field == true )
        {
            if( c == '"' )
            {
                if( started_string == false )
                {
                    started_string = true;
                }
                else
                {
                    started_string = false;
                    expecting_field = false;
                    fields.push_back( ',' );
                }
            }
            else if ( started_string == true )
            {
                fields.push_back( c );
            }
        }
        else
        {
            if( started_value == false && c == ':' )
            {
                started_value = true;
                f >> noskipws >> c;
                while( c == ' ' )
                {
                    f >> noskipws >> c;
                }
                if( c == '"' )
                {
                    f >> noskipws >> c;
                    while( c != '"' )
                    {
                        values.push_back( c );
                        f >> noskipws >> c;
                    }
                    started_value = false;
                    expecting_field = true;
                    values.push_back( ',' );
                    value_count++;

                }
                else
                {
                    while( c >= '0' && c <= '9' )
                    {
                        values.push_back( c );
                        f >> noskipws >> c;
                    }
                    started_value = false;
                    expecting_field = true;
                    values.push_back( ',' );
                    value_count++;

                }
            }
        }

        f >> noskipws >> c;
    }

    g << fields << endl << values << endl;

    started_value = false;
    while( !f.eof() )
    {
       while( c != ':' )
       {
           f >> noskipws >> c;
           if( f.eof() )
           {
               return 0;
           }
       }
       f >> noskipws >> c;
       while( c == ' ' )
       {
            f >> noskipws >> c;
       }
       if( c == '"' )
       {
           f >> noskipws >> c;
           while( c != '"' )
           {
               g << c;
               f >> noskipws >> c;
           }
           current_values++;
           g << ',';
           if( current_values == value_count )
           {
               g << endl;
               current_values = 0;
           }
       }
       else
       {
           while( c >= '0' && c <= '9' )
           {
               g << c;
               f >> noskipws >> c;
           }
           current_values++;
           g << ',';
           if( current_values == value_count )
           {
               g << endl;
               current_values = 0;
           }
       }
    }

    return 0;
}