Cod sursa(job #1349107)

Utilizator gabriel93Robu Gabriel gabriel93 Data 19 februarie 2015 23:36:51
Problema Convertor Scor 100
Compilator cpp Status done
Runda rosedu_cdl_2015 Marime 1.28 kb
#include <fstream>
#include <string>
using namespace std;


void getKeys(const string s, fstream &fout) {
    size_t i = 0;

    do {
        i = s.find('"',i);
        string key = s.substr(i+1,s.find('"',i+1)-i-1);
        if ( !key.empty() )
            fout << key << ',';
        i = s.find(',',i);
    }while(i != string::npos);

    fout << '\n';
}

void getValues(const string s, fstream &fout){
    size_t i = s.find(':',0);

	while( i != string::npos) {
        string key;

        i = s.find_first_not_of(" \n",i+1);
        if(s[i] == '"')
            key = s.substr(i+1,s.find('"',i+1)-i-1);
        else
            key = s.substr(i,s.find_first_of(",\n ",i+1)-i);
        if( !key.empty() )
            fout << key << ',';
        i = s.find(':',i);
	}

	fout << '\n';
}


int main(){
	fstream fin,fout;
    fin.open("convertor.in",fstream::in);
    fout.open("convertor.out",fstream::out);

	string s;
	bool flagKeys = true;
	do{
		getline(fin,s,'}');
        if( s.empty() || s.find_last_of(']') != string::npos )
            continue;

        if(flagKeys) {
            getKeys(s,fout);
            flagKeys = false;
        }

        getValues(s,fout);

    }while(!fin.eof());

    fin.close();
	fout.close();
	return 0;
}