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