Cod sursa(job #1343583)

Utilizator cfandreiCeapa Andrei Florin cfandrei Data 15 februarie 2015 17:11:23
Problema Convertor Scor 90
Compilator c Status done
Runda rosedu_cdl_2015 Marime 1.53 kb
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define NMAX 4096000

int main()
{
    char *aux, *head, *headaux, JSON[NMAX], *JSON1;
    int i = 0, k;
    FILE *f, *g;

    f = fopen ("convertor.in", "r" );
    g = fopen ("convertor.out", "w") ;

    fscanf(f,"%[^&]s",JSON);
    JSON1 = strdup(JSON) ;
    head = strtok(JSON,"}");
    headaux = strtok(head,",{:");
    while ( headaux != NULL ) {
	if ( i % 2 == 1 ) {
		int j1 ,j2, j;
		for ( j = 0 ; j < strlen(headaux) ; j++ ) {
			if ( headaux[j] == '"') j2 = j ;
		}
		for ( j = strlen(headaux) - 1 ; j >= 0 ; j-- ) {
			if ( headaux[j] == '"' ) j1 = j ;
		}
		for ( j = j1 + 1 ; j < j2 ; j++ ) {
			fprintf(g,"%c",headaux[j]);
		}
		fprintf(g,",");
	}	
	headaux = strtok(NULL,",{:");
	i = i + 1 ;
    }
    k = i ;
    i = 0 ;
    aux = strtok(JSON1,",:");
    while ( aux != NULL ){
	if ( i % 2 == 1 ) {
		int j1 = 0, j2 = 0, j;
		for ( j = 0 ; j < strlen(aux) ; j++ ) {
			if ( aux[j] == '"') j2 = j ;
		}
		for ( j = strlen(aux) - 1 ; j >= 0 ; j-- ) {
			if ( aux[j] == '"') j1 = j ;
		}
		if ( j1 != 0 && j2 != 0 )
			for ( j = j1 + 1  ; j < j2 ; j++ ) {
				fprintf(g,"%c",aux[j]);
		} 
		if ( j2 == 0 && j1 == 0 ) 
			for ( j = 0 ; j < strlen(aux) ; j++ ) {
				if ( isdigit(aux[j]) ) fprintf(g,"%c",aux[j]) ;
			}

		fprintf(g,",");
        }
	if (  i % (k - 1) == 0 ) fprintf(g,"\n");
        aux = strtok(NULL,",:");
	i = i + 1 ;
    } 
    fprintf(g,"\n");
    fclose(f);
    fclose(g);

    return 0;
}