Cod sursa(job #1347190)

Utilizator Marian_25Stanciulica Marian Marian_25 Data 18 februarie 2015 20:41:39
Problema Convertor Scor 20
Compilator c Status done
Runda rosedu_cdl_2015 Marime 1.56 kb
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
#include<time.h>

int main()
{
	clock_t t;
	t = clock();
	FILE *input, *output;
	int i = 0, MAX = 30000000;
	//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);
	}
	
	char a = getc(input);
	int nr_quotes = 0;
	
	if(input)
	{
		while((c = getc(input)) != EOF)
		{
			if(c == '\"')
			{
				nr_quotes++;
			}
			if (nr_quotes % 2 == 1)
			{
				str[i] = c;
				i++;
			}
			else if(isdigit(c) != 0 && isdigit(a) == 0 && nr_quotes % 2 == 0)
			{
				str[i] = '\"';
				i++;
				str[i] = c;
				i++;
			}
			else if (isdigit(c) != 0 && nr_quotes % 2 == 0)
			{
				str[i] = c;
				i++;
			}
			a = c;
		}
	}
	str[i] = '\0';
	//printf("%s\n", str);
	
	char **mat, *p;
	
	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,", mat[i], mat[i+2], mat[i+4], mat[i+6]);
		if(i < nr - 7)
			fprintf(output,"\n");
	}
	t = clock() - t;
	printf("\n%f", (float)t/CLOCKS_PER_SEC);
	
	fclose(input);
	fclose(output);

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