Cod sursa(job #1348317)

Utilizator ShardamaKaTache Alexandru Marius ShardamaKa Data 19 februarie 2015 17:14:42
Problema Convertor Scor 50
Compilator c Status done
Runda rosedu_cdl_2015 Marime 2.8 kb
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
void creare_string(char string[100000], FILE *input)
{
    char c;
    int nr = 0;
    c = fgetc(input);
    while(c != EOF)
    {
        string[nr] = c;
        c = fgetc(input);
        nr++;
    }
}
void identificare_elemente(char *string, char *elemente)
{
    int 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)
{
    int 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++;
            }
        }

    }
}
int numar_chei(char *string)
{
    int 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;
    int 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;
    int numar = 0;
    token = strtok(elemente, ",\"");
    while(token != NULL)
    {
        puts(token);
        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, ",\"");
    }
}
int main()
{
    FILE *input, *output;
    char string[100000], elemente[100000], chei[100000];
    int nr_carac, nr_chei;
    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);
    introducere_chei(chei, nr_chei, output);
    introducere_elemente(elemente, nr_chei, output);
    fclose(input);
    fclose(output);
    return 0;
}