Cod sursa(job #1350082)

Utilizator remus.ionitaIonita Remus remus.ionita Data 20 februarie 2015 17:35:03
Problema Convertor Scor 50
Compilator c Status done
Runda rosedu_cdl_2015 Marime 2.22 kb
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

void reset_count (char x, int *count, FILE *f, char *line, int *pos) {
	if ( x == '}' ) {
		line[*pos] = '\n';
		//strcat(line,"\n");
		*pos = *pos + 1;
		*count = 0;
	}
}
void prelucrare(char *namein, char *nameout) {
        FILE *f1 = fopen(namein, "r");
        FILE *f2 = fopen(nameout, "w");
	int count = 0;
	int virg = 0;
	char c1, c2;

	char line[300000];
	int pos = 0, pos1 = 0;

	while ( (c1 = fgetc(f1)) != '}' ) {
	
		if ( c1 == '"' ) {
		//	pos = 0;
			virg = 1;
			count ++;
			while ( (c2 = fgetc(f1)) != '}') { 
				if (c2 == '"')
					break;
				if (count % 2 == 1 ) {
					line[pos] = c2;
					pos ++;
				}			
			}		
		}
		if ( (c1 - '0') >= 0 && (c1 - '0') <= 9 ) {
			virg = 1;
			count ++;
			while ( (c2 = fgetc(f1)) != EOF) {
				if ( (c2 - '0') < 0 || (c2 - '0') > 9 )
					break;
                        }
                }

	if (virg && count % 2 == 1) {
		line[pos] = ',';
		pos ++;
		//fputc(',',f2);
		virg = 0;
		}
	}

	strcat(line,"\n");
	fputs(line,f2);
	
	strcpy(line,"");
	virg = 0;
	count = 0;
	fseek(f1,0,0);	

	while ( (c1 = fgetc(f1)) != EOF ) {
		
		reset_count(c1,&count,f2,line,&pos1);

		if ( c1 == '"' ) {
	//		pos = 0;
			virg = 1;
			count ++;
			while ( (c2 = fgetc(f1)) != EOF) { 
					if (c2 == '"') 
						break;
					if (count % 2 == 0 ) {
						line[pos1] = c2;
						pos1 ++;
					}
					reset_count(c2,&count,f2,line,&pos1);
			}
		}
	
		if ( (c1 - '0') >= 0 && (c1 - '0') <= 9 ) {
			virg = 1;
			count ++;
			if (count % 2 == 0 ){
				line[pos1] = c1;
				pos1++;
			}
			while ( (c2 = fgetc(f1)) != EOF) {     
                                        if ( (c2 - '0') < 0 || (c2 - '0') > 9 )
						break;					
					if (count % 2 == 0 )  {
                                                line[pos1] = c2;
                                                pos1 ++;
                                        }
					reset_count(c2,&count,f2,line,&pos1);
                        }
		}

		if (virg && count % 2 == 0) {
			line[pos1] = ',';	
			pos1++;
			virg = 0;
		}
	}
	fputs(line,f2);
        fclose(f2);
}
int main(void)
{
        char namein[] = "convertor.in";
        char nameout[] = "convertor.out";
        prelucrare(namein,nameout);
        return 0;
}