Cod sursa(job #1348821)

Utilizator vlad.florescu94FMI Florescu Vlad - Adrian vlad.florescu94 Data 19 februarie 2015 21:13:47
Problema Convertor Scor 100
Compilator cpp Status done
Runda rosedu_cdl_2015 Marime 2.77 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;
    ofstream &g = *out;
    vector<string> firstValues;

    f>>noskipws;    //not to skip whitespaces

    char c;
    f>>c;

    while(true){

        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!='"'){
            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{

            string firstElementString;

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

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

        while(c!=',' && c!='}'){
            f>>c;
        }
        if(c=='}'){
            g<<'\n';
            break;  //finished first object
        }
    }

    for(int i=0; i<firstValues.size(); ++i){ //printing first values

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

    while(true){

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

        while(true){

            f>>c;
            while(c!=':'){
                f>>c;
            }
            f>>c;
            while((c<'0' || c>'9') && c!='"'){
                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;
}