Cod sursa(job #1346001)

Utilizator Marian_25Stanciulica Marian Marian_25 Data 17 februarie 2015 23:01:41
Problema Convertor Scor 20
Compilator c Status done
Runda rosedu_cdl_2015 Marime 1.43 kb
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>

int main()
{
	FILE *input, *output;
	int i = 0, MAX = 1024000;
	int j = 0;
	
	input = fopen("convertor.in", "r");
	output = fopen("convertor.out", "w");
	
	char c;
	char *str;
	
	str = malloc(MAX * sizeof(char));
	if(str == NULL)
	{
		printf("Error alocating memory!");
		exit(1);
	}
	
	if(input)
	{
		while((c = getc(input)) != EOF)
		{
			/*if(strlen(str) > MAX - 1)
			{
				MAX += 1024;
				str = (char *) realloc(str, MAX * sizeof(char));
			}*/
			str[i] = c;
			i++;
		}
	}
	str[i] = '\0';
	
	char **mat, *p;
	
	int nr_quotes = 0;
	
	for (i = 0; i < strlen(str); i++)
	{
		if(str[i] == '\"')
		{
			nr_quotes++;
		}
		if (nr_quotes % 2 == 1)
		{
			str[j] = str[i];
			j++;
		}
		if(isdigit(str[i]) != 0 && isdigit(str[i-1]) == 0 && nr_quotes % 2 == 0)
		{
			str[j] = '\"';
			j++;
		}
		if (isdigit(str[i]) && nr_quotes % 2 == 0)
		{
			str[j] = str[i];
			j++;
		}
	}
	str[j] = '\0';

	i = 1;
	int	nr = 1;
	mat = malloc(strlen(str) * sizeof(char));
	if(mat == NULL)
	{
		printf("Error alocating memory!");
		exit(1);
	}
	mat[0] = strtok(str, "\"");
	while((p = strtok(NULL, "\"")) != NULL)
	{
		mat[i] = p;
		i++;
		nr++;
	}
	
	fprintf(output, "%s,%s,%s,%s,\n", mat[0], mat[2], mat[4], mat[6]);
	for(i = 1; i < nr - 6; i += 8)
	{
		fprintf(output, "%s,%s,%s,%s,\n", mat[i], mat[i+2], mat[i+4], mat[i+6]);
	}
	
	fclose(input);
	fclose(output);

	free(mat);
	free(str);
	return 0;
}