Cod sursa(job #1359514)

Utilizator alexandru.ghergutAlexandru-Gabriel Ghergut alexandru.ghergut Data 24 februarie 2015 23:09:08
Problema Convertor Scor 100
Compilator cpp Status done
Runda rosedu_cdl_2015 Marime 2.27 kb
#include <fstream>
#include <string>
using namespace std;

int main()
{
    string line;
    string keyArray, valueArray;
    ifstream f("convertor.in");
    ofstream g("convertor.out");
    string::size_type index, left, right;
    bool firstObject = true, key = false;
    while (getline(f, line))
    {
        do
        {
            if (!key)
            {
                index = line.find_first_of("}\"");
                if (index != string::npos)
                {
                    if (line[index] == '}')
                    {
                        if (firstObject)
                        {
                            g << keyArray << '\n';
                            firstObject = false;
                        }
                        left = right = index;
                        g << valueArray << '\n';
                        valueArray = "";
                    }
                    else
                    {
                        left = index;
                        right = line.find("\"", left + 1);
                        if (firstObject)
                            keyArray += line.substr(left + 1, right - left - 1) + ",";
                        key = true;
                    }
                    index = right + 1;
                }
            }
            else
            {
                index = line.find_first_of("0123456789\"");
                if (index != string::npos)
                {
                    if (line[index] >= '0' && line[index] <= '9')
                    {
                        left = index;
                        right = line.find_first_not_of("0123456789", left + 1);
                        valueArray += line.substr(left, right - left) + ",";
                        index = right;
                    }
                    else
                    {
                        left = index;
                        right = line.find("\"", index + 1);
                        valueArray += line.substr(left + 1, right - left - 1) + ",";
                        index = right + 1;
                    }
                    key = false;
                }
            }
            if (index != string::npos)
                line = line.substr(index);
        } while (index != string::npos);

    }
    f.close();
    g.close();

    return 0;
}