Cod sursa(job #1355087)

Utilizator SebiSebiPirtoaca George Sebastian SebiSebi Data 22 februarie 2015 12:56:34
Problema Convertor Scor 100
Compilator cpp Status done
Runda rosedu_cdl_2015 Marime 3.17 kb
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;

#define DIM 10000
#define INF (1<<30)
 
char buff[DIM];
 
int findfirst(const char ch, int poz)
{
    int i;
    do {
        if(poz>=DIM) {
            fread(buff, sizeof(char), DIM, stdin);
            i=0;
        }
        else i = poz; //incepem la pozitia poz
        while (i <= DIM - 1) {
            if (buff[i] == ch)
                return i;
            if (buff[i] == '}') //s-a terminat o intrare JSON
                return -i-1; //returnam pozitia "codificata"
			if (buff[i] == ']')
                return -INF;
            i++;
        }
        poz = DIM;
    } while (i >= DIM);
    return -1;
}

inline void go_next(int &i) //se muta pe urmatorul caracter
{
	i++;
	if (i >= DIM) {
		fread(buff, sizeof(char), DIM, stdin); //daca am depasit blocul citit, citim urmatorul bloc
		i = 0;
	}
}

int main ()
{
    int i;
    freopen("convertor.in", "r", stdin);
    freopen("convertor.out", "w", stdout);
    // gasim doar cheile din obiectele JSON
	i = DIM;
    while(1) {
        i = findfirst('"', i);
        if (i < 0)
            break;
        go_next(i);
        while (buff[i] != '"') {
            fwrite(buff + i, sizeof(char), 1, stdout);
            go_next(i);
        }
        printf(",");
		go_next(i);
        while (buff[i] != '"' && isdigit(buff[i]) == 0) {
            if (buff[i] == '}')
                break;
            go_next(i);
        }
        if (buff[i] == '"') {
            i = findfirst('"', i + 1);
            if (i < 0)
                break;
            go_next(i);
        }
        else {
            while (isdigit(buff[i])) 
                go_next(i);
        }
        if (i >= DIM) {
            fread(buff, sizeof(char), DIM, stdin);
            i = 0;
        }
    }
    printf("\n");
	//ne mutam la inceputul fisierului
	//si gasim valorile din lista unui obiect
    fseek(stdin, 0, SEEK_SET);
    for (i = findfirst('"', DIM); ;i = findfirst('"', i)) {
        if (i == -INF)
            break;
        go_next(i);
        while(1) {
            i = findfirst('"', i);
            go_next(i);
            while(1) {
                if (buff[i] == '"' || isdigit(buff[i]))
                    break;
                go_next(i);
            }
            if (buff[i] == '"') {
                go_next(i);
                while (buff[i] != '"') {
                    fwrite(buff + i, sizeof(char), 1, stdout);
                    go_next(i);
                }
				go_next(i);
            }
            else {
                while (isdigit(buff[i])) {
                    fwrite(buff + i, sizeof(char), 1, stdout);
                    go_next(i);
                }
            }
            printf(",");
            i = findfirst('"', i);
            if (i < 0) { //inseamna ca s-a terminat lista unui obiect
                i = -i - 1; //decodificam pozitia
                go_next(i); 
                break; //si trecem la urmatorul obiect
            }
            else go_next(i);
        }
        printf("\n");
    }
    fclose(stdin);
    fclose(stdout);
    return 0;
}