Cod sursa(job #1338462)

Utilizator Iustin_BulimarFMI Iustin Bulimar Iustin_Bulimar Data 10 februarie 2015 02:29:55
Problema Convertor Scor 20
Compilator cpp Status done
Runda rosedu_cdl_2015 Marime 1.6 kb
#include <fstream>
#include <vector>
#include <string>
#include <stdio.h>
#include <cstring>

using namespace std;

ifstream cin("convertor.in");
ofstream cout("convertor.out");

vector<string> values;
char filling[] = ",: \n\"{}[]";

bool ignoreFilling() {
    char c;
    c = cin.get();
    while(strchr(filling, c)) {
        if(c == ']' || c == '}')
            return false;
        c = cin.get();
    }
    cin.unget();
    return true;
}

string readWord() {
    string word;
    char c;
    cin.unget();
    c = cin.get();
    if(c == '"') {
        c = cin.get();
        while(c != '"') {
            word += c;
            c = cin.get();
        }
    } else {
        c = cin.get();
        while(!strchr(filling, c)) {
            word += c;
            c = cin.get();
        }
    }
    cin.unget();
    return word;
}


bool readPair(int nr) {
    if(!ignoreFilling())
        return false;
    string key = readWord();
    if(nr == 1)
        cout << key << ",";
    ignoreFilling();
    string value = readWord();
    values.push_back(value);
}

bool readObj(int nr) {
    if(!ignoreFilling())
        return false;
    bool thereArePairs = true;
    while(thereArePairs)
        thereArePairs = readPair(nr);

    if(nr == 1)
        cout << '\n';
    for(int i = 0; i < values.size(); i++)
        cout << values[i] << ",";
    values.erase(values.begin(), values.end());
    cout << '\n';

}

int main() {
    int nr = 1;
    bool thereAreObjects = true;
    while(thereAreObjects)
        thereAreObjects = readObj(nr++);
    return 0;
}