Upload files to "sfusi"
This commit is contained in:
parent
58bb2b1b98
commit
d1efa75fc2
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,38 @@
|
|||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main(void) {
|
||||
int binario;
|
||||
int decimale = 0;
|
||||
int potenza = 1;
|
||||
|
||||
cout << "Inserisci un numero binario: ";
|
||||
cin >> binario;
|
||||
|
||||
int temp = binario;
|
||||
while (temp > 0) {
|
||||
int cifra = temp % 10;
|
||||
if (cifra != 0 && cifra != 1) {
|
||||
cout << "Numero non valido!" << endl;
|
||||
return 0;
|
||||
}
|
||||
temp = temp / 10;
|
||||
}
|
||||
|
||||
temp = binario;
|
||||
while (temp > 0) {
|
||||
decimale = decimale + (temp % 10) * potenza;
|
||||
temp = temp / 10;
|
||||
potenza = potenza * 2;
|
||||
}
|
||||
|
||||
if (decimale < 0 || decimale > 255) {
|
||||
cout << "Numero non valido!" << endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
cout << "Numero decimale: " << decimale << endl;
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,36 @@
|
|||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main(void) {
|
||||
int decimale;
|
||||
|
||||
cout << "Inserisci un numero decimale: ";
|
||||
cin >> decimale;
|
||||
|
||||
if (decimale < 0 || decimale > 255) {
|
||||
cout << "Numero decimale non valido!" << endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int binario[9] = {0};
|
||||
int i = 0;
|
||||
|
||||
while (decimale > 0 && i < 9) {
|
||||
binario[i++] = (decimale % 2);
|
||||
decimale = decimale / 2;
|
||||
}
|
||||
binario[i] = '\0';
|
||||
|
||||
cout << "Numero binario: ";
|
||||
for (int j = 0; j < 8 - i; j++) {
|
||||
cout << "0";
|
||||
}
|
||||
|
||||
for (int j = i - 1; j >= 0; j--) {
|
||||
cout << binario[j];
|
||||
}
|
||||
cout << endl;
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,38 @@
|
|||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main(void) {
|
||||
int decimale;
|
||||
|
||||
cout << "Inserisci un numero decimale: ";
|
||||
cin >> decimale;
|
||||
|
||||
if (decimale < 0 || decimale > 255) {
|
||||
cout << "Numero non valido!" << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
char esadecimale[3] = {0};
|
||||
int i = 0;
|
||||
|
||||
if (decimale == 0) {
|
||||
esadecimale[i++] = '0';
|
||||
}
|
||||
|
||||
while (decimale > 0) {
|
||||
int resto = decimale % 16;
|
||||
esadecimale[i++] = (resto < 10) ? ('0' + resto) : ('A' + resto - 10);
|
||||
decimale = decimale / 16;
|
||||
}
|
||||
esadecimale[i] = '\0';
|
||||
|
||||
cout << "Numero esadecimale: ";
|
||||
for (int j = i - 1; j >= 0; j--) {
|
||||
cout << esadecimale[j];
|
||||
}
|
||||
cout << endl;
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,37 @@
|
|||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main(void) {
|
||||
int decimale;
|
||||
|
||||
cout << "Inserisci un numero decimale: ";
|
||||
cin >> decimale;
|
||||
|
||||
if (decimale < 0 || decimale > 255) {
|
||||
cout << "Numero non valido!" << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
char ottale[4] = {0};
|
||||
int i = 0;
|
||||
|
||||
if (decimale == 0) {
|
||||
ottale[i++] = '0';
|
||||
}
|
||||
|
||||
while (decimale > 0) {
|
||||
ottale[i++] = '0' + (decimale % 8);
|
||||
decimale = decimale / 8;
|
||||
}
|
||||
ottale[i] = '\0';
|
||||
|
||||
cout << "Numero ottale: ";
|
||||
for (int j = i - 1; j >= 0; j--) {
|
||||
cout << ottale[j];
|
||||
}
|
||||
cout << endl;
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
// Esercizi vol2p141 (file binari)
|
||||
|
||||
// es01: Scrivi un programma che generi un file (utilizzare il generatore di numeri pseudocasuali) per la rappresentazione di un insieme di 90 numeri (<100 come nel gioco del lotto).
|
||||
|
||||
// es02: Scrivi un programma che utilizzi il file dell'esercizio precedente per leggere i numeri del lotto e che, data una coppia di numeri, verifichi se questa corrisponde ad un ambo.
|
||||
|
||||
// es03: Scrivi una funzione che preso in ingresso il nome di un file contenente interi, lo modifichi in modo da contenere gli stessi interi ma in ordine decrescente.
|
||||
|
||||
// es04: Scrivi una funzione che preso in ingresso il nome di due file produca un nuovo file ottenuto dalla concatenazione dei due file.
|
||||
|
||||
// es05: Scrivi una funzione che preso in ingresso il nome di due file produca un nuovo file ottenuto dalla concatenazione dei due file escludendo però i numeri duplicati.
|
||||
|
||||
// es06: Scrivi una funzione che preso in ingresso il nome di due file produca un nuovo file ottenuto dalla intersezione dei due file, cioè inserendo solo i numeri in comune presenti nei due file.
|
||||
|
||||
// es07: Scrivi un programma che legga da un file "numeri.dat" di 10 righe che rappresenti un array di 10 elementi interi, con ogni elemento memorizzato in una riga, e li memorizzi in un array.
|
||||
|
||||
// es08: Scrivi un programma per salvare, giorno per giorno, su un file binario di nome "pioggia.dat" la quantità di mm di pioggia caduta.
|
||||
Successivamente leggi i dati registrati nel file "pioggia.dat" e calcola:
|
||||
- la quantità di pioggia mediamente caduta al giorno
|
||||
- la quantità massima di pioggia caduta in un giorno;
|
||||
- il periodo (numero di giorni consecutivi) più lungo di siccità.
|
||||
|
||||
// es09: Scrivi un programma che generi un numero random (compreso fra 100 e 200) di numeri interi e li salvi su un file formattato. Quindi vengano ripresi da una funzione che li ordini in due nuovi file, una volta in modo crescente e l'altra in modo decrescente.
|
||||
|
||||
// es10: In un programma realizza due funzioni, la prima delle quali crei un file in modalità formattata e inserisca nel file valori interi acquisiti da tastiera, fino ad un massimo di 30; la seconda che legga i dati dal file prodotto dalla esecuzione della prima funzione e verifichi che nel file contenente le medie sia presente un numero di valori pari al numero di esecuzioni della coppia di programmi.
|
||||
|
||||
// es11: Dopo aver completato la struttura dati studente, scrivi la funzione inserisciMedia() che legga un file binario contenente i dati di tutti gli studenti di una classe, privi della media dei voti riportati, e lo aggiorni inserendo nell'apposito campo la media suddetta. La funzione dovrà a sua volta far uso di un'ulteriore, appropriataa, funzione che calcoli la media.
|
||||
|
||||
// es12: Scrivi un programma che legga un file di testo "matrice.dat" contenente 3 righe, con 3 interi separati da uno spazio per ogni riga. Il programma deve leggere il file, creare una matrice 3x3, contenente i dati presenti in "matrice.dat" e stamparne la trasposta.
|
||||
|
||||
// es13: Il programma dovrà permettere all'utente di inserire una serie di rilevamenti geografici (latitudine e longitudine). L'inserimento di una latitudine pari a 100 indicherà la fine della fase di inserimento. L'elaboratore salverà i dati in un file binario. Successivamente l'utente inserirà una latitudine e l'elaboratore, cercando nel file, troverà e visualizzerà tutti i rilevamenti, caratterizzati da qual valore di latitudine. Infine l'elaboratore visualizzerà il numero di rilevamenti trovati.
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,240 @@
|
|||
Input/Output carattere per carattere
|
||||
|
||||
es01. (stampavideo1) Scrivere un programma che stampi a video il contenuto del file in.txt.
|
||||
|
||||
es02. (stampavideo2) Scrivere una funzione che riceva in ingresso il nome di un file e ne stampi a video il contenuto. Utilizzare la funzione per mostrare il contenuto dei file in.txt e in2.txt.
|
||||
|
||||
es03. (contacaratteri) Scrivere una funzione che, ricevuto in ingresso il nome di un file, restituisca il conteggio dei caratteri presenti al suo interno oppure -1 nel caso in cui non sia possibile accedere al file. Utilizzare la funzione per mostrare il numero di caratteri dei file in.txt, in2.txt e nonesiste.txt (si supponga che quest'ultimo file non sia presente nel disco), emettendo un apposito messaggio in presenza di errori di apertura dei file.
|
||||
|
||||
es04. (contaparole) Un file di testo contiene un certo numero di parole. Ogni parola (a eccezione dell'ultima) e` separata da quella successiva da un solo carattere di whitespace. Scrivere un programma che, richiesta l'immissione da tastiera del nome del file da elaborare, visualizzi il conteggio delle parole contenute (suggerimento: contare i whitespace...).
|
||||
|
||||
es05. (copiafile1) Scrivere una funzione che, ricevuti in ingresso i nomi di due file, effettui la copia carattere per carattere del primo file nel secondo file. La funzione restituisce true se la copia si e` conclusa con successo, false in caso contrario. Utilizzare la funzione per copiare il file in.txt nel file out.txt.
|
||||
|
||||
es06. (copiafile2) Scrivere un programma che effettui la copia un file di testo in un altro file trasformando le lettere minuscole in maiuscole e le maiuscole in minuscole.
|
||||
|
||||
es07. (copialettere) Scrivere un programma che, richiesto da tastiera il nome di un file testo, memorizzi i caratteri dell'alfabeto nel file lettere.txt e tutti gli altri caratteri in altri_car.txt.
|
||||
|
||||
es08. (accodaparole) Dopo aver creato due file di testo parole1.txt e parole2.txt, ciascuno contenente un elenco di parole separate da uno spazio, scrivere un programma che accodi il contenuto del primo file al secondo file. Alla fine dell'esecuzione, osservare il contenuto del secondo file con un editor testuale.
|
||||
|
||||
|
||||
|
||||
Input/Output per linee di testo
|
||||
|
||||
es09. (inverti) Scrivere un programma che inverta ogni riga contenuta nel file righe.txt e riporti il risultato sullo schermo (per esempio, la riga Prova di stampa diventa apmats id avorP).
|
||||
|
||||
es10. (filtralinee) Scrivere una funzione che accetti come parametri d'ingresso due nomi di file e una parola P e memorizzi nel secondo file le sole righe del primo che contengono P (suggerimento: usare la funzione strstr).
|
||||
|
||||
es11. (accodalinee) Scrivere un programma che, chiesto in input il nome di un file di testo, accodi le linee del file al file di output risultato.txt, sostituendo le linee più lunghe di 20 caratteri con la stringa --LINEA TROPPO LUNGA--.
|
||||
|
||||
es12. (contalinee) Scrivere una funzione che accetti due parametri (un nome di file e una stringa S) e restituisca il conteggio delle righe contenenti S oppure -1 nel caso di errori nella gestione del
|
||||
file. Dopo aver creato un file di testo, utilizzare la funzione per contare le linee del file che contengono la parola BIANCO.
|
||||
|
||||
es13. (paginaweb) Scrivere un programma che generi automaticamente una pagina web di nome test.html avente il seguente contenuto testuale:
|
||||
<!doctype html>
|
||||
<html lang="it">
|
||||
<head>
|
||||
<meta charset='UTF-8'>
|
||||
<title>Pagina generata automaticamente</title>
|
||||
</head>
|
||||
<body>
|
||||
<p style='color: red;'>
|
||||
Questa pagina è stata generata automaticamente
|
||||
da un programma scritto in linguaggio C.
|
||||
</p>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
|
||||
|
||||
Input/Output formattato
|
||||
|
||||
es14. (numeri) Scrivere un programma che memorizzi in un file di testo e su una sola linea i primi 15 numeri pari separati da uno spazio.
|
||||
|
||||
es15. (multipli) Scrivere una funzione a due parametri interi A e N che memorizzi nel file di testo output.txt i primi N multipli di A (un solo multiplo per ogni riga).
|
||||
|
||||
es16. (casuali) Scrivere una funzione che, ricevuti in ingresso un nome di file e un numero intero N, memorizzi nel file N numeri casuali disposti a coppie su linee diverse (una coppia per ogni linea).
|
||||
|
||||
es17. (statistica) Scrivere un programma che, a partire da uno dei file prodotti nell'esercizio precedente, determini, per ogni coppia, una statistica formata dalla somma e dalla media dei due valori, riportando i risultati nel file statistica.txt (una statistica per ogni linea; all'interno della linea, separare la somma dalla media con un tabulatore).
|
||||
|
||||
es18. (occorrenze) Scrivere una funzione che, ricevuti in ingresso un nome di file e una parola S, restituisca il numero di occorrenze di S all'interno del file oppure -1 in caso di errore nella gestione del file. La funzione opera sotto le seguenti condizioni: una parola è una sequenza di caratteri priva di whitespace; un file è composto da una sequenza di parole separate da whitespace; la ricerca delle occorrenze è di tipo case insensitive.
|
||||
|
||||
es19. (maxmin) Scrivere un programma che calcoli il massimo e il minimo di una sequenza di valori interi contenuti in un file numeri.txt e memorizzi i risultati in maxmin.txt su due righe diverse nel seguente formato:
|
||||
Valore minimo: ....
|
||||
Valore massimo: ....
|
||||
Si ipotizzi di non conoscere a priori ne` il numero esatto di valori contenuti nel file ne` il numero massimo di valori previsto per questo esercizio.
|
||||
|
||||
es20. (voti1) Un file di testo voti.txt contiene i voti di un certo numero di studenti secondo il seguente formato:
|
||||
<cognome> <nome> <N> <voto1> <voto2> ... <votoN>
|
||||
Poiche` ogni studente puo` avere un numero diverso di voti, ogni riga include (dopo il nome) il numero N di voti registrati per quello studente, seguito da un elenco di N voti (approssimati a una cifra decimale). Un possibile esempio di file e` il seguente:
|
||||
Bianchi Paolo 3 7.0 7.5 6.0
|
||||
Rossi Maria 4 8.5 8.0 8.0 9.0
|
||||
Scrivere un programma che acquisisca i voti degli studenti e produca in output:
|
||||
a) Il file medie.txt contenente le medie (approssimate a due cifre decimali) dei voti di ciascuno studente, nel formato:
|
||||
<cognome> <nome> <media>
|
||||
b) Il file esiti.txt contenente l'esito di fine anno scolastico di ogni studente: "debito" se la media e` inferiore a 6, "promosso" in caso contrario. Il formato del file e` <cognome> <nome> <esito>
|
||||
|
||||
es21. (misurecampo) Si vuole realizzare un programma che calcoli il perimetro e l'area di un terreno poligonale.
|
||||
Utilizzando un ricevitore GPS si determinano innanzitutto le coordinate x,y degli N vertici del poligono. I dati sono successivamente memorizzati in un file di testo secondo il seguente formato:
|
||||
u_m
|
||||
N
|
||||
x0 y0
|
||||
x1 y1
|
||||
.
|
||||
xN-1 yN-1
|
||||
La prima riga del file contiene una stringa rappresentante l'unita` di misura in cui sono espresse le coordinate; la seconda riga contiene un intero N che indica il numero dei vertici del poligono; le successive N righe contengono le coordinate reali dei vertici (una coppia di coordinate per ogni riga).
|
||||
Il programma deve svolgere le seguenti operazioni:
|
||||
a) chiedere all'utente il nome del file di input e acquisirne l'intero contenuto, memorizzando le coordinate in un'apposita struttura dati;
|
||||
b) determinare il perimetro e l'area del terreno utilizzando le seguenti formule:
|
||||
perimetro: P = somma{k =0->N-1} (x[(i+1)mod N]-x[i])^2 + (y[(i+1)mod N]-y[i])^2 (somma dei lati)
|
||||
area: A = 1/2*|somma{k=0,N-1} (x[i]*y[(i+1) mod N] + x[(i+1) mod N]*y[i])| (formula di Gauss)
|
||||
c) chiedere all'utente il nome di un file di ouput e memorizzare al suo interno i risultati ottenuti (valore e unita` di misura di perimetro e area).
|
||||
Per esempio, a partire dal seguente file di input:
|
||||
dam
|
||||
6
|
||||
-0.57 -1.05
|
||||
4.05 8.19
|
||||
15.60 10.50
|
||||
24.84 3.57
|
||||
20.22 -1.05
|
||||
8.67 -3.36
|
||||
il programma deve riportare in un nuovo file i messaggi:
|
||||
Perimetro: 61.50 dam
|
||||
Area: 229.45 dam^2
|
||||
|
||||
es22. (scrivitab) Scrivere un programma che legga da tastiera e memorizzi in un'apposita struttura dati la seguente tabella:
|
||||
+-----------------+-----------------+-------------+-------------+
|
||||
| Cognome | Nome | Altezza (m) | Peso (kg) |
|
||||
+-----------------+-----------------+-------------+-------------+
|
||||
| Rossi | Mario | 1.75 | 76 |
|
||||
| Ferraro | Carlo | 1.84 | 82 |
|
||||
| Marelli | Chiara | 1.65 | 58 |
|
||||
+-----------------+-----------------+-------------+-------------+
|
||||
Terminato il caricamento, il programma deve memorizzare l'intera tabella nel file di testo tabella.txt (un record per linea, campi separati con un tabulatore; non deve essere memorizzata la riga d'intestazione). Si ipotizzi che tutti i campi testuali siano privi di spazi, che le altezze siano numeri float e i pesi numeri interi.
|
||||
|
||||
es23. (mostratab) Scrivere un programma che acquisisca da un file di testo la tabella dell'esercizio precedente e la visualizzi sullo schermo nel seguente formato:
|
||||
+-----------------+-----------------+-------------+-------------+
|
||||
| Cognome | Nome | Altezza (m) | Peso (kg) |
|
||||
+-----------------+-----------------+-------------+-------------+
|
||||
| Rossi | Mario | 1.75 | 76 |
|
||||
| Ferraro | Carlo | 1.84 | 82 |
|
||||
| Marelli | Chiara | 1.65 | 58 |
|
||||
+-----------------+-----------------+-------------+-------------+
|
||||
|
||||
|
||||
|
||||
Esercizi extra (file di testo)
|
||||
// es91: Scrivere un programma che legge da file di testo una sequenza di parole e scrive un nuovo file sostituendo ad alcune parole (presenti in una lista) i loro contrari. Es: odio->amore, guerra->pace, ecc.
|
||||
void uno(char c) {
|
||||
static char parola[50+1] = "";
|
||||
static char oldc = ' ';
|
||||
static unsigned i = 0;
|
||||
|
||||
if (isspace(c))
|
||||
if (isspace(oldc)) {
|
||||
// due spazi consecutivi
|
||||
printf("%c", c);
|
||||
else {
|
||||
// e` appena terminata una parola; se appartiene ad una lista, sostituiscila
|
||||
parola[i++] = '\0';
|
||||
if (strcmp(parola, "odio"))
|
||||
strcpy(parola, "amore");
|
||||
else if (strcmp(parola, "guerra"))
|
||||
strcpy(parola, "pace");
|
||||
printf("%s", parola);
|
||||
i = 0;
|
||||
}
|
||||
}
|
||||
else
|
||||
// accodo la lettera in una parola
|
||||
parola[i++] = c;
|
||||
oldc = c;
|
||||
}
|
||||
|
||||
// es92: Scrivere un programma che legge da file di testo una stringa e un carattere e verifica se il carattere e` presente nella stringa. Il risultato viene scritto su un nuovo file: se il carattere e` presente scrive "true", altrimenti scrive "false"
|
||||
bool presente(char str[], char c) {
|
||||
// soluzione1
|
||||
bool result = false;
|
||||
for (unsigned int j = 0; j < strlen(str); j++) {
|
||||
if (str[j]==c) {
|
||||
result = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
|
||||
// soluzione2
|
||||
return (strchr(str, c) != NULL);
|
||||
}
|
||||
|
||||
|
||||
// es93: Scrivere un programma che legge da file di testo una stringa e la riscrive su un nuovo file dopo averla ruotata a SX per num volte.
|
||||
void nshift(char stringa[], unsigned int num) {
|
||||
for (unsigned int j = 0; j < num; j++) {
|
||||
char temp = stringa[0];
|
||||
for (unsigned int i = 0; i < strlen(stringa)-1; i++)
|
||||
stringa[i] = stringa[i+1];
|
||||
stringa[strlen(stringa)-1] = temp;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
Soluzione impropria (con fread/fwrite) dell'es05(copiafile1).
|
||||
E' evidente che il file non viene copiato "carattere per carattere" ma "1000 caratteri per 1000 caratteri"
|
||||
*/
|
||||
bool es05(const char * filenamein, const char * filenameout) {
|
||||
FILE * fi = fopen(filenamein, "r");
|
||||
if (fi != NULL) {
|
||||
FILE * fo = fopen(filenameout, "w");
|
||||
if (fo != NULL) {
|
||||
char buffer[MAXBUFFER]; // non e` una stringa, ma una sequenza di bytes...
|
||||
int nr, nw;
|
||||
while ((nr = fread(buffer, sizeof(buffer[0]), MAXBUFFER, fi)) > 0) {
|
||||
nw = fwrite(buffer, sizeof(buffer[0]), nr, fo);
|
||||
if (nw < nr)
|
||||
return false;
|
||||
}
|
||||
if (fclose(fo)<0)
|
||||
return false;
|
||||
}
|
||||
if (fclose(fi)<0)
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/*
|
||||
Soluzione impropria (con fread/fwrite) dell'es10(filtralinee).
|
||||
Contiene una apertura multipla del file di input, l'utilizzo di un buffer come stringa, e altra brutta roba
|
||||
*/
|
||||
void es10(const char * filenamein, const char * filenameout) {
|
||||
char parola[] = "ciao";
|
||||
|
||||
FILE * fi = fopen(filenamein, "r");
|
||||
if (fi != NULL) {
|
||||
FILE * ft = fopen(filenamein, "r"); // ad esclusivo uso della fread
|
||||
int n = 0;
|
||||
int c;
|
||||
while((c=fgetc(fi))!=EOF) {
|
||||
n++;
|
||||
if (c=='\n'){
|
||||
char frase[n+1];
|
||||
int nr = fread(frase, sizeof(frase[0]), n, ft);
|
||||
frase[nr] = '\0';
|
||||
|
||||
if (nr < n)
|
||||
// TODO: gestire errore
|
||||
;
|
||||
|
||||
if (strstr(frase, parola)!=NULL) {
|
||||
// TODO: gestire errori
|
||||
FILE * fo = fopen(filenameout, "a");
|
||||
fwrite(frase, sizeof(frase[0]), nr, fo);
|
||||
fclose(fo);
|
||||
}
|
||||
n = 0;
|
||||
}
|
||||
}
|
||||
fclose(ft);
|
||||
fclose(fi);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,55 @@
|
|||
/*
|
||||
Nome: Mario
|
||||
Cognome: Montanari
|
||||
Classe: 3AIN
|
||||
Data: 07/04/2025
|
||||
|
||||
File di Testo CSV
|
||||
con l'uso di strtok()
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
#include <cstring>
|
||||
|
||||
#define SIZE_LINE 1000+1
|
||||
|
||||
using namespace std;
|
||||
|
||||
void apriFile(const char * nomeFile);
|
||||
|
||||
int main(void) {
|
||||
apriFile("File CSV.txt");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void apriFile(const char * nomeFile) {
|
||||
FILE * fp = fopen(nomeFile, "rt");
|
||||
|
||||
if (fp != NULL) {
|
||||
char *ptr;
|
||||
char str[SIZE_LINE];
|
||||
char nome[SIZE_LINE];
|
||||
char cognome[SIZE_LINE];
|
||||
int i = 0;
|
||||
|
||||
while (fgets(str, sizeof(str), fp) != NULL) {
|
||||
ptr = strtok(str, " ");
|
||||
strcpy(nome, ptr);
|
||||
cout << nome << " ";
|
||||
|
||||
ptr = strtok(NULL, ";");
|
||||
strcpy(cognome, ptr);
|
||||
cout << cognome << " ";
|
||||
|
||||
ptr = strtok(NULL, ";");
|
||||
cout << atoi(ptr) << endl;
|
||||
|
||||
i++;
|
||||
}
|
||||
|
||||
fclose(fp);
|
||||
} else {
|
||||
perror("Error (soure)");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,42 @@
|
|||
/*
|
||||
Nome: Mario
|
||||
Cognome: Montanari
|
||||
Classe: 3AIN
|
||||
Data: 07/04/2025
|
||||
|
||||
File di Testo CSV
|
||||
con l'uso di fscanf()
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
|
||||
#define SIZE_LINE 1000+1
|
||||
|
||||
using namespace std;
|
||||
|
||||
void apriFile(const char * nomeFile);
|
||||
|
||||
int main(void) {
|
||||
apriFile("File CSV.txt");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void apriFile(const char * nomeFile) {
|
||||
FILE * fp = fopen(nomeFile, "rt");
|
||||
|
||||
if (fp != NULL) {
|
||||
char nome[SIZE_LINE];
|
||||
char cognome[SIZE_LINE];
|
||||
int eta;
|
||||
int i = 0;
|
||||
|
||||
while (fscanf(fp, "%[^ ] %[^;];%d", nome, cognome, &eta) == 3) {
|
||||
cout << nome << " " << cognome << " " << eta;
|
||||
}
|
||||
|
||||
fclose(fp);
|
||||
} else {
|
||||
perror("Error (source)");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,53 @@
|
|||
/*
|
||||
Nome: Mario
|
||||
Cognome: Montanari
|
||||
Classe: 3AIN
|
||||
Data: 05/04/2025
|
||||
|
||||
File di Testo a Campi Fissi
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
|
||||
#define SIZE_LINE 1000+1
|
||||
|
||||
using namespace std;
|
||||
|
||||
void apriFile(const char * nomeFile);
|
||||
|
||||
int main(void) {
|
||||
apriFile("File Campi Fissi.txt");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void apriFile(const char * nomeFile) {
|
||||
FILE * fp = fopen(nomeFile, "rt");
|
||||
|
||||
if (fp != NULL) {
|
||||
char str[SIZE_LINE];
|
||||
char nome[SIZE_LINE];
|
||||
char cognome[SIZE_LINE];
|
||||
char eta[SIZE_LINE];
|
||||
int i = 0;
|
||||
|
||||
while (fgets(str, sizeof(str), fp) != NULL) {
|
||||
strncpy(nome, str, 15);
|
||||
nome[15] = '\0';
|
||||
|
||||
strncpy(cognome, str + 15, 20);
|
||||
cognome[20] = '\0';
|
||||
|
||||
strncpy(eta, str + 35, 2);
|
||||
eta[2] = '\0';
|
||||
|
||||
cout << nome << cognome << eta << endl;
|
||||
|
||||
i++;
|
||||
}
|
||||
|
||||
fclose(fp);
|
||||
} else {
|
||||
perror("Error (source)");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,6 @@
|
|||
Mario Montanari; 17
|
||||
Manuel Zlatanov; 17
|
||||
Matteo Maruccia; 17
|
||||
Luigi Scifo; 18
|
||||
Alex Venturini; 17
|
||||
Enrico Fracasso; 18
|
|
@ -0,0 +1,6 @@
|
|||
Mario Montanari 17
|
||||
Manuel Zlatanov 17
|
||||
Matteo Maruccia 17
|
||||
Luigi Scifo 18
|
||||
Alex Venturini 17
|
||||
Enrico Fracasso 18
|
|
@ -0,0 +1,44 @@
|
|||
#include <iostream>
|
||||
#include <cstring>
|
||||
#include <cctype>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main(void) {
|
||||
char esadecimale[3];
|
||||
|
||||
cout << "Inserisci un numero esadecimale: ";
|
||||
cin >> esadecimale;
|
||||
|
||||
int decimale = 0;
|
||||
int potenza = 1;
|
||||
int lunghezza = strlen(esadecimale);
|
||||
|
||||
for (int i = lunghezza - 1; i >= 0; i--) {
|
||||
char c = toupper(esadecimale[i]);
|
||||
int valore;
|
||||
|
||||
if (c >= '0' && c <= '9') {
|
||||
valore = c - '0';
|
||||
} else if (c >= 'A' && c <= 'F') {
|
||||
valore = 10 + (c - 'A');
|
||||
} else {
|
||||
cout << "Numero esadecimale non valido!" << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
decimale = decimale + (valore * potenza);
|
||||
potenza = potenza * 16;
|
||||
}
|
||||
|
||||
if (decimale < 0 || decimale > 255) {
|
||||
cout << "Numero esadecimale non valido!" << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
cout << "Numero decimale: " << decimale << endl;
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,132 @@
|
|||
/*
|
||||
Nome: Mario
|
||||
Cognome: Montanari
|
||||
Classe: 3AIN
|
||||
Data: 31/01/2025
|
||||
|
||||
Testo della Prova
|
||||
|
||||
Prova di Laboratorio - Programmazione in C++ Durata: 80 minuti
|
||||
|
||||
Obiettivo: Scrivere le dichiarazioni delle funzioni utilizzate nel programma, implementate correttamente nel file <tuo_cognome>.cpp.
|
||||
|
||||
Requisiti:
|
||||
|
||||
1. Non modificare il main.
|
||||
|
||||
2. Completare il programma dichiarando e implementando le seguenti funzioni:
|
||||
○ funzione0a: Riempie un array con numeri interi casuali pari.
|
||||
○ funzione1a: Conta quanti numeri dell'array sono multipli di 7 e quanti sono maggiori di 30, restituendo il risultato in una Struct1a.
|
||||
○ funzione2a: Conta quanti elementi di un array di Struct2ab soddisfano il criterio:
|
||||
■ il campo car è una vocale e il campo num è maggiore di 5.
|
||||
|
||||
3. Non aggiungere altre funzioni o modificare il flusso del programma.
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
#include <cstdlib>
|
||||
#include <ctime>
|
||||
|
||||
#define NUM_MIN 0
|
||||
#define NUM_MAX 100
|
||||
|
||||
using namespace std;
|
||||
|
||||
// Struct per la funzione 1a
|
||||
typedef struct {
|
||||
int nMulti7;
|
||||
int nMagg30;
|
||||
} Struct1a;
|
||||
|
||||
// Struct per le funzioni 2a
|
||||
typedef struct {
|
||||
char car;
|
||||
int num;
|
||||
} Struct2ab;
|
||||
|
||||
// Dichiarazione delle funzioni da implementare
|
||||
void funzione0a(int arr[], int size);
|
||||
Struct1a funzione1a(int arr[], int size);
|
||||
int funzione2a(Struct2ab arr[], int size);
|
||||
|
||||
// Funzioni già implementate per stampa
|
||||
void stampaArray(int arr[], int size) {
|
||||
for (int i = 0; i < size; ++i) {
|
||||
cout << arr[i] << " ";
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
|
||||
void stampaArrayStruct2ab(Struct2ab arr[], int size) {
|
||||
for (int i = 0; i < size; ++i) {
|
||||
cout << "{" << arr[i].car << ", " << arr[i].num << "} ";
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
|
||||
int main() {
|
||||
srand(time(0)); // Inizializza il generatore di numeri casuali
|
||||
|
||||
// Funzione 0a
|
||||
int arr[10];
|
||||
funzione0a(arr, 10);
|
||||
cout << "Array riempito con numeri pari casuali:" << endl;
|
||||
stampaArray(arr, 10);
|
||||
|
||||
// Funzione 1a
|
||||
Struct1a result1a = funzione1a(arr, 10);
|
||||
cout << "\nNumero di multipli di 7: " << result1a.nMulti7 << endl;
|
||||
cout << "Numero di valori > 30: " << result1a.nMagg30 << endl;
|
||||
|
||||
// Funzione 2a
|
||||
Struct2ab structArr[5] = {{'a', 6}, {'b', 4}, {'e', 7}, {'o', 5}, {'u', 3}};
|
||||
cout << "\nArray di struct (Struct2ab):" << endl;
|
||||
stampaArrayStruct2ab(structArr, 5);
|
||||
|
||||
int countVocaliMag5 = funzione2a(structArr, 5);
|
||||
cout << "\nElementi con vocale e num > 5: " << countVocaliMag5 << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Riempie un array con numeri interi casuali pari
|
||||
void funzione0a(int arr[], int size) {
|
||||
for (int i = 0; i < size; i++) {
|
||||
arr[i] = rand() % (NUM_MAX - NUM_MIN + 1) + NUM_MIN;
|
||||
for (int j = 0; j < size; j++) {
|
||||
if (arr[i] % 2 != 0) {
|
||||
arr[i] = rand() % (NUM_MAX - NUM_MIN + 1) + NUM_MIN;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Conta quanti numeri dell'array sono multipli di 7 e quanti sono maggiori di 30, restituendo il risultato in una Struct1a
|
||||
Struct1a funzione1a(int arr[], int size) {
|
||||
Struct1a result1a;
|
||||
result1a.nMulti7 = 0;
|
||||
result1a.nMagg30 = 0;
|
||||
|
||||
int countMagg30 = 0;
|
||||
for (int i = 0; i < size; i++) {
|
||||
if (arr[i] % 7 == 0) {
|
||||
result1a.nMulti7++;
|
||||
}
|
||||
if (arr[i] > 30) {
|
||||
result1a.nMagg30++;
|
||||
}
|
||||
}
|
||||
|
||||
return result1a;
|
||||
}
|
||||
|
||||
// Conta quanti elementi di un array di Struct2ab soddisfano il criterio
|
||||
int funzione2a(Struct2ab arr[], int size) {
|
||||
int countVocaliMag5 = 0;
|
||||
for (int i = 0; i < size; i++) {
|
||||
if ((arr[i].car == 'a' || arr[i].car == 'e' || arr[i].car == 'i' || arr[i].car == 'o' || arr[i].car == 'u') && (arr[i].num > 5)) {
|
||||
countVocaliMag5++;
|
||||
}
|
||||
}
|
||||
return countVocaliMag5;
|
||||
}
|
Binary file not shown.
|
@ -0,0 +1,37 @@
|
|||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main(void) {
|
||||
int ottale;
|
||||
|
||||
cout << "Inserisci un numero in base ottale: ";
|
||||
cin >> ottale;
|
||||
|
||||
int decimale = 0;
|
||||
int base = 1;
|
||||
|
||||
while (ottale > 0) {
|
||||
int cifra = ottale % 10;
|
||||
|
||||
if (cifra < 0 || cifra > 7) {
|
||||
cout << "Numero ottale non valido!" << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
decimale = decimale + (cifra * base);
|
||||
base = base * 8;
|
||||
ottale = ottale / 10;
|
||||
}
|
||||
|
||||
if (decimale < 0 || decimale > 255) {
|
||||
cout << "Numero ottale non valido!" << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
cout << "Numero decimale: " << decimale << endl;
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,103 @@
|
|||
ARRAY:
|
||||
- Struttura omogenea (contiene elementi dello stesso tipo) e finita (ha una dimensione definita)
|
||||
|
||||
STRUCT:
|
||||
- Struttura non omogenea (può contenere elementi di tipi diversi) e finita (ha una dimensione definita)
|
||||
|
||||
FILE:
|
||||
- Struttura omogenea (inteso come una sequenza di byte o strutture dello stesso tipo) e (virtualmente) infinita (la dimensione può crescere nel tempo fino ai limiti del file)
|
||||
|
||||
|
||||
|
||||
File di Testo:
|
||||
- Contengono sequenze di caratteri (stringhe)
|
||||
- Sono facilmente leggibili (contengono lettere, numeri e segni di punteggiatura)
|
||||
- Possono essere letti e modificati facilmente con un editor
|
||||
- Occupano molto spazio su disco
|
||||
- Possono essere di tipo .html, .xml, .xsl, .txt, etc.
|
||||
|
||||
File Binari:
|
||||
- Contengono sequenze di byte o di strutture
|
||||
- Non è possibile saltare in una posizione specifica (vengono quindi lette tutte le righe del file)
|
||||
- Non sono leggibili direttamente dall’uomo
|
||||
- Possono essere di tipo .doc, .xls, .ppt, .bin, .dat, .exe, etc.
|
||||
|
||||
File BMP
|
||||
- Sono File Binari che memorizzano immagini raster
|
||||
- Contengono una testata con le informazioni sull’immagine (dimensioni, profondità di colore, etc.)
|
||||
- I dati dell’immagine descrivono ogni pixel (posizione e colore)
|
||||
- Occupano molto spazio su disco non essendo compressi (o comunque vengono compressi in modo semplice)
|
||||
- Vengono utilizzati per immagini ad alta qualità e fedeltà visiva
|
||||
- L’estensione tipica è .bmp
|
||||
|
||||
La testa dei File BMP è composta da:
|
||||
- BITMAPFILEHEADER
|
||||
o È la prima struttura del File BMP
|
||||
o Contiene informazioni generali come il tipo del file (char bfType[2]), la dimensione totale del file (int bfSize), l’offset dove iniziano i dati dell’immagine (pixel) (int bfOffBits), e contiene anche due byte riservati, ovvero che sono nulli (int bfReserved)
|
||||
- BITMAPINFOHEADER
|
||||
o Viene subito dopo la prima testata
|
||||
o Specifica i dettagli come la larghezza e l’altezza dell’immagine, il numero di bit per pixel, il metodo di compressione, la dimensione dei dati dell’immagine, la risoluzione e la palette dei colori usata dall’immagine
|
||||
|
||||
|
||||
|
||||
FILE * fopen(const char * filename, char * mode);
|
||||
filename: nome del file
|
||||
mode: r (sola lettura del file; il file deve esistere)
|
||||
w (sola scrittura del file; se il file non esiste lo crea, se esiste lo svuota)
|
||||
a (scrive nel file a partire dalla fine; se il file non esiste lo crea)
|
||||
r+ (legge/aggiorna il file; posso aprire il file in lettura, e nel caso modificarlo)
|
||||
w+ (scrive/aggiorna il file; se il file esiste non lo svuota)
|
||||
a+ (scrive a partire dalla fine/aggiorna il file)
|
||||
|
||||
mode può concludersi specificando il tipo di file su cui si sta lavorando:
|
||||
- t (text): specifica che il file su cui si sta lavorando è un File di Testo (se non viene specificato è di default)
|
||||
- b (binary): specifica che il file su cui si sta lavorando è un File Binario
|
||||
|
||||
int fclose(FILE * file);
|
||||
- Non si può fare la fclose() di un puntatore nullo (restituisce 0 oppure -1 se in errore)
|
||||
|
||||
int fputc(char ch, FILE * fp);
|
||||
- Scrittura del carattere chr su fp (file di testo)
|
||||
|
||||
char * fgets(char * str, int size, FILE * fp);
|
||||
- Lettura di una stringa str da fp (file di testo)
|
||||
|
||||
int fputs(char * str, FILE * fp);
|
||||
- Scrittura di una stringa str su fp (file di testo)
|
||||
|
||||
int fscanf(FILE * fp, const char * format, ...);
|
||||
- Lettura formattata da fp (file di testo), restituisce il numero di elementi letti
|
||||
|
||||
int fprintf(FILE * fp, const char * format, ...);
|
||||
- Scrittura formattata su fp (file di testo), restituisce il numero di elementi scritti
|
||||
|
||||
void rewind(FILE * fp);
|
||||
- Riposizionamento all'inizio (azzeramento posizione testina di lettura/scrittura), ignorata in caso di file aperto
|
||||
|
||||
int fflush(FILE * fp);
|
||||
- Forza la scrittura dei dati bufferizzati sullo stream. Se fp vale NULL, forza tutti i buffer aperti in scrittura. Restituisce 0/EOF (ma normalmente non si chiama mai)
|
||||
|
||||
int rename(char * old, char * new);
|
||||
- Rinomina un file
|
||||
|
||||
int remove(char * filename);
|
||||
- Cancella il file filename (restituisce 0 se l'operazione è riuscita)
|
||||
|
||||
void perror(char * str);
|
||||
- Stampa il messaggio con la descrizione dell'errore dell'ultimo errore che c'è stato
|
||||
|
||||
size_t fread(void * ptr, size_t size, size_t count, FILE * fp);
|
||||
- Leggi su un File Binario
|
||||
|
||||
size_t fwrite(const void * ptr, size_t size, size_t count, FILE * fp);
|
||||
- Scrive su un File Binario
|
||||
|
||||
int fseek(FILE * fp, long offset, int origin);
|
||||
origin:
|
||||
o SEEK_SET (sposta la posizione del puntatore all’inizio del file)
|
||||
o SEEK_CUR (sposta la posizione del puntatore alla posizione corrente)
|
||||
o SEEK_END (sposta la posizione del puntatore alla fine del file)
|
||||
offset è il numero di byte da spostare rispetto a origin
|
||||
|
||||
long ftell(FILE * fp);
|
||||
- Restituisce la posizione corrente del puntatore di lettura/scrittura in un file. Può essere usata per determinare la dimensione del file o per verificare la posizione nel file durante la lettura/scrittura.
|
Binary file not shown.
|
@ -0,0 +1,39 @@
|
|||
/*
|
||||
Nome: Mario
|
||||
Cognome: Montanari
|
||||
Classe: 3AIN
|
||||
Data: 20/03/2024
|
||||
|
||||
Scrivere una funzione che, data una stringa,
|
||||
ne elimini le lettere specficate, accorciandola
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
char * delChr(char * str, char chr);
|
||||
|
||||
int main(void) {
|
||||
char str[] = "Ciao mamma, come stai?";
|
||||
char chr = 'a';
|
||||
|
||||
cout << str << endl;
|
||||
cout << delChr(str, chr) << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
char * delChr(char * str, char chr) {
|
||||
int j = 0;
|
||||
|
||||
for (int i = 0; str[i] != '\0'; i++) {
|
||||
if (str[i] != chr) {
|
||||
str[j++] = str[i];
|
||||
}
|
||||
}
|
||||
|
||||
str[j] = '\0';
|
||||
|
||||
return str;
|
||||
}
|
|
@ -0,0 +1,27 @@
|
|||
/*
|
||||
Nome: Mario
|
||||
Cognome: Montanari
|
||||
Classe: 3AIN
|
||||
Data: 10/03/2025
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main(void) {
|
||||
int a;
|
||||
int p;
|
||||
|
||||
cout << "a = ";
|
||||
cin >> a;
|
||||
|
||||
cout << "p = ";
|
||||
cin >> p;
|
||||
|
||||
int additivo = p - a;
|
||||
|
||||
cout << "Additivo inverso: " << additivo << endl;
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1 @@
|
|||
, 0 ? .
|
|
@ -0,0 +1,55 @@
|
|||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
int ANDLogico(int a, int b); // PROTOTIPO DELLA FUNZIONE DELL'AND LOGICO
|
||||
void stampaTabellaANDLogico(int (*f)(int, int)); // PROTOTIPO DELLA FUNZIONE PER STAMPARE LA TABELLA DELL'AND LOGICO
|
||||
|
||||
int ORLogico(int a, int b); // PROTOTIPO DELLA FUNZIONE DELL'OR LOGICO
|
||||
void stampaTabellaORLogico(int (*f)(int, int)); // PROTOTIPO DELLA FUNZIONE PER STAMPARE LA TABELLA DELL'OR LOGICO
|
||||
|
||||
int main() {
|
||||
stampaTabellaANDLogico(ANDLogico); // STAMPA DELLA TABELLA DELL'AND LOGICO
|
||||
|
||||
stampaTabellaORLogico(ORLogico); // STAMPA DELLA TABELLA DELL'OR LOGICO
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
int ANDLogico(int a, int b) {
|
||||
return a && b; //
|
||||
}
|
||||
|
||||
// FUNZIONE PER STAMPARE LA TABELLA DELL'AND LOGICO
|
||||
void stampaTabellaANDLogico(int (*f)(int, int)) {
|
||||
cout << "A\tB\tRisultato" << endl;
|
||||
cout << "-------------------" << endl;
|
||||
|
||||
for (int a = 0; a <= 1; ++a) {
|
||||
for (int b = 0; b <= 1; ++b) {
|
||||
int risultato = f(a, b); // Calcola il risultato della funzione logica
|
||||
cout << a << "\t" << b << "\t" << risultato << endl;
|
||||
}
|
||||
}
|
||||
|
||||
cout << endl << endl;
|
||||
}
|
||||
|
||||
int ORLogico(int a, int b) {
|
||||
return a || b; //
|
||||
}
|
||||
|
||||
// FUNZIONE PER STAMPARE LA TABELLA DELLA VERITA DELL'OR LOGICO
|
||||
void stampaTabellaORLogico(int (*f)(int, int)) {
|
||||
cout << "A\tB\tRisultato" << endl;
|
||||
cout << "-------------------" << endl;
|
||||
|
||||
for (int a = 0; a <= 1; ++a) {
|
||||
for (int b = 0; b <= 1; ++b) {
|
||||
int risultato = f(a, b); // Calcola il risultato della funzione logica
|
||||
cout << a << "\t" << b << "\t" << risultato << endl;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,52 @@
|
|||
#include <iostream>
|
||||
#include <iomanip>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main() {
|
||||
|
||||
// AND Logico
|
||||
cout << "A" << " | " << "B" << " | " << "A AND B" << endl;
|
||||
cout << "----------------" << endl;
|
||||
for (int A = 0; A <= 1; ++A) {
|
||||
for (int B = 0; B <= 1; ++B) {
|
||||
int result = A && B;
|
||||
cout << A << " | " << B << " | " << result << endl;
|
||||
}
|
||||
}
|
||||
|
||||
cout << endl;
|
||||
|
||||
// OR Logico
|
||||
cout << "A" << " | " << "B" << " | " << "A OR B" << endl;
|
||||
cout << "----------------" << endl;
|
||||
for (int A = 0; A <= 1; ++A) {
|
||||
for (int B = 0; B <= 1; ++B) {
|
||||
int result = A || B;
|
||||
cout << A << " | " << B << " | " << result << endl;
|
||||
}
|
||||
}
|
||||
|
||||
cout << endl;
|
||||
|
||||
// XOR Logico
|
||||
cout << "A" << " | " << "B" << " | " << "A XOR B" << endl;
|
||||
cout << "----------------" << endl;
|
||||
for (int A = 0; A <= 1; ++A) {
|
||||
for (int B = 0; B <= 1; ++B) {
|
||||
int result = A != B;
|
||||
cout << A << " | " << B << " | " << result << endl;
|
||||
}
|
||||
}
|
||||
|
||||
cout << endl;
|
||||
|
||||
cout << "A" << " | " << "NOT A" << endl;
|
||||
cout << "----------" << endl;
|
||||
for (int A = 0; A <= 1; ++A) {
|
||||
int result = !A;
|
||||
cout << A << " | " << result << endl;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,63 @@
|
|||
/*
|
||||
Cognome, Nome: Montanari, Mario
|
||||
|
||||
PROGRAMMA CHE, DATO UN ARRAY CONTENENTE 10 VALORI GENERATI CASUALMENTE, IMPOSTA A ZERO GLI ELEMENTI DISPARI DELL'ARRAY.
|
||||
*/
|
||||
|
||||
#include <iostream> // LIBRERIA NECESSARIA PER L'INPUT E L'OUTPUT STANDARD
|
||||
#include <array> // LIBRERIA NECESSARIA PER LAVORARE CON GLI ARRAY
|
||||
#include <cstdlib> // LIBRERIA NECESSARIA PER LAVORARE CON LA FUNZIONE 'rand'
|
||||
#include <ctime> // LIBRERIA NECESSARIA PER LAVORARE CON LA FUNZIONE 'time'
|
||||
|
||||
#define CAPACITY 10 // DIMENSIONE MASSIMA DELL'ARRAY
|
||||
#define SIZE 10 // NUMERO DI CELLE OCCUPATE NELL'ARRAY
|
||||
#define MIN 1 // VALORE MINIMO PER I NUMERI GENERATI CASUALMENTE
|
||||
#define MAX 100 // VALORE MASSIMO PER I NUMERI GENERATI CASUALMENTE
|
||||
|
||||
using namespace std;
|
||||
|
||||
void riempiArray(int arr[], int size); // PROTOTIPO DELLA FUNZIONE CHE RIEMPIE L'ARRAY INIZIALE CON NUMERI CASUALI
|
||||
void stampaArray(int arr[], int size); // PROTOTIPO DELLA FUNZIONE CHE STAMPA GLI ELEMENTI PRESENTI NELL'ARRAY
|
||||
void azzeraElementiDispari(int arr[], int size); // PROTOTIPO DELLA FUNZIONE CHE RESETTA GLI ELEMENTI DISPARI DELL'ARRAY
|
||||
|
||||
int main(void) {
|
||||
int arr[CAPACITY]; // DICHIARAZZIONE DELL'ARRAY 'arr[]' CON CAPACITA' MASSIMA DI 10
|
||||
int size = SIZE; // INIZIALIZZAZIONE DELLA VARIABILE 'size' CHE SPECIFICA IL NUMERO DI ELEMENTI PRESENTI NELL'ARRAY
|
||||
|
||||
riempiArray(arr, size); // CHIAMATA DELLA FUNZIONE CHE RIEMPIE L'ARRAY INIZIALE CON NUMERI CASUALI
|
||||
|
||||
cout << "Initial array: ";
|
||||
stampaArray(arr, size); // CHIAMATA DELLA FUNZIONE CHE STAMPA GLI ELEMENTI PRESENTI NELL'ARRAY
|
||||
|
||||
azzeraElementiDispari(arr, size); // CHIAMATA DELLA FUNZIONE CHE RESETTA GLI ELEMENTI DISPARI DELL'ARRAY
|
||||
|
||||
cout << "Final array: ";
|
||||
stampaArray(arr, size); // CHIAMATA DELLA FUNZIONE CHE STAMPA GLI ELEMENTI PRESENTI NELL'ARRAY
|
||||
|
||||
return 0; // TERMINA IL PROGRAMMA
|
||||
}
|
||||
|
||||
// DICHIARAZIONE DELLA FUNZIONE CHE RIEMPIE L'ARRAY INIZIALE CON NUMERI CASUALI
|
||||
void riempiArray(int arr[], int size) {
|
||||
srand(time(NULL)); // INIZIALIZZA IL GENERATORE DI NUMERI CASUALI
|
||||
for (int i = 0; i < size; i++) { // ISTRUZIONE CHE CICLA ATTRAVERSO TUTTE LE CELLE DELL'ARRAY
|
||||
arr[i] = rand() % (MAX - MIN + 1) + MIN; // GENERA UN NUMERO CASUALE TRA 'MIN' E 'MAX' E LO ASSEGNA ALLA CELLA CORRENTE
|
||||
}
|
||||
}
|
||||
|
||||
// DICHIARAZIONE DELLA FUNZIONE CHE STAMPA GLI ELEMENTI PRESENTI NELL'ARRAY
|
||||
void stampaArray(int arr[], int size) {
|
||||
for (int i = 0; i < size; i++) { // ISTRUZIONE CHE CICLA ATTRAVERSO TUTTI GLI ELEMENTI PRESENTI NELL'ARRAY
|
||||
cout << arr[i] << " "; // STAMPA OGNI ELEMENTO SEGUITO DA UNO SPAZIO
|
||||
}
|
||||
cout << endl; // AGGIUNGE UN A CAPO DOPO LA STAMPA DI TUTTI GLI ELEMENTI
|
||||
}
|
||||
|
||||
// DICHIARAZIONE DELLA FUNZIONE CHE RESETTA GLI ELEMENTI DISPARI DELL'ARRAY
|
||||
void azzeraElementiDispari(int arr[], int size) {
|
||||
for (int i = 0; i < size; i++) { // ISTRUZIONE CHE CICLA ATTRAVERSO TUTTI GLI ELEMENTI PRESENTI NELL'ARRAY
|
||||
if (arr[i] % 2 != 0) { // ISTRUZIONE CHE VERIFICA SE L'ELEMENTO CORRENTE E' DISPARI
|
||||
arr[i] = 0; // SE L'ELEMENTO E' DISPARI LO IMPOSTA A ZERO
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,63 @@
|
|||
/*
|
||||
Nome: Mario
|
||||
Cognome: Montanari
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
#include <ctime>
|
||||
#include <cstdlib>
|
||||
|
||||
#define SIZE 10
|
||||
#define MASSIMO 9
|
||||
#define MINIMO 0
|
||||
|
||||
using namespace std;
|
||||
|
||||
typedef struct {
|
||||
int arr[SIZE];
|
||||
} array;
|
||||
|
||||
array riempiArray(int arr[SIZE], int size);
|
||||
void stampaArray(int arr[SIZE], int size);
|
||||
void azzeraPari(int arr[SIZE], int size);
|
||||
|
||||
int main(void) {
|
||||
int arr[SIZE];
|
||||
int size = SIZE;
|
||||
|
||||
array numArr = riempiArray(numArr.arr, size);
|
||||
|
||||
riempiArray(numArr.arr, size);
|
||||
stampaArray(numArr.arr, size);
|
||||
|
||||
cout << endl;
|
||||
|
||||
azzeraPari(numArr.arr, size);
|
||||
|
||||
stampaArray(numArr.arr, size);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
array riempiArray(int arr[SIZE], int size) {
|
||||
srand(time(NULL));
|
||||
array numArr;
|
||||
for (int i = 0; i < size; i++) {
|
||||
numArr.arr[i] = rand() % (MASSIMO - MINIMO + 1) + MINIMO;
|
||||
}
|
||||
return numArr;
|
||||
}
|
||||
|
||||
void stampaArray(int arr[SIZE], int size) {
|
||||
for (int i = 0; i < size; i++) {
|
||||
cout << arr[i] << " ";
|
||||
}
|
||||
}
|
||||
|
||||
void azzeraPari(int arr[], int size) {
|
||||
for (int i = 0; i < size; i++) {
|
||||
if (arr[i] % 2 == 0) {
|
||||
arr[i] = 0;
|
||||
}
|
||||
}
|
||||
}
|
Binary file not shown.
|
@ -0,0 +1,3 @@
|
|||
WHITE BIANCO BLANCO?
|
||||
Se WHITE allora non BLANCO ma forse BIANCO, ma se BIANCO è BLANCO?
|
||||
e se bianco cosa fai?
|
|
@ -0,0 +1,39 @@
|
|||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
typedef struct {
|
||||
char cognome[100+1];
|
||||
char nome[100+1];
|
||||
int anno;
|
||||
} Structure;
|
||||
|
||||
int main(void) {
|
||||
const char * fileNameIn = "fileIn.bin";
|
||||
const char * fileNameOut = "fileOut.txt";
|
||||
|
||||
FILE * fileIn = fopen(fileNameIn, "rb");
|
||||
|
||||
if (fileIn != NULL) {
|
||||
FILE * fileOut = fopen(fileNameOut, "wt");
|
||||
|
||||
if (fileOut != NULL) {
|
||||
Structure structure;
|
||||
|
||||
while (fread(&structure, sizeof(structure), 1, fileIn) == 1) {
|
||||
fprintf(fileOut, "%s\t%s\t%d", structure.cognome, structure.nome, structure.anno);
|
||||
cout << structure.cognome << " " << structure.nome << " " << structure.anno;
|
||||
}
|
||||
|
||||
fclose(fileOut);
|
||||
} else {
|
||||
perror("Error (destination)");
|
||||
}
|
||||
|
||||
fclose(fileIn);
|
||||
} else {
|
||||
perror("Error (source)");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main(void) {
|
||||
int x = 5;
|
||||
int i = 1;
|
||||
|
||||
x = x & (x - 1);
|
||||
|
||||
cout << x;
|
||||
|
||||
return 0;
|
||||
}
|
Binary file not shown.
After Width: | Height: | Size: 8.4 KiB |
|
@ -0,0 +1,100 @@
|
|||
1 un libro
|
||||
2 la penna
|
||||
3 una macchina
|
||||
4 il computer
|
||||
5 una sedia
|
||||
6 l'orologio
|
||||
7 un telefono
|
||||
8 la tavola
|
||||
9 una porta
|
||||
10 il quaderno
|
||||
11 una lampada
|
||||
12 il cellulare
|
||||
13 un bicchiere
|
||||
14 la scrivania
|
||||
15 una pianta
|
||||
16 il tavolo
|
||||
17 un foglio
|
||||
18 la finestra
|
||||
19 una tazza
|
||||
20 il televisore
|
||||
21 una borsa
|
||||
22 l'armadio
|
||||
23 un cuscino
|
||||
24 la lavagna
|
||||
25 una matita
|
||||
26 il divano
|
||||
27 un frigorifero
|
||||
28 la coperta
|
||||
29 un quadro
|
||||
30 il tappeto
|
||||
31 una poltrona
|
||||
32 il frullatore
|
||||
33 una forchetta
|
||||
34 il tostapane
|
||||
35 una tenda
|
||||
36 il forno
|
||||
37 un coltello
|
||||
38 la lampadina
|
||||
39 un piatto
|
||||
40 il termosifone
|
||||
41 una tovaglia
|
||||
42 il phon
|
||||
43 un orologio
|
||||
44 la spazzatura
|
||||
45 un'accendino
|
||||
46 il lavandino
|
||||
47 una candela
|
||||
48 il termometro
|
||||
49 una sveglia
|
||||
50 il tappo
|
||||
51 una presa
|
||||
52 il monitor
|
||||
53 un telecomando
|
||||
54 la lavatrice
|
||||
55 un martello
|
||||
56 il cartello
|
||||
57 una bici
|
||||
58 il giornale
|
||||
59 un portachiavi
|
||||
60 la borsa
|
||||
61 un'auto
|
||||
62 il telefono
|
||||
63 un ombrello
|
||||
64 la carta
|
||||
65 un fazzoletto
|
||||
66 il rubinetto
|
||||
67 un asciugamano
|
||||
68 il cestino
|
||||
69 una cassetta
|
||||
70 il gatto
|
||||
71 un armadio
|
||||
72 la spugna
|
||||
73 un cuscino
|
||||
74 il chiavistello
|
||||
75 un lenzuolo
|
||||
76 la busta
|
||||
77 un secchio
|
||||
78 il pennarello
|
||||
79 una lavagna
|
||||
80 il cuscino
|
||||
81 una catena
|
||||
82 il coprirete
|
||||
83 una cassetta
|
||||
84 il battiscopa
|
||||
85 un orecchino
|
||||
86 la sedia
|
||||
87 una spazzola
|
||||
88 il guanto
|
||||
89 un libro
|
||||
90 la forbice
|
||||
91 un gancio
|
||||
92 il tappetino
|
||||
93 un rossetto
|
||||
94 la sedia
|
||||
95 un profumo
|
||||
96 la vasca
|
||||
97 un piumino
|
||||
98 il vaso
|
||||
99 una tazza
|
||||
100 il bicchiere
|
Binary file not shown.
After Width: | Height: | Size: 625 KiB |
Binary file not shown.
After Width: | Height: | Size: 2.5 MiB |
Binary file not shown.
After Width: | Height: | Size: 2.5 MiB |
|
@ -0,0 +1,63 @@
|
|||
/*
|
||||
Cognome, Nome: Montanari, Mario
|
||||
|
||||
PROGRAMMA CHE, DATO UN ARRAY CONTENENTE 10 VALORI GENERATI CASUALMENTE, CONTA LE COPPIE CONSECUTIVE PRESENTI ALL'INTERNO DELL'ARRAY.
|
||||
*/
|
||||
|
||||
#include <iostream> // LIBRERIA NECESSARIA PER L'INPUT E L'OUTPUT STANDARD
|
||||
#include <array> // LIBRERIA NECESSARIA PER LAVORARE CON GLI ARRAY
|
||||
#include <cstdlib> // LIBRERIA NECESSARIA PER LAVORARE CON LA FUNZIONE 'rand'
|
||||
#include <ctime> // LIBRERIA NECESSARIA PER LAVORARE CON LA FUNZIONE 'time'
|
||||
|
||||
#define CAPACITY 10 // DIMENSIONE MASSIMA DELL'ARRAY
|
||||
#define SIZE 10 // NUMERO DI CELLE OCCUPATE NELL'ARRAY
|
||||
#define MIN 1 // VALORE MINIMO PER I NUMERI GENERATI CASUALMENTE
|
||||
#define MAX 2 // VALORE MASSIMO PER I NUMERI GENERATI CASUALMENTE
|
||||
|
||||
using namespace std;
|
||||
|
||||
void riempiArray(int arr[], int size); // PROTOTIPO DELLA FUNZIONE CHE RIEMPIE L'ARRAY INIZIALE CON NUMERI CASUALI
|
||||
void stampaArray(int arr[], int size); // PROTOTIPO DELLA FUNZIONE CHE STAMPA GLI ELEMENTI PRESENTI NELL'ARRAY
|
||||
void contaCoppieConsecutive(int arr[], int size); // PROTOTIPO DELLA FUNZIONE CHE CONTA LE COPPIE CONSECUTIVE PRESENTI ALL'INTERNO DELL'ARRAY
|
||||
|
||||
int main(void) {
|
||||
int arr[CAPACITY]; // DICHIARAZIONE DELL'ARRAY 'arr[]' CON CAPACITA' MASSIMA DI 10
|
||||
int size = SIZE; // INIZIALIZZAZIONE DELLA VARIABILE 'size' CHE SPECIFICA IL NUMERO DI ELEMENTI PRESENTI NELL'ARRAY
|
||||
|
||||
riempiArray(arr, size); // CHIAMATA DELLA FUNZIONE CHE RIEMPIE L'ARRAY INIZIALE CON NUMERI CASUALI
|
||||
|
||||
cout << "Initial array: ";
|
||||
stampaArray(arr, size); // CHIAMATA DELLA FUNZIONE CHE STAMPA GLI ELEMENTI PRESENTI NELL'ARRAY
|
||||
|
||||
cout << "Consecutive pairs: ";
|
||||
contaCoppieConsecutive(arr, size); // CHIAMATA DELLA FUNZIONE CHE CONTA LE COPPIE CONSECUTIVE PRESENTI ALL'INTERNO DELL'ARRAY
|
||||
|
||||
return 0; // TERMINA IL PROGRAMMA
|
||||
}
|
||||
|
||||
// DICHIARAZIONE DELLA FUNZIONE CHE RIEMPIE L'ARRAY INIZIALE CON NUMERI CASUALI
|
||||
void riempiArray(int arr[], int size) {
|
||||
srand(time(NULL)); // INIZIALIZZA IL GENERATORE DI NUMERI CASUALI
|
||||
for (int i = 0; i < size; i++) { // ISTRUZIONE CHE CICLA ATTRAVERSO TUTTE LE CELLE DELL'ARRAY
|
||||
arr[i] = rand() % (MAX - MIN + 1) + MIN; // GENERA UN NUMERO CASUALE TRA 'MIN' E 'MAX' E LO ASSEGNA ALLA CELLA CORRENTE
|
||||
}
|
||||
}
|
||||
|
||||
// DICHIARAZIONE DELLA FUNZIONE CHE STAMPA GLI ELEMENTI PRESENTI NELL'ARRAY
|
||||
void stampaArray(int arr[], int size) {
|
||||
for (int i = 0; i < size; i++) { // ISTRUZIONE CHE CICLA ATTRAVERSO TUTTI GLI ELEMENTI PRESENTI NELL'ARRAY
|
||||
cout << arr[i] << " "; // STAMPA OGNI ELEMENTO SEGUITO DA UNO SPAZIO
|
||||
}
|
||||
cout << endl; // AGGIUNGE UN A CAPO DOPO LA STAMPA DI TUTTI GLI ELEMENTI
|
||||
}
|
||||
|
||||
// DICHIARAZIONE DELLA FUNZIONE CHE CONTA LE COPPIE CONSECUTIVE PRESENTI ALL'INTERNO DELL'ARRAY
|
||||
void contaCoppieConsecutive(int arr[], int size) {
|
||||
int count = 0;
|
||||
for (int i = 1; i < size; i++) {
|
||||
if (arr[i-1] == arr[i]) { // SE DUE ELEMENTI CONSECUTIVI SONO UGUALI
|
||||
count++; // INCREMENTA IL CONTATORE
|
||||
}
|
||||
}
|
||||
cout << count << endl; // STAMPA IL NUMERO DI COPPIE CONSECUTIVE
|
||||
}
|
|
@ -0,0 +1,64 @@
|
|||
/*
|
||||
Cognome, Nome: Montanari, Mario
|
||||
|
||||
PROGRAMMA CHE, DATO UN ARRAY CONTENENTE 10 VALORI GENERATI CASUALMENTE, CONTA LE OCCORRENZE DEGLI ELEMENTI PRESENTI ALL'INTERNO DELL'ARRAY.
|
||||
*/
|
||||
|
||||
#include <iostream> // LIBRERIA NECESSARIA PER L'INPUT E L'OUTPUT STANDARD
|
||||
#include <array> // LIBRERIA NECESSARIA PER LAVORARE CON GLI ARRAY
|
||||
#include <cstdlib> // LIBRERIA NECESSARIA PER LAVORARE CON LA FUNZIONE 'rand'
|
||||
#include <ctime> // LIBRERIA NECESSARIA PER LAVORARE CON LA FUNZIONE 'time'
|
||||
|
||||
#define CAPACITY 10 // DIMENSIONE MASSIMA DELL'ARRAY
|
||||
#define SIZE 5 // NUMERO DI CELLE OCCUPATE NELL'ARRAY
|
||||
#define MIN 1 // VALORE MINIMO PER I NUMERI GENERATI CASUALMENTE
|
||||
#define MAX 5 // VALORE MASSIMO PER I NUMERI GENERATI CASUALMENTE
|
||||
|
||||
using namespace std;
|
||||
|
||||
void riempiArray(int arr[], int size); // PROTOTIPO DELLA FUNZIONE CHE RIEMPIE L'ARRAY INIZIALE CON NUMERI CASUALI
|
||||
void stampaArray(int arr[], int size); // PROTOTIPO DELLA FUNZIONE CHE STAMPA GLI ELEMENTI PRESENTI NELL'ARRAY
|
||||
void contaOccorrenzeElementi(int arr[], int size); // PROTOTIPO DELLA FUNZIONE CHE CONTA LE OCCORRENZE DEGLI ELEMENTI PRESENTI ALL'INTERNO DELL'ARRAY
|
||||
|
||||
int main(void) {
|
||||
int arr[CAPACITY]; // DICHIARAZIONE DELL'ARRAY 'arr[]' CON CAPACITA' MASSIMA DI 10
|
||||
int size = SIZE; // INIZIALIZZAZIONE DELLA VARIABILE 'size' CHE SPECIFICA IL NUMERO DI ELEMENTI PRESENTI NELL'ARRAY
|
||||
|
||||
riempiArray(arr, size); // CHIAMATA DELLA FUNZIONE CHE RIEMPIE L'ARRAY INIZIALE CON NUMERI CASUALI
|
||||
|
||||
cout << "Initial array: ";
|
||||
stampaArray(arr, size); // CHIAMATA DELLA FUNZIONE CHE STAMPA GLI ELEMENTI PRESENTI NELL'ARRAY
|
||||
|
||||
contaOccorrenzeElementi(arr, size); // CHIAMATA DELLA FUNZIONE CHE CONTA LE OCCORRENZE DEGLI ELEMENTI PRESENTI ALL'INTERNO DELL'ARRAY
|
||||
|
||||
return 0; // TERMINA IL PROGRAMMA
|
||||
}
|
||||
|
||||
// DICHIARAZIONE DELLA FUNZIONE CHE RIEMPIE L'ARRAY INIZIALE CON NUMERI CASUALI
|
||||
void riempiArray(int arr[], int size) {
|
||||
srand(time(NULL)); // INIZIALIZZA IL GENERATORE DI NUMERI CASUALI
|
||||
for (int i = 0; i < size; i++) { // ISTRUZIONE CHE CICLA ATTRAVERSO TUTTE LE CELLE DELL'ARRAY
|
||||
arr[i] = rand() % (MAX - MIN + 1) + MIN; // GENERA UN NUMERO CASUALE TRA 'MIN' E 'MAX' E LO ASSEGNA ALLA CELLA CORRENTE
|
||||
}
|
||||
}
|
||||
|
||||
// DICHIARAZIONE DELLA FUNZIONE CHE STAMPA GLI ELEMENTI PRESENTI NELL'ARRAY
|
||||
void stampaArray(int arr[], int size) {
|
||||
for (int i = 0; i < size; i++) { // ISTRUZIONE CHE CICLA ATTRAVERSO TUTTI GLI ELEMENTI PRESENTI NELL'ARRAY
|
||||
cout << arr[i] << " "; // STAMPA OGNI ELEMENTO SEGUITO DA UNO SPAZIO
|
||||
}
|
||||
cout << endl; // AGGIUNGE UN A CAPO DOPO LA STAMPA DI TUTTI GLI ELEMENTI
|
||||
}
|
||||
|
||||
// DICHIARAZIONE DELLA FUNZIONE CHE CONTA LE OCCORRENZE DEGLI ELEMENTI PRESENTI ALL'INTERNO DELL'ARRAY
|
||||
void contaOccorrenzeElementi(int arr[], int size) {
|
||||
for(int i = 0; i < size; i++) {
|
||||
int count = 0;
|
||||
for (int j = i; j < size; j++) {
|
||||
if (arr[j] == arr[i]) {
|
||||
count++;
|
||||
}
|
||||
}
|
||||
cout << "Il valore " << arr[i] << " compare " << count << " volte." << endl;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,66 @@
|
|||
/*
|
||||
Nome: Mario
|
||||
Cognome: Montanari
|
||||
Classe: 3AIN
|
||||
Data: 22/11/2024
|
||||
|
||||
Contare quante volte un determinato valore (tra 3 e 6 compesi) compare all'interno di un array.
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
#include <ctime>
|
||||
#define DIM 10
|
||||
#define VMIN 3
|
||||
#define VMAX 6
|
||||
|
||||
using namespace std;
|
||||
|
||||
void stampaArray(int arr[], int size);
|
||||
unsigned int riempiArray(int arr[], int size);
|
||||
unsigned int contaArray(int arr[], int size, int valore);
|
||||
|
||||
int main(void) {
|
||||
int arr[DIM];
|
||||
int valore;
|
||||
|
||||
riempiArray(arr, DIM);
|
||||
|
||||
cout << "Inserisci un valore da cercare (tra 3 e 6): ";
|
||||
cin >> valore;
|
||||
|
||||
cout << "Numero di occorrenze: " << contaArray(arr, DIM, valore) << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void stampaArray(int arr[], int size) {
|
||||
|
||||
for (int i = 0; i < DIM; i++) {
|
||||
cout << arr[i] << " ";
|
||||
}
|
||||
|
||||
cout << endl;
|
||||
}
|
||||
|
||||
unsigned int riempiArray(int arr[], int size) {
|
||||
|
||||
for (int i = 0; i < size; i++) {
|
||||
arr[i] = rand() % (VMAX - VMIN + 1) + VMIN;
|
||||
}
|
||||
|
||||
return size;
|
||||
}
|
||||
|
||||
unsigned int contaArray(int arr[], int size, int valore) {
|
||||
unsigned int contatore = 0;
|
||||
|
||||
for (int i = 0; i < size; i++) {
|
||||
|
||||
if (arr[i] == valore) {
|
||||
contatore = contatore + 1;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return contatore;
|
||||
}
|
|
@ -0,0 +1 @@
|
|||
parola che
|
|
@ -0,0 +1,2 @@
|
|||
2024-02-20T15:21:33.406Z In(05) vmx Log for VMware Workstation pid=17632 version=17.5.0 build=build-
|
||||
BY: Mario Montanari
|
|
@ -0,0 +1,32 @@
|
|||
#include <stdio.h>
|
||||
|
||||
int countChar(FILE * file);
|
||||
|
||||
int main(void) {
|
||||
FILE * file = fopen("file.txt", "rt");
|
||||
|
||||
if (file != NULL) {
|
||||
int countChr = 0;
|
||||
|
||||
countChr = countChar(file);
|
||||
|
||||
printf("Number of character: %d\n", countChr);
|
||||
|
||||
fclose(file);
|
||||
} else {
|
||||
perror("Error (source)");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int countChar(FILE * file) {
|
||||
int countChr = 0;
|
||||
int chr;
|
||||
|
||||
while ((chr = fgetc(file)) != EOF) {
|
||||
countChr++;
|
||||
}
|
||||
|
||||
return countChr;
|
||||
}
|
|
@ -0,0 +1,34 @@
|
|||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int countChar(FILE * file);
|
||||
|
||||
int main(void) {
|
||||
FILE * file = fopen("file.txt", "rt");
|
||||
|
||||
if (file != NULL) {
|
||||
int countChr = 0;
|
||||
|
||||
countChr = countChar(file);
|
||||
|
||||
cout << "Number of character: " << countChr << endl;
|
||||
|
||||
fclose(file);
|
||||
} else {
|
||||
perror("Error (source)");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int countChar(FILE * file) {
|
||||
int countChr = 0;
|
||||
int chr;
|
||||
|
||||
while ((chr = fgetc(file)) != EOF) {
|
||||
countChr++;
|
||||
}
|
||||
|
||||
return countChr;
|
||||
}
|
|
@ -0,0 +1,34 @@
|
|||
#include <stdio.h>
|
||||
|
||||
#define SIZE_LINE 1000+1
|
||||
|
||||
int countLine(FILE * file);
|
||||
|
||||
int main(void) {
|
||||
FILE * file = fopen("file.txt", "rt");
|
||||
|
||||
if (file != NULL) {
|
||||
int countRow = 0;
|
||||
|
||||
countRow = countLine(file);
|
||||
|
||||
printf("Number of character: %d\n", countRow);
|
||||
|
||||
fclose(file);
|
||||
} else {
|
||||
perror("Error (source)");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int countLine(FILE * file) {
|
||||
int countRow = 0;
|
||||
char line[SIZE_LINE];
|
||||
|
||||
while (fgets(line, sizeof(line), file) != NULL) {
|
||||
countRow++;
|
||||
}
|
||||
|
||||
return countRow;
|
||||
}
|
|
@ -0,0 +1,36 @@
|
|||
#include <iostream>
|
||||
|
||||
#define SIZE_LINE 1000+1
|
||||
|
||||
using namespace std;
|
||||
|
||||
int countLine(FILE * file);
|
||||
|
||||
int main(void) {
|
||||
FILE * file = fopen("file.txt", "rt");
|
||||
|
||||
if (file != NULL) {
|
||||
int countRow = 0;
|
||||
|
||||
countRow = countLine(file);
|
||||
|
||||
cout << "Number of character: " << countRow << endl;
|
||||
|
||||
fclose(file);
|
||||
} else {
|
||||
perror("Error (source)");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int countLine(FILE * file) {
|
||||
int countRow = 0;
|
||||
char line[SIZE_LINE];
|
||||
|
||||
while (fgets(line, sizeof(line), file) != NULL) {
|
||||
countRow++;
|
||||
}
|
||||
|
||||
return countRow;
|
||||
}
|
|
@ -0,0 +1,65 @@
|
|||
#include <stdio.h>
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
#include <ctype.h>
|
||||
|
||||
#define SIZE_LINE 1000+1
|
||||
|
||||
bool isAllLower(const char * str);
|
||||
int countLowerWord(const char * str);
|
||||
|
||||
int main(void) {
|
||||
FILE * file = fopen("parole.txt", "rt");
|
||||
|
||||
if (file != NULL) {
|
||||
char str[SIZE_LINE];
|
||||
int countWord = 0;
|
||||
|
||||
while (fgets(str, sizeof(str), file)) {
|
||||
countWord = countWord + countLowerWord(str);
|
||||
}
|
||||
|
||||
printf("Total all-lowercase words: %d\n", countWord);
|
||||
|
||||
fclose(file);
|
||||
} else {
|
||||
perror("Error (source)");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool isAllLower(const char * str) {
|
||||
int countAplha = 0;
|
||||
|
||||
if (strlen(str) == 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
for (int i = 0; str[i] != '\0'; i++) {
|
||||
if (isalpha(str[i])) {
|
||||
countAplha++;
|
||||
|
||||
if (isupper(str[i])) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
int countLowerWord(const char * str) {
|
||||
char buffer[SIZE_LINE];
|
||||
int countWord = 0;
|
||||
|
||||
strcpy(buffer, str);
|
||||
|
||||
for (char * pc = buffer; (pc = strtok(pc, " \n")) != NULL; pc = NULL) {
|
||||
if (isAllLower(pc)) {
|
||||
countWord++;
|
||||
}
|
||||
}
|
||||
|
||||
return countWord;
|
||||
}
|
|
@ -0,0 +1,66 @@
|
|||
#include <iostream>
|
||||
#include <cstring>
|
||||
#include <cctype>
|
||||
|
||||
#define SIZE_LINE 1000+1
|
||||
|
||||
using namespace std;
|
||||
|
||||
bool isAllLower(const char * str);
|
||||
int countLowerWord(const char * str);
|
||||
|
||||
int main(void) {
|
||||
FILE * file = fopen("parole.txt", "rt");
|
||||
|
||||
if (file != NULL) {
|
||||
char str[SIZE_LINE];
|
||||
int countWord = 0;
|
||||
|
||||
while (fgets(str, sizeof(str), file)) {
|
||||
countWord = countWord + countLowerWord(str);
|
||||
}
|
||||
|
||||
cout << "Total all-lowercase words: " << countWord << endl;
|
||||
|
||||
fclose(file);
|
||||
} else {
|
||||
perror("Error (source)");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool isAllLower(const char * str) {
|
||||
int countAplha = 0;
|
||||
|
||||
if (strlen(str) == 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
for (int i = 0; str[i] != '\0'; i++) {
|
||||
if (isalpha(str[i])) {
|
||||
countAplha++;
|
||||
|
||||
if (isupper(str[i])) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
int countLowerWord(const char * str) {
|
||||
char buffer[SIZE_LINE];
|
||||
int countWord = 0;
|
||||
|
||||
strcpy(buffer, str);
|
||||
|
||||
for (char * pc = buffer; (pc = strtok(pc, " \n")) != NULL; pc = NULL) {
|
||||
if (isAllLower(pc)) {
|
||||
countWord++;
|
||||
}
|
||||
}
|
||||
|
||||
return countWord;
|
||||
}
|
|
@ -0,0 +1,50 @@
|
|||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <ctype.h>
|
||||
|
||||
#define SIZE_LINE 1000+1
|
||||
|
||||
int countOccurrence(FILE * file, const char * word);
|
||||
|
||||
int main(void) {
|
||||
FILE * file = fopen("parole.txt", "rt");
|
||||
|
||||
if (file != NULL) {
|
||||
int countOcc = 0;
|
||||
|
||||
countOcc = countOccurrence(file, "l");
|
||||
|
||||
printf("Total occurrences: %d\n", countOcc);
|
||||
|
||||
fclose(file);
|
||||
} else {
|
||||
perror("Error (source)");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int countOccurrence(FILE * file, const char * word) {
|
||||
char str[SIZE_LINE];
|
||||
char lowerWord[SIZE_LINE];
|
||||
int countOcc = 0;
|
||||
|
||||
strncpy(lowerWord, word, SIZE_LINE - 1);
|
||||
lowerWord[SIZE_LINE - 1] = '\0';
|
||||
|
||||
for (int i = 0; lowerWord[i] != '\0'; i++) {
|
||||
lowerWord[i] = tolower(lowerWord[i]);
|
||||
}
|
||||
|
||||
while (fgets(str, sizeof(str), file) != NULL) {
|
||||
for (int i = 0; str[i] != '\0'; i++) {
|
||||
str[i] = tolower(str[i]);
|
||||
}
|
||||
|
||||
if (strstr(str, lowerWord) != NULL) {
|
||||
countOcc++;
|
||||
}
|
||||
}
|
||||
|
||||
return countOcc;
|
||||
}
|
|
@ -0,0 +1,52 @@
|
|||
#include <iostream>
|
||||
#include <cstring>
|
||||
#include <cctype>
|
||||
|
||||
#define SIZE_LINE 1000+1
|
||||
|
||||
using namespace std;
|
||||
|
||||
int countOccurrence(FILE * file, const char * word);
|
||||
|
||||
int main(void) {
|
||||
FILE * file = fopen("parole.txt", "rt");
|
||||
|
||||
if (file != NULL) {
|
||||
int countOcc = 0;
|
||||
|
||||
countOcc = countOccurrence(file, "l");
|
||||
|
||||
cout << "Total occurrences: " << countOcc << endl;
|
||||
|
||||
fclose(file);
|
||||
} else {
|
||||
perror("Error (source)");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int countOccurrence(FILE * file, const char * word) {
|
||||
char str[SIZE_LINE];
|
||||
char lowerWord[SIZE_LINE];
|
||||
int countOcc = 0;
|
||||
|
||||
strncpy(lowerWord, word, SIZE_LINE - 1);
|
||||
lowerWord[SIZE_LINE - 1] = '\0';
|
||||
|
||||
for (int i = 0; lowerWord[i] != '\0'; i++) {
|
||||
lowerWord[i] = tolower(lowerWord[i]);
|
||||
}
|
||||
|
||||
while (fgets(str, sizeof(str), file) != NULL) {
|
||||
for (int i = 0; str[i] != '\0'; i++) {
|
||||
str[i] = tolower(str[i]);
|
||||
}
|
||||
|
||||
if (strstr(str, lowerWord) != NULL) {
|
||||
countOcc++;
|
||||
}
|
||||
}
|
||||
|
||||
return countOcc;
|
||||
}
|
|
@ -0,0 +1,65 @@
|
|||
#include <stdio.h>
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
#include <ctype.h>
|
||||
|
||||
#define SIZE_LINE 1000+1
|
||||
|
||||
bool isAllUpper(const char * str);
|
||||
int countUpperWord(const char * str);
|
||||
|
||||
int main(void) {
|
||||
FILE * file = fopen("parole.txt", "rt");
|
||||
|
||||
if (file != NULL) {
|
||||
char str[SIZE_LINE];
|
||||
int countWord = 0;
|
||||
|
||||
while (fgets(str, sizeof(str), file)) {
|
||||
countWord = countWord + countUpperWord(str);
|
||||
}
|
||||
|
||||
printf("Total all-uppercase words: %d\n", countWord);
|
||||
|
||||
fclose(file);
|
||||
} else {
|
||||
perror("Error (source)");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool isAllUpper(const char * str) {
|
||||
int countAplha = 0;
|
||||
|
||||
if (strlen(str) == 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
for (int i = 0; str[i] != '\0'; i++) {
|
||||
if (isalpha(str[i])) {
|
||||
countAplha++;
|
||||
|
||||
if (!isupper(str[i])) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
int countUpperWord(const char * str) {
|
||||
char buffer[SIZE_LINE];
|
||||
int countWord = 0;
|
||||
|
||||
strcpy(buffer, str);
|
||||
|
||||
for (char * pc = buffer; (pc = strtok(pc, " \n")) != NULL; pc = NULL) {
|
||||
if (isAllUpper(pc)) {
|
||||
countWord++;
|
||||
}
|
||||
}
|
||||
|
||||
return countWord;
|
||||
}
|
|
@ -0,0 +1,66 @@
|
|||
#include <iostream>
|
||||
#include <cstring>
|
||||
#include <cctype>
|
||||
|
||||
#define SIZE_LINE 1000+1
|
||||
|
||||
using namespace std;
|
||||
|
||||
bool isAllUpper(const char * str);
|
||||
int countUpperWord(const char * str);
|
||||
|
||||
int main(void) {
|
||||
FILE * file = fopen("parole.txt", "rt");
|
||||
|
||||
if (file != NULL) {
|
||||
char str[SIZE_LINE];
|
||||
int countWord = 0;
|
||||
|
||||
while (fgets(str, sizeof(str), file)) {
|
||||
countWord = countWord + countUpperWord(str);
|
||||
}
|
||||
|
||||
cout << "Total all-uppercase words: " << countWord << endl;
|
||||
|
||||
fclose(file);
|
||||
} else {
|
||||
perror("Error (source)");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool isAllUpper(const char * str) {
|
||||
int countAplha = 0;
|
||||
|
||||
if (strlen(str) == 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
for (int i = 0; str[i] != '\0'; i++) {
|
||||
if (isalpha(str[i])) {
|
||||
countAplha++;
|
||||
|
||||
if (!isupper(str[i])) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
int countUpperWord(const char * str) {
|
||||
char buffer[SIZE_LINE];
|
||||
int countWord = 0;
|
||||
|
||||
strcpy(buffer, str);
|
||||
|
||||
for (char * pc = buffer; (pc = strtok(pc, " \n")) != NULL; pc = NULL) {
|
||||
if (isAllUpper(pc)) {
|
||||
countWord++;
|
||||
}
|
||||
}
|
||||
|
||||
return countWord;
|
||||
}
|
|
@ -0,0 +1,40 @@
|
|||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#define SIZE_LINE 1000+1
|
||||
|
||||
int countWord(const char * str);
|
||||
|
||||
int main(void) {
|
||||
FILE * file = fopen("parole.txt", "rt");
|
||||
|
||||
if (file != NULL) {
|
||||
char str[SIZE_LINE];
|
||||
int countWrd = 0;
|
||||
|
||||
while (fgets(str, sizeof(str), file)) {
|
||||
countWrd = countWrd + countWord(str);
|
||||
}
|
||||
|
||||
printf("Total words: %d\n", countWrd);
|
||||
|
||||
fclose(file);
|
||||
} else {
|
||||
perror("Error (source)");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int countWord(const char * str) {
|
||||
char buffer[SIZE_LINE];
|
||||
int countWrd = 0;
|
||||
|
||||
strcpy(buffer, str);
|
||||
|
||||
for (char * pc = buffer; (pc = strtok(pc, " \n")) != NULL; pc = NULL) {
|
||||
countWrd++;
|
||||
}
|
||||
|
||||
return countWrd;
|
||||
}
|
|
@ -0,0 +1,42 @@
|
|||
#include <iostream>
|
||||
#include <cstring>
|
||||
|
||||
#define SIZE_LINE 1000+1
|
||||
|
||||
using namespace std;
|
||||
|
||||
int countWord(const char * str);
|
||||
|
||||
int main(void) {
|
||||
FILE * file = fopen("parole.txt", "rt");
|
||||
|
||||
if (file != NULL) {
|
||||
char str[SIZE_LINE];
|
||||
int countWrd = 0;
|
||||
|
||||
while (fgets(str, sizeof(str), file)) {
|
||||
countWrd = countWrd + countWord(str);
|
||||
}
|
||||
|
||||
cout << "Total words: " << countWrd << endl;
|
||||
|
||||
fclose(file);
|
||||
} else {
|
||||
perror("Error (source)");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int countWord(const char * str) {
|
||||
char buffer[SIZE_LINE];
|
||||
int countWrd = 0;
|
||||
|
||||
strcpy(buffer, str);
|
||||
|
||||
for (char * pc = buffer; (pc = strtok(pc, " \n")) != NULL; pc = NULL) {
|
||||
countWrd++;
|
||||
}
|
||||
|
||||
return countWrd;
|
||||
}
|
Binary file not shown.
Binary file not shown.
After Width: | Height: | Size: 469 KiB |
|
@ -0,0 +1,55 @@
|
|||
/*
|
||||
Nome: Mario
|
||||
Cognome: Montanari
|
||||
Classe: 3AIN
|
||||
Data: 11/03/2025
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
#include <cctype>
|
||||
|
||||
#define SIZE 100+1
|
||||
|
||||
using namespace std;
|
||||
|
||||
char *decryptAffineCipher(char *const str, const int a, const int b);
|
||||
|
||||
int main(void) {
|
||||
char str[SIZE];
|
||||
int a;
|
||||
int b;
|
||||
|
||||
cout << "Inserisci una frase: ";
|
||||
cin.getline(str, SIZE);
|
||||
|
||||
cout << "a: ";
|
||||
cin >> a;
|
||||
|
||||
cout << "b: ";
|
||||
cin >> b;
|
||||
|
||||
cout << "Frase codificata: " << decryptAffineCipher(str, a, b) << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
char *decryptAffineCipher(char *const str, const int a, const int b) {
|
||||
int a_inv = 0;
|
||||
|
||||
for (int i = 0; i < 26; i++) {
|
||||
if ((a * i) % 26 == 1) {
|
||||
a_inv = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; str[i] != '\0'; i++) {
|
||||
if (isupper(str[i])) {
|
||||
str[i] = ((a_inv * ((str[i] - 'A') - b + 26)) % 26) + 'A';
|
||||
} else if (islower(str[i])) {
|
||||
str[i] = ((a_inv * ((str[i] - 'a') - b + 26)) % 26) + 'a';
|
||||
}
|
||||
}
|
||||
|
||||
return str;
|
||||
}
|
|
@ -0,0 +1,46 @@
|
|||
/*
|
||||
Nome: Mario
|
||||
Cognome: Montanari
|
||||
Classe: 3AIN
|
||||
Data: 08/03/2025
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
#include <cctype>
|
||||
|
||||
#define SIZE 100+1
|
||||
|
||||
using namespace std;
|
||||
|
||||
void decryptAtbashCipher(char *str);
|
||||
|
||||
int main(void) {
|
||||
char str[SIZE]; //ABCDEFGHIJKLMNOPQRSTUVWXYZ
|
||||
|
||||
cout << "Inserisci una frase: ";
|
||||
cin.getline(str, SIZE);
|
||||
|
||||
decryptAtbashCipher(str);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void decryptAtbashCipher(char *str) {
|
||||
int j = 0;
|
||||
|
||||
for (int i = 0; str[i] != '\0'; i++) {
|
||||
if (!isalpha(str[i])) {
|
||||
str[j++] = str[i];
|
||||
} else {
|
||||
if (isupper(str[i])) {
|
||||
str[j++] = 'Z' - (str[i] - 'A');
|
||||
} else if (islower(str[i])) {
|
||||
str[j++] = 'z' - (str[i] - 'a');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
str[j] = '\0';
|
||||
|
||||
cout << "Frase decodificata: " << str << endl;
|
||||
}
|
|
@ -0,0 +1,40 @@
|
|||
/*
|
||||
Nome: Mario
|
||||
Cognome: Montanari
|
||||
Classe: 3AIN
|
||||
Data: 08/03/2025
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
#include <cctype>
|
||||
|
||||
#define SIZE 100+1
|
||||
|
||||
using namespace std;
|
||||
|
||||
void decryptCaesarCipher(char *str);
|
||||
|
||||
int main(void) {
|
||||
char str[SIZE]; //DEFGHIJKLMNOPQRSTUVWXYZABC
|
||||
|
||||
cout << "Inserisci una frase: ";
|
||||
cin.getline(str, SIZE);
|
||||
|
||||
decryptCaesarCipher(str);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void decryptCaesarCipher(char *str) {
|
||||
for (int i = 0; str[i] != '\0'; i++) {
|
||||
if (isalpha(str[i])) {
|
||||
if (isupper(str[i])) {
|
||||
str[i] = ((str[i] - 'A' + 23) % 26) + 'A';
|
||||
} else if (islower(str[i])) {
|
||||
str[i] = ((str[i] - 'a' + 23) % 26) + 'a';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
cout << "Frase decodificata: " << str << endl;
|
||||
}
|
|
@ -0,0 +1,61 @@
|
|||
/*
|
||||
Nome: Mario
|
||||
Cognome: Montanari
|
||||
Classe: 3AIN
|
||||
Data: 08/03/2025
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
#include <cctype>
|
||||
|
||||
#define SIZE 100+1
|
||||
|
||||
using namespace std;
|
||||
|
||||
void decryptCarbonarCipher(char *str, const char *carbonarAlphabet);
|
||||
|
||||
int main(void) {
|
||||
const char carbonarAlphabet[] = "OPGTIVCHE RNMABQLZDUF S";
|
||||
char str[SIZE]; //ABCDEFGHI LMNOPQRSTUV Z
|
||||
|
||||
cout << "Inserisci una frase: ";
|
||||
cin.getline(str, SIZE);
|
||||
|
||||
decryptCarbonarCipher(str, carbonarAlphabet);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void decryptCarbonarCipher(char *str, const char *carbonarAlphabet) {
|
||||
char carbonarStr[SIZE];
|
||||
int j = 0;
|
||||
|
||||
for (int i = 0; str[i] != '\0'; i++) {
|
||||
if (
|
||||
str[i] == 'J' || str[i] == 'j' ||
|
||||
str[i] == 'K' || str[i] == 'k' ||
|
||||
str[i] == 'W' || str[i] == 'w' ||
|
||||
str[i] == 'X' || str[i] == 'x' ||
|
||||
str[i] == 'Y' || str[i] == 'y'
|
||||
) {
|
||||
cout << "Frase non valida!" << endl;
|
||||
return;
|
||||
}
|
||||
|
||||
if (!isalpha(str[i])) {
|
||||
carbonarStr[j++] = str[i];
|
||||
} else {
|
||||
if (isupper(str[i])) {
|
||||
int r = str[i] - 'A';
|
||||
carbonarStr[j++] = toupper(carbonarAlphabet[r]);
|
||||
} else if (islower(str[i])) {
|
||||
int r = str[i] - 'a';
|
||||
carbonarStr[j++] = tolower(carbonarAlphabet[r]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
carbonarStr[j] = '\0';
|
||||
|
||||
cout << "Frase decodificata: " << carbonarStr << endl;;
|
||||
}
|
|
@ -0,0 +1,40 @@
|
|||
/*
|
||||
Nome: Mario
|
||||
Cognome: Montanari
|
||||
Classe: 3AIN
|
||||
Data: 08/03/2025
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
#include <cctype>
|
||||
|
||||
#define SIZE 100+1
|
||||
|
||||
using namespace std;
|
||||
|
||||
void decryptRot13Cipher(char *str);
|
||||
|
||||
int main(void) {
|
||||
char str[SIZE]; //NOPQRSTUVWXYZABCDEFGHIJKLM
|
||||
|
||||
cout << "Inserisci una frase: ";
|
||||
cin.getline(str, SIZE);
|
||||
|
||||
decryptRot13Cipher(str);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void decryptRot13Cipher(char *str) {
|
||||
for (int i = 0; str[i] != '\0'; i++) {
|
||||
if (isalpha(str[i])) {
|
||||
if (isupper(str[i])) {
|
||||
str[i] = ((str[i] - 'A' + 13) % 26) + 'A';
|
||||
} else if (islower(str[i])) {
|
||||
str[i] = ((str[i] - 'a' + 13) % 26) + 'a';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
cout << "Frase decodificata: " << str << endl;
|
||||
}
|
|
@ -0,0 +1 @@
|
|||
LunRiga 16
|
|
@ -0,0 +1,139 @@
|
|||
/*
|
||||
Nome: Mario
|
||||
Cognome: Montanari
|
||||
Classe: 3AIN
|
||||
Data: 29/01/2025
|
||||
|
||||
Scrivere un programma in C che:
|
||||
1. Definisce una struct Studente con i seguenti campi:
|
||||
- char inizialeNome;
|
||||
- char inizialeCognome;
|
||||
- int matricola;
|
||||
- float media.
|
||||
2. Definisce una struct Risultato con i seguenti campi:
|
||||
- int matricola;
|
||||
- char esito (può contenere ‘P’ o ‘B’, in base alla media dello studente);
|
||||
|
||||
3. Definire la seguente funzione:
|
||||
- void elaboraStudenti(Studente studenti[], int n, Risultato risultati[]).
|
||||
|
||||
4. La funzione riceve un array di struct Studente e un array di struct Risultato.
|
||||
5. Determina se lo studente è promosso (media >= 6.0) o bocciato (media < 6.0).
|
||||
6. Aggiorna l'array di struct Risultato con la matricola e l'esito.
|
||||
|
||||
Nel main, dichiarare e inizializzare un array di struct Studente con almeno
|
||||
3 elementi, chiamare la funzione e stampare il contenuto del secondo array.
|
||||
|
||||
VARIANTE:
|
||||
modificare la funzione o crearne un’altra in modo che,
|
||||
oltre ad elaborare come visto sopra, restituisce il numero dei promossi
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
#include <ctime>
|
||||
#include <cstdlib>
|
||||
#include <iomanip>
|
||||
|
||||
#define LMIN 65
|
||||
#define LMAX 90
|
||||
|
||||
#define NMIN 0
|
||||
#define NMAX 99999999
|
||||
|
||||
#define MMIN 2
|
||||
#define MMAX 9
|
||||
|
||||
#define PDMIN 10
|
||||
#define PDMAX 100
|
||||
|
||||
#define NSTUDENTI 10
|
||||
|
||||
using namespace std;
|
||||
|
||||
typedef struct {
|
||||
char inizialeNome;
|
||||
char inizialeCognome;
|
||||
int matricola;
|
||||
float media;
|
||||
} Studente;
|
||||
|
||||
typedef struct {
|
||||
char esito;
|
||||
} Risultato;
|
||||
|
||||
void riempiElaborazione(Studente studenti[], int nstudenti, Risultato risultato[]);
|
||||
void elaboraStudenti(const Studente studenti[], int nstudenti, Risultato risultati[]);
|
||||
void contaPromossi(Risultato risultati[], int nstudenti);
|
||||
|
||||
int main(void) {
|
||||
Studente studenti[NSTUDENTI];
|
||||
Risultato risultati[NSTUDENTI];
|
||||
int nstudenti = NSTUDENTI;
|
||||
|
||||
srand(time(NULL));
|
||||
|
||||
riempiElaborazione(studenti, nstudenti, risultati);
|
||||
elaboraStudenti(studenti, nstudenti, risultati);
|
||||
contaPromossi(risultati, nstudenti);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void riempiElaborazione(Studente studenti[], int nstudenti, Risultato risultati[]) {
|
||||
float parteDecimale = 0;
|
||||
|
||||
for (int i = 0; i < nstudenti; i++) {
|
||||
studenti[i].inizialeNome = rand() % (LMAX - LMIN + 1) + LMIN;
|
||||
studenti[i].inizialeCognome = rand() % (LMAX - LMIN + 1) + LMIN;
|
||||
studenti[i].matricola = rand() % (NMAX - NMIN + 1) + NMIN;
|
||||
|
||||
studenti[i].media = rand() % (MMAX - MMIN + 1) + MMIN;
|
||||
parteDecimale = rand() % (PDMAX - PDMIN + 1) + PDMIN;
|
||||
for (int j = 0; j < nstudenti; j++) {
|
||||
if (parteDecimale == 10 || parteDecimale == 20 || parteDecimale == 30 ||parteDecimale == 40 ||parteDecimale == 50 ||parteDecimale == 60 ||parteDecimale == 70 || parteDecimale == 80 || parteDecimale == 90 || parteDecimale == 100) {
|
||||
parteDecimale = rand() % (PDMAX - PDMIN + 1) + PDMIN;
|
||||
}
|
||||
}
|
||||
studenti[i].media = studenti[i].media + (parteDecimale / 100);
|
||||
|
||||
if (studenti[i].media >= 6.0) {
|
||||
risultati[i].esito = 'P';
|
||||
} else if (studenti[i].media < 6.0) {
|
||||
risultati[i].esito = 'B';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void elaboraStudenti(const Studente studenti[], int nstudenti, Risultato risultati[]) {
|
||||
cout << " -------------------------------------------------------------" << endl;
|
||||
|
||||
cout << " | Nome:"
|
||||
<< setw(13) << " | Cognome:"
|
||||
<< setw(13) << " | Matricola:"
|
||||
<< setw(13) << " | Media:"
|
||||
<< setw(13) << " | Stato:" << setw(2) << "|"
|
||||
<< endl;
|
||||
|
||||
cout << " -------------------------------------------------------------" << endl;
|
||||
|
||||
for (int i = 0; i < nstudenti; i++) {
|
||||
cout << " | " << studenti[i].inizialeNome << "."
|
||||
<< setw(8) << " | " << studenti[i].inizialeCognome << "."
|
||||
<< " | " << setfill('0') << setw(8) << studenti[i].matricola << setfill(' ')
|
||||
<< setw(9) << " | " << studenti[i].media
|
||||
<< setw(9) << " | " << risultati[i].esito << "." << setw(6) << "|"
|
||||
<< endl;
|
||||
}
|
||||
|
||||
cout << " -------------------------------------------------------------" << endl;
|
||||
}
|
||||
|
||||
void contaPromossi(Risultato risultati[], int nstudenti) {
|
||||
int promossi = 0;
|
||||
for (int i = 0; i < nstudenti; i++) {
|
||||
if (risultati[i].esito == 'P') {
|
||||
promossi++;
|
||||
}
|
||||
}
|
||||
cout << endl << " Numero di studenti promossi: " << promossi << endl;
|
||||
}
|
|
@ -0,0 +1,66 @@
|
|||
/*
|
||||
Cognome, Nome: Montanari, Mario
|
||||
|
||||
PROGRAMMA CHE, DATO UN ARRAY CONTENENTE 10 VALORI GENERATI CASUALMENTE, ELIMINA GLI ELEMENTI DISPARI DELL'ARRAY.
|
||||
*/
|
||||
|
||||
#include <iostream> // LIBRERIA NECESSARIA PER L'INPUT E L'OUTPUT STANDARD
|
||||
#include <array> // LIBRERIA NECESSARIA PER LAVORARE CON GLI ARRAY
|
||||
#include <cstdlib> // LIBRERIA NECESSARIA PER LAVORARE CON LA FUNZIONE 'rand'
|
||||
#include <ctime> // LIBRERIA NECESSARIA PER LAVORARE CON LA FUNZIONE 'time'
|
||||
|
||||
#define CAPACITY 10 // DIMENSIONE MASSIMA DELL'ARRAY
|
||||
#define SIZE 10 // NUMERO DI CELLE OCCUPATE NELL'ARRAY
|
||||
#define MIN 1 // VALORE MINIMO PER I NUMERI GENERATI CASUALMENTE
|
||||
#define MAX 100 // VALORE MASSIMO PER I NUMERI GENERATI CASUALMENTE
|
||||
|
||||
using namespace std;
|
||||
|
||||
void riempiArray(int arr[], int size); // PROTOTIPO DELLA FUNZIONE CHE RIEMPIE L'ARRAY INIZIALE CON NUMERI CASUALI
|
||||
void stampaArray(int arr[], int size); // PROTOTIPO DELLA FUNZIONE CHE STAMPA GLI ELEMENTI PRESENTI NELL'ARRAY
|
||||
int eliminaElementiDispari(int arr[], int size); // PROTOTIPO DELLA FUNZIONE CHE ELIMINA GLI ELEMENTI INDESIDERATI PRESENTI NELLA FUNZIONE INIZIALE
|
||||
|
||||
int main(void) {
|
||||
int arr[CAPACITY]; // DICHIARAZZIONE DELL'ARRAY 'arr[]' CON CAPACITA' MASSIMA DI 10
|
||||
int size = SIZE; // INIZIALIZZAZIONE DELLA VARIABILE 'size' CHE SPECIFICA IL NUMERO DI ELEMENTI PRESENTI NELL'ARRAY
|
||||
|
||||
riempiArray(arr, size); // CHIAMATA DELLA FUNZIONE CHE RIEMPIE L'ARRAY INIZIALE CON NUMERI CASUALI
|
||||
|
||||
cout << "Initial array: ";
|
||||
stampaArray(arr, size); // CHIAMATA DELLA FUNZIONE CHE STAMPA GLI ELEMENTI PRESENTI NELL'ARRAY
|
||||
|
||||
size = eliminaElementiDispari(arr, size); // CHIAMATA DELLA FUNZIONE CHE ELIMINA GLI ELEMENTI INDESIDERATI PRESENTI NELLA FUNZIONE
|
||||
|
||||
cout << "Final array: ";
|
||||
stampaArray(arr, size); // CHIAMATA DELLA FUNZIONE CHE STAMPA GLI ELEMENTI PRESENTI NELL'ARRAY
|
||||
|
||||
return 0; // TERMINA IL PROGRAMMA
|
||||
}
|
||||
|
||||
// DICHIARAZIONE DELLA FUNZIONE CHE RIEMPIE L'ARRAY INIZIALE CON NUMERI CASUALI
|
||||
void riempiArray(int arr[], int size) {
|
||||
srand(time(NULL)); // INIZIALIZZA IL GENERATORE DI NUMERI CASUALI
|
||||
for (int i = 0; i < size; i++) { // ISTRUZIONE CHE CICLA ATTRAVERSO TUTTE LE CELLE DELL'ARRAY
|
||||
arr[i] = rand() % (MAX - MIN + 1) + MIN; // GENERA UN NUMERO CASUALE TRA 'MIN' E 'MAX' E LO ASSEGNA ALLA CELLA CORRENTE
|
||||
}
|
||||
}
|
||||
|
||||
// DICHIARAZIONE DELLA FUNZIONE CHE STAMPA GLI ELEMENTI PRESENTI NELL'ARRAY
|
||||
void stampaArray(int arr[], int size) {
|
||||
for (int i = 0; i < size; i++) { // ISTRUZIONE CHE CICLA ATTRAVERSO TUTTI GLI ELEMENTI PRESENTI NELL'ARRAY
|
||||
cout << arr[i] << " "; // STAMPA OGNI ELEMENTO SEGUITO DA UNO SPAZIO
|
||||
}
|
||||
cout << endl; // AGGIUNGE UN A CAPO DOPO LA STAMPA DI TUTTI GLI ELEMENTI
|
||||
}
|
||||
|
||||
// DICHIARAZIONE DELLA FUNZIONE CHE ELIMINA GLI ELEMENTI INDESIDERATI PRESENTI NELLA FUNZIONE INIZIALE
|
||||
int eliminaElementiDispari(int arr[], int size) {
|
||||
int j = 0; // INIZIALIZZAZIONE DELLA VARIABILE 'j'
|
||||
for (int i = 0; i < size; i++) { // ISTRUZIONE CHE CICLA ATTRAVERSO TUTTI GLI ELEMENTI PRESENTI NELL'ARRAY
|
||||
if (arr[i] % 2 == 0) { // ISTRUZIONE CHE VERIFICA SE L'ELEMENTO CORRENTE E' PARI
|
||||
arr[j] = arr[i]; // SE L'ELEMENTO E' PARI LO COPIA NELLA POSIZIONE CORRENTE DI 'j'
|
||||
j++; // INCREMENTA 'j' PER SPOSTARSI ALLA PROSSIMA POSIZIONE LIBERA DELL'ARRAY
|
||||
}
|
||||
}
|
||||
return j; // LA FUNZIONE RESTITUISCE IL NUMERO DI ELEMENTI RIMASTI NELL'ARRAY
|
||||
}
|
|
@ -0,0 +1,61 @@
|
|||
/*
|
||||
Nome: Mario
|
||||
Cognome: Montanari
|
||||
Classe: 3AIN
|
||||
Data: 27/11/2024
|
||||
|
||||
Funzione che, ricevuto un array, elimina (accorciando il vettore) i valori pari.
|
||||
SENZA MEMMOVE.
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
#include <ctime>
|
||||
#define CAPACITY 100
|
||||
#define SIZE 10
|
||||
#define MIN 1
|
||||
#define MAX 100
|
||||
using namespace std;
|
||||
|
||||
void stampaArray(int arr[], int size);
|
||||
void riempiArray(int arr[], int size);
|
||||
int eliminaPari(int arr[], int size);
|
||||
|
||||
int main(void) {
|
||||
int arr[CAPACITY];
|
||||
int size = SIZE;
|
||||
|
||||
riempiArray(arr, size);
|
||||
stampaArray(arr, size);
|
||||
|
||||
size = eliminaPari(arr, size);
|
||||
stampaArray(arr, size);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void stampaArray(int arr[], int size) {
|
||||
for (int i = 0; i < size; i++) {
|
||||
cout << arr[i] << " ";
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
|
||||
void riempiArray(int arr[], int size) {
|
||||
srand(time(NULL));
|
||||
for (int i = 0; i < size; i++) {
|
||||
arr[i] = rand() % (MAX - MIN + 1) + MIN;
|
||||
}
|
||||
}
|
||||
|
||||
int eliminaPari(int arr[], int size) {
|
||||
for (int i = 0; i < size; i++) {
|
||||
if (arr[i] % 2 == 0) {
|
||||
for (int j = i; j < size - 1; j++) {
|
||||
arr[j] = arr[j + 1];
|
||||
}
|
||||
i--;
|
||||
size--;
|
||||
}
|
||||
}
|
||||
return size;
|
||||
}
|
|
@ -0,0 +1,67 @@
|
|||
/*
|
||||
Nome: Mario
|
||||
Cognome: Montanari
|
||||
Classe: 3AIN
|
||||
Data: 27/11/2024
|
||||
|
||||
Funzione che, ricevuto un array, elimina (accorciando il vettore) i valori pari.
|
||||
CON MEMMOVE.
|
||||
|
||||
memmove(oggetto_di_destinazione, oggetto_di_origine, numero_di_byte);
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
#include <ctime>
|
||||
#include <cstring>
|
||||
#define CAPACITY 100
|
||||
#define SIZE 10
|
||||
#define MIN 1
|
||||
#define MAX 100
|
||||
using namespace std;
|
||||
|
||||
void stampaArray(int arr[], int size);
|
||||
void riempiArray(int arr[], int size);
|
||||
int eliminaPari(int arr[], int size);
|
||||
|
||||
int main(void) {
|
||||
int arr[CAPACITY];
|
||||
int size = SIZE;
|
||||
|
||||
riempiArray(arr, size);
|
||||
stampaArray(arr, size);
|
||||
|
||||
size = eliminaPari(arr, size);
|
||||
stampaArray(arr, size);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void stampaArray(int arr[], int size) {
|
||||
for (int i = 0; i < size; i++) {
|
||||
cout << arr[i] << " ";
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
|
||||
void riempiArray(int arr[], int size) {
|
||||
srand(time(NULL));
|
||||
for (int i = 0; i < size; i++) {
|
||||
arr[i] = rand() % (MAX - MIN + 1) + MIN;
|
||||
}
|
||||
}
|
||||
|
||||
int eliminaPari(int arr[], int size) {
|
||||
for (int i = 0; i < size; i++) {
|
||||
if (arr[i] % 2 == 0) {
|
||||
memmove(&arr[i], &arr[i + 1], (size - i - 1) * sizeof(int));
|
||||
/*
|
||||
for (int j = i; j < size - 1; j++) {
|
||||
arr[j] = arr[j + 1];
|
||||
}
|
||||
*/
|
||||
i--;
|
||||
size--;
|
||||
}
|
||||
}
|
||||
return size;
|
||||
}
|
|
@ -0,0 +1,46 @@
|
|||
/*
|
||||
Nome: Mario
|
||||
Cognome: Montanari
|
||||
Classe: 3AIN
|
||||
Data: 08/03/2025
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
#include <cctype>
|
||||
|
||||
#define SIZE 100+1
|
||||
|
||||
using namespace std;
|
||||
|
||||
char *encryptAffineCipher(char *const str, const int a, const int b);
|
||||
|
||||
int main(void) {
|
||||
char str[SIZE];
|
||||
int a;
|
||||
int b;
|
||||
|
||||
cout << "Inserisci una frase: ";
|
||||
cin.getline(str, SIZE);
|
||||
|
||||
cout << "a: ";
|
||||
cin >> a;
|
||||
|
||||
cout << "b: ";
|
||||
cin >> b;
|
||||
|
||||
cout << "Frase codificata: " << encryptAffineCipher(str, a, b) << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
char *encryptAffineCipher(char *const str, const int a, const int b) {
|
||||
for (int i = 0; str[i] != '\0'; i++) {
|
||||
if (isupper(str[i])) {
|
||||
str[i] = (a * (str[i] - 'A') + b) % 26 + 'A';
|
||||
} else if (islower(str[i])) {
|
||||
str[i] = (a * (str[i] - 'a') + b) % 26 + 'a';
|
||||
}
|
||||
}
|
||||
|
||||
return str;
|
||||
}
|
|
@ -0,0 +1,46 @@
|
|||
/*
|
||||
Nome: Mario
|
||||
Cognome: Montanari
|
||||
Classe: 3AIN
|
||||
Data: 08/03/2025
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
#include <cctype>
|
||||
|
||||
#define SIZE 100+1
|
||||
|
||||
using namespace std;
|
||||
|
||||
void encryptAtbashCipher(char *str);
|
||||
|
||||
int main(void) {
|
||||
char str[SIZE]; //ABCDEFGHIJKLMNOPQRSTUVWXYZ
|
||||
|
||||
cout << "Inserisci una frase: ";
|
||||
cin.getline(str, SIZE);
|
||||
|
||||
encryptAtbashCipher(str);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void encryptAtbashCipher(char *str) {
|
||||
int j = 0;
|
||||
|
||||
for (int i = 0; str[i] != '\0'; i++) {
|
||||
if (!isalpha(str[i])) {
|
||||
str[j++] = str[i];
|
||||
} else {
|
||||
if (isupper(str[i])) {
|
||||
str[j++] = 'Z' - (str[i] - 'A');
|
||||
} else if (islower(str[i])) {
|
||||
str[j++] = 'z' - (str[i] - 'a');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
str[j] = '\0';
|
||||
|
||||
cout << "Frase codificata: " << str << endl;
|
||||
}
|
|
@ -0,0 +1,40 @@
|
|||
/*
|
||||
Nome: Mario
|
||||
Cognome: Montanari
|
||||
Classe: 3AIN
|
||||
Data: 08/03/2025
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
#include <cctype>
|
||||
|
||||
#define SIZE 100+1
|
||||
|
||||
using namespace std;
|
||||
|
||||
void encryptCaesarCipher(char *str);
|
||||
|
||||
int main(void) {
|
||||
char str[SIZE]; //ABCDEFGHIJKLMNOPQRSTUVWXYZ
|
||||
|
||||
cout << "Inserisci una frase: ";
|
||||
cin.getline(str, SIZE);
|
||||
|
||||
encryptCaesarCipher(str);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void encryptCaesarCipher(char *str) {
|
||||
for (int i = 0; str[i] != '\0'; i++) {
|
||||
if (isalpha(str[i])) {
|
||||
if (isupper(str[i])) {
|
||||
str[i] = ((str[i] - 'A' + 3) % 26) + 'A';
|
||||
} else if (islower(str[i])) {
|
||||
str[i] = ((str[i] - 'a' + 3) % 26) + 'a';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
cout << "Frase codificata: " << str << endl;
|
||||
}
|
|
@ -0,0 +1,61 @@
|
|||
/*
|
||||
Nome: Mario
|
||||
Cognome: Montanari
|
||||
Classe: 3AIN
|
||||
Data: 08/03/2025
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
#include <cctype>
|
||||
|
||||
#define SIZE 100+1
|
||||
|
||||
using namespace std;
|
||||
|
||||
void encryptCarbonarCipher(char *str, const char *carbonarAlphabet);
|
||||
|
||||
int main(void) {
|
||||
const char carbonarAlphabet[] = "OPGTIVCHE RNMABQLZDUF S";
|
||||
char str[SIZE]; //ABCDEFGHI LMNOPQRSTUV Z
|
||||
|
||||
cout << "Inserisci una frase: ";
|
||||
cin.getline(str, SIZE);
|
||||
|
||||
encryptCarbonarCipher(str, carbonarAlphabet);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void encryptCarbonarCipher(char *str, const char *carbonarAlphabet) {
|
||||
char carbonarStr[SIZE];
|
||||
int j = 0;
|
||||
|
||||
for (int i = 0; str[i] != '\0'; i++) {
|
||||
if (
|
||||
str[i] == 'J' || str[i] == 'j' ||
|
||||
str[i] == 'K' || str[i] == 'k' ||
|
||||
str[i] == 'W' || str[i] == 'w' ||
|
||||
str[i] == 'X' || str[i] == 'x' ||
|
||||
str[i] == 'Y' || str[i] == 'y'
|
||||
) {
|
||||
cout << "Frase non valida!" << endl;
|
||||
return;
|
||||
}
|
||||
|
||||
if (!isalpha(str[i])) {
|
||||
carbonarStr[j++] = str[i];
|
||||
} else {
|
||||
if (isupper(str[i])) {
|
||||
int r = str[i] - 'A';
|
||||
carbonarStr[j++] = toupper(carbonarAlphabet[r]);
|
||||
} else if (islower(str[i])) {
|
||||
int r = str[i] - 'a';
|
||||
carbonarStr[j++] = tolower(carbonarAlphabet[r]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
carbonarStr[j] = '\0';
|
||||
|
||||
cout << "Frase codificata: " << carbonarStr << endl;;
|
||||
}
|
|
@ -0,0 +1,77 @@
|
|||
/*
|
||||
Nome: Mario
|
||||
Cognome: Montanari
|
||||
|
||||
pigLatin
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
#include <cstring>
|
||||
|
||||
#define SIZE 100+1
|
||||
|
||||
using namespace std;
|
||||
|
||||
bool isVowel(char chr);
|
||||
void pigLatin(char *mssg);
|
||||
|
||||
int main(void) {
|
||||
char mssg[SIZE];
|
||||
|
||||
cout << "Inserisci una frase: ";
|
||||
cin.getline(mssg, SIZE);
|
||||
|
||||
cout << "Frase codificata: ";
|
||||
pigLatin(mssg);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool isVowel(char chr) {
|
||||
return (
|
||||
chr == 'A' || chr == 'a' ||
|
||||
chr == 'E' || chr == 'e' ||
|
||||
chr == 'I' || chr == 'i' ||
|
||||
chr == 'O' || chr == 'o' ||
|
||||
chr == 'U' || chr == 'u'
|
||||
);
|
||||
}
|
||||
|
||||
void pigLatin(char *mssg) {
|
||||
char *token = strtok(mssg, " ");
|
||||
|
||||
while (token != nullptr) {
|
||||
int len = strlen(token);
|
||||
bool hasPunctuation = ispunct(token[len - 1]);
|
||||
|
||||
char punctuation = '\0';
|
||||
if (hasPunctuation) {
|
||||
punctuation = token[len - 1];
|
||||
token[len - 1] = '\0';
|
||||
len--;
|
||||
}
|
||||
|
||||
if (isVowel(token[0])) {
|
||||
cout << token << "way";
|
||||
} else {
|
||||
char first = token[0];
|
||||
for (int i = 0; i < len - 1; i++) {
|
||||
token[i] = token[i + 1];
|
||||
}
|
||||
token[len - 1] = first;
|
||||
token[len] = '\0';
|
||||
|
||||
cout << token << "ay";
|
||||
}
|
||||
|
||||
if (hasPunctuation) {
|
||||
cout << punctuation;
|
||||
}
|
||||
|
||||
cout << " ";
|
||||
|
||||
token = strtok(nullptr, " ");
|
||||
}
|
||||
|
||||
cout << endl;
|
||||
}
|
|
@ -0,0 +1,76 @@
|
|||
/*
|
||||
Nome: Mario
|
||||
Cognome: Montanari
|
||||
Classe: 3AIN
|
||||
Data: 11/03/2025
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
#include <cctype>
|
||||
#include <cstring>
|
||||
|
||||
#define ALFABETO 26
|
||||
|
||||
#define RIGHE 5
|
||||
#define COLONNE 5
|
||||
|
||||
#define SIZE 200+1
|
||||
|
||||
using namespace std;
|
||||
|
||||
typedef struct {
|
||||
int righe;
|
||||
int colonne;
|
||||
} coordinate;
|
||||
|
||||
coordinate cerca(char carattere, char campo[][COLONNE]);
|
||||
|
||||
int main(void) {
|
||||
char campo[RIGHE][COLONNE];
|
||||
int i = 0;
|
||||
|
||||
for (char chr = 'a'; chr <= 'z'; chr++) {
|
||||
if (chr == 'j') continue;
|
||||
campo[i / COLONNE][i % COLONNE] = chr;
|
||||
i++;
|
||||
}
|
||||
|
||||
char str[SIZE];
|
||||
|
||||
cout << "Inserisci una frase: ";
|
||||
cin.getline(str, SIZE);
|
||||
|
||||
cout << "Frase codificata: ";
|
||||
|
||||
for (int i = 0; i < strlen(str); i++) {
|
||||
char carattere = str[i];
|
||||
if (isalpha(carattere)) {
|
||||
carattere = tolower(carattere);
|
||||
if (carattere == 'j') {
|
||||
carattere = 'i';
|
||||
}
|
||||
|
||||
coordinate ris = cerca(carattere, campo);
|
||||
cout << ris.righe << ris.colonne << " ";
|
||||
}
|
||||
}
|
||||
|
||||
cout << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
coordinate cerca(char carattere, char campo[][COLONNE]) {
|
||||
coordinate result;
|
||||
|
||||
for (int i = 0; i < RIGHE; i++) {
|
||||
for (int j = 0; j < COLONNE; j++) {
|
||||
if (campo[i][j] == carattere) {
|
||||
result.righe = i + 1;
|
||||
result.colonne = j + 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
|
@ -0,0 +1,40 @@
|
|||
/*
|
||||
Nome: Mario
|
||||
Cognome: Montanari
|
||||
Classe: 3AIN
|
||||
Data: 08/03/2025
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
#include <cctype>
|
||||
|
||||
#define SIZE 100+1
|
||||
|
||||
using namespace std;
|
||||
|
||||
void encryptRot13Cipher(char *str);
|
||||
|
||||
int main(void) {
|
||||
char str[SIZE]; //ABCDEFGHIJKLMNOPQRSTUVWXYZ
|
||||
|
||||
cout << "Inserisci una frase: ";
|
||||
cin.getline(str, SIZE);
|
||||
|
||||
encryptRot13Cipher(str);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void encryptRot13Cipher(char *str) {
|
||||
for (int i = 0; str[i] != '\0'; i++) {
|
||||
if (isalpha(str[i])) {
|
||||
if (isupper(str[i])) {
|
||||
str[i] = ((str[i] - 'A' + 13) % 26) + 'A';
|
||||
} else if (islower(str[i])) {
|
||||
str[i] = ((str[i] - 'a' + 13) % 26) + 'a';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
cout << "Frase codificata: " << str << endl;
|
||||
}
|
|
@ -0,0 +1,55 @@
|
|||
/*
|
||||
Nome: Mario
|
||||
Cognome: Montanari
|
||||
Classe: 3AIN
|
||||
Data: 08/03/2025
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
#include <cctype>
|
||||
|
||||
#define SIZE 100+1
|
||||
|
||||
using namespace std;
|
||||
|
||||
void encryptVingenereCipher(char *str, int offset, char *wormOffset);
|
||||
|
||||
int main(void) {
|
||||
char str[SIZE]; //ABCDEFGHIJKLMNOPQRSTUVWXYZ
|
||||
char wormOffset[SIZE];
|
||||
int offset;
|
||||
|
||||
cout << "Inserisci una frase: ";
|
||||
cin.getline(str, SIZE);
|
||||
|
||||
cout << "Inserisci la chiave: ";
|
||||
cin.getline(wormOffset, SIZE);
|
||||
|
||||
encryptVingenereCipher(str, offset, wormOffset);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void encryptVingenereCipher(char *str, int offset, char *wormOffset) {
|
||||
int j = 0;
|
||||
|
||||
for (int i = 0; str[i] != '\0'; i++) {
|
||||
if (isalpha(str[i])) {
|
||||
if (isupper(wormOffset[j % strlen(wormOffset)])) {
|
||||
offset = wormOffset[j % strlen(wormOffset)] - 'A';
|
||||
} else {
|
||||
offset = wormOffset[j % strlen(wormOffset)] - 'a';
|
||||
}
|
||||
|
||||
if (isupper(str[i])) {
|
||||
str[i] = ((str[i] - 'A' + offset + 26) % 26) + 'A';
|
||||
} else if (islower(str[i])) {
|
||||
str[i] = ((str[i] - 'a' + offset + 26) % 26) + 'a';
|
||||
}
|
||||
|
||||
j++;
|
||||
}
|
||||
}
|
||||
|
||||
cout << "Frase codificata: " << str << endl;
|
||||
}
|
|
@ -0,0 +1,84 @@
|
|||
#include <iostream>
|
||||
#include <cmath>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main(void) {
|
||||
|
||||
// Coefficienti della parabola
|
||||
float a = 1;
|
||||
float b = 5;
|
||||
float c = 4;
|
||||
|
||||
cout << "Equazione: " << a << "x^2 + " << b << "x + " << c << " = 0" << endl;
|
||||
|
||||
// Apertura della parabola
|
||||
if (a > 0) {
|
||||
cout << "Parabola rivolta verso l'alto." << endl;
|
||||
} else if (a < 0) {
|
||||
cout << "Parabola rivolta verso il basso." << endl;
|
||||
} else {
|
||||
cout << "Errore." << endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Calcolo del discriminante
|
||||
float delta = (b * b) - (4 * a * c);
|
||||
cout << "Delta: " << delta << endl;
|
||||
|
||||
if (delta > 0) {
|
||||
float x1 = ((-b) - (sqrt(delta))) / (2 * a);
|
||||
float x2 = ((-b) + (sqrt(delta))) / (2 * a);
|
||||
cout << "S = {x1 = " << x1 << " V " << "x2 = " << x2 << "}" << endl;
|
||||
} else if (delta == 0) {
|
||||
float x0 = (-b) / (2 * a);
|
||||
cout << "S = {x = " << x0 << "}" << endl;
|
||||
} else {
|
||||
cout << "Soluzioni troppo complesse per ora." << endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Calcolo del vertice
|
||||
float xVertice = (-b) / (2 * a);
|
||||
float yVertice = (a * xVertice * xVertice) + (b * xVertice) + c;
|
||||
cout << "V(" << xVertice << "; " << yVertice << ")" << endl;
|
||||
|
||||
// Calcolo del parametro focale
|
||||
float parametroFocale = (1) / (4 * a);
|
||||
|
||||
// Calcolo del fuoco
|
||||
float xFuoco = xVertice;
|
||||
float yFuoco = yVertice + parametroFocale;
|
||||
cout << "F(" << xFuoco << "; " << yFuoco << ")" << endl;
|
||||
|
||||
// Calcolo della direttrice
|
||||
float direttrice = yVertice - parametroFocale;
|
||||
cout << "d: y = " << direttrice << endl;
|
||||
|
||||
// Asse di simmetria
|
||||
cout << "s: x = " << xVertice << endl;
|
||||
|
||||
// Disegnamo il grafico (solo se le soluzioni sono numeri interi)
|
||||
const int width = 20;
|
||||
const int height = 10;
|
||||
|
||||
for (int y = height; y >= -height; --y) {
|
||||
for (int x = -width; x <= width; ++x) {
|
||||
float fx = (a * x * x) + (b * x) + c;
|
||||
if (round(fx) == y) {
|
||||
cout << "*";
|
||||
} else if (y == 0 && x == 0) {
|
||||
cout << "+";
|
||||
} else if (y == 0) {
|
||||
cout << "-";
|
||||
} else if (x == 0) {
|
||||
cout << "|";
|
||||
} else {
|
||||
cout << " ";
|
||||
}
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,39 @@
|
|||
/*
|
||||
Nome: Mario
|
||||
Cognome: Montanari
|
||||
Classe: 3AIN
|
||||
Data: 07/03/2025
|
||||
|
||||
es01: scrivi una implementazione dellafunzione di
|
||||
libreria su stringhe che ritornano la sua lunghezza
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
|
||||
#define SIZE 100+1
|
||||
|
||||
using namespace std;
|
||||
|
||||
void lenStr(char *str);
|
||||
|
||||
int main(void) {
|
||||
char str[SIZE];
|
||||
|
||||
cin.getline(str, SIZE);
|
||||
|
||||
lenStr(str);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void lenStr(char *str) {
|
||||
int contaChr = 0;
|
||||
|
||||
for (int i = 0; str[i] != '\0'; i++) {
|
||||
if (str[i] != '\0') {
|
||||
contaChr++;
|
||||
}
|
||||
}
|
||||
|
||||
cout << contaChr << endl;
|
||||
}
|
|
@ -0,0 +1,48 @@
|
|||
/*
|
||||
Nome: Mario
|
||||
Cognome: Montanari
|
||||
Classe: 3AIN
|
||||
Data: 16/04/2025
|
||||
|
||||
es01:
|
||||
Scrivi un programma che generi un file (utilizzare il generatore di numeri
|
||||
pseudocasuali) per la rappresentazione di un insieme di 90 numeri (<100
|
||||
come nel gioco del lotto).
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <time.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#define SIZE 90
|
||||
#define MIN_VALUE 0
|
||||
#define MAX_VALUE 99
|
||||
|
||||
void generaNumeri(FILE * file);
|
||||
|
||||
int main(void) {
|
||||
srand(time(NULL));
|
||||
|
||||
FILE * file = fopen("file.bin", "wb");
|
||||
|
||||
if (file != NULL) {
|
||||
generaNumeri(file);
|
||||
|
||||
fclose(file);
|
||||
} else {
|
||||
perror("Error (source)");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void generaNumeri(FILE * file) {
|
||||
int numeri[SIZE];
|
||||
|
||||
for (int i = 0; i < SIZE; i++) {
|
||||
numeri[i] = rand() % (MAX_VALUE - MIN_VALUE + 1) + MIN_VALUE;
|
||||
fwrite(&numeri[i], sizeof(int), 1, file);
|
||||
printf("%d ", numeri[i]);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
|
@ -0,0 +1,37 @@
|
|||
/*
|
||||
Nome: Mario
|
||||
Cognome: Montanari
|
||||
Classe: 3AIN
|
||||
Data: 04/04/2025
|
||||
|
||||
es01. (stampavideo1)
|
||||
Scrivere un programma che stampi a video il contenuto del file in.txt.
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
void apriFile(const char * nomeFile);
|
||||
|
||||
int main(void) {
|
||||
apriFile("file.txt");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void apriFile(const char * nomeFile) {
|
||||
FILE * fp = fopen(nomeFile, "rt");
|
||||
|
||||
if (fp != NULL) {
|
||||
char chr;
|
||||
|
||||
while ((chr = fgetc(fp)) != EOF) {
|
||||
cout << chr;
|
||||
}
|
||||
|
||||
fclose(fp);
|
||||
} else {
|
||||
perror("Error");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,55 @@
|
|||
/*
|
||||
Nome: Mario
|
||||
Cognome: Montanari
|
||||
Classe: 3AIN
|
||||
Data: 07/05/2025
|
||||
|
||||
1) Aprire https://hexed.it/
|
||||
2) Visualizza la dimensione del tipo int.
|
||||
3) Crea tre file binari ("file_70.bin", "file_100.bin" e
|
||||
"file_200.bin") e vi scrive rispettivamente 70, 100 e 200
|
||||
interi, da 1 fino al numero di interi specificato.
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main(void) {
|
||||
cout << "sizeof(int): " << sizeof(int) << endl;
|
||||
|
||||
FILE * file_70 = fopen("file_70.bin", "wb");
|
||||
FILE * file_100 = fopen("file_100.bin", "wb");
|
||||
FILE * file_200 = fopen("file_200.bin", "wb");
|
||||
|
||||
if (file_70 != NULL && file_100 != NULL && file_200 != NULL) {
|
||||
int num = 1;
|
||||
|
||||
for (int i = 0; i < 70; i++) {
|
||||
fwrite(&num, sizeof(int), 1, file_70);
|
||||
num++;
|
||||
}
|
||||
|
||||
num = 1;
|
||||
|
||||
for (int i = 0; i < 100; i++) {
|
||||
fwrite(&num, sizeof(int), 1, file_100);
|
||||
num++;
|
||||
}
|
||||
|
||||
num = 1;
|
||||
|
||||
for (int i = 0; i < 200; i++) {
|
||||
fwrite(&num, sizeof(int), 1, file_200);
|
||||
num++;
|
||||
}
|
||||
|
||||
fclose(file_70);
|
||||
fclose(file_100);
|
||||
fclose(file_200);
|
||||
} else {
|
||||
perror("Error");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,40 @@
|
|||
/*
|
||||
Nome: Mario
|
||||
Cognome: Montanari
|
||||
Classe: 3AIN
|
||||
Data: 16/04/2025
|
||||
|
||||
leggere tutti i valori con una sola fread in un array
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#define SIZE 1000+1
|
||||
|
||||
void leggiNumeri(FILE * file);
|
||||
|
||||
int main(void) {
|
||||
FILE *file = fopen("file_70.bin", "rb");
|
||||
|
||||
if (file != NULL) {
|
||||
leggiNumeri(file);
|
||||
|
||||
fclose(file);
|
||||
} else {
|
||||
perror("Error (source)");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void leggiNumeri(FILE * file) {
|
||||
int numeri[SIZE];
|
||||
int numeriLetti = fread(numeri, sizeof(int), SIZE, file);
|
||||
|
||||
fclose(file);
|
||||
|
||||
for (int i = 0; i < numeriLetti; i++) {
|
||||
printf("%d ", numeri[i]);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
|
@ -0,0 +1,58 @@
|
|||
/*
|
||||
Nome: Mario
|
||||
Cognome: Montanari
|
||||
Classe: 3AIN
|
||||
Data: 16/04/2025
|
||||
|
||||
leggere con un ciclo while tutti gli elementi del file e metterli in un array
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
void leggiNumeri(FILE * file);
|
||||
void creaFile(FILE * file);
|
||||
|
||||
int main(void) {
|
||||
FILE *file = fopen("file_70.bin", "rb");
|
||||
|
||||
if (file != NULL) {
|
||||
leggiNumeri(file);
|
||||
fseek(file, 0, SEEK_SET);
|
||||
printf("\n");
|
||||
creaFile(file);
|
||||
|
||||
fclose(file);
|
||||
} else {
|
||||
perror("Error (source)");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void leggiNumeri(FILE * file) {
|
||||
int numero;
|
||||
|
||||
while (fread(&numero, sizeof(int), 1, file) == 1) {
|
||||
printf("%d ", numero);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
void creaFile(FILE * file) {
|
||||
FILE * out = fopen("out.bin", "wb");
|
||||
|
||||
if (out != NULL) {
|
||||
int numero;
|
||||
|
||||
while (fread(&numero, sizeof(int), 1, file) == 1) {
|
||||
if (numero % 2 == 0) {
|
||||
fwrite(&numero, sizeof(int), 1, out);
|
||||
printf("%d ", numero);
|
||||
}
|
||||
}
|
||||
|
||||
fclose(out);
|
||||
} else {
|
||||
perror("Error (destination)");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,39 @@
|
|||
/*
|
||||
Nome: Mario
|
||||
Cognome: Montanari
|
||||
Classe: 3AIN
|
||||
Data: 07/03/2025
|
||||
|
||||
es02: scrivi un programma che, lette due stringhe
|
||||
di caratteri indicanti il nome ed il cognome di una
|
||||
persona, le riscriva con le sole iniziali maiuscole.
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
#include <cctype>
|
||||
|
||||
#define SIZE 100+1
|
||||
|
||||
using namespace std;
|
||||
|
||||
void inizialeNome(char *nome);
|
||||
|
||||
int main(void) {
|
||||
char nome[SIZE];
|
||||
char cognome[SIZE];
|
||||
|
||||
cin.getline(nome, SIZE);
|
||||
|
||||
cin.getline(cognome, SIZE);
|
||||
|
||||
inizialeNome(nome);
|
||||
inizialeNome(cognome);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void inizialeNome(char *nome) {
|
||||
nome[0] = toupper(nome[0]);
|
||||
|
||||
cout << nome[0] << ". ";
|
||||
}
|
|
@ -0,0 +1,65 @@
|
|||
/*
|
||||
Nome: Mario
|
||||
Cognome: Montanari
|
||||
Classe: 3AIN
|
||||
Data: 16/04/2025
|
||||
|
||||
es02:
|
||||
Scrivi un programma che utilizzi il file dell'esercizio precedente per
|
||||
leggere i numeri del lotto e che, data una coppia di numeri, verifichi
|
||||
se questa corrisponde ad un ambo.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <time.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#define MIN_VALUE 0
|
||||
#define MAX_VALUE 99
|
||||
|
||||
void leggiNumeri(FILE * file);
|
||||
void verificaAmbo(FILE * file);
|
||||
|
||||
int main(void) {
|
||||
srand(time(NULL));
|
||||
|
||||
FILE * file = fopen("file.bin", "rb");
|
||||
|
||||
if (file != NULL) {
|
||||
leggiNumeri(file);
|
||||
|
||||
rewind(file);
|
||||
|
||||
verificaAmbo(file);
|
||||
|
||||
fclose(file);
|
||||
} else {
|
||||
perror("Error (source)");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void leggiNumeri(FILE * file) {
|
||||
int numeri;
|
||||
|
||||
while (fread(&numeri, sizeof(int), 1, file) == 1) {
|
||||
printf("%d ", numeri);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
void verificaAmbo(FILE * file) {
|
||||
int numeri;
|
||||
int n1 = rand() % (MAX_VALUE - MIN_VALUE + 1) + MIN_VALUE;
|
||||
int n2 = rand() % (MAX_VALUE - MIN_VALUE + 1) + MIN_VALUE;
|
||||
|
||||
while (fread(&numeri, sizeof(int), 1, file) == 1) {
|
||||
if (numeri == n1 || numeri == n2) {
|
||||
printf("Coppia di numeri: %d %d\nLa coppia corrisponde ad un ambo.", n1, n2);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
printf("Coppia di numeri: %d %d\nLa coppia NON corrisponde ad un ambo.", n1, n2);
|
||||
}
|
|
@ -0,0 +1,68 @@
|
|||
/*
|
||||
Nome: Mario
|
||||
Cognome: Montanari
|
||||
Classe: 3AIN
|
||||
Data: 04/04/2025
|
||||
|
||||
es02. (stampavideo2)
|
||||
Scrivere una funzione che riceva in ingresso il nome di un
|
||||
file e ne stampi a video il contenuto. Utilizzare la funzione
|
||||
per mostrare il contenuto dei file in.txt e in2.txt.
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
|
||||
#define SIZE_LINE 1000+1
|
||||
|
||||
using namespace std;
|
||||
|
||||
void apriFile(const char * nomeFile);
|
||||
void copiaFile(const char * nomeFileSorgente, const char * nomeFileDestinazione);
|
||||
|
||||
int main(void) {
|
||||
apriFile("in.txt");
|
||||
copiaFile("in.txt", "in2.txt");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void apriFile(const char * nomeFile) {
|
||||
FILE * fp = fopen(nomeFile, "rt");
|
||||
|
||||
if (fp != NULL) {
|
||||
char str[SIZE_LINE];
|
||||
|
||||
while (fgets(str, sizeof(str), fp) != NULL) {
|
||||
cout << str;
|
||||
}
|
||||
cout << endl;
|
||||
|
||||
fclose(fp);
|
||||
} else {
|
||||
perror("Error");
|
||||
}
|
||||
}
|
||||
|
||||
void copiaFile(const char * nomeFileSorgente, const char * nomeFileDestinazione) {
|
||||
FILE * fr = fopen(nomeFileSorgente, "rt");
|
||||
|
||||
if (fr != NULL) {
|
||||
FILE * fw = fopen(nomeFileDestinazione, "wt");
|
||||
|
||||
if (fw != NULL) {
|
||||
char str[SIZE_LINE];
|
||||
|
||||
while (fgets(str, sizeof(str), fr) != NULL) {
|
||||
fputs(str, fw);
|
||||
}
|
||||
|
||||
fclose(fw);
|
||||
} else {
|
||||
perror("Error");
|
||||
}
|
||||
|
||||
fclose(fr);
|
||||
} else {
|
||||
perror("Error");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,137 @@
|
|||
/*
|
||||
Nome: Mario
|
||||
Cognome: Montanari
|
||||
Classe: 3AIN
|
||||
Data: 07/05/2025
|
||||
|
||||
1) Apre un file binario in modalità di lettura binaria ("rb"),
|
||||
2) Legge fino a 100 interi dal file e memorizza i valori letti
|
||||
nell'array dati.
|
||||
3) Successivamente, stampa il numero effettivo di valori letti
|
||||
utilizzando la funzione fread.
|
||||
4) Testarlo:
|
||||
- sui file dell’es1
|
||||
- su un file di testo contenente “1234567\n”
|
||||
5) Dai file "file_70.bin", "file_100.bin" e "file_200.bin”
|
||||
6) Leggere il 30° valore
|
||||
7) Leggere il 2° valore
|
||||
8) Leggere il penultimo
|
||||
9) Leggerne uno a caso
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
#include <cstdlib>
|
||||
#include <ctime>
|
||||
|
||||
#define DIM 100
|
||||
#define VAL_MIN 0
|
||||
#define VAL_MAX 100
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main(void) {
|
||||
srand(time(NULL));
|
||||
|
||||
FILE * file_70 = fopen("file_70.bin", "rb");
|
||||
FILE * file_100 = fopen("file_100.bin", "rb");
|
||||
FILE * file_200 = fopen("file_200.bin", "rb");
|
||||
FILE * file_7 = fopen("file_7.txt", "rt");
|
||||
|
||||
if (file_70 != NULL && file_100 != NULL && file_200 != NULL && file_7 != NULL) {
|
||||
int dati_70[DIM] = {0};
|
||||
int dati_100[DIM] = {0};
|
||||
int dati_200[DIM * 2] = {0};
|
||||
|
||||
|
||||
// --- Lettura dei file binari ---
|
||||
int letti_70 = fread(dati_70, sizeof(int), DIM, file_70);
|
||||
int letti_100 = fread(dati_100, sizeof(int), DIM, file_100);
|
||||
int letti_200 = fread(dati_200, sizeof(int), DIM * 2, file_200);
|
||||
|
||||
cout << "Valori letti da file_70.bin (" << letti_70 << "): ";
|
||||
for (int i = 0; i < letti_70; i++) {
|
||||
cout << dati_70[i] << " ";
|
||||
}
|
||||
cout << endl << endl;
|
||||
|
||||
cout << "Valori letti da file_100.bin (" << letti_100 << "): ";
|
||||
for (int i = 0; i < letti_100; i++) {
|
||||
cout << dati_100[i] << " ";
|
||||
}
|
||||
cout << endl << endl;
|
||||
|
||||
cout << "Valori letti da file_200.bin (" << letti_200 << "): ";
|
||||
for (int i = 0; i < letti_200; i++) {
|
||||
cout << dati_200[i] << " ";
|
||||
}
|
||||
cout << endl << endl;
|
||||
|
||||
// --- Lettura del file di testo ---
|
||||
int x;
|
||||
int i = 0;
|
||||
|
||||
cout << "Valori letti da file_7.txt: ";
|
||||
while (fscanf(file_7, "%d", &x) == 1 && i < DIM) {
|
||||
cout << x << endl;
|
||||
i++;
|
||||
}
|
||||
cout << "Totale valori letti dal file_7.txt: " << i << endl << endl;
|
||||
|
||||
// --- Estrazione dei valori specifici ---
|
||||
if (letti_70 >= 30) {
|
||||
cout << "30esimo valore nel file_70.bin: " << dati_70[29] << endl;
|
||||
}
|
||||
if (letti_100 >= 30) {
|
||||
cout << "30esimo valore nel file_100.bin: " << dati_100[29] << endl;
|
||||
}
|
||||
if (letti_200 >= 30) {
|
||||
cout << "30esimo valore nel file_200.bin: " << dati_200[29] << endl;
|
||||
}
|
||||
cout << endl;
|
||||
|
||||
if (letti_70 >= 2) {
|
||||
cout << "Secondo valore nel file_70.bin: " << dati_70[1] << endl;
|
||||
}
|
||||
if (letti_100 >= 2) {
|
||||
cout << "Secondo valore nel file_100.bin: " << dati_100[1] << endl;
|
||||
}
|
||||
if (letti_200 >= 2) {
|
||||
cout << "Secondo valore nel file_200.bin: " << dati_200[1] << endl;
|
||||
}
|
||||
cout << endl;
|
||||
|
||||
if (letti_70 >= 2) {
|
||||
cout << "Penultimo valore nel file_70.bin: " << dati_70[letti_70 - 2] << endl;
|
||||
}
|
||||
if (letti_100 >= 2) {
|
||||
cout << "Penultimo valore nel file_100.bin: " << dati_100[letti_100 - 2] << endl;
|
||||
}
|
||||
if (letti_200 >= 2) {
|
||||
cout << "Penultimo valore nel file_200.bin: " << dati_200[letti_200 - 2] << endl;
|
||||
}
|
||||
cout << endl;
|
||||
|
||||
// --- Valori casuali ---
|
||||
if (letti_70 > 0) {
|
||||
int casual = rand() % letti_70;
|
||||
cout << "Valore letto casualmente da file_70.bin: " << dati_70[casual] << endl;
|
||||
}
|
||||
if (letti_100 > 0) {
|
||||
int casual = rand() % letti_100;
|
||||
cout << "Valore letto casualmente da file_100.bin: " << dati_100[casual] << endl;
|
||||
}
|
||||
if (letti_200 > 0) {
|
||||
int casual = rand() % letti_200;
|
||||
cout << "Valore letto casualmente da file_200.bin: " << dati_200[casual] << endl;
|
||||
}
|
||||
|
||||
fclose(file_70);
|
||||
fclose(file_100);
|
||||
fclose(file_200);
|
||||
fclose(file_7);
|
||||
} else {
|
||||
perror("Error");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,38 @@
|
|||
/*
|
||||
Nome: Mario
|
||||
Cognome: Montanari
|
||||
Classe: 3AIN
|
||||
Data: 07/03/2025
|
||||
|
||||
es03: scrivi un programma che, data una stringa di N
|
||||
caratteri, la inverta (per esempio: "Paolo" diventa "oloaP")
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
#include <cstring>
|
||||
|
||||
#define SIZE 100+1
|
||||
|
||||
using namespace std;
|
||||
|
||||
void invertiParola(char *str);
|
||||
|
||||
int main(void) {
|
||||
char str[SIZE];
|
||||
|
||||
cin >> str;
|
||||
|
||||
invertiParola(str);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void invertiParola(char *str) {
|
||||
for (int i = 0; i < strlen(str) / 2; i++) {
|
||||
char temp = str[i];
|
||||
str[i] = str[strlen(str) - i - 1];
|
||||
str[strlen(str) - i - 1] = temp;
|
||||
}
|
||||
|
||||
cout << str;
|
||||
}
|
|
@ -0,0 +1,78 @@
|
|||
/*
|
||||
Nome: Mario
|
||||
Cognome: Montanari
|
||||
Classe: 3AIN
|
||||
Data: 04/04/2025
|
||||
|
||||
es03. (contacaratteri)
|
||||
Scrivere una funzione che, ricevuto in ingresso il nome di un file,
|
||||
restituisca il conteggio dei caratteri presenti al suo interno oppure
|
||||
-1 nel caso in cui non sia possibile accedere al file. Utilizzare la
|
||||
funzione per mostrare il numero di caratteri dei file in.txt, in2.txt
|
||||
e nonesiste.txt (si supponga che quest'ultimo file non sia presente nel
|
||||
disco), emettendo un apposito messaggio in presenza di errori di apertura
|
||||
dei file.
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int contaChrFile(const char * nomeFile);
|
||||
void copiaFile(const char * nomeFileSorgente, const char * nomeFileDestinazione);
|
||||
|
||||
int main(void) {
|
||||
cout << "Caratteri in 'in.txt': " << contaChrFile("in.txt") << endl;
|
||||
copiaFile("in.txt", "in2.txt");
|
||||
cout << "Caratteri in 'in2.txt': " << contaChrFile("in2.txt") << endl;
|
||||
copiaFile("in.txt", "nonesiste.txt");
|
||||
cout << "Caratteri in 'nonesiste.txt': " << contaChrFile("nonesiste.txt") << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int contaChrFile(const char * nomeFile) {
|
||||
int countChr = 0;
|
||||
|
||||
FILE * fp = fopen(nomeFile, "rt");
|
||||
|
||||
if (fp != NULL) {
|
||||
char chr;
|
||||
|
||||
while ((chr = fgetc(fp)) != EOF) {
|
||||
if (chr != '\n') {
|
||||
countChr++;
|
||||
}
|
||||
}
|
||||
|
||||
fclose(fp);
|
||||
} else {
|
||||
return -1;
|
||||
}
|
||||
|
||||
return countChr;
|
||||
}
|
||||
|
||||
void copiaFile(const char * nomeFileSorgente, const char * nomeFileDestinazione) {
|
||||
FILE * fr = fopen(nomeFileSorgente, "rt");
|
||||
|
||||
if (fr != NULL) {
|
||||
FILE * fw = fopen(nomeFileDestinazione, "wt");
|
||||
|
||||
if (fw != NULL) {
|
||||
char chr;
|
||||
|
||||
while ((chr = fgetc(fr)) != EOF) {
|
||||
fputc(chr, fw);
|
||||
}
|
||||
|
||||
fclose(fw);
|
||||
} else {
|
||||
perror("Error");
|
||||
}
|
||||
|
||||
fclose(fr);
|
||||
} else {
|
||||
perror("Error");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,63 @@
|
|||
/*
|
||||
Nome: Mario
|
||||
Cognome: Montanari
|
||||
Classe: 3AIN
|
||||
Data: 16/04/2025
|
||||
|
||||
es03:
|
||||
Scrivi una funzione che preso in ingresso il nome di un file contenente
|
||||
interi, lo modifichi in modo da contenere gli stessi interi ma in ordine
|
||||
decrescente.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#define SIZE 1000+1
|
||||
|
||||
void leggiNumeri(FILE *file);
|
||||
|
||||
int main(void) {
|
||||
FILE *file = fopen("file.bin", "rb");
|
||||
|
||||
if (file != NULL) {
|
||||
leggiNumeri(file);
|
||||
|
||||
fclose(file);
|
||||
} else {
|
||||
perror("Error (source)");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void leggiNumeri(FILE *file) {
|
||||
int numeri[SIZE];
|
||||
int i = 0;
|
||||
|
||||
while (fread(&numeri[i], sizeof(int), 1, file) == 1) {
|
||||
i++;
|
||||
}
|
||||
|
||||
for (int j = 0; j < i - 1; j++) {
|
||||
for (int k = j + 1; k < i; k++) {
|
||||
if (numeri[j] < numeri[k]) {
|
||||
int temp = numeri[j];
|
||||
numeri[j] = numeri[k];
|
||||
numeri[k] = temp;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (int j = 0; j < i; j++) {
|
||||
printf("%d ", numeri[j]);
|
||||
}
|
||||
printf("\n");
|
||||
|
||||
file = fopen("file.bin", "wb");
|
||||
if (file != NULL) {
|
||||
fwrite(numeri, sizeof(int), i, file);
|
||||
fclose(file);
|
||||
} else {
|
||||
perror("Error (write)");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,144 @@
|
|||
/*
|
||||
Nome: Mario
|
||||
Cognome: Montanari
|
||||
Classe: 3AIN
|
||||
Data: 09/05/2025
|
||||
|
||||
Definire una struttura di dati chiamata Persona, che contiene tre campi:
|
||||
- Nome (di tipo array di 20 caratteri);
|
||||
- Cognome (di tipo array di 20 caratteri);
|
||||
- Reddito (di tipo intero).
|
||||
|
||||
1) Visualizzare la dimensione del tipo Persona (aumentare di 1 la dimensione
|
||||
del campo nome e ricontrollare la dimensione del tipo Persona);
|
||||
2) Creare una funzione per assegnare valore ai campi di una persona e
|
||||
scriverla su file binario;
|
||||
3) Controllare il file con hexed.it;
|
||||
4) Creare una funzione che legga da file e inserisca nei campi di una Persona;
|
||||
5) Scrivere su file un array di 5 persone;
|
||||
6) Leggere da file 5 persone e memorizzarle su un array
|
||||
7) Copiare le struct dal file binario a un file di testo organizzando un record per riga
|
||||
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
#include <cstdlib>
|
||||
#include <ctime>
|
||||
|
||||
#define SIZE 20
|
||||
|
||||
#define MAX_PERSONA 3
|
||||
|
||||
#define MIN_ALPHA 65
|
||||
#define MAX_ALPHA 90
|
||||
|
||||
#define MIN_REDDITO 90000
|
||||
#define MAX_REDDITO 15000
|
||||
|
||||
#define MIN_NOME 2
|
||||
#define MAX_NOME 10
|
||||
|
||||
#define MIN_COGNOME 4
|
||||
#define MAX_COGNOME 14
|
||||
|
||||
#define MAX_LINE 1000+1
|
||||
|
||||
using namespace std;
|
||||
|
||||
typedef struct {
|
||||
char nome[SIZE];
|
||||
char cognome[SIZE];
|
||||
int reddito;
|
||||
} Persona;
|
||||
|
||||
typedef struct {
|
||||
char nome_prova[21];
|
||||
char cognome_prova[20];
|
||||
int reddito_prova;
|
||||
} Persona_prova;
|
||||
|
||||
void riempiCampi(Persona persona[MAX_PERSONA], FILE * file) {
|
||||
for (int i = 0; i < MAX_PERSONA; i++) {
|
||||
|
||||
char nome[SIZE];
|
||||
int casual = rand() % (MAX_NOME - MIN_NOME + 1) + MIN_NOME;
|
||||
for (int j = 0; j < casual; j++) {
|
||||
nome[j] = rand() % (MAX_ALPHA - MIN_ALPHA + 1) + MIN_ALPHA;
|
||||
}
|
||||
nome[casual] = '\0';
|
||||
strcpy(persona[i].nome, nome);
|
||||
|
||||
char cognome[SIZE];
|
||||
casual = rand() % (MAX_COGNOME - MIN_COGNOME + 1) + MIN_COGNOME;
|
||||
for (int j = 0; j < casual; j++) {
|
||||
cognome[j] = rand() % (MAX_ALPHA - MIN_ALPHA + 1) + MIN_ALPHA;
|
||||
}
|
||||
cognome[casual] = '\0';
|
||||
strcpy(persona[i].cognome, cognome);
|
||||
|
||||
int reddito = rand() % (MAX_REDDITO - MIN_REDDITO + 1) + MIN_REDDITO;
|
||||
persona[i].reddito = reddito;
|
||||
|
||||
fwrite(&persona[i], sizeof(Persona), 1, file);
|
||||
}
|
||||
}
|
||||
|
||||
void leggiCampi(Persona persona[MAX_PERSONA], FILE * file) {
|
||||
for (int i = 0; i < MAX_PERSONA; i++) {
|
||||
fread(&persona[i], sizeof(Persona), 1, file);
|
||||
|
||||
cout << "Persona " << i + 1 << ":" << endl;
|
||||
cout << " Nome: " << persona[i].nome << endl;
|
||||
cout << " Cognome: " << persona[i].cognome << endl;
|
||||
cout << " Reddito: " << persona[i].reddito << endl << endl;
|
||||
}
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
srand(time(NULL));
|
||||
|
||||
cout << "sizeof(Persona): " << sizeof(Persona) << endl;
|
||||
cout << "sizeof(Persona_prova): " << sizeof(Persona_prova) << endl << endl;
|
||||
|
||||
FILE * file_scritto = fopen("esercizio_3.bin", "wb");
|
||||
|
||||
if (file_scritto != NULL) {
|
||||
Persona persona[MAX_PERSONA];
|
||||
|
||||
riempiCampi(persona, file_scritto);
|
||||
|
||||
fclose(file_scritto);
|
||||
} else {
|
||||
perror("Error");
|
||||
}
|
||||
|
||||
FILE * file_letto = fopen("esercizio_3.bin", "rb");
|
||||
|
||||
if (file_letto != NULL) {
|
||||
Persona persona[MAX_PERSONA];
|
||||
|
||||
leggiCampi(persona, file_letto);
|
||||
|
||||
FILE * file_copia = fopen("esercizio_3.txt", "wt");
|
||||
|
||||
if (file_copia != NULL) {
|
||||
rewind(file_letto);
|
||||
|
||||
Persona copia;
|
||||
|
||||
while (fread(&copia, sizeof(Persona), 1, file_letto) == 1) {
|
||||
fprintf(file_copia, "%s %s %d\n", copia.nome, copia.cognome, copia.reddito);
|
||||
}
|
||||
|
||||
fclose(file_copia);
|
||||
} else {
|
||||
perror("Error (destination)");
|
||||
}
|
||||
|
||||
fclose(file_letto);
|
||||
} else {
|
||||
perror("Error (source)");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,82 @@
|
|||
/*
|
||||
Nome: Mario
|
||||
Cognome: Montanari
|
||||
Classe: 3AIN
|
||||
Data: 04/04/2025
|
||||
|
||||
es04. (contaparole)
|
||||
Un file di testo contiene un certo numero di parole.
|
||||
Ogni parola (a eccezione dell'ultima) e` separata da
|
||||
quella successiva da un solo carattere di whitespace.
|
||||
Scrivere un programma che, richiesta l'immissione da
|
||||
tastiera del nome del file da elaborare, visualizzi il
|
||||
conteggio delle parole contenute (suggerimento: contare
|
||||
i whitespace...).
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
|
||||
#define SIZE_LINE 1000+1
|
||||
|
||||
using namespace std;
|
||||
|
||||
void apriFile(const char * nomeFile);
|
||||
int numeroParoleFile(const char * nomeFile);
|
||||
|
||||
int main(void) {
|
||||
char nomeFile[SIZE_LINE];
|
||||
|
||||
cout << "Immetti il nome del file da aprire: ";
|
||||
cin >> nomeFile;
|
||||
|
||||
cout << endl;
|
||||
apriFile(nomeFile);
|
||||
cout << endl;
|
||||
|
||||
cout << "Numero di parole: " << numeroParoleFile(nomeFile) << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void apriFile(const char * nomeFile) {
|
||||
FILE * fp = fopen(nomeFile, "rt");
|
||||
|
||||
if (fp != NULL) {
|
||||
char str[SIZE_LINE];
|
||||
|
||||
while (fgets(str, sizeof(str), fp) != NULL) {
|
||||
cout << str;
|
||||
}
|
||||
cout << endl;
|
||||
|
||||
fclose(fp);
|
||||
} else {
|
||||
perror("Error");
|
||||
}
|
||||
}
|
||||
|
||||
int numeroParoleFile(const char * nomeFile) {
|
||||
int countWord = 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 (str[i] != ' ') {
|
||||
if (str[i + 1] == ' ' || i == strlen(str) - 1) {
|
||||
countWord++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fclose(fp);
|
||||
} else {
|
||||
return -1;
|
||||
}
|
||||
|
||||
return countWord;
|
||||
}
|
|
@ -0,0 +1,67 @@
|
|||
/*
|
||||
Nome: Mario
|
||||
Cognome: Montanari
|
||||
Classe: 3AIN
|
||||
Data: 17/04/2025
|
||||
|
||||
es04:
|
||||
Scrivi una funzione che preso in ingresso il nome di due file
|
||||
produca un nuovo file ottenuto dalla concatenazione dei due file.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#define SIZE 1000+1
|
||||
|
||||
void concatenaFile(FILE * file_input1, FILE * file_input2);
|
||||
|
||||
int main(void) {
|
||||
char input1[SIZE];
|
||||
char input2[SIZE];
|
||||
|
||||
printf("First input file: ");
|
||||
scanf("%s", input1);
|
||||
|
||||
printf("Second input file: ");
|
||||
scanf("%s", input2);
|
||||
|
||||
FILE * file_input1 = fopen(input1, "rb");
|
||||
FILE * file_input2 = fopen(input2, "rb");
|
||||
|
||||
if (
|
||||
file_input1 != NULL &&
|
||||
file_input2 != NULL
|
||||
) {
|
||||
concatenaFile(file_input1, file_input2);
|
||||
|
||||
fclose(file_input1);
|
||||
fclose(file_input2);
|
||||
} else {
|
||||
perror("Error (source)");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void concatenaFile(FILE * file_input1, FILE * file_input2) {
|
||||
FILE * file_output = fopen("file_output.bin", "wb");
|
||||
|
||||
if (file_output != NULL) {
|
||||
char buffer[SIZE];
|
||||
int byteLetti;
|
||||
|
||||
while ((byteLetti = fread(buffer, 1, sizeof(buffer), file_input1)) > 0) {
|
||||
fwrite(buffer, 1, byteLetti, file_output);
|
||||
}
|
||||
|
||||
while ((byteLetti = fread(buffer, 1, sizeof(buffer), file_input2)) > 0) {
|
||||
fwrite(buffer, 1, byteLetti, file_output);
|
||||
}
|
||||
|
||||
printf("\nCheck the output file.");
|
||||
|
||||
fclose(file_output);
|
||||
} else {
|
||||
perror("Error (destination)");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,474 @@
|
|||
/*
|
||||
Nome: Mario
|
||||
Cognome: Montanari
|
||||
Classe: 3AIN
|
||||
Data: 09/05/2025
|
||||
|
||||
1) Leggere il primo pixel (BGR);
|
||||
2) Modificarlo;
|
||||
3) Elaborare un’immagine (crearla con paint con dimensioni in pixel multiple di 4):
|
||||
- Rendendo i pixel casuali;
|
||||
- Generando blocchi uniformi di colore;
|
||||
4) Inventare un banale algoritmo di compressione e decompressione:
|
||||
- stessi header poi … i prossimi 50 px sono verdi, i prossimi 20 px sono blu, …
|
||||
5) Steganografia:
|
||||
- Nascondere i byte corrispondenti ai caratteri di una parola sparpagliandoli
|
||||
nei pixel secondo una mappa prestabilita;
|
||||
- Nascondere i bit corrispondenti al byte di una o più lettere mascherandoli
|
||||
come lsbit dei pixel secondo una mappa prestabilita;
|
||||
- Decomprimere un file bmp compresso secondo un algoritmo banale (vedi es 4.e).
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
#include <cstdlib>
|
||||
#include <ctime>
|
||||
|
||||
using namespace std;
|
||||
|
||||
#pragma pack(1)
|
||||
|
||||
typedef struct {
|
||||
char bfType[2];
|
||||
int bfSize;
|
||||
int bfReserved;
|
||||
int bfOffBits;
|
||||
} BITMAPFILEHEADER;
|
||||
|
||||
typedef struct {
|
||||
unsigned char b;
|
||||
unsigned char g;
|
||||
unsigned char r;
|
||||
} BGR;
|
||||
|
||||
void compressioneBMP(const char * inputFilename, const char * outputFilename, int lunghezza, int altezza, int offset);
|
||||
void decompressioneBMP(const char * inputFilename, const char * outputFilename, int lunghezza, int altezza, int offset);
|
||||
void nascondiByteNeiPixel(const char * inputFile, const char * outputFile, const char * messaggio, int offset);
|
||||
void estraiByteDaiPixel(const char * filename, int offset, int intervallo, int lunghezzaMessaggio);
|
||||
void nascondiBitNeiPixel(const char * inputFilename, const char * outputFilename, const char * messaggio, int offset);
|
||||
void estraiBitDaiPixel(const char * filename, int offset, int lunghezzaMessaggio);
|
||||
|
||||
int main(void) {
|
||||
cout << "sizeof(BITMAPFILEHEADER): " << sizeof(BITMAPFILEHEADER) << endl;
|
||||
|
||||
FILE * file = fopen("tuttaBianca.bmp", "rb");
|
||||
|
||||
if (file != NULL) {
|
||||
srand(time(NULL));
|
||||
|
||||
int bfOffBits;
|
||||
|
||||
BITMAPFILEHEADER bf;
|
||||
|
||||
// Scrivo l'header del file .bmp
|
||||
fseek(file, 0, SEEK_SET);
|
||||
fread(&bf, sizeof(bf), 1, file);
|
||||
cout << "bfType: " << bf.bfType[0] << bf.bfType[1] << endl;
|
||||
|
||||
fseek(file, 2, SEEK_CUR);
|
||||
cout << "bfSize: " << bf.bfSize << endl;
|
||||
|
||||
fseek(file, 10, SEEK_CUR);
|
||||
cout << "bfOffBits: " << bf.bfOffBits << endl;
|
||||
|
||||
fseek(file, bf.bfOffBits, SEEK_SET);
|
||||
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
// 1. Copiare il file BMP originale in modificato.bmp
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
rewind(file);
|
||||
|
||||
FILE * file_mod = fopen("modificato.bmp", "wb");
|
||||
|
||||
if (file_mod != NULL) {
|
||||
char buffer[1024];
|
||||
int byte;
|
||||
|
||||
while ((byte = fread(buffer, 1, sizeof(buffer), file)) > 0) {
|
||||
fwrite(buffer, 1, byte, file_mod);
|
||||
}
|
||||
|
||||
fclose(file_mod);
|
||||
} else {
|
||||
perror("Error");
|
||||
}
|
||||
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
// 2. Modificare il primo pixel in rosso nel file modificato.bmp
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
file_mod = fopen("modificato.bmp", "r+b");
|
||||
|
||||
if (file_mod != NULL) {
|
||||
fseek(file_mod, bf.bfOffBits, SEEK_SET);
|
||||
|
||||
BGR bgr;
|
||||
|
||||
bgr.b = 255;
|
||||
bgr.g = 0;
|
||||
bgr.r = 0;
|
||||
|
||||
fwrite(&bgr, sizeof(BGR), 1, file_mod);
|
||||
|
||||
fclose(file_mod);
|
||||
} else {
|
||||
perror("Error");
|
||||
}
|
||||
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
// 3. Creare il file modificato2.bmp e riempire con pixel casuali
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
rewind(file);
|
||||
|
||||
FILE * file_mod2 = fopen("modificato2.bmp", "wb");
|
||||
|
||||
if (file_mod2 != NULL) {
|
||||
char buffer[1024];
|
||||
int byte;
|
||||
|
||||
while ((byte = fread(buffer, 1, sizeof(buffer), file)) > 0) {
|
||||
fwrite(buffer, 1, byte, file_mod2);
|
||||
}
|
||||
|
||||
fclose(file_mod2);
|
||||
} else {
|
||||
perror("Error");
|
||||
}
|
||||
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
// Sovrascrivere i pixel con valori casuali in modificato2.bmp
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
file_mod2 = fopen("modificato2.bmp", "r+b");
|
||||
|
||||
if (file_mod2 != NULL) {
|
||||
fseek(file_mod2, bf.bfOffBits, SEEK_SET);
|
||||
|
||||
int lunghezza = 400;
|
||||
int altezza = 400;
|
||||
int padding = (4 - (lunghezza * 3) % 4) % 4;
|
||||
|
||||
fseek(file_mod2, bf.bfOffBits, SEEK_SET);
|
||||
|
||||
for (int y = 0; y < altezza; y++) {
|
||||
for (int x = 0; x < lunghezza; x++) {
|
||||
BGR pixel;
|
||||
|
||||
pixel.b = rand() % 256;
|
||||
pixel.g = rand() % 256;
|
||||
pixel.r = rand() % 256;
|
||||
|
||||
fwrite(&pixel, sizeof(BGR), 1, file_mod2);
|
||||
}
|
||||
|
||||
// Scrivi il padding se necassario
|
||||
for (int p = 0; p < padding; p++) {
|
||||
fputc(0x00, file_mod2);
|
||||
}
|
||||
}
|
||||
|
||||
fclose(file_mod2);
|
||||
} else {
|
||||
perror("Error");
|
||||
}
|
||||
|
||||
cout << endl;
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
// 4A. Compressione del file modificato2.bmp
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
compressioneBMP("modificato2.bmp", "compresso.bmpc", 400, 400, bf.bfOffBits);
|
||||
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
// 4B. Decompressione del file compresso.bmp
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
decompressioneBMP("compresso.bmpc", "decompresso.bmp", 400, 400, bf.bfOffBits);
|
||||
|
||||
cout << endl;
|
||||
|
||||
// Steganografia dei byte interi
|
||||
nascondiByteNeiPixel("tuttaBianca.bmp", "conMessaggio_byte.bmp", "MONTANARI", bf.bfOffBits);
|
||||
estraiByteDaiPixel("conMessaggio_byte.bmp", bf.bfOffBits, 50, 9);
|
||||
|
||||
cout << endl;
|
||||
|
||||
// Steganografia dei bit
|
||||
nascondiBitNeiPixel("tuttaBianca.bmp", "conMessaggio_bit.bmp", "MONTANARI", bf.bfOffBits);
|
||||
estraiBitDaiPixel("conMessaggio_bit.bmp", bf.bfOffBits, 9);
|
||||
|
||||
fclose(file);
|
||||
} else {
|
||||
perror("Error (source)");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void compressioneBMP(const char * inputFilename, const char * outputFilename, int lunghezza, int altezza, int offset) {
|
||||
FILE * inputFile = fopen(inputFilename, "rb");
|
||||
FILE * outputFile = fopen(outputFilename, "wb");
|
||||
|
||||
if (inputFile != NULL && outputFile != NULL) {
|
||||
|
||||
// Copia l'header
|
||||
char header[1024];
|
||||
|
||||
fread(header, 1, offset, inputFile);
|
||||
fwrite(header, 1, offset, outputFile);
|
||||
|
||||
|
||||
int padding = (4 - (lunghezza * 3) % 4) % 4;
|
||||
|
||||
BGR pixel;
|
||||
BGR precedente;
|
||||
int count = 0;
|
||||
bool primo = true;
|
||||
|
||||
// Legge i pixel uno a uno e li comprime
|
||||
for (int y = 0; y < altezza; y++) {
|
||||
for (int x = 0; x < lunghezza; x++) {
|
||||
fread(&pixel, sizeof(BGR), 1, inputFile);
|
||||
|
||||
if (primo) {
|
||||
precedente = pixel;
|
||||
count = 1;
|
||||
primo = false;
|
||||
} else if (
|
||||
pixel.b == precedente.b &&
|
||||
pixel.g == precedente.g &&
|
||||
pixel.r == precedente.r &&
|
||||
count < 255
|
||||
) {
|
||||
count++;
|
||||
} else {
|
||||
// Scrive [numero_pixel_uguali][colore]
|
||||
fputc(count, outputFile);
|
||||
fwrite(&precedente, sizeof(BGR), 1, outputFile);
|
||||
precedente = pixel;
|
||||
count = 1;
|
||||
}
|
||||
}
|
||||
|
||||
fseek(inputFile, padding, SEEK_CUR);
|
||||
}
|
||||
|
||||
// Scrive l'ultimo blocco
|
||||
if (count > 0) {
|
||||
fputc(count, outputFile);
|
||||
fwrite(&precedente, sizeof(BGR), 1, outputFile);
|
||||
}
|
||||
|
||||
cout << "Compressione completata in: " << outputFilename << endl;
|
||||
|
||||
fclose(inputFile);
|
||||
fclose(outputFile);
|
||||
} else {
|
||||
perror("Error");
|
||||
}
|
||||
}
|
||||
|
||||
void decompressioneBMP(const char * inputFilename, const char * outputFilename, int lunghezza, int altezza, int offset) {
|
||||
FILE * inputFile = fopen(inputFilename, "rb");
|
||||
FILE * outputFile = fopen(outputFilename, "wb");
|
||||
|
||||
if (inputFile != NULL && outputFile != NULL) {
|
||||
|
||||
// Leggi e scrivi l'header BMP
|
||||
char header[1024];
|
||||
|
||||
fread(header, 1, offset, inputFile);
|
||||
fwrite(header, 1, offset, outputFile);
|
||||
|
||||
int padding = (4 - (lunghezza * 3) % 4) % 4;
|
||||
int totalPixels = lunghezza * altezza;
|
||||
int writtenPixels = 0;
|
||||
|
||||
while (writtenPixels < totalPixels) {
|
||||
int count = fgetc(inputFile);
|
||||
|
||||
BGR pixel;
|
||||
|
||||
fread(&pixel, sizeof(BGR), 1, inputFile);
|
||||
|
||||
for (int i = 0; i < count; i++) {
|
||||
fwrite(&pixel, sizeof(BGR), 1, outputFile);
|
||||
writtenPixels++;
|
||||
|
||||
// Dopo ogni riga, aggiungi un padding
|
||||
if (writtenPixels % lunghezza == 0) {
|
||||
for (int p = 0; p < padding; p++) {
|
||||
fputc(0x00, outputFile);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fclose(inputFile);
|
||||
fclose(outputFile);
|
||||
|
||||
cout << "Decompressione completata in: " << outputFilename << endl;
|
||||
} else {
|
||||
perror("Error");
|
||||
}
|
||||
}
|
||||
|
||||
// Nasconde ogni byte di un messaggio nei colori di pixel a intervalli
|
||||
void nascondiByteNeiPixel(const char * inputFilename, const char * outputFilename, const char * messaggio, int offset) {
|
||||
FILE * inputFile = fopen(inputFilename, "rb");
|
||||
FILE * outputFile = fopen(outputFilename, "wb");
|
||||
|
||||
if (inputFile != NULL && outputFile != NULL) {
|
||||
char header[1024];
|
||||
|
||||
fread(header, 1, offset, inputFile);
|
||||
fwrite(header, 1, offset, outputFile);
|
||||
|
||||
BGR pixel;
|
||||
|
||||
int i = 0;
|
||||
int intervallo = 50;
|
||||
int posizionePixel = 0;
|
||||
|
||||
int lunghezza = strlen(messaggio);
|
||||
|
||||
while (fread(&pixel, sizeof(BGR), 1, inputFile) == 1) {
|
||||
if (posizionePixel % intervallo == 0 && i < lunghezza) {
|
||||
pixel.r = messaggio[i++];
|
||||
}
|
||||
|
||||
fwrite(&pixel, sizeof(BGR), 1, outputFile);
|
||||
posizionePixel++;
|
||||
}
|
||||
|
||||
fclose(inputFile);
|
||||
fclose(outputFile);
|
||||
|
||||
cout << "Byte nascosti in: " << outputFilename << endl;
|
||||
} else {
|
||||
perror("Error");
|
||||
}
|
||||
}
|
||||
|
||||
// Recupera il messaggio nascosto
|
||||
void estraiByteDaiPixel(const char * filename, int offset, int intervallo, int lunghezzaMessaggio) {
|
||||
FILE * file = fopen(filename, "rb");
|
||||
|
||||
if (file != NULL) {
|
||||
fseek(file, offset, SEEK_SET);
|
||||
|
||||
BGR pixel;
|
||||
|
||||
int posizionePixel = 0;
|
||||
int letti = 0;
|
||||
|
||||
cout << "Messaggio estratto: ";
|
||||
while (fread(&pixel, sizeof(BGR), 1, file) == 1 && letti < lunghezzaMessaggio) {
|
||||
if (posizionePixel % intervallo == 0) {
|
||||
cout << (char)pixel.r;
|
||||
|
||||
letti++;
|
||||
}
|
||||
|
||||
posizionePixel++;
|
||||
}
|
||||
|
||||
cout << endl;
|
||||
|
||||
fclose(file);
|
||||
} else {
|
||||
perror("Error");
|
||||
}
|
||||
}
|
||||
|
||||
// Nasconde bit a bit un messaggio nei pixel
|
||||
void nascondiBitNeiPixel(const char * inputFilename, const char * outputFilename, const char * messaggio, int offset) {
|
||||
FILE * inputFile = fopen(inputFilename, "rb");
|
||||
FILE * outputFile = fopen(outputFilename, "wb");
|
||||
|
||||
if (inputFile != NULL && outputFile != NULL) {
|
||||
char header[1024];
|
||||
|
||||
fread(header, 1, offset, inputFile);
|
||||
fwrite(header, 1, offset, outputFile);
|
||||
|
||||
int posizioneBit = 0;
|
||||
int posizioneChr = 0;
|
||||
|
||||
BGR pixel;
|
||||
|
||||
int len = strlen(messaggio);
|
||||
|
||||
while (fread(&pixel, sizeof(BGR), 1, inputFile) == 1) {
|
||||
if (posizioneChr < len) {
|
||||
char chr = messaggio[posizioneChr];
|
||||
int bit = (chr >> (7 - posizioneBit)) & 1;
|
||||
|
||||
pixel.b = (pixel.b & 0xFE) | bit;
|
||||
|
||||
posizioneBit++;
|
||||
|
||||
if (posizioneBit == 8) {
|
||||
posizioneBit = 0;
|
||||
|
||||
posizioneChr++;
|
||||
}
|
||||
}
|
||||
|
||||
fwrite(&pixel, sizeof(BGR), 1, outputFile);
|
||||
}
|
||||
|
||||
fclose(inputFile);
|
||||
fclose(outputFile);
|
||||
|
||||
cout << "Bit nascosti in: " << outputFilename << endl;
|
||||
} else {
|
||||
perror("Error");
|
||||
}
|
||||
}
|
||||
|
||||
// Recupera i bit nascosti nei pixel
|
||||
void estraiBitDaiPixel(const char * filename, int offset, int lunghezzaMessaggio) {
|
||||
FILE * file = fopen(filename, "rb");
|
||||
|
||||
if (file != NULL) {
|
||||
fseek(file, offset, SEEK_SET);
|
||||
|
||||
BGR pixel;
|
||||
|
||||
int posizioneBit = 0;
|
||||
int chr = 0;
|
||||
int estratti = 0;
|
||||
|
||||
cout << "Messaggio estratto: ";
|
||||
while (fread(&pixel, sizeof(BGR), 1, file) == 1 && estratti < lunghezzaMessaggio) {
|
||||
int bit = pixel.b & 1;
|
||||
|
||||
chr = (chr << 1) | bit;
|
||||
|
||||
posizioneBit++;
|
||||
|
||||
if (posizioneBit == 8) {
|
||||
cout << (char)chr;
|
||||
|
||||
posizioneBit = 0;
|
||||
chr = 0;
|
||||
|
||||
estratti++;
|
||||
}
|
||||
}
|
||||
|
||||
cout << endl;
|
||||
|
||||
fclose(file);
|
||||
} else {
|
||||
perror("Error");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,53 @@
|
|||
/*
|
||||
Nome: Mario
|
||||
Cognome: Montanari
|
||||
Classe: 3AIN
|
||||
Data: 04/04/2025
|
||||
|
||||
es05. (copiafile1)
|
||||
Scrivere una funzione che, ricevuti in ingresso i nomi di
|
||||
due file, effettui la copia carattere per carattere del primo
|
||||
file nel secondo file. La funzione restituisce true se la copia
|
||||
si e` conclusa con successo, false in caso contrario. Utilizzare
|
||||
la funzione per copiare il file in.txt nel file out.txt.
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
|
||||
#define SIZE_LINE 1000+1
|
||||
|
||||
using namespace std;
|
||||
|
||||
bool copiaFile(const char * nomeFileSorgente, const char * nomeFileDestinazione);
|
||||
|
||||
int main(void) {
|
||||
cout << copiaFile("in.txt", "out.txt") << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool copiaFile(const char * nomeFileSorgente, const char * nomeFileDestinazione) {
|
||||
FILE * fr = fopen(nomeFileSorgente, "rt");
|
||||
|
||||
if (fr != NULL) {
|
||||
FILE * fw = fopen(nomeFileDestinazione, "wt");
|
||||
|
||||
if (fw != NULL) {
|
||||
char str[SIZE_LINE];
|
||||
|
||||
while (fgets(str, sizeof(str), fr) != NULL) {
|
||||
fputs(str, fw);
|
||||
}
|
||||
|
||||
fclose(fw);
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
|
||||
fclose(fr);
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
|
@ -0,0 +1,68 @@
|
|||
/*
|
||||
Nome: Mario
|
||||
Cognome: Montanari
|
||||
Classe: 3AIN
|
||||
Data: 24/04/2025
|
||||
|
||||
es05:
|
||||
Scrivi una funzione che preso in ingresso il nome di due file produca un nuovo
|
||||
file ottenuto dalla concatenazione dei due file escludendo però i numeri duplicati.
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
|
||||
#define SIZE_FILE_NAME 1000+1
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main(void) {
|
||||
char in1_name[SIZE_FILE_NAME];
|
||||
char in2_name[SIZE_FILE_NAME];
|
||||
|
||||
cout << "First input file: ";
|
||||
cin >> in1_name;
|
||||
|
||||
cout << "Second input file: ";
|
||||
cin >> in2_name;
|
||||
|
||||
FILE * in1 = fopen(in1_name, "rb");
|
||||
FILE * in2 = fopen(in2_name, "rb");
|
||||
|
||||
if ((in1 != NULL) && (in2 != NULL)) {
|
||||
|
||||
FILE * out = fopen("out.bin", "wb");
|
||||
|
||||
if (out != NULL) {
|
||||
int num;
|
||||
int ultimo_letto = -1;
|
||||
|
||||
while (fread(&num, sizeof(int), 1, in1) == 1) {
|
||||
cout << "Letto da in1: " << num << endl;
|
||||
if (num != ultimo_letto) {
|
||||
fwrite(&num, sizeof(int), 1, out);
|
||||
ultimo_letto = num;
|
||||
}
|
||||
}
|
||||
|
||||
//fwrite("\n", sizeof(char), 1, out);
|
||||
while (fread(&num, sizeof(int), 1, in2) == 1) {
|
||||
cout << "Letto da in2: " << num << endl;
|
||||
if (num != ultimo_letto) {
|
||||
fwrite(&num, sizeof(int), 1, out);
|
||||
ultimo_letto = num;
|
||||
}
|
||||
}
|
||||
|
||||
fclose(out);
|
||||
} else {
|
||||
perror("Error (destination)");
|
||||
}
|
||||
|
||||
fclose(in1);
|
||||
fclose(in2);
|
||||
} else {
|
||||
perror("Error (source)");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,209 @@
|
|||
/*
|
||||
Nome: Mario
|
||||
Cognome: Montanari
|
||||
Classe: 3AIN
|
||||
Data: 14/05/2025
|
||||
|
||||
Scrivere un programma che replica le funzionalità base dell'editor
|
||||
esadecimale Hexed.it
|
||||
|
||||
1) Funzione che visualizza in esadecimale il valore dei byte contenuti
|
||||
nel nomeFile ricevuto come parametro:
|
||||
- Andare 1a capo ogni LunRiga byte;
|
||||
- LunRiga = 16;
|
||||
- LunRiga chiesto in input dal main e passato alla funzione;
|
||||
- LunRiga letto dal file di testo editor16.ini contenente una
|
||||
riga ad esempio “LunRiga 16”.
|
||||
|
||||
2) Funzione che riceve nomeFile come parametro, chiede all’utente l’offset
|
||||
di un byte del file, mostra il valore (little-endian) di:
|
||||
- Un byte in decimale ed esadecimale;
|
||||
- Un intero 32 bit;
|
||||
- Un float 32 bit.
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
#include <iomanip>
|
||||
|
||||
using namespace std;
|
||||
|
||||
void creaFileBin(const char * filename);
|
||||
void visualizzaEsadecimale(const char * filename, int lungRiga);
|
||||
void visualizzaEsadecimaleFile(const char * inputFile, const char * outputFile);
|
||||
void mostraValoriOffset(const char * filename);
|
||||
|
||||
int main(void) {
|
||||
creaFileBin("file_esadecimale.bin");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void creaFileBin(const char * filename) {
|
||||
int lungRiga;
|
||||
|
||||
cout << "Inserisci lungRiga: ";
|
||||
cin >> lungRiga;
|
||||
|
||||
FILE * file = fopen(filename, "wb");
|
||||
|
||||
if (file != NULL) {
|
||||
for (int i = 0; i < 50; i++) {
|
||||
fwrite(&i, sizeof(int), 1, file);
|
||||
}
|
||||
|
||||
fclose(file);
|
||||
} else {
|
||||
perror("Error (source)");
|
||||
}
|
||||
|
||||
visualizzaEsadecimale("file_esadecimale.bin", lungRiga);
|
||||
// visualizzaEsadecimaleFile("file_esadecimale.bin", "editor16.ini");
|
||||
mostraValoriOffset("file_esadecimale.bin");
|
||||
}
|
||||
|
||||
void visualizzaEsadecimale(const char * filename, int lungRiga) {
|
||||
FILE * file = fopen(filename, "rb");
|
||||
|
||||
if (file != NULL) {
|
||||
char valoreEsadecimale;
|
||||
int count = 0;
|
||||
|
||||
while (fread(&valoreEsadecimale, sizeof(char), 1, file) == 1) {
|
||||
cout << setw(2) << setfill('0') << hex << (int)valoreEsadecimale << " ";
|
||||
count++;
|
||||
|
||||
if (count % lungRiga == 0) {
|
||||
cout << endl;
|
||||
}
|
||||
}
|
||||
|
||||
fclose(file);
|
||||
} else {
|
||||
perror("Error (source)");
|
||||
}
|
||||
}
|
||||
|
||||
void visualizzaEsadecimaleFile(const char * inputFile, const char * outputFile) {
|
||||
FILE * fileIn = fopen(inputFile, "rb");
|
||||
|
||||
if (fileIn != NULL) {
|
||||
FILE * fileOut = fopen(outputFile, "rt");
|
||||
|
||||
if (fileOut != NULL) {
|
||||
int lungRiga = 0;
|
||||
char line[1000+1];
|
||||
|
||||
if (fgets(line, sizeof(line), fileOut) != NULL) {
|
||||
char * token = strtok(line, " \t");
|
||||
token = strtok(NULL, " \t\n");
|
||||
|
||||
if (token != NULL) {
|
||||
lungRiga = atoi(token);
|
||||
cout << "LunRiga letto dal file: " << lungRiga << endl;
|
||||
} else {
|
||||
cout << "Errore nella lettura della riga." << endl;
|
||||
fclose(fileIn);
|
||||
fclose(fileOut);
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
cout << "Errore nella lettura della riga." << endl;
|
||||
fclose(fileIn);
|
||||
fclose(fileOut);
|
||||
return;
|
||||
}
|
||||
|
||||
unsigned char valore;
|
||||
int count = 0;
|
||||
|
||||
while (fread(&valore, sizeof(char), 1, fileIn) == 1) {
|
||||
cout << setw(2) << setfill('0') << hex << (int)valore << " ";
|
||||
count++;
|
||||
|
||||
if (count % lungRiga == 0) {
|
||||
cout << endl;
|
||||
}
|
||||
}
|
||||
|
||||
fclose(fileOut);
|
||||
} else {
|
||||
perror("Error (destination)");
|
||||
}
|
||||
|
||||
fclose(fileIn);
|
||||
} else {
|
||||
perror("Error (source)");
|
||||
}
|
||||
}
|
||||
|
||||
void mostraValoriOffset(const char * filename) {
|
||||
FILE * file = fopen(filename, "rb");
|
||||
|
||||
if (file == NULL) {
|
||||
perror("Errore apertura file");
|
||||
return;
|
||||
}
|
||||
|
||||
int offset;
|
||||
|
||||
cout << "\nInserisci offset del byte da leggere: ";
|
||||
cin >> offset;
|
||||
|
||||
if (fseek(file, offset, SEEK_SET) != 0) {
|
||||
cout << "Errore: offset non valido." << endl;
|
||||
fclose(file);
|
||||
return;
|
||||
}
|
||||
|
||||
fseek(file, 0, SEEK_END);
|
||||
long fileSize = ftell(file);
|
||||
rewind(file);
|
||||
|
||||
if (offset < 0 || offset >= fileSize) {
|
||||
cout << "Errore: offset fuori dai limiti del file." << endl;
|
||||
fclose(file);
|
||||
return;
|
||||
}
|
||||
|
||||
unsigned char byte;
|
||||
|
||||
if (fread(&byte, sizeof(byte), 1, file) != 1) {
|
||||
cout << "Errore nella lettura del byte." << endl;
|
||||
fclose(file);
|
||||
return;
|
||||
}
|
||||
|
||||
cout << "\nByte letto all'offset " << offset << ": "
|
||||
<< dec << (int)byte << " (DEC) | "
|
||||
<< hex << setw(2) << setfill('0') << (int)byte << " (HEX)" << endl;
|
||||
|
||||
if (fseek(file, offset, SEEK_SET) != 0) {
|
||||
cout << "Errore nel seek per lettura int." << endl;
|
||||
fclose(file);
|
||||
return;
|
||||
}
|
||||
|
||||
int valoreInt;
|
||||
|
||||
if (fread(&valoreInt, sizeof(valoreInt), 1, file) == 1) {
|
||||
cout << "Valore intero (32 bit): " << dec << valoreInt << endl;
|
||||
} else {
|
||||
cout << "Errore lettura int." << endl;
|
||||
}
|
||||
|
||||
if (fseek(file, offset, SEEK_SET) != 0) {
|
||||
cout << "Errore nel seek per lettura float." << endl;
|
||||
fclose(file);
|
||||
return;
|
||||
}
|
||||
|
||||
float valoreFloat;
|
||||
|
||||
if (fread(&valoreFloat, sizeof(valoreFloat), 1, file) == 1) {
|
||||
cout << "Valore float (32 bit): " << fixed << setprecision(8) << valoreFloat << endl;
|
||||
} else {
|
||||
cout << "Errore lettura float." << endl;
|
||||
}
|
||||
|
||||
fclose(file);
|
||||
}
|
|
@ -0,0 +1,62 @@
|
|||
/*
|
||||
Nome: Mario
|
||||
Cognome: Montanari
|
||||
Classe: 3AIN
|
||||
Data: 04/04/2025
|
||||
|
||||
es06. (copiafile2)
|
||||
Scrivere un programma che effettui la copia un file
|
||||
di testo in un altro file trasformando le lettere
|
||||
minuscole in maiuscole e le maiuscole in minuscole.
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
#include <cctype>
|
||||
|
||||
using namespace std;
|
||||
|
||||
char reverseLowerUpper(char chr);
|
||||
void copiaFile(const char * nomeFileSorgente, const char * nomeFileDestinazione);
|
||||
|
||||
int main(void) {
|
||||
copiaFile("in.txt", "out.txt");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
char reverseLowerUpper(char chr) {
|
||||
if (isalpha(chr)) {
|
||||
if (islower(chr)) {
|
||||
chr = toupper(chr);
|
||||
} else if (isupper(chr)) {
|
||||
chr = tolower(chr);
|
||||
}
|
||||
}
|
||||
|
||||
return chr;
|
||||
}
|
||||
|
||||
void copiaFile(const char * nomeFileSorgente, const char * nomeFileDestinazione) {
|
||||
FILE * fr = fopen(nomeFileSorgente, "rt");
|
||||
|
||||
if (fr != NULL) {
|
||||
FILE * fw = fopen(nomeFileDestinazione, "wt");
|
||||
|
||||
if (fw != NULL) {
|
||||
char chr;
|
||||
|
||||
while ((chr = fgetc(fr)) != EOF) {
|
||||
chr = reverseLowerUpper(chr);
|
||||
fputc(chr, fw);
|
||||
}
|
||||
|
||||
fclose(fw);
|
||||
} else {
|
||||
perror("Error (destination)");
|
||||
}
|
||||
|
||||
fclose(fr);
|
||||
} else {
|
||||
perror("Error (source)");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,63 @@
|
|||
/*
|
||||
Nome: Mario
|
||||
Cognome: Montanari
|
||||
Classe: 3AIN
|
||||
Data: 25/04/2025
|
||||
|
||||
es06:
|
||||
Scrivi una funzione che preso in ingresso il nome di due file
|
||||
produca un nuovo file ottenuto dalla intersezione dei due file,
|
||||
cioè inserendo solo i numeri in comune presenti nei due file.
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
|
||||
#define SIZE 100+1
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main(void) {
|
||||
char in1_file[SIZE];
|
||||
char in2_file[SIZE];
|
||||
|
||||
cout << "First input file: ";
|
||||
cin >> in1_file;
|
||||
|
||||
cout << "Second input file: ";
|
||||
cin >> in2_file;
|
||||
|
||||
FILE * in1 = fopen(in1_file, "rb");
|
||||
FILE * in2 = fopen(in2_file, "rb");
|
||||
|
||||
if (in1 != NULL && in2 != NULL) {
|
||||
FILE * out = fopen("out.bin", "wb");
|
||||
|
||||
if (out != NULL) {
|
||||
int num1;
|
||||
int num2;
|
||||
|
||||
while (fread(&num1, sizeof(int), 1, in1) == 1) {
|
||||
rewind(in2);
|
||||
|
||||
while (fread(&num2, sizeof(int), 1, in2) == 1) {
|
||||
if (num1 == num2) {
|
||||
fwrite(&num1, sizeof(int), 1, out);
|
||||
cout << num1 << " ";
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fclose(out);
|
||||
} else {
|
||||
perror("Error (destination)");
|
||||
}
|
||||
|
||||
fclose(in1);
|
||||
fclose(in2);
|
||||
} else {
|
||||
perror("Error (source)");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,55 @@
|
|||
/*
|
||||
Nome: Mario
|
||||
Cognome: Montanari
|
||||
Classe: 3AIN
|
||||
Data: 09/05/2025
|
||||
|
||||
Creare un file di 10 byte casuali e contare quanti byte hanno il bit3 a 1
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
#include <cstdlib>
|
||||
#include <ctime>
|
||||
|
||||
#define BYTE 10
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main(void) {
|
||||
srand(time(NULL));
|
||||
|
||||
FILE * file = fopen("file.bin", "wb");
|
||||
|
||||
if (file != NULL) {
|
||||
for (int i = 0; i < BYTE; i++) {
|
||||
char byte = rand() % 256;
|
||||
fwrite(&byte, sizeof(char), 1, file);
|
||||
}
|
||||
|
||||
fclose(file);
|
||||
} else {
|
||||
perror("Error");
|
||||
}
|
||||
|
||||
file = fopen("file.bin", "rb");
|
||||
|
||||
if (file != NULL) {
|
||||
int count = 0;
|
||||
char byte;
|
||||
|
||||
for (int i = 0; i < BYTE; i++) {
|
||||
fread(&byte, sizeof(char), 1, file);
|
||||
|
||||
if (byte & (1 << 3)) {
|
||||
count++;
|
||||
}
|
||||
}
|
||||
cout << count << endl;
|
||||
|
||||
fclose(file);
|
||||
} else {
|
||||
perror("Error");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,59 @@
|
|||
/*
|
||||
Nome: Mario
|
||||
Cognome: Montanari
|
||||
Classe: 3AIN
|
||||
Data: 09/04/2025
|
||||
|
||||
es07. (copialettere)
|
||||
Scrivere un programma che, richiesto da tastiera il nome di un file testo,
|
||||
memorizzi i caratteri dell'alfabeto nel file lettere.txt e tutti gli altri
|
||||
caratteri in altri_car.txt.
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
void apriFile(const char * nomeFileSorgente, const char * nomeFileDestinazione1, const char * nomeFileDestinazione2);
|
||||
|
||||
int main(void) {
|
||||
apriFile("testo.txt", "lettere.txt", "altri_caratteri.txt");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void apriFile(const char * nomeFileSorgente, const char * nomeFileDestinazione1, const char * nomeFileDestinazione2) {
|
||||
FILE * fr = fopen(nomeFileSorgente, "rt");
|
||||
|
||||
if (fr != NULL) {
|
||||
FILE * fw1 = fopen(nomeFileDestinazione1, "wt");
|
||||
|
||||
if (fw1 != NULL) {
|
||||
FILE * fw2 = fopen(nomeFileDestinazione2, "wt");
|
||||
|
||||
if (fw2 != NULL) {
|
||||
char chr;
|
||||
|
||||
while ((chr = fgetc(fr)) != EOF) {
|
||||
if (isalpha(chr)) {
|
||||
fputc(chr, fw1);
|
||||
} else {
|
||||
fputc(chr, fw2);
|
||||
}
|
||||
}
|
||||
|
||||
fclose(fw2);
|
||||
} else {
|
||||
perror("Error (destination)");
|
||||
}
|
||||
|
||||
fclose(fw1);
|
||||
} else {
|
||||
perror("Error (destination)");
|
||||
}
|
||||
|
||||
fclose(fr);
|
||||
} else {
|
||||
perror("Error (source)");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,40 @@
|
|||
/*
|
||||
Nome: Mario
|
||||
Cognome: Montanari
|
||||
Classe: 3AIN
|
||||
Data: 19/04/2025
|
||||
|
||||
es07:
|
||||
Scrivi un programma che legga da un file "numeri.dat" di 10 righe che rappresenti
|
||||
un array di 10 elementi interi, con ogni elemento memorizzato in una riga, e li
|
||||
memorizzi in un array.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#define SIZE 10
|
||||
|
||||
int main(void) {
|
||||
FILE *file = fopen("numeri.dat", "rb");
|
||||
|
||||
if (file != NULL) {
|
||||
int array[SIZE];
|
||||
|
||||
if (fread(array, sizeof(int), SIZE, file)) {
|
||||
|
||||
printf("Elementi dell'array: ");
|
||||
for (int i = 0; i < SIZE; i++) {
|
||||
printf("%d ", array[i]);
|
||||
}
|
||||
printf("\n");
|
||||
fclose(file);
|
||||
|
||||
} else {
|
||||
perror("Errore durante la lettura del file");
|
||||
}
|
||||
} else {
|
||||
perror("Errore nell'apertura del file");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,47 @@
|
|||
/*
|
||||
Nome: Mario
|
||||
Cognome: Montanari
|
||||
Classe: 3AIN
|
||||
Data: 2025
|
||||
|
||||
Dato un file di interi 32bit little-endian, traformarlo in big-endian.
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main(void) {
|
||||
const char * inputFile = "interi_littleEndian.bin";
|
||||
const char * outputFile = "interi_bigEndian.bin";
|
||||
|
||||
FILE * fileIn = fopen(inputFile, "rb");
|
||||
|
||||
if (fileIn != NULL) {
|
||||
FILE * fileOut = fopen(outputFile, "wb");
|
||||
|
||||
if (fileOut != NULL) {
|
||||
uint32_t numero;
|
||||
|
||||
while (fread(&numero, sizeof(numero), 1, fileIn) == 1) {
|
||||
uint32_t converti =
|
||||
((numero & 0x000000FF) << 24) |
|
||||
((numero & 0x0000FF00) << 8) |
|
||||
((numero & 0x00FF0000) >> 8) |
|
||||
((numero & 0xFF000000) >> 24);
|
||||
|
||||
fwrite(&converti, sizeof(converti), 1, fileOut);
|
||||
}
|
||||
|
||||
fclose(fileOut);
|
||||
} else {
|
||||
perror("Error (destination)");
|
||||
}
|
||||
|
||||
fclose(fileIn);
|
||||
} else {
|
||||
perror("Error (source)");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,47 @@
|
|||
/*
|
||||
Nome: Mario
|
||||
Cognome: Montanari
|
||||
Classe: 3AIN
|
||||
Data: 13/05/2025
|
||||
|
||||
Dato un file di interi 32bit big-endian, traformarlo in little-endian.
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main(void) {
|
||||
const char * inputFile = "interi_bigEndian.bin";
|
||||
const char * outputFile = "interi_littleEndian.bin";
|
||||
|
||||
FILE * fileIn = fopen(inputFile, "rb");
|
||||
|
||||
if (fileIn != NULL) {
|
||||
FILE * fileOut = fopen(outputFile, "wb");
|
||||
|
||||
if (fileOut != NULL) {
|
||||
uint32_t numero;
|
||||
|
||||
while (fread(&numero, sizeof(numero), 1, fileIn) == 1) {
|
||||
uint32_t converti =
|
||||
((numero & 0x000000FF) << 24) |
|
||||
((numero & 0x0000FF00) << 8) |
|
||||
((numero & 0x00FF0000) >> 8) |
|
||||
((numero & 0xFF000000) >> 24);
|
||||
|
||||
fwrite(&converti, sizeof(converti), 1, fileOut);
|
||||
}
|
||||
|
||||
fclose(fileOut);
|
||||
} else {
|
||||
perror("Error (destination)");
|
||||
}
|
||||
|
||||
fclose(fileIn);
|
||||
} else {
|
||||
perror("Error (source)");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue