Cod sursa(job #1355525)

Utilizator cosmincavCrecana Constantin-Cosmin cosmincav Data 22 februarie 2015 20:05:46
Problema Convertor Scor 90
Compilator cpp Status done
Runda rosedu_cdl_2015 Marime 3.54 kb
#include <iostream>
#include <fstream>
#include<string>
#include <algorithm>
#include <functional>
#include <cctype>
#include <locale>
std::ifstream  fin("convertor.in");
std::ofstream fout("convertor.out");
int numberOfKeys = 0;
int index=0;
int findKey(std::string line, int i) {
    unsigned int j;

    for (j = i + 1; j < line.length(); ++j) {
        if (line.at(j) == '"') {
            fout << line.substr(i + 1, j - i - 1) << ",";
            numberOfKeys++;
            return j;
        }
    }
    return 0;
}
void start(std::string line) {
    int position = 0;
    unsigned int i;
    int found = 0;
    while (1) {
        for (i = 0; i < line.length(); ++i) {
            if (line.at(i) == '"') {
                position = findKey(line, i);
                found = 1;
                break;
            }
        }
        if (!found) {
            std::getline(fin, line);
        }
        else {
            found = 0;
            line = line.substr(position + 1);
            while (1) {

                for (i = 0; i < line.length(); ++i) {

                    if (line.at(i) == ',') {
                        found = 1;
                        line = line.substr(i + 1);
                        break;
                    }
                    if (line.at(i) == '}') {
                        return;
                    }
                }
                if (!found) {
                    std::getline(fin, line);
                }
                else {
                    found = 0;
                    break;
                }
            }
        }
    }
}
std::string getValue(std::string line) {
    std::string value;
    unsigned int i;

            for( i = 0; i < line.length() && !( line[i] == '"' || std::isdigit(line[i] ) ); ++ i) ;

            line = line.substr(i);
           // line.erase(line.begin(), std::find_if(line.begin(), line.end(), std::not1(std::ptr_fun<int, int>(std::isspace))));
            if (line.at(0) == '"') {
                value = line.substr(1);
                value = value.substr(0, value.find('"') );
            }
            else {
                for (i = 0; i < line.length(); i++) {
                    if ( std::isspace((line.at(i))) || line.at(i) == ',' || line.at(i) == '}') {
                     break;
                   }
                }
                value = line.substr(0, i);
            }
            fout << value << ",";
            index++;
            if (index % numberOfKeys == 0) {
                fout << "\n";
             }

            line = line.substr(value.length());
            return line;



}
int main()
{
    std::string    line;
    std::getline(fin, line);
    if (line.find('{') == std::string::npos) {
        std::getline(fin, line);
    }
    start(line);
    fout << "\n";
    fin.close();
    std::ifstream  fin("convertor.in");
    std::getline(fin, line);
    while (1) {
        if (line.length() != 0) {
            if (line.find(':') != std::string::npos) {
                line = line.substr(line.find(':') + 1);
                while( line.length() == 0 ) {
                    std::getline(fin,line);
                }
                line = getValue(line);
            } else {
                if (!std::getline(fin, line)) {
                    break;
                }
            }
        }
        else {
            if (!std::getline(fin, line)) {
                break;
            }
        }
    }
fin.close();
    fout.close();
    return 0;
    }