Cod sursa(job #1359546)

Utilizator mircea_marian.popaPopa Mircea-Marian mircea_marian.popa Data 24 februarie 2015 23:23:02
Problema Convertor Scor 60
Compilator c Status done
Runda rosedu_cdl_2015 Marime 2.98 kb
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
    char *v = (char*) malloc( 1048576 * sizeof( char ) );
    FILE *f = fopen("convertor.in" , "rt" );
    unsigned short h = fread( v , sizeof(char) , 1048575 , 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 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 );
        if ( tok[strlen(tok) - 1] == '}' )
            fputc( '\n' , f );
        tok = strtok( NULL , delim );
    }

    free( v );
    fclose( f );

}