Cod sursa(job #1354872)

Utilizator alexandru.ghergutAlexandru-Gabriel Ghergut alexandru.ghergut Data 22 februarie 2015 09:38:05
Problema Convertor Scor 20
Compilator cpp Status done
Runda rosedu_cdl_2015 Marime 2.33 kb
#include <fstream>
#include <string>
using namespace std;

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

    string::size_type iIndex, cIndex, left, right;
    int objectCount = 0;
    bool key = false;
    while (getline(f, line))
    {
        iIndex = 0;
        do
        {
            if (!key)
            {
                cIndex = line.find_first_of("}\"");
                if (cIndex != string::npos)
                {
                    if (line[cIndex] == '}')
                    {
                        objectCount++;
                        left = cIndex;
                        right = left + 1;
                        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, line.size() - cIndex + 1);
                }
            }
            else
            {
                cIndex = line.find_first_of("0123456789\"");
                if (cIndex != string::npos)
                {
                    if (line[cIndex] >= '0' && line[cIndex] <= '9')
                    {
                        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, line.size() - cIndex + 1);
                }
            }
        } while (cIndex != string::npos);

    }
    f.close();

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

    return 0;
}