Cod sursa(job #1354401)

Utilizator achirialexAlexandru Achiritoaei achirialex Data 21 februarie 2015 20:06:05
Problema Convertor Scor 40
Compilator c Status done
Runda rosedu_cdl_2015 Marime 3.45 kb
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

int comparare(char cuvant[], int lungime, char vector[256][1024]){
	int i;
	for(i = 0; i < lungime; i++)
		if(strcmp(cuvant, vector[i]) == 0)
			return 0;
	return 1;
}

int main(void){
	freopen("convertor.in", "r", stdin);
	freopen("convertor.out", "w", stdout);
	char buffer[1024];
	char auxiliar[1024];
	//Parsare dupa ,
	char *tokenVirgula;
	char *end_Virgula;
	//Parsare dupa :
	char auxPuncte[1024];
	char *tokenPuncte;
	char *end_Puncte;
	//Parsare dupa "
	char auxGhilimele[1024];
	char *tokenGhilimele;
	char *end_Ghilimele;
	//Retinem elementele
	int semHeader = 0;
	int semAfisare = 0;
	int semElem = 0;
	char header[256][1024];
	char contents[1024][1024];
	int contHeader = 0, contCont = 0;
	int i;
	while(gets(buffer)){
		semElem = 0;
		strcpy(auxiliar, buffer);
		tokenVirgula = strtok_r(auxiliar, ",", &end_Virgula);
		while(tokenVirgula != NULL){
//			printf("%s\n", tokenVirgula);
			strcpy(auxPuncte, tokenVirgula);
			tokenPuncte = strtok_r(auxPuncte, ":", &end_Puncte);
			//Cap de tabel
			strcpy(auxGhilimele, tokenPuncte);
			if(auxGhilimele[0] != '"'){
				int sem = 2;
				tokenGhilimele = strtok_r(auxGhilimele, "\"", &end_Ghilimele);
				while(tokenGhilimele != NULL){
					if(sem == 1){
						if(comparare(tokenGhilimele, contHeader, header) == 1){
							strcpy(header[contHeader++], tokenGhilimele);
						}
						else
							semHeader = 1;
//						printf("%s\n", header[contHeader - 1]);
						sem = 0;
						break;
					}
					tokenGhilimele = strtok_r(0, "\"", &end_Ghilimele);
					sem--;
				}
			}
			else{
				tokenGhilimele = strtok_r(auxGhilimele, "\"", &end_Ghilimele);
				if(comparare(tokenGhilimele, contHeader, header) == 1){
					strcpy(header[contHeader++], tokenGhilimele);
				}
				else
					semHeader = 1;
//				printf("%s-\n", header[contHeader - 1]);
			}
			//Elemente tabel;
			int sem = 2;
			while(tokenPuncte != NULL){
				if(sem == 1){
					strcpy(auxGhilimele, tokenPuncte);
					char *pos = strchr(auxGhilimele, '"');
					if(pos != NULL){
						if(auxGhilimele[0] != '"'){
							int semafor = 2;
							tokenGhilimele = strtok_r(auxGhilimele, "\"", &end_Ghilimele);
							while(tokenGhilimele != NULL){
								if(semafor == 1){
									strcpy(contents[contCont++], tokenGhilimele);
									semafor = 0;
									break;
								}
								tokenGhilimele = strtok_r(0, "\"", &end_Ghilimele);
								semafor --;
							}
						}
						else{
							tokenGhilimele = strtok_r(auxGhilimele, "\"", &end_Ghilimele);
							strcpy(contents[contCont++], tokenGhilimele);
						}
					}
					else{

						int len = 0;
						for(i = 0; i < strlen(auxGhilimele); i++)
							if(!isspace(auxGhilimele[i]) && auxGhilimele[i] > 0){
								contents[contCont][len++] = auxGhilimele[i];
							}
						contCont++;
					}
//					printf("%s\n", auxGhilimele);
					sem = 0;
					break;
				}
				tokenPuncte = strtok_r(0, ":", &end_Puncte);
				sem --;
			}
//			printf("%s\n", contents[contCont - 1]);
			tokenVirgula = strtok_r(0, ",", &end_Virgula);
			//Afisare Header
			if(semAfisare == 0 && semHeader == 1){
				for(i = 0; i < contHeader; i++)
					printf("%s,", header[i]);
				printf("\n");
				semAfisare = 1;
			}
//			if(semElem == 1){
//				for(i = 0; i < contCont; i++)
//					printf("%s,", contents[i]);
//				contCont = 0;
//				printf("\n");
//			}
		}
	}
	for(i = 0; i < contCont; i++){
		if(i % contHeader == 0 && i != 0)
			printf("\n");
		printf("%s,", contents[i]);

	}
	return 0;
}