Cod sursa(job #1350345)

Utilizator george.ioana1995Ioana Alexandru george.ioana1995 Data 20 februarie 2015 19:25:54
Problema Convertor Scor 0
Compilator c Status done
Runda rosedu_cdl_2015 Marime 2.41 kb
#include<stdio.h>
#include<string.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);
    for(i=0;i<strlen(s);i++)
        if((s[i]==' ' && s[i+1]==',') || (s[i]==' ' && s[i-1]==',') || (s[i]==',' && s[i+1]==','))
            strcpy(s+i,s+i+1);
    for(i=0;i<strlen(s)-1;i++)
        if((s[i]==',' && s[i+1]==',') || s[i]=='\t')
            strcpy(s+i,s+i+1);
    while(s[0]==' ' || s[0]=='{' || s[0]=='[' || s[0]==',')
        strcpy(s,s+1);
    for(i=0;i<strlen(s);i++)
        while(s[i-1]==',' && s[i]==' ')
            strcpy(s+i,s+i+1);
}
void sterge(char s[])
{
  int i;

  for(i=0; s[i]!='\n'; ++i)
    ;
  --i;  /* To consider raw line without \n */

  for(i >0; ((s[i] == ' ') || (s[i] =='\t'));--i)
    ; /* Removing the Trailing Blanks and Tab Spaces */

  if( i >= 0) /* Non Empty Line */
  {
    ++i;
    s[i] = '\n';
    ++i;
    s[i] = '\0';
  }
}
int main()
{
    FILE* f;
    FILE* g;
    char s[1024];
    char* cuv=malloc( sizeof(char) * (1014+ 1 ) );
    char* c=malloc( sizeof(char) * ( 1024+ 1 ) );
    char cap[1024],inf[1024];
    int k;
    int ok,d;


    if((f=fopen("convertor.in","r"))==NULL)
    {
        printf("Nu s-a deshis fisierul.");
        exit(1);
    }

    if((g=fopen("convertor.out","w"))==NULL)
    {
        printf("Nu s-a deshis fisierul.");
        exit(1);
    }

    strcpy(cap,"");
    strcpy(inf,"");

    while(feof(f)==0)
    {
        fgets(s,2014,f);
        s[strlen(s)-1]='\0';
        sterge(s);
        d=1;
        cuv=strtok(s,",:");
        c=strdup(cuv);
        while(cuv!=NULL)
        {
            if(d==1)
            {strcat(cap,c); strcat(cap,","); d=0;
              if(strchr(cap,'[')) ok=1;}
            else
            {strcat(inf,c); strcat(inf,","); d=1;}
            cuv=strtok(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,"");
            }
            c=strdup(cuv);
        }
    }
    fclose(f);
    fclose(g);
    free(cuv);
    free(c);
    return 0;
}