School-Coding-Cpp/sfusi/es3_file.cpp

271 lines
4.9 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
Nome: _____ Mario
Cognome: __ Montanari
Classe: ___ 3AIN
Data: _____ 21/03/2025
Realizzare una funzione per ciascuna delle seguenti richieste:
- estrarre una linea a caso da un file
esempio "femminili.txt", “maschili.txt”, “verbi.txt”, “complementiOggetto.txt”;
- modificare il file verbi.txt mettendo in minuscolo la prima lettera di ogni riga;
- modificare il file “complOggNUM.txt” togliendo la parte numerica e lo spazio posti
allinizio di ogni riga esempio di riga: “71 un armadio” → “un armadio”;
- calcolare il soggetto più lungo fra:
- soggetti (maschili e femminili);
- verbi;
- complementi oggetto.
- comporre delle frasi casuali con la struttura soggetto + verbo + complementoOggetto.
*/
#include <iostream>
#include <cstdlib>
#include <cctype>
#include <ctime>
#define MAX 4
#define MIN 1
#define MAX_LEN 1000+1
using namespace std;
void estraiNomeCasuale(int n) {
switch(n) {
case 1: {
FILE *fp = fopen("femminili.txt", "rt");
if (fp != NULL) {
char line[MAX_LEN];
int righe = 0;
while (fgets(line, MAX_LEN, fp) != NULL) {
righe++;
}
int risultato = rand() % righe;
int riga = 0;
rewind(fp);
char string[MAX_LEN];
while (fgets(line, MAX_LEN, fp) != NULL) {
if (riga == risultato) {
strcpy(string, line);
break;
} else {
riga++;
}
}
fclose(fp);
cout << string << endl;
} else {
perror("Error (source)");
}
break;
}
case 2: {
FILE *fp = fopen("maschili.txt", "rt");
if (fp != NULL) {
char line[MAX_LEN];
int righe = 0;
while (fgets(line, MAX_LEN, fp) != NULL) {
righe++;
}
int risultato = rand() % righe;
int riga = 0;
rewind(fp);
char string[MAX_LEN];
while (fgets(line, MAX_LEN, fp) != NULL) {
if (riga == risultato) {
strcpy(string, line);
break;
} else {
riga++;
}
}
fclose(fp);
cout << string << endl;
} else {
perror("Error (source)");
}
break;
}
case 3: {
FILE *fp = fopen("verbi.txt", "rt");
if (fp != NULL) {
char line[MAX_LEN];
int righe = 0;
while (fgets(line, MAX_LEN, fp) != NULL) {
righe++;
}
int risultato = rand() % righe;
int riga = 0;
rewind(fp);
char string[MAX_LEN];
while (fgets(line, MAX_LEN, fp) != NULL) {
if (riga == risultato) {
strcpy(string, line);
break;
} else {
riga++;
}
}
fclose(fp);
cout << string << endl;
} else {
perror("Error (source)");
}
break;
}
case 4: {
FILE *fp = fopen("complementiOggettoN.txt", "rt");
if (fp != NULL) {
char line[MAX_LEN];
int righe = 0;
while (fgets(line, MAX_LEN, fp) != NULL) {
righe++;
}
int risultato = rand() % righe;
int riga = 0;
rewind(fp);
char string[MAX_LEN];
while (fgets(line, MAX_LEN, fp) != NULL) {
if (riga == risultato) {
strcpy(string, line);
break;
} else {
riga++;
}
}
fclose(fp);
cout << string << endl;
} else {
perror("Error (source)");
}
break;
}
default:
cout << "Numero non valido!" << endl;
return;
}
}
void makeLower(char *line) {
for (int i = 0; line[i] != '\0'; i++) {
line[i] = tolower(line[i]);
}
}
void rendiMinuscolo(char *nomeFile) {
FILE *fp = fopen(nomeFile, "r+t");
if (fp != NULL) {
char line[MAX_LEN];
long pos = ftell(fp);
while (fgets(line, MAX_LEN, fp) != NULL) {
fseek(fp, pos, SEEK_SET);
makeLower(line);
fputs(line, fp);
fseek(fp, 0, SEEK_CUR);
pos = ftell(fp);
cout << line;
}
fclose(fp);
} else {
perror("Error (source)");
}
}
void eliminaParteNumerica(char *nomeFile) {
FILE *fp = fopen(nomeFile, "r+t");
if (fp != NULL) {
char str[MAX_LEN];
while ((fgets(str, sizeof(str), fp)) != NULL) {
for (int i = 0; str[i] != '\0'; i++) {
if (isalpha(str[i]) || str[i] == ' ') {
cout << str[i];
}
}
cout << endl;
}
fclose(fp);
} else {
perror("Error (source)");
}
}
void calcolaLunghezza(char *nomeFile) {
}
void formaFrase(char *nomeFile) {
}
int main(void) {
srand(time(NULL));
int n = rand() % (MAX - MIN + 1) + MIN;
estraiNomeCasuale(n);
rendiMinuscolo("verbi.txt");
cout << endl << endl;
eliminaParteNumerica("complementiOggettoN.txt");
cout << endl << endl;
//calcolaLunghezza();
//cout << endl << endl;
//formaFrase();
//cout << endl;
return 0;
}