C++ 链表凯撒密码 Vigenere Cipher
Posted
技术标签:
【中文标题】C++ 链表凯撒密码 Vigenere Cipher【英文标题】:C++ Linked List Caesar Cipher Vigenere Cipher 【发布时间】:2016-11-30 20:17:59 【问题描述】:我无法让此代码正常工作。我不清楚如何使用链表来实现凯撒密码和维吉纳密码。任何解决此问题的帮助将不胜感激。 该程序应该接受用户的菜单选择。用户将使用创建链接列表的字符字符串填充列表。密码应该比较链表中的每个元素并根据输入的密钥进行加密/解密。
#include <iostream>
using namespace std;
#include <string>
#undef NULL
const int NULL = 0;
const string ELEMENT_NAME = "whole number";
typedef int element;
const char SENTINEL = '\n';
class listnode
public:
element data;
listnode * next;
;
class LList
private:
listnode * head;
listnode * tail;
public:
LList();
~LList();
void Print();
void InsertHead(element thing);
void InsertTail(element thing);
element DeleteHead();
void ReadForward();
void ReadBackward();
void Clean();
void Steal(LList & Victim);
void Duplicate(LList & Source);
void Reverse();
void displayMenu();
char userval;
void CaesarEncryption(element CaesarKey);
void CaesarDecryption(element CaesarKey);
void VigenereEncryption(char VigenereKey);
void VigenereDecryption(char VigenereKey);
char EncryptedMessage;
char DecryptedMessage;
void Display();
;
element read_element();
// Main function
int main()
// Declare the Ceaser key
element CaesarKey;
//Declare the Vigenere key
char VigenereKey;
// Declare menu_choice
char menu_choice;
LList L;
do
L.Display();
// Display the menu
L.displayMenu();
// Prompt the user to enter their menu selection
cout << "Please make your menu selection: " << endl;
cin >> menu_choice;
// Use validation techniques to make sure the user enters a
//valid menu choice
while((menu_choice != 'h')&&(menu_choice != 'c')&&
(menu_choice != 'C')&&(menu_choice != 'v')&& (menu_choice!= 'V')&&(menu_choice != 'q')&&(menu_choice != 'm'))
cout << "Error Invalid Choice. Re-enter:";
cin >> menu_choice;
switch(menu_choice)
case 'h' :
L.displayMenu();
break;
case 'm' :
// Read the message from the user
L.ReadForward();
break;
case 'c':
// Prompt the user
cout << "Enter the numeric key" ;
// Get the user to enter the key
CaesarKey = read_element();
// encrypt using ceaser cipher
L.CaesarEncryption(CaesarKey);
//display the current message
cout << "The current key is " << CaesarKey;
cout << "." << endl;
break;
case 'C':
// prompt the user
cout << "Enter the numeric key: ";
// Get the user to enter the key
CaesarKey = read_element();
// decrypt using ceaser cipher
L.CaesarDecryption(CaesarKey);
cout << "The current key is " << CaesarKey;
cout << "." << endl;
break;
case 'v':
// Prompt the user
cout << "Enter the key: ";
// Get the user to enter the key
VigenereKey = cin.get();
//encrypt using vigenere cipher
L.VigenereEncryption(VigenereKey);
//display the key
cout << "The current key is " << VigenereKey;
cout << "." << endl;
break;
case 'V':
// Prompt the user
cout << "Please enter the key: ";
//Get the user to enter the Vigenere key
VigenereKey = cin.get();
// decrypt using vigenere cipher
L.VigenereDecryption(VigenereKey);
//display the key
cout << "The current key is " << VigenereKey;
cout << "." << endl;
break;
case 'q' :
// Tell the user they are exiting the program
cout << "You are quitting the program." ;
break;
default:
;
break;
while(menu_choice != 'q');
return 0;
LList :: LList()
// PRE: NONE
// POST: the N.O. LList is valid and empty
head = NULL;
LList :: ~LList()
// PRE: the N.O. LList is valud
// POST: the N.O. LList is valid and empty, and its
// listnodes have been deleted
Clean();
void LList :: Print()
// PRE: the N.O. LList is valid
// POST: the N.O. LList is unchanged and its
// elements have been displayed
listnode * temp;
temp = head;
while( temp != NULL)
cout << temp -> data << endl;
temp = temp -> next;
void LList :: InsertHead(element thing)
// PRE : the N.O. LList is valid
// POST : the N.O. LList is unchanged, except that a
// new listnode containing element thing
// has been inserted at the head of the list
listnode * temp;
temp = new listnode;
temp -> data = thing;
temp -> next = head;
temp -> next = head;
if(head == NULL)
tail = temp;
else
;
head = temp;
void LList :: InsertTail(element thing)
// PRE : the N.O. LList is valid
// POST : the N.O. LList is unchanged, except that a
// new listnode containing element thing has been
// inserted at the tail end of the list
listnode * temp;
temp = new listnode;
temp -> data = thing;
temp -> next = NULL;
if(head == NULL)
head = temp;
else
tail -> next = temp;
tail = temp;
element LList :: DeleteHead()
// PRE: the N.O. LList is valid and not empty
// POST: the N.O. LList is unchanged, except that
// the listnode at the head end of the list has
// been deleted and its data element has
// been returned
listnode * temp;
element thing;
temp = head;
head = head -> next;
thing = temp -> data;
delete temp;
return thing;
void LList :: ReadForward()
// PRE: the N.O. LList is valid
// POST : the N.O. LList is valid, all of its
// previous listnodes have been deleted, and
// it now consists of new listnodes containing
// elements given by the user in foward order
Clean();
cout << "Enter the message: ";
userval = cin.get();
while (cin.get() != SENTINEL)
InsertTail(userval);
userval = cin.get();
void LList :: ReadBackward()
// PRE: The N.O. LList is valid
// POST: the N.O. LList is valid, all of its
// previous listnodes have been deleted,
// and it now consists of new listnodes
// containg elements given by the user
// in backwards order
element userval;
Clean();
cout << "Enter the elements: ";
userval = read_element();
while (userval != SENTINEL)
InsertHead(userval);
userval = read_element();
void LList :: Clean()
// PRE: the N.O. LList is valid
// POST:: the N.O. LList is valid and empty, and all of its
// listnodes have been deleted
while( head != NULL)
DeleteHead();
void LList :: Steal(LList & Victim)
// PRE: the N.O. and Victim LLists are valid
// POST : the Victim LList is valid and empty the N.O. LList
// is valid, all of its previous listnodes have been
// deleted, and it now consists of the listnodes originally
// on the Victim LList
Clean();
head = Victim.head;
tail = Victim.tail;
Victim.head = NULL;
void LList :: Duplicate(LList & Source)
// PRE: the N.O. LList and Source LLists are valid
// POST :: the Source LList is unchanged
// the N.O. LList is valid, all of its previous listnodes
// have been deleted, and it now consists of listnodes
// containing the same elements and in the same order as on
// the Source LList
listnode * temp;
temp = Source.head;
while (temp != NULL)
InsertTail(temp -> data);
temp = temp -> next;
void LList :: Reverse()
// PRE: the N.O. LList is valid
// POST: the N.O. LList is unchanged, excepts its elements are in
// reverse order
listnode * temp;
LList Helper;
temp = head;
while(temp != NULL)
Helper.InsertHead(temp -> data);
temp = temp -> next;
Steal(Helper);
element read_element()
element userval;
cin >> userval;
while(!cin.good())
cin.clear();
cin.ignore(80, '\n');
cout << "Invalid data type, should be an element ( ";
cout << ELEMENT_NAME << " ), try agian. " << endl;
cin >> userval;
return userval;
void LList :: displayMenu()
// PRE: none
// POST: the menu is displayed for the user along with the current
userval = ' ';
EncryptedMessage = ' ';
DecryptedMessage = ' ';
cout << "------------------------------------------------------------";
cout << endl;
cout << "Command (h for help): h" << endl;
cout << "m : enter a new current message from the keyboard" << endl;
cout << "c : encrypt the current message using the Caesar Cipher";
cout << endl;
cout << "C : decrypt the current message using the Caesar Cipher";
cout << endl;
cout << "v : encrypt the current message using the Vigenere Cipher";
cout << endl;
cout << "V : decrypt the current message using the Vigenere Cipher";
cout << endl;
cout << "h : show the help menu" << endl;
cout << "q : quit the program" << endl;
void LList :: CaesarEncryption(element CaesarKey)
//PRE:
// POST:
int num;
num = userval;
for(int x = 0; x < num; x++)
if(userval >= 'A' && userval <='Z')
EncryptedMessage=(char)(((userval+CaesarKey-'A'+26)%26)+'A');
else if(userval >= 'a' && userval <= 'z')
EncryptedMessage=(char)(((userval+CaesarKey-'a'+26)% 26)+'a');
else
;
cout << "-------------------------------------------------------"<<endl;
cout << "The encrypted message is " << EncryptedMessage << "."<<endl;
void LList:: CaesarDecryption(element CaesarKey)
//PRE:
//POST:
int num;
num = userval;
for(int x = 0; x < num; x++)
if(userval >= 'A' && userval <= 'Z')
DecryptedMessage = (char)(((userval - CaesarKey + 26) % 26) + 'A');
else if(userval >= 'a' && userval <= 'z')
DecryptedMessage = (char)(((userval -CaesarKey + 26)% 26) + 'a');
else
;
cout << "-------------------------------------------------------"<<endl;
cout << "The decrypted message is '"<< DecryptedMessage << "'."<<endl;
void LList :: VigenereEncryption(char VigenereKey)
//PRE:
//POST:
unsigned int j = 0;
for(int x = 0; x < userval; x++)
if(isalpha(x))
EncryptedMessage += VigenereKey;
if(userval > 'Z')
EncryptedMessage += 'Z' + 'A' - 1;
else
;
else
;
cout << "-------------------------------------------------------"<<endl;
cout << "The encrypted message is '" << EncryptedMessage << "'."<<endl;
void LList :: VigenereDecryption(char VigenereKey)
// PRE:
//POST:
int num;
num = userval;
for(int x = 0; x < num; x++)
if(isalpha(x))
DecryptedMessage = (userval - VigenereKey % 26);
cout << "------------------------------------------------------"<<endl;
cout << "The decrypted message is \"" <<DecryptedMessage << "\"."<<endl;
void LList :: Display()
//PRE
//POST
cout << "--------------------------------------------------"<<endl;
if(userval = ' ')
cout << "The current message is \"\". " << endl;
else
cout << "The current message is \"" << userval << "\"." << endl;
cout << "----------------------------------------------------";
cout << endl;
【问题讨论】:
有什么问题? 这段代码实在是太大了。您应该创建一个minimal reproducible example,以便让更多人帮助您。 【参考方案1】:#include <iostream>
#include<string>
using namespace std;``
int main()
string alphapet, plaintext, cipher;
int option, key;
alphapet = "abcdefghijklmnopqrstuvwxyz";
cipher = "xyzabcdefghijklmnopqrstuvw";
cout << "choose an option atbash cioher : \n 1)to Cipher \n 2)to Decipher \n";
cin >> option;
cout << "enter key \n";
cin >> key;
cin.ignore();
char letter;
if (option == 1 && (key == 3))
cout << "enter the plaintext to cipher :\n ";
getline(cin, plaintext);
for (int i = 0; i < plaintext.size(); ++i)
for (int j = 0; j < 26; ++j)
if (plaintext[i] == alphapet[j])
plaintext[i] = cipher[j];
break;
if (option == 2)
cout << "rnter the plaintext to decipher : \n";
getline(cin, plaintext);
for (int i = 0; i < plaintext.size(); ++i)
for (int j = 0; j < 26; j++)
if (plaintext[i] == cipher[j])
plaintext[i] = alphapet[j];
break;
cout << plaintext;
return 0;
【讨论】:
您能否提供一个简短的解释以及您的代码?以上是关于C++ 链表凯撒密码 Vigenere Cipher的主要内容,如果未能解决你的问题,请参考以下文章