Cod sursa(job #1354793)

Utilizator alexandru.ghergutAlexandru-Gabriel Ghergut alexandru.ghergut Data 22 februarie 2015 00:18:04
Problema Convertor Scor 60
Compilator cpp Status done
Runda rosedu_cdl_2015 Marime 2.03 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;
    while (getline(f, line))
    {
        iIndex = 0;
        cIndex = line.find_first_of(":}");
        while (cIndex != string::npos)
        {
            if (line[cIndex] == ':')
            {
                if (!objectCount)
                {
                    currentSubString = line.substr(iIndex, cIndex - iIndex + 1);
                    left = currentSubString.find("\"");
                    right = currentSubString.rfind("\"");
                    keyArray += line.substr(left + 1, right - left - 1) + ",";
                }
                left = line.find_first_of("0123456789\"", cIndex + 1);
                while (left == string::npos)
                {
                    getline(f, line);
                    left = line.find_first_of("0123456789\"");
                }
                if (line[left] >= '0' && line[left] <= '9')
                {
                    right = line.find(",", left + 1);
                    valueArray += line.substr(left, right - left) + ",";
                }
                else
                {
                    right = line.find("\"", left + 1);
                    valueArray += line.substr(left + 1, right - left - 1) + ",";
                }
                cIndex = right;
            }
            else if (line[cIndex] == '}' && line[cIndex + 1] == ',')
            {
                if (!objectCount)
                    keyArray += '\n';
                valueArray += '\n';
                objectCount++;
            }
            if (cIndex != string::npos)
                line = line.substr(cIndex + 1, line.size() - cIndex + 1);
            else
                line = "";
            cIndex = line.find_first_of(":}");
        }
    }
    f.close();

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

    return 0;
}