📄 sub.cpp
字号:
/*
* sub.c - substitution cipher assistant
*
* Will display the ciphertext underneath the guessed plaintext, in
* rows that are 50 columns wide each. The user enters in letter
* substitutions and can request for letter, digram, or trigram
* frequencies.
*
*/
#include "sub.h"
// First letter is the most frequent, etc
static char rank_list[] = {'E', 'T', 'A', 'O', 'I', 'N', 'S', 'H', 'R',
'D', 'L', 'C', 'U', 'M', 'W', 'F', 'G', 'Y',
'P', 'B', 'V', 'K', 'J', 'X', 'Q', 'Z'};
// Frequencies of occurrence in English of each alphabet letter
static float freq_list[] = {0.082, 0.015, 0.028, 0.043, 0.127, 0.022, 0.020,
0.061, 0.070, 0.002, 0.008, 0.040, 0.024, 0.067,
0.075, 0.019, 0.001, 0.060, 0.063, 0.091, 0.028,
0.010, 0.023, 0.001, 0.020, 0.001};
int main (int argc, char *argv[])
{
short int selection = 0;
string CiphertextFilename = "",
PlaintextFilename = "",
Ciphertext = "",
Plaintext = "";
string CiphertextSequence = ""; // For digram/trigram/sequence
// frequency analyses
char CipherLetter = ' ',
PlaintextLetter= ' ';
float frequency = 0.0;
if (argc != 3)
{
cout << "Usage: sub <ciphertext-file> <plaintext-file>" << endl;
exit(1);
}
// Put argv[1] and argv[2] into CiphertextFilename and PlaintextFilename
CiphertextFilename.assign(argv[1]);
PlaintextFilename.assign(argv[2]);
// Read ciphertext into an array, cutting out all of the whitespace
Ciphertext = read_ciphertext_into_string (CiphertextFilename);
// Now that we know the size of the Ciphertext, copy that many -'s
// into the plaintext string to sort of "initialize" it for display
Plaintext.assign(Ciphertext.size(), '-');
// Everything works fine up to here for SURE
for (;;)
{
// Display the texts for the user to see
display_texts (Ciphertext, Plaintext);
// Prompt for a decision
selection = prompt_user();
switch (selection)
{
case 0:
cout << "Exiting..." << endl;
exit (1);
break;
// "Assign ciphertext letter to plaintext letter"
case 1:
// Prompt for ciphertext letter assignment
cout << "Enter ciphertext letter to change: ";
cin >> CipherLetter;
// Prompt for plaintext letter to assign
cout << "Enter plaintext letter to assign to " << CipherLetter << ": ";
cin >> PlaintextLetter;
cout << "Exchanging all occurrences of " << CipherLetter << " in the ciphertext "
<< "with " << PlaintextLetter << "." << endl;
assign_letter (CipherLetter, &Ciphertext, PlaintextLetter, &Plaintext);
break;
// "Display letter frequency"
case 2:
// Prompt for ciphertext letter to count
cout << "Enter ciphertext letter to count: ";
cin >> CipherLetter;
cout << "Analyzing ciphertext for frequency of \"" << CipherLetter << "\"..." << endl;
frequency = get_letter_frequency (CipherLetter, Ciphertext);
// Set float to max of four decimal digits
cout.precision(4);
cout << "Frequency of occurrence of this letter was " << frequency << " (" << frequency*100 << "%)" << endl;
break;
// "Display frequencies of ALL letters"
case 3:
display_all_frequencies (Ciphertext);
break;
// "Display sequence frequency (arbitrary length)"
case 4:
// Prompt for ciphertext sequence
cout << "Enter ciphertext sequence to count: ";
cin >> CiphertextSequence;
cout << "Analyzing ciphertext for frequency of \"" << CiphertextSequence << "\"..." << endl;
frequency = get_sequence_frequency (CiphertextSequence, Ciphertext);
// Set float to max of four decimal digits
cout.precision(4);
cout << "Frequency of occurrence of this letter was " << frequency << " {" << frequency*100 << "%)" << endl;
break;
// "Write plaintext to file"
case 5:
cout << "Writing to file " << PlaintextFilename << "." << endl;
write_plaintext_to_file (PlaintextFilename, Plaintext);
break;
default:
// Display menu again, entered non-valid choice
break;
}
}
// Should never get here of course, but I specified
// a return of "int", so ... here it is.
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -