Cod sursa(job #1347142)

Utilizator cristian.cazanCristian-Claudiu Cazan cristian.cazan Data 18 februarie 2015 20:15:02
Problema Convertor Scor 0
Compilator c Status done
Runda rosedu_cdl_2015 Marime 1.52 kb
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main ()
{
    FILE *f,*g;
    int i,j,a=0,b=0;
    char c, p[500],q[500],*bf;
    f=fopen("convertor.in","rt");
    g=fopen("convertor.out","wt");
    i=0;

    while(!feof(f))
    {
        fscanf(f,"%c",&c);
        if(c == '\n' )
            continue;
        if(c == ':')
            a++;
        if(c == '{')
            b++;
        p[i]=c;
        i++;
    }
    p[i-1]='\0';
    bf=strtok(p,"{}\",\n:[]");
    i=0;
    j=0;
    while(bf != NULL)
    {
        if(strcmp(bf," ")!=0)
            {
                i++;
                if(i%2 == 1 && i< (a/b)*2 )
                {
                    fprintf(g,"%s,",bf);
                    if(i == (a/b)*2 -1)
                        fprintf(g,"\n");
                }
                if(i%2 == 0)
                {

                    if(j == 0)
                        {
                            strcpy(q,bf);
                            j++;
                            strcat(q,",");
                        }
                    if( i%((a/b)*2) == 0)
                    {   strcat(q,bf);
                        strcat(q,",");
                        strcat(q,"\n");
                    }
                    else{
                        strcat(q,bf);
                        strcat(q,",");
                    }

                }

            }
        bf=strtok(NULL,"{}\"\n,:[]");
    }
    fputs(q,g);
    fclose(f);
    fclose(g);

    return 0;

}