Cod sursa(job #1346111)

Utilizator ShardamaKaTache Alexandru Marius ShardamaKa Data 18 februarie 2015 00:54:21
Problema Convertor Scor 0
Compilator c Status done
Runda rosedu_cdl_2015 Marime 4.44 kb
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int numar_aparitii(char *string, char *token)
{
    int nr = 0;
    for(string = strstr(string, token); string; string = strstr(string + strlen(token), token))
    {
        nr++;
    }
    return nr;
}
int numar_linii(FILE *input)
{
    char c;
    int numar = 1;
    c = fgetc(input);
    while(c != EOF)
    {
        if(c == '\n')
        {
            numar++;
        }
        c = fgetc(input);
    }
    return numar;
}
void creare_string(FILE *input, char *string)
{
    int i = 0;
    char c;
    c = fgetc(input);
    while( c != EOF )
    {
        string[i] = c;
        i++;
        c = fgetc(input);
    }
}
int numar_chei(FILE *input)
{
    char c;
    int nr = 0;
    c = fgetc(input);
    while(c != '}')
    {
        if(c == ':')
        {
            nr++;
        }
        c = fgetc(input);
    }
    return nr;
}
void introducere_chei(FILE *input, FILE *output, char *string, int nr_chei, int nr_linii)
{
    char str[100], *token;
    int i, numar_introd_in_output = 0;

    fgets(str,100,input);
    token = strtok(str, "\"");
    while(token != NULL)
    {

        if(numar_aparitii(string, token) != 1)
        {
            if(!isalpha(token[0]))
            {
                ;
            }
            else
            {
                    fprintf(output, "%s,", token);
                    numar_introd_in_output++;
            }
        }
        token = strtok(NULL, "\"");
    }
    if(numar_introd_in_output != nr_chei)
    {
        fgets(str, 100, input);
        token = strtok(str, "\"");
        while(token != NULL)
        {

            if(numar_aparitii(string, token) != 1)
            {
                if(!isalpha(token[0]))
                {
                    ;
                }
                else
                {
                    fprintf(output, "%s,", token);
                    numar_introd_in_output++;
                }
            }
            if(numar_introd_in_output == nr_chei)
            {
                fprintf(output, "%s", "\n");
                break;
            }
            token = strtok(NULL, "\"");
        }
    }

}

void introducere_elemente(FILE *input, FILE *output, char *string, int nr_chei, int nr_linii)
{
    char str[100], *token;
    //char aux[20] = {':','{','}','[',']','\n',','};
    int numar = 0, i, lungime;
    for(i = 0; i < nr_linii; i++)
    {
        fgets(str, 100, input);
        token = strtok(str, "\":}]{[,");
        while(token != NULL)
        {
            lungime = strlen(token);
            if(numar_aparitii(string, token) == 1)
            {
                if(token[lungime - 1] == ',')
                {
                    strcpy(token, token + lungime);
                }
                fprintf(output, "%s,", token);
                numar++;

                if(numar == nr_chei)
                {
                    fprintf(output, "%s", "\n");
                    numar = 0;
                }
            }
            token = strtok(NULL, "\":}]{[,");
        }
    }
}
int main()
{
    FILE *input, *output;
    char *string;
    int nr_linii, nr_chei, nr_carac, numar;
//------->Numar de caractere din fisierul input
    input = fopen("convertor.in", "rb");
    if(input == NULL)
    {
        printf("Eroare la deschidere!");
        exit(1);
    }
    fseek(input, 0L, SEEK_END);
    nr_carac = ftell(input);
    fclose(input);
//------>deschidere
    input = fopen("convertor.in", "rt");
    if(input == NULL)
    {
        printf("Eroare la deschidere!");
        exit(1);
    }
    output = fopen("convertor.out", "wt");
    if(output == NULL)
    {
        printf("Eroare la deschidere!");
        exit(1);
    }
//-------->Creare string
    string = malloc(sizeof(char) * nr_carac);
    if(string == NULL)
    {
        printf("Alocare dinamica esuata!");
        exit(1);
    }
    creare_string(input, string);
    rewind(input);
//--------->Numarul de linii din fisier
    nr_linii = numar_linii(input);
    rewind(input);
//--------->Numarul de chei
    nr_chei = numar_chei(input);
    rewind(input);
//--------->
    introducere_chei(input, output, string, nr_chei, nr_linii);
    rewind(input);
    introducere_elemente(input, output, string, nr_chei, nr_linii);
    fclose(input);
    fclose(output);
    return 0;
}