|
|||||||
|
|
|
![]() |
|
|
Strumenti |
|
|
#1 |
|
Junior Member
Iscritto dal: Sep 2011
Messaggi: 1
|
[C++] Vigenere con alfabeto italiano
salve a tutti...
![]() ho scritto un programma per implementare il cifrario di vigenere usando l'alfabeto italiano. tutto sembra funzionare perfettamente, o almeno quasi tutto. nel programma ho definito una funzione in questo modo Codice:
string encode(string text){
string line;
char ALPH[21] = {'A','B','C','D','E','F','G','H','I','L','M','N','O','P','Q','R','S','T','U','V','Z'};
text = StringToUpper(text);
if(!text.empty()){
text = EraseChar(text);
}
cout << "String text: " << text << endl;
int index_k[key.length()]; //array degli indici della chiave
for(int i = 0; i < key.length(); i++){
for(int j = 0; j < 21; j++){
if(key[i] == ALPH[j])
index_k[i] = j;
}
cout << "Indice chiave " << i << ": " << index_k[i] << endl;
}
int index_t[text.length()]; //array degli indici del testo
for(int i = 0; i < text.length(); i++){
for(int j = 0; j < 21; j++){
if(text[i] == ALPH[j])
index_t[i] = j;
}
cout << "Indice testo " << i << ": " << index_t[i] << endl;
}
//calcola la lettera codificata
for(int i = 0; i < text.length(); i++){
line[i] = ALPH[(index_k[i%key.length()] + index_t[i])%21];
cout << "Lettera codificata " << i << ": " << line[i] << endl;
}
return line;
}
Codice:
string testo;
cout << "Inserire testo\n";
cin >> testo;
string cifrato = cipher.encode(testo);
cout << "Testo codificato: " << cifrato << endl;
eppure tramite i debug prints che ho inserito all'interno della definizione della funzione, si può verificare chiaramente che la funzione svolge correttamente tutte le operazione che dovrebbe fare. |
|
|
|
|
|
#2 |
|
Senior Member
Iscritto dal: Oct 2007
Città: Padova
Messaggi: 4131
|
Hai allocato la memoria per la tua string line, dentro la tua funzione 'encode'?
__________________
As long as you are basically literate in programming, you should be able to express any logical relationship you understand. If you don’t understand a logical relationship, you can use the attempt to program it as a means to learn about it. (Chris Crawford) |
|
|
|
|
| Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 02:45.




















