Cod sursa(job #1349874)

Utilizator ShardamaKaTache Alexandru Marius ShardamaKa Data 20 februarie 2015 15:37:53
Problema Convertor Scor 40
Compilator c Status done
Runda rosedu_cdl_2015 Marime 4 kb
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
void creare_string(char string[800000], FILE *input)
{
    char c;
    short nr = 0;
    c = fgetc(input);
    while(c != EOF)
    {
        string[nr] = c;
        c = fgetc(input);
        nr++;
    }
}
void identificare_elemente(char *string, char *elemente)
{
    short i, j, nr = 0;
    for(i = 0; i < strlen(string); i++)
    {
        if(string[i] == ':')
        {
            j = i + 2;
            while(string[j-1] != ',')
            {
                elemente[nr] = string[j];
                j++;
                nr++;
            }
        }

    }
}
void identificare_chei(char *string, char *chei)
{
    short i, j, nr = 0;
    for(i = 0; i < strlen(string); i++)
    {
        if(string[i] == '{' || string[i] == ',')
        {
            j = i + 2;
            while(string[j-1] != ':')
            {
                chei[nr] = string[j];
                j++;
                nr++;
            }
        }

    }
}
short numar_chei(char *string)
{
    short i, numar = 0;
    for(i = 0; i < strlen(string); i++)
    {
        if(string[i] == '}')
        {
           break;
        }
        if(string[i] == ':')
        {
            numar++;
        }
    }
    return numar;
}
void introducere_chei(char *chei, int nr_chei, FILE *output)
{
    char *token;
    short numar = 0;
    token = strtok(chei, "\"");
    while(token != NULL)
    {
        if(isalpha(token[0]) != 0 || isdigit(token[0]) != 0)
        {
            fprintf(output, "%s,", token);
            numar++;
        }
        if(numar == nr_chei)
        {
            fprintf(output, "%s", "\n");
            break;
        }
        token = strtok(NULL, "\"");
    }
}
void introducere_elemente(char *elemente, int nr_chei, FILE *output)
{
    char *token;
    short numar = 0, i;
    token = strtok(elemente, ",\"");
    while(token != NULL)
    {
        i = strlen(token) - 1;
        while(token[i] == ' ')
        {
            token[i] = '\0';
            i--;
        }
        if(isalpha(token[0]) != 0 || isdigit(token[0]) != 0)
        {
            fprintf(output, "%s,", token);
            numar++;
        }
        if(numar == nr_chei)
        {
            fprintf(output, "%s", "\n");
            numar = 0;
        }
    token = strtok(NULL, ",\"");
    }
}

void stergere_trash(char *str, char c)
{
    char *src, *dst;
    for(src = dst = str; *src != '\0'; src++)
    {
        *dst = *src;
        if(*dst != c)
        {
            dst++;
        }
    }
    *dst = '\0';
}
void stergere_spatii(char *elemente)
{
    int i, j, k, cont = 0, nr = 0;
    for(i = 0; i < strlen(elemente); i++)
    {
        if(elemente[i] == '"')
        {
            cont++;
        }
        if(cont % 2 == 0)
        {
            j = i;
            while(elemente[j+1] != '"' && j != strlen(elemente)-1)
            {
                if(elemente[j] == ' ')
                {
                    for(k = j; k < strlen(elemente) - 1; k++)
                    {
                        elemente[k] = elemente[k+1];
                    }
                }
                j++;
            }
        }
    }
}

int main()
{
    FILE *input, *output;
    char string[700000], elemente[600000], chei[600000], trash[7] = {'}', ']', '[','{','\n', ':'};
    short nr_chei, i, j, cont = 1;
    input = fopen("convertor.in", "rt");
    output = fopen("convertor.out", "wt");
    creare_string(string, input);
    identificare_elemente(string, elemente);
    identificare_chei(string, chei);
    nr_chei = numar_chei(string);
    for(i = 0; i < strlen(trash); i++)
    {
        stergere_trash(elemente, trash[i]);
        stergere_trash(chei, trash[i]);
    }
    stergere_spatii(elemente);
    introducere_chei(chei, nr_chei, output);
    introducere_elemente(elemente, nr_chei, output);
    fclose(input);
    fclose(output);
    return 0;
}