School-Coding-Cpp/sfusi/es10 - file.cpp

146 lines
3.0 KiB
C++

/*
Nome: Mario
Cognome: Montanari
Classe: 3AIN
Data: 02/04/2025
Dato il file “60000_parole_italiane.txt” contare:
- Il numero esatto di parole (1 parola per riga, una parola
può essere di un solo carattere)
- Le parole che contengono almeno una doppia consonante
- Le parole parivocaliche (es banana, muse)
- Le parole di lunghezza 6
- Le parole palindrome
- Visualizzare la 34567° parola
- Verificare che le parole siano scritte in ordine alfabetico
*/
#include <iostream>
#define SIZE_LINE 1000+1
using namespace std;
bool isVowel(char chr);
int contaParole(const char * nomeFile);
int contaDoppieConsonanti(const char * nomeFile);
//int contaParoleParivocaliche(const char * nomeFile);
int paroleLunghe6(const char * nomeFile);
//int leggiParola34567(const char * nomeFile);
int main(void) {
cout << "Parole presenti nel file: " << contaParole("60000_parole_italiane.txt") << endl;
cout << "Parole con doppie consonanti: " << contaDoppieConsonanti("60000_parole_italiane.txt") << endl;
cout << "Parole lunghe 6 caratteri: " << paroleLunghe6("60000_parole_italiane.txt") << endl;
//cout << "Parola numero 34567: " << leggiParola34567("60000_parole_italiane.txt") << endl;
return 0;
}
bool isVowel(char chr) {
return (strchr("aeiouAEIOU", chr));
}
int contaParole(const char * nomeFile) {
int countWord = 0;
FILE * fp = fopen(nomeFile, "rt");
if (fp != NULL) {
char chr;
while ((chr = fgetc(fp)) != EOF) {
if (chr == '\n') {
countWord++;
}
}
fclose(fp);
} else {
perror("Error");
}
return countWord;
}
int contaDoppieConsonanti(const char * nomeFile) {
int countDoubleConsonant = 0;
int countConsonant = 0;
FILE * fp = fopen(nomeFile, "rt");
if (fp != NULL) {
char str[SIZE_LINE];
while (fgets(str, sizeof(str), fp) != NULL) {
countConsonant = 0;
for (int i = 0; str[i] != '\0'; i++) {
if (str[i] == str[i + 1]) {
countConsonant++;
}
}
if (countConsonant != 0) {
countDoubleConsonant++;
}
}
fclose(fp);
} else {
perror("Error");
}
return countDoubleConsonant;
}
/*int contaParoleParivocaliche(const char * nomeFile) {
int parivocalicWord = 0;
FILE * fp = fopen(nomeFile, "rt");
if (fp != NULL) {
char str[SIZE_LINE];
while (fgets(str, sizeof(str), fp) != NULL) {
for (int i = 0; str[i] != '\0'; i++) {
if (isVowel(str[i]) && isalpha(str[i])) {
}
}
}
fclose(fp);
} else {
perror("Error");
}
}*/
int paroleLunghe6(const char * nomeFile) {
int wordLunghe6 = 0;
FILE * fp = fopen(nomeFile, "rt");
if (fp != NULL) {
char str[SIZE_LINE];
while (fgets(str, sizeof(str), fp) != NULL) {
if (str[strlen(str) - 1] == '\n') {
str[strlen(str) - 1] = '\0';
}
if (strlen(str) == 6) {
wordLunghe6++;
}
}
fclose(fp);
} else {
perror("Error");
}
return wordLunghe6;
}
/*int leggiParola34567(const char * nomeFile) {
}*/