Pagini recente » Cod sursa (job #2406100) | Cod sursa (job #1343408) | Cod sursa (job #1333954) | Cod sursa (job #2923846) | Cod sursa (job #1347280)
#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';
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++;
}
int nr_camp = 0;
for(i = 1; i < nr; i++)
{
nr_camp++;
if(strcmp(mat[0], mat[i]) == 0)
break;
}
for(i = 0; i < nr_camp; i += 2)
{
fprintf(output, "%s,", mat[i]);
}
fprintf(output, "\n");
for(i = 1; i < nr - nr_camp; i += 2)
{
fprintf(output, "%s,", mat[i]);
if((i+1) % nr_camp == 0)
fprintf(output,"\n");
}
t = clock() - t;
printf("\n%f", (float)t/CLOCKS_PER_SEC);
fclose(input);
fclose(output);
free(mat);
free(str);
return 0;
}