Cod sursa(job #1355333)

Utilizator alexandru.ghergutAlexandru-Gabriel Ghergut alexandru.ghergut Data 22 februarie 2015 16:47:22
Problema Convertor Scor 90
Compilator cpp Status done
Runda rosedu_cdl_2015 Marime 2.26 kb
#include <fstream>
#include <string>
using namespace std;

int main()
{
    string line;
    string keyArray, valueArray;
    ifstream f("convertor.in");

    string::size_type cIndex, left, right;
    int objectCount = 0;
    bool key = false;
    while (getline(f, line))
    {
        do
        {
            if (!key)
            {
                cIndex = line.find_first_of("}\"");
                if (cIndex != string::npos)
                {
                    if (line[cIndex] == '}')
                    {
                        objectCount++;
                        left = cIndex;
                        right = left;
                        valueArray += '\n';
                    }
                    else
                    {
                        left = cIndex;
                        right = line.find("\"", cIndex + 1);
                        if (!objectCount)
                            keyArray += line.substr(left + 1, right - left - 1) + ",";
                        key = true;
                    }
                    cIndex = right + 1;
                    line = line.substr(cIndex);
                }
            }
            else
            {
                cIndex = line.find_first_of("-123456789\"");
                if (cIndex != string::npos)
                {
                    if ((line[cIndex] >= '1' && line[cIndex] <= '9') || (line[cIndex] == '-'))
                    {
                        left = cIndex;
                        right = line.find_first_not_of("0123456789", left + 1);
                        valueArray += line.substr(left, right - left) + ",";
                        cIndex = right + 1;
                        key = false;
                    }
                    else
                    {
                        left = cIndex;
                        right = line.find("\"", cIndex + 1);
                        valueArray += line.substr(left + 1, right - left - 1) + ",";
                        key = false;
                        cIndex = right + 1;
                    }
                    line = line.substr(cIndex);
                }
            }
        } while (cIndex != string::npos);

    }
    f.close();

    ofstream g("convertor.out");
    g << keyArray << "\n" << valueArray;
    g.close();

    return 0;
}