Cod sursa(job #1361660)

Utilizator monicalegendaLegenda Monica monicalegenda Data 25 februarie 2015 22:45:18
Problema Convertor Scor 100
Compilator c Status done
Runda rosedu_cdl_2015 Marime 2.17 kb
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BLSIZE 30

int count(FILE * f, char c, int *v)
{
    fseek(f, 0, 0);
    int nr = 0, poz = 0;
    char buff[BLSIZE], *p;
    while (fread(buff, 1, BLSIZE, f)) {
        p = strchr(buff, c);
        while (p) {
                v[nr] = poz + (p - buff);
                nr++;             
                p = strchr(p + 1, c);
        }
        poz += BLSIZE;
          
    }
    v[nr] = ftell(f) - 1;
    fseek(f, 0, 0);
    return nr;
}

int main()
{
    FILE *input = fopen("convertor.in", "rb");
    FILE *output = fopen("convertor.out", "w");
    int i, j, nr = 0, nr_chei = 0, nr_val;
    char *array, *p;
    int poz[1000];
    memset(poz, 0, 1000 * sizeof(int));
    nr = count(input, '{', poz);  
    array = calloc(poz[1] - poz[0], 1);
    fread(array, 1, poz[1] - poz[0], input);
    p = strchr(array, ':');
    while (p) {
        nr_chei ++;
        i = 0;
        while (p[i] != '\"')
            i--;
        i--;
        while (p[i] != '\"')
            i--;
        i++;
        while (p[i] != '\"') {
            fputc(p[i], output);
            i++;
        }
        fprintf(output, ",");
        p = strchr(p + 1, ':');
    }
    fprintf(output, "\n");
    fseek(input, 0, 0);
    free(array);
    nr_val = 0;
    fseek(input, poz[0], 0);
    for(i = 1; i <= nr; i++) {
        array = calloc(poz[i] - poz[i - 1], 1);
        fread(array, 1, poz[i] - poz[i - 1], input);     
        p = strchr(array, ':');
        while (p) {
            nr_val ++;
            j = 1;
            while (p[j] <= ' ')
                j++;
            if (p[j] == '\"') {
                j++;
                while (p[j] != '\"') {
                    fputc(p[j], output);
                    j++;
                }
            } else {
                while (strchr("0123456789-", p[j])) {
                    fputc(p[j], output);
                    j++;
                }
            }
            fprintf(output, ",");
            if(nr_val == nr_chei) {
                fprintf(output, "\n");
				nr_val=0;
			}
            p = strchr(p + 1, ':');
        }
        free(array);      
    }
    fclose(input);
    fclose(output);
    return 0;
}