Update verifica_4.cpp

This commit is contained in:
Vichingo455 2025-03-07 13:55:37 +00:00
parent 5b61a76c03
commit cbb1494fa5
1 changed files with 125 additions and 125 deletions

View File

@ -1,125 +1,125 @@
#include <stdio.h> #include <stdio.h>
#include <ctype.h> #include <ctype.h>
#include <string.h> #include <string.h>
#include <iostream> #include <iostream>
#include <cstdlib> #include <cstdlib>
#define DIM (10+3+3+1+1) //msg2 con carattere di controllo #define DIM (10+3+3+1+1) //msg2 con carattere di controllo
#define DEST 3 #define DEST 3
#define SORG 3 #define SORG 3
#define MSG1 10 #define MSG1 10
using namespace std; using namespace std;
bool isThreeDigitNumeric(const char *str) { bool isThreeDigitNumeric(const char *str) {
int digits = 0; int digits = 0;
for (int i = 0; str[i] != '\0'; i++) for (int i = 0; str[i] != '\0'; i++)
if (isdigit(str[i])) if (isdigit(str[i]))
digits++; digits++;
return digits == 3; return digits == 3;
} }
void anteponi(const char *dest, const char *sorg, const char *msg1, char *msg2) { void anteponi(const char *dest, const char *sorg, const char *msg1, char *msg2) {
char terminator[] = {'\0'}; char terminator[] = {'\0'};
strcat(msg2,dest); strcat(msg2,dest);
strcat(msg2,sorg); strcat(msg2,sorg);
strcat(msg2,msg1); strcat(msg2,msg1);
cout << msg2; cout << msg2;
} }
void rovina(char * const msg2) { void rovina(char * const msg2) {
int lunghezza = strlen(msg2); int lunghezza = strlen(msg2);
size_t i = rand() % lunghezza-2; //Evita il carattere di controllo e di check size_t i = rand() % lunghezza-2; //Evita il carattere di controllo e di check
msg2[i] = 'A' + rand() % 26; msg2[i] = 'A' + rand() % 26;
} }
// restituisce una lettera maiuscola calcolata come somma di tutti i caratteri di msg2 // restituisce una lettera maiuscola calcolata come somma di tutti i caratteri di msg2
// rimappati nelle lettere maiuscole // rimappati nelle lettere maiuscole
// es sommaCar=70 -> ck='F' // es sommaCar=70 -> ck='F'
// es sommaCar=122 -> ck= 'F' // es sommaCar=122 -> ck= 'F'
char checkCar(char * const msg2){ char checkCar(char * const msg2){
int sum = 0; int sum = 0;
int resto = 0; int resto = 0;
for (size_t i = 0; msg2[i] != '\0'; i++) { for (size_t i = 0; msg2[i] != '\0'; i++) {
sum += (char)msg2[i]; sum += (char)msg2[i];
} }
resto = sum % ('Z' - 'A' + 1); resto = sum % ('Z' - 'A' + 1);
return 'A' + resto; return 'A' + resto;
} }
//Aggiungi in fondo il carattere di controllo //Aggiungi in fondo il carattere di controllo
void addCheckCar(char * const msg2, char ck) { void addCheckCar(char * const msg2, char ck) {
int lunghezza = strlen(msg2); int lunghezza = strlen(msg2);
msg2[lunghezza] = ck; msg2[lunghezza] = ck;
msg2[lunghezza+1] = '\0'; msg2[lunghezza+1] = '\0';
} }
char estraiCheckCar(char * const msg2){ char estraiCheckCar(char * const msg2){
int lunghezza = strlen(msg2); int lunghezza = strlen(msg2);
return msg2[lunghezza-1]; return msg2[lunghezza-1];
} }
void delCheckCar(char * const msg2){ void delCheckCar(char * const msg2){
int lunghezza = strlen(msg2); int lunghezza = strlen(msg2);
msg2[lunghezza-1] = '\0'; msg2[lunghezza-1] = '\0';
} }
int main() { int main() {
char dest[DEST+1] = "752"; char dest[DEST+1] = "752";
char sorg[SORG+1] = "402"; char sorg[SORG+1] = "402";
char msg1[MSG1+1] = "FALENA"; //lunghezza 6 fissa char msg1[MSG1+1] = "FALENA"; //lunghezza 6 fissa
char msg2[DEST+SORG+MSG1+1+1];//ci sarà anche un controllo char msg2[DEST+SORG+MSG1+1+1];//ci sarà anche un controllo
memset(msg2, 0, sizeof(msg2)); // Inizializza l'array memset(msg2, 0, sizeof(msg2)); // Inizializza l'array
cout << "dest: " << dest << endl; cout << "dest: " << dest << endl;
cout << "sorg: " << sorg << endl; cout << "sorg: " << sorg << endl;
cout << "msg1: " << msg1 << endl; cout << "msg1: " << msg1 << endl;
cout << "msg2: " << msg2 << endl << endl; cout << "msg2: " << msg2 << endl << endl;
//controlla che dest e sorg siano composte da 3 caratteri numerici //controlla che dest e sorg siano composte da 3 caratteri numerici
if (!isThreeDigitNumeric(dest)) { if (!isThreeDigitNumeric(dest)) {
cout << "Errore: dest non è un numero di 3 cifre!" << endl; cout << "Errore: dest non è un numero di 3 cifre!" << endl;
exit(1); exit(1);
} }
if (!isThreeDigitNumeric(sorg)) { if (!isThreeDigitNumeric(sorg)) {
cout << "Errore: sorg non è un numero di 3 cifre!" << endl; cout << "Errore: sorg non è un numero di 3 cifre!" << endl;
exit(1); exit(1);
} }
// Anteponi dest e sorg a msg1 e costruisci msg2 // Anteponi dest e sorg a msg1 e costruisci msg2
anteponi(dest, sorg, msg1, msg2); anteponi(dest, sorg, msg1, msg2);
cout << "dest + sorg + msg1 = msg2: " << msg2 << endl << endl; cout << "dest + sorg + msg1 = msg2: " << msg2 << endl << endl;
char ck1 = checkCar(msg2); //calcla il carattere di controllo char ck1 = checkCar(msg2); //calcla il carattere di controllo
cout << "carattere di controllo: " << ck1 << endl << endl; cout << "carattere di controllo: " << ck1 << endl << endl;
// Aggiungi in fondo il carattere di controllo // Aggiungi in fondo il carattere di controllo
addCheckCar(msg2, ck1); addCheckCar(msg2, ck1);
cout << "Messaggio con controllo: " << msg2 << endl << endl; cout << "Messaggio con controllo: " << msg2 << endl << endl;
// Rovina il messaggio // Rovina il messaggio
// modifica una caratere QUALSIASI di msg2 // modifica una caratere QUALSIASI di msg2
rovina(msg2); rovina(msg2);
cout << "Messaggio rovinato" << endl << endl; cout << "Messaggio rovinato" << endl << endl;
cout << "Messaggio trasmesso: " << msg2 << endl << endl; cout << "Messaggio trasmesso: " << msg2 << endl << endl;
ck1 = estraiCheckCar(msg2); //estrai il ck1 che mi è arrivato ck1 = estraiCheckCar(msg2); //estrai il ck1 che mi è arrivato
cout << "ck1: " << ck1 << endl; cout << "ck1: " << ck1 << endl;
delCheckCar(msg2); //accorcio msg2 togliendo il ck delCheckCar(msg2); //accorcio msg2 togliendo il ck
cout << "msg2: " << msg2 << endl << endl; cout << "msg2: " << msg2 << endl << endl;
//ricalcola il carattere di controllo //ricalcola il carattere di controllo
char ck2 = checkCar(msg2); char ck2 = checkCar(msg2);
cout << "carattere di controllo: " << ck2 << endl << endl; cout << "carattere di controllo: " << ck2 << endl << endl;
if(ck1 != ck2){ if(ck1 != ck2){
cout << "ck1 != ck2 ERRORE NELLA TRASMISSIONE" << endl; cout << "ck1 != ck2 ERRORE NELLA TRASMISSIONE" << endl;
} }
else{ else{
cout << "ck1 == ck2 TRASMISSIONE corretta" << endl; cout << "ck1 == ck2 TRASMISSIONE corretta" << endl;
} }
return 0; return 0;
} }