Cod sursa(job #1359874)

Utilizator mircea_marian.popaPopa Mircea-Marian mircea_marian.popa Data 25 februarie 2015 09:15:51
Problema Convertor Scor 60
Compilator c Status done
Runda rosedu_cdl_2015 Marime 3.01 kb
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
    char *v = (char*) malloc( 2097152 * sizeof( char ) );//3145728
    FILE *f = fopen("convertor.in" , "rt" );
    unsigned int h = fread( v , sizeof(char) , 2097151 , f );
    fclose( f );
    v[h] = '\0';


    char *tag = (char*) malloc(1024 * sizeof(char) );
    unsigned short i = 0 , dt = 0;
    const char *delim = ",";
    char *tok = strtok ( v , delim );
    f = fopen("convertor.out" , "wt" );
    while ( tok[strlen(tok) - 1] != '}' )
    {
        i = 0;
        while ( tok[i] != '"' )
            i++;
        i++;
        while ( tok[i] != '"' )
        {
            fputc( tok[i] , f );
            i++;
        }
        fputc( ',' , f );
        i++;
        while ( ( ( tok[i] < '1' ) || ( tok[i] > '9' ) ) && ( tok[i] != '"' ) )
            i++;
        if ( tok[i] == '"' )
        {
            i++;
            while ( tok[i] != '"')
            {
                tag[dt] = tok[i];
                dt++;
                i++;
            }
        }
        else
        {
            while ( (tok[i] > 47) && ( tok[i] < 58 ) )
            {
                tag[dt] = tok[i];
                i++;
                dt++;
            }
        }
        tag[dt] = ',';
        dt++;
        tok = strtok( NULL , delim );
    }





    i = 0;
    while ( tok[i] != '"' )
        i++;
    i++;
    while ( tok[i] != '"' )
    {
        fputc( tok[i] , f );
        i++;
    }
    fputc( ',' , f );
    i++;
    while ( ( ( tok[i] < '1' ) || ( tok[i] > '9' ) ) && ( tok[i] != '"' ) )
        i++;
    if ( tok[i] == '"' )
    {
        i++;
        while ( tok[i] != '"')
        {
            tag[dt] = tok[i];
            dt++;
            i++;
        }
    }
    else
    {
        while ( (tok[i] > 47) && ( tok[i] < 58 ) )
        {
            tag[dt] = tok[i];
            i++;
            dt++;
        }
    }
    tag[dt] = ',';
    dt++;
    tok = strtok( NULL , delim );
    tag[dt] = '\n';
    dt++;
    tag[dt] = '\0';
    fprintf( f , "\n%s" , tag );
    free(tag);


    unsigned char lim;
    unsigned char t;
    while ( tok != NULL )
    {
        i = 1;
        t = 2;
        while ( t )
        {
            if ( tok[i] == '"' )
                t--;
            i++;
        }
        while ( ( ( tok[i] < '1' ) || ( tok[i] > '9' ) ) && ( tok[i] != '"' ) )
            i++;
        if ( tok[i] == '"' )
        {
            i++;
            while ( tok[i] != '"')
            {
                fputc( tok[i] , f );
                i++;
            }
        }
        else
        {
            while ( (tok[i] > 47) && ( tok[i] < 58 ) )
            {
                fputc( tok[i] , f );
                i++;
            }
        }
        fputc( ',' , f );
        lim = strlen(tok) - 1;
        while ( ( ( tok[lim] < '1' ) || ( tok[lim] > '9' ) ) && ( tok[lim] != '"' ) )
        {
            if( tok[lim] == '}' )
                fputc( '\n' , f );
            lim--;
        }

        tok = strtok( NULL , delim );
    }

    free( v );
    fclose( f );

}