Cod sursa(job #1361132)

Utilizator dorinel.filipFilip Ion Dorinel dorinel.filip Data 25 februarie 2015 19:49:38
Problema Convertor Scor 80
Compilator cpp Status done
Runda rosedu_cdl_2015 Marime 1.67 kb
#include <iostream>
#include <fstream>
#include <string>

int main ( void )
{

    std::ifstream f("convertor.in");
    std::ofstream g("convertor.out");
    if( !g.is_open() )
    {
        std::cout << "Failed to open the file." << std::endl;
        throw errno;
    }

    std::string entry;
    std::getline(f, entry, '}');
    size_t pos = 0;
    size_t next_quote;
    while((next_quote = entry.find('"',pos)) != std::string::npos)
    {
        size_t start = entry.find('"', pos);
        size_t end = entry.find('"', start+1);
        g << entry.substr(start + 1, end - start - 1) << ",";
        pos = entry.find(',', end+1);

    }

    g << std::endl;
    f.seekg(0, f.beg);

    std::string delim = "}";
    while(std::getline(f, entry, '}'))
    {
        size_t pos = 0;
        size_t next_quote;
        while((next_quote = entry.find('"',pos)) != std::string::npos)
        {
            pos = entry.find('"', pos+1);
            if (pos == std::string::npos)
                break;
            pos = entry.find('"', pos+1);
            pos = entry.find(":",pos+1);
            size_t start = entry.find_first_not_of(" ", pos+1);
            size_t end;
            if(entry[start] == '"')
            {
                end = entry.find('"', start+1);
            }
            else
            {
                for(end = start; end < entry.length() && entry[end] >= '0' && entry[end] <= '9'; end++);
                start--;
            }

            g << entry.substr(start + 1, end - start - 1) << ",";

            pos = end +1;
        }
        g << std::endl;
    }
    f.close();
    g.close();
    return 0;
}