Cod sursa(job #1348883)

Utilizator vlad.florescu94FMI Florescu Vlad - Adrian vlad.florescu94 Data 19 februarie 2015 21:34:52
Problema Convertor Scor 100
Compilator cpp Status done
Runda rosedu_cdl_2015 Marime 3.36 kb
#include<fstream>
#include<iostream>
#include<cstring>
#include<vector>

using namespace std;

class Convertor{

    ifstream *in;
    ofstream *out;

public:

    Convertor(ifstream &in, ofstream &out);
    void solve();
};

Convertor::Convertor(ifstream &in, ofstream &out){

    this->in = &in; this->out = &out;
}

void Convertor::solve(){

    ifstream &f = *in;      //I used new references to avoid using writing (*in)>>something
    ofstream &g = *out;
    vector<string> firstValues;

    f>>noskipws;    //not to skip whitespaces

    char c;
    f>>c;

    while(true){    //solving for the first object

        while(c!='"'){
            f>>c;
        }

        f>>c;  //we're jumping over the '"'

        while(c!='"'){

            g<<c;   //printing the mutual keys
            f>>c;
        }
        g<<',';

        f>>c;
        while((c<'0' || c>'9') && c!='"'){  //going to the next string or number
            f>>c;
        }

        if(c=='"'){    //if we have a string

            f>>c;
            string firstElementString;

            while(c!='"'){

                firstElementString.append(1, c);
                f>>c;
            }
            firstValues.push_back(firstElementString);
        }
        else{           //if we have a number

            string firstElementString;

            while(c>='0' && c<='9'){

                firstElementString.append(1, c);
                f>>c;
            }
            firstValues.push_back(firstElementString);
        }

        while(c!=',' && c!='}'){
            f>>c;
        }
        if(c=='}'){     //then we finished the first object
            g<<'\n';
            break;
        }
    }
    ///<>------------------------------------------------------------------------------------------------
    for(int i=0; i<firstValues.size(); ++i){ //printing first values

        g<<firstValues[i]<<',';
    }
    g<<'\n';
    ///<>------------------------------------------------------------------------------------------------

    while(true){        //solving for the rest of the file

        f>>c;
        while (c!= '{' && c != ']'){
            f>>c;
        }
        if(c==']'){     //if we meet the ']' character then the Jason file is over
            break;
        }

        while(true){

            f>>c;
            while(c!=':'){      //going to the first value, step 1: to ":"
                f>>c;
            }
            f>>c;
            while((c<'0' || c>'9') && c!='"'){  //step 2: to the number or string which is after the ":"
                f>>c;
            }

            if(c=='"'){

                f>>c;
                while(c!='"'){
                    g<<c;
                    f>>c;
                }
                g<<',';
            }
            else{

                while(c>='0'&&c<='9'){
                    g<<c;
                    f>>c;
                }
                g<<',';
            }

            while(c!=',' && c!='}'){
                f>>c;
            }
            if(c=='}'){

                g<<'\n';
                break;
            }
        }
    }

}

int main(){

    ifstream f("convertor.in");
    ofstream g("convertor.out");
    Convertor *newConvertor = new Convertor(f,g);
    newConvertor->solve();

    return 0;
}