Cod sursa(job #1353210)

Utilizator george.ioana1995Ioana Alexandru george.ioana1995 Data 21 februarie 2015 13:53:54
Problema Convertor Scor 0
Compilator c Status done
Runda rosedu_cdl_2015 Marime 1.95 kb
#include <stdio.h>
#include <stdlib.h>
void eliminare(char s[])
{
    int i;

    for(i=0;i<strlen(s);i++)
    {
        if(s[i]=='{' || s[i]=='}' || s[i]=='[' || s[i]==']' || s[i]=='"')
            strcpy(s+i,s+i+1);
        while(s[i-1]==',' && s[i]==' ')
             strcpy(s+i,s+i+1);
    }

     for(i=0;i<strlen(s);i++)
    {
        if(s[i]=='{' || s[i]=='}' || s[i]=='[' || s[i]==']' || s[i]=='"')
            strcpy(s+i,s+i+1);
    }

    for(i=0;i<strlen(s);i++)
    {
        if(s[i-1]==',' && s[i]==',')
            strcpy(s+i,s+i+1);
    }
    while(s[0]==' ' || s[0]==',' || s[0]=='[' || s[i]=='{' || s[0]=='"')
        strcpy(s,s+1);
}
int main()
{
    FILE* f;
    FILE* g;
    char s[1000000];
    char* cuv;
    char* c;
    char cap[100000],inf[100000];
    int d=1;
    int ok=1;

    if((f=fopen("convertor.in","r"))==NULL)
       {
           exit(1);
       }

    if((g=fopen("convertor.out","w"))==NULL)
       {
           exit(1);
       }

    strcpy(cap,"");
    strcpy(inf,"");
    while(feof(f)==0)
        {
            fgets(s,10000000,f);
            s[strlen(s)-1]='\0';
            cuv=strtok(s,":,");
            c=strdup(cuv);
            d=1;
            while(c!=NULL)
            {
                if(d==1 )
                    {strcat(cap,c); strcat(cap,","); d=0; }
                else
                        {strcat(inf,c); strcat(inf,","); d=0;}

                if(strchr(inf,'}') || strchr(cap,'}'))
                {
                    eliminare(cap);
                    eliminare(inf);

                    cap[strlen(cap)]='\n';
                    inf[strlen(inf)]='\n';

                    if(ok){fputs(cap,g); ok=0;}
                    fputs(inf,g);

                    strcpy(cap,"");
                    strcpy(inf,"");
                }
                cuv=strtok(0,":,");
                c=strdup(cuv);
            }
        }
    return 0;
}