Cod sursa(job #1346597)

Utilizator florin.nastaseNastase Florin florin.nastase Data 18 februarie 2015 13:49:32
Problema Convertor Scor 100
Compilator c Status done
Runda rosedu_cdl_2015 Marime 2.35 kb
#include <stdio.h>
#include <string.h>

void trim(char **p)
{
    while ((*p)[0] == ' ')
    {
        (*p)++;
    }

    while ((*p)[strlen(*p)-1] == '\n')
    {
        (*p)[strlen(*p)-1] = '\0';
    }

    while ((*p)[strlen(*p)-1] == ' ')
    {
        (*p)[strlen(*p)-1] = '\0';
    }
}

void writeValues(int nr)
{
    FILE *f = fopen("convertor.in","rt");
    FILE *g = fopen("convertor.out","at");
    fprintf(g,"\n");

    int valoriScrise = 0, poz = 0;
    char line[1025];
    char *p ;

    while (!feof(f))
    {
        fgets(line, 1025, f);
        p = strtok(line,":,{}\"[]");

        while (p != NULL)
        {
            trim(&p);
            if (strcmp(p,"\n") != 0 && strcmp(p,"") != 0)
            {
                poz++;
                if (valoriScrise == nr)
                {
                    fprintf(g,"\n");
                    valoriScrise = 0;
                }
                else if (poz % 2 == 0)
                {
                    fprintf(g,"%s,",p);
                    valoriScrise++;
                }
            }
            p = strtok(NULL, ":,{}\"[]");
        }
    }

    fclose(g);
    fclose(f);
}

int writeKeys()
{
    FILE *f = fopen("convertor.in","rt");
    FILE *g = fopen("convertor.out","wt");

    char line[1025];
    char *p, *prim = "";
    int poz = 0;
    int nr = 0;
    int gata = 0;

    while (!feof(f) && !gata)
    {
        fgets(line, 1025, f);
        p = strtok(line,":,{}\"[]");

        while (p!=NULL && !gata)
        {
            trim(&p);
            if (strcmp(p,"\n") != 0 && strcmp(p,"") != 0)
            {
                poz++;
                if (strcmp(prim,"") == 0)
                {
                    nr = 1;
                    prim = strdup(p);
                    fprintf(g,"%s,",p);
                }
                else if (poz % 2 == 1)
                {
                    if (strcmp (p, prim) == 0) gata = 1;
                    else
                    {
                        fprintf(g,"%s,",p);
                        nr++;
                    }
                }
            }
            p = strtok(NULL, ":,{}\"[]");
        }
    }

    fclose(g);
    fclose(f);

    return nr;
}

int main()
{
    int nr = 0;
    nr = writeKeys();
    writeValues(nr);

    return 0;
}