Cod sursa(job #1266636)

Utilizator TheBos5Alexandru Iercosan TheBos5 Data 18 noiembrie 2014 22:54:38
Problema A+B Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.79 kb
// Fact.cpp : Defines the entry point for the console application.
//
#include <stdio.h>
#include <stdlib.h>

// Problema este formulata gresit. In cerinta se spune ca textul se afla DOAR
// pe prima linie (cu alte cuvinte, daca fisierul are mai multe linii, celelalte se ignora)
// Totusi, testele 2, 5, 6, 7, 8, 9 functioneaza doar daca se citeste TOT fisierul,
// indiferent cate linii are.

int main()
{
	int i = 0;
	int j = 0;
	int lungime = 0;
	bool AreLitere = false;
	int charcnt = 0;
	int wordcnt = 0, FileLength = 0;
	char *string = NULL;
	FILE *TextIn = NULL;
	FILE *TextOut = NULL;
	string = (char*)malloc(1024 * 1024 + 1);				//(string[i] <= 'a' && string[i] >= 'z') || (string[i] <= 'A' && string[i] >= 'Z')
	TextIn = fopen("text.in", "rb");							//Conditie Litera
	if (TextIn == NULL) {
		printf("Eroare deschidere fisier intrare");
		return 0;
	}
	TextOut = fopen("text.out", "wt");
//	fgets(string , 1024 * 1024 + 1, TextIn);
	// Get file length
	fseek(TextIn, 0, SEEK_END);
	FileLength = ftell(TextIn);
	fseek(TextIn, 0, SEEK_SET);
	fread(string, 1, FileLength, TextIn);
	string[FileLength] = 0;
	while (string[i] != 0) {
		if ((string[i] >= 'a' && string[i] <= 'z') || (string[i] >= 'A' && string[i] <= 'Z')) {
			charcnt++;
		}
		i++;
	}
	while (j < i) {
		AreLitere = 0;
		while (((string[j] >= 'a' && string[j] <= 'z') || (string[j] >= 'A' && string[j] <= 'Z')) && j < i) {
			j++;
			AreLitere = 1;
		}
		while (!((string[j] >= 'a' && string[j] <= 'z') || (string[j] >= 'A' && string[j] <= 'Z')) && j < i) {
			j++;
		}
		if ( AreLitere ) {
			wordcnt++;
		}
	}
	if (wordcnt == 0) {
		lungime = 0;
	} else {
		lungime = charcnt / wordcnt;
	}
	fprintf(TextOut, "%d", lungime);
	fclose(TextIn);
	fclose(TextOut);
	free(string);
	return 0;
}