Cod sursa(job #1348481)

Utilizator gabriel93Robu Gabriel gabriel93 Data 19 februarie 2015 18:44:07
Problema Convertor Scor 10
Compilator cpp Status done
Runda rosedu_cdl_2015 Marime 1.68 kb
#include <iostream>
#include <fstream>
#include <string>
#include <vector>

using namespace std;

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

    vector<string> Values;
    bool KeysFlag = true;
    int nKeys = 0;

    do {
        string s;
        getline(fin,s,'}');
        if( s.find_last_of(']') != string::npos )
            break;

        bool flag = true;
        string value;

        for(size_t i=0;i<s.size();++i) {
            while (s[i] != '"')
                ++i;

            ++i;
            while(s[i] != '"') {
                value.push_back(s[i]);
                ++i;
            }

            if(KeysFlag) {
                fout<< value << ',';
                ++nKeys;
            }
            value.clear();

            while( s[i] != ':')
                ++i;
            ++i;
            while( s[i] == ' ' || s[i] == '\n')
                ++i;

            if(s[i] == '"') {
                ++i;
                while(s[i] != '"') {
                    value.push_back(s[i]);
                    ++i;
                }
                ++i;
            }
            else
                while(s[i] != ' ' && s[i] != '\n' && s[i] != ',') {
                    value.push_back(s[i]);
                    ++i;
                }
            Values.push_back(value);
            value.clear();

        }

        KeysFlag = false;
    }while(!fin.eof());

    fout << '\n';

    for(size_t i=0;i<Values.size();++i) {
        fout << Values[i] << ',';
        if(i%nKeys == nKeys-1)
            fout << '\n';
    }

    return 0;
}