📄 vigenere_square.cpp
字号:
// Solution: Vigenere Square
// File: vigenere_square.cpp
// Programmed by: Jay Catelli
// EMail: jcatelli@student.umass.edu
// Date: 3/11/04
//
// Discription: This program encodes and decodes a character using
// the Vigenere Square method
//
// Compiler: Microsoft Visual C++ .Net
// Build: 54 final
// Build Date: 3/12/04 3:01:32 AM
// include libaries
#include <iostream>
// Namespaces
using namespace std;
// Globle Constant Varables
const int size=26; // The maxium size of the matrix
void create_vigenere_square(char matrix[][size])
// This function creates a Vigenere Square
// Pre: This funtion takes in a matrix size by size
// Post: This funtion returns (passes back) the Vigenere Square to matrix
{
for (int i=0;i<size;i++)
for (int j=0;j<size;j++)
matrix[i][j]=char(((i+j)%size)+'A');
}
char encode_from_square(char key, char plainchar,const char matrix[][size])
// This function takes plainchar and encodes it with the key using
// the Vigenere Square
// Pre: This function takes in a key char, a plainchar char, and the
// matrix holding the Vigenere Square. Note: All chars are upper case
// Post: This funtion returns the cipherchar char for the plainchar
// using the key
{
// Declaring Vars
char cipherchar; // Var to hold the cipher char
int row; // Var to hold the row number which is based off the plainchar
int col; // Var to hold the col number which is based off the key
// Finding the cipher char
row=int(plainchar-'A');
col=int(key-'A');
cipherchar=matrix[row][col];
// Return the cipherchar
return (cipherchar);
}
char decode_from_square(char key, char cipherchar,const char matrix[][size])
// This function takes chpherchar and decodes it with the key using
// the Vigenere Square
// Pre: This function takes in a key char, a cipherchar char, and the
// matrix holding the Vigenere Square. Note: All chars are upper case
// Post: This function returns the plainchar for the cipherchar using
// the key
{
// Declaring Vars
char plainchar; // Var to hold the plain char
int row; // Var to hold the row number which is found in the Square
int col; // Var to hold the col number which is based off the key
// Finding the plain char
row=0;
col=int(key-'A');
while (matrix[row][col]!=cipherchar)
row++;
plainchar=char(row+'A');
// Return the row that holds the cipherchar in char form
return (plainchar);
}
void encode_menu(const char matrix[][size])
// This function diplays the encode menu
// Pre:
// Post: Runs the encode_from_square_function
{
// Declare Vars
char key;
char plainchar;
char cipherchar;
// Get the key
do
{
cout<<"Please enter in your key : ";cin>>key;
// Check entery
if (islower(key))
key-='a'-'A';
else if (!isupper(key))
cout<<endl<<key<<" is not a vaild key."<<endl<<endl;
} while (!isupper(key));
// Get the plainchar
cout<<endl;
do
{
cout<<"Please enter your plainchar : ";cin>>plainchar;
// Check entery
if (islower(plainchar))
plainchar-='a'-'A';
else if (!isupper(plainchar))
cout<<endl<<plainchar<<" is not a vaild plainchar."<<endl<<endl;
} while (!isupper(plainchar));
// Encode the plainchar
cout<<endl;
cipherchar=encode_from_square(key, plainchar, matrix);
// Display result
cout<<"Your encoded char for "<<plainchar<<" using "<<key<<" as the key"
<<" is : "<<cipherchar<<endl;
cout<<endl;
cout<<"Please press enter to continue.";cin.ignore();getchar();
}
void decode_menu(const char matrix[][size])
// This function diplays the decode menu
// Pre:
// Post: Runs the decode_from_square_function
{
// Declare Vars
char key;
char plainchar;
char cipherchar;
// Get the key
do
{
cout<<"Please enter in your key : ";cin>>key;
// Check entery
if (islower(key))
key-='a'-'A';
else if (!isupper(key))
cout<<endl<<key<<" is not a vaild key."<<endl<<endl;
} while (!isupper(key));
// Get the cipherchar
cout<<endl;
do
{
cout<<"Please enter your cipherchar : ";cin>>cipherchar;
// Check entery
if (islower(cipherchar))
cipherchar-='a'-'A';
else if (!isupper(cipherchar))
cout<<endl<<cipherchar<<" is not a vaild cipherchar."<<endl<<endl;
} while (!isupper(cipherchar));
// Encode the cipherchar
cout<<endl;
plainchar=decode_from_square(key, cipherchar, matrix);
// Display result
cout<<"Your decoded char for "<<cipherchar<<" using "<<key<<" as the key"
<<" is : "<<plainchar<<endl;
cout<<endl;
cout<<"Please press enter to continue.";cin.ignore();getchar();
}
void print_square(const char matrix[][size])
// This function print out the Vigenere Square
// Pre: Takes in the matrix which holds the Vigenere Square
// Post: Displays the Vigenere Square
{
// Print the key guide
cout<<" ";
for (char i='A';i<='Z';i++)
cout<<'|'<<i;
cout<<endl;
for (int i=0;i<53;i++)
cout<<'-';
cout<<endl;
// Print out the plainchar guide with the row in the matrix
for (char i='A';i<='Z';i++)
{
cout<<i<<"| ";
for (int j=0;j<size;j++)
cout<<matrix[int(i-'A')][j]<<' ';
cout<<endl;
}
cout<<"Please press enter to continue.";cin.ignore();getchar();
}
void menu(const char matrix[][size])
// This function displays the program menu
// Pre:
// Post: This function calls encode and decode until the user quits
{
// Declare Vars
char choice='E'; // Var to hold the choice of the menu
// Display Menu
do
{
system("cls");
if (!(choice=='E'||choice=='D'||choice=='P'))
cout<<"Invaild Choice. Please try again."<<endl<<endl;
cout<<"Welcome to Vigenere Square Cipher Program"<<endl;
cout<<endl;
cout<<"E : Encode a character"<<endl;
cout<<"D : Decode a character"<<endl;
cout<<"P : Print the Vigenere Square"<<endl;
cout<<"Q : To quit"<<endl;
cout<<endl;
cout<<"Please enter in your choice: ";cin>>choice;
// Check and see if input is in range if not make it so
if (islower(choice))
choice-='a'-'A';
system("cls");
// Preform require fuctions for choice
switch (choice)
{
case 'E':
encode_menu(matrix);
break;
case 'D':
decode_menu(matrix);
break;
case 'P':
print_square(matrix);
break;
default:
break;
}
} while (choice!='Q');
}
int main()
{
char matrix[size][size]; // Matrix to hold the Vigener Square
// Make the Vigenere Square
create_vigenere_square(matrix);
// Display the Program Menu
menu(matrix);
return (0);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -