Lab11.cppLab11.cpp//**********************************
*****************************************************
***********************
// FILE: Lab11.cpp
//
// DESCRIPTION: Implements a simple substitution cipher to e
ncrypt secret messages.
//
// AUTHORS: your-name (your-email-address)
// your-partner's-name (your-partners-email-address)
//
// COURSE: CSE100 Principles of Programming with C++, F
all 2015
//
// LAB INFO: Lab 11 Date/Time: your-lab-date-and-
time TA: your-lab-ta
//***************************************************
*****************************************************
******
???// For exit()
???// For ifstream, ofstream
???// For cout, endl
usingnamespace std;
// Define an int constant named MAX_MSG_LEN which is the
maximum number of characters in the plaintext message.
// Initialize MAX_MSG_LEN to 50.
???
//-------------------------------------------------------------------------
-------------------------------------
// FUNCTION: error()
//
// DESCRIPTION
// This function is called when an non-
recoverable error occurs. We display the message in p_err_msg
on the out-
// put window and terminate the program with an exit code of -
1.
//
// PSEUDOCODE
// Function error(In: p_err_msg : string) Returns Nothing
// Send to cout p_err_msg followed by ". Terminating..." foll
owed by endl
// Call exit(-1)
// End Function
//-------------------------------------------------------------------------
-------------------------------------
???
//-------------------------------------------------------------------------
-------------------------------------
// FUNCTION: encrypt_plain_char()
//
// DESCRIPTION
// This function has two parameters,
//
// p_plain_char - is a plaintext character that is being encrypted.
// p_index -
is the index of p_plain_char in the plaintext character array.
//
// This function determines what the encrypted character should
be and returns it. Because our plaintext charac-
// ter set is limited to uppercase letters, which have ASCII value
s 65 (A), 66 (B), 67 (C), ... 90 (Z), we can
// subtract 65 from p_plain_char to form the index of the substit
ution character in either sub_even or sub_odd.
// It may seem weird that we are subtracting an integer from a c
haracter variable, but remember that the values
// of character variables are in fact integers, so something like p
_plain_char - 'A' is perfectly legal since
// 'A' evaluates to 65.
//
// PSEUDOCODE
// Function encrypt_plain_char(In: p_plain_char : char; In: p_in
dex : int) Returns char
// Define and initialize sub_even array as discussed in the lab
project document
// Define and initialize sub_odd array as discussed in the lab
project document
//
// Define int sub_index = p_plain_char - 'A'
// Define char cipher_char
//
// If p_index is even Then
// cipher_char = sub_even[sub_index]
// Else
// cipher_char = sub_odd[sub_index]
// End If
//
// Return cipher_char
// End Function
//-------------------------------------------------------------------------
-------------------------------------
???
//-------------------------------------------------------------------------
-------------------------------------
// FUNCTION: encrypt()
//
// DESCRIPTION
// This function has two parameters,
//
// p_plain - a C-
string (an array of characters with a null character at the end) co
ntaining the plaintext
// message. This is an input parameter.
// p_cipher - a C-
string containing the ciphertext. This is an output parameter.
// Given the plaintext message p_plain, this function encrypts it
(character by character) and returns the
// ciphertext message in p_cipher.
//
// PSEUDOCODE
// Function encrypt(In: p_plain : char[]; Out: p_cipher : char[])
Returns Nothing
// Define int index
//
// --
This is a vary loop where we are varying the value of index so
we can access the characters of p_plain
// --
in order. Note the conditional expression: we continue looping
until index has been advanced to point
// --
to the null character that follows the last valid character of p_p
lain.
// For index = 0; p_plain[index] != '0'; ++index Do
// Define char plain_char = p_plain[index]
// Define cipher_char = encrypt_plain_char(plain_char, ind
ex)
// p_cipher[index] = cipher_char
// End For
//
// --
Note, we have to place the null character at the end of p_cipher
in order to form a valid C-string.
// p_cipher[index] = '0'
// End Function
//-------------------------------------------------------------------------
-------------------------------------
???
//-------------------------------------------------------------------------
-------------------------------------
// FUNCTION: main()
//
// DESCRIPTION
// This is where execution begins. We open "plain.txt" for readi
ng and "cipher.txt" for writing. For each plain-
// text message in "plain.txt" we encrypt it to form a ciphertext
message, which is written to "cipher.txt".
//
// PSEUDOCODE
// Function main() Returns int
// Open "plain.txt" for reading using an ifstream object name
d fin
// Open "cipher.txt" for writing using an ofstream object nam
ed fout
//
// If "plain.txt" could not be opened for reading Then
// Call error("Could not open plain.txt for reading")
// Else If "cipher.txt" could not be opened for writing Then
// Call error("Could not open cipher.txt for writing")
// End If
//
// Define char arrays plain and cipher, both of size MAX_MS
G_LEN
//
// --
This is an EOF loop. We read strings from fin until fin fails be
cause we have reached the end of file.
// While fin >> plain Do
// Call encrypt(plain, cipher)
// Send cipher to fout followed by endl
// End While
//
// Close fin
// Close fout
// End Function
//-------------------------------------------------------------------------
-------------------------------------
???

Lab11.cppLab11.cpp.docx

  • 1.
    Lab11.cppLab11.cpp//********************************** ***************************************************** *********************** // FILE: Lab11.cpp // //DESCRIPTION: Implements a simple substitution cipher to e ncrypt secret messages. // // AUTHORS: your-name (your-email-address) // your-partner's-name (your-partners-email-address) // // COURSE: CSE100 Principles of Programming with C++, F all 2015 // // LAB INFO: Lab 11 Date/Time: your-lab-date-and- time TA: your-lab-ta //*************************************************** ***************************************************** ****** ???// For exit() ???// For ifstream, ofstream ???// For cout, endl usingnamespace std; // Define an int constant named MAX_MSG_LEN which is the maximum number of characters in the plaintext message. // Initialize MAX_MSG_LEN to 50. ??? //------------------------------------------------------------------------- -------------------------------------
  • 2.
    // FUNCTION: error() // //DESCRIPTION // This function is called when an non- recoverable error occurs. We display the message in p_err_msg on the out- // put window and terminate the program with an exit code of - 1. // // PSEUDOCODE // Function error(In: p_err_msg : string) Returns Nothing // Send to cout p_err_msg followed by ". Terminating..." foll owed by endl // Call exit(-1) // End Function //------------------------------------------------------------------------- ------------------------------------- ??? //------------------------------------------------------------------------- ------------------------------------- // FUNCTION: encrypt_plain_char() // // DESCRIPTION // This function has two parameters, // // p_plain_char - is a plaintext character that is being encrypted. // p_index - is the index of p_plain_char in the plaintext character array. // // This function determines what the encrypted character should be and returns it. Because our plaintext charac- // ter set is limited to uppercase letters, which have ASCII value s 65 (A), 66 (B), 67 (C), ... 90 (Z), we can // subtract 65 from p_plain_char to form the index of the substit ution character in either sub_even or sub_odd.
  • 3.
    // It mayseem weird that we are subtracting an integer from a c haracter variable, but remember that the values // of character variables are in fact integers, so something like p _plain_char - 'A' is perfectly legal since // 'A' evaluates to 65. // // PSEUDOCODE // Function encrypt_plain_char(In: p_plain_char : char; In: p_in dex : int) Returns char // Define and initialize sub_even array as discussed in the lab project document // Define and initialize sub_odd array as discussed in the lab project document // // Define int sub_index = p_plain_char - 'A' // Define char cipher_char // // If p_index is even Then // cipher_char = sub_even[sub_index] // Else // cipher_char = sub_odd[sub_index] // End If // // Return cipher_char // End Function //------------------------------------------------------------------------- ------------------------------------- ??? //------------------------------------------------------------------------- ------------------------------------- // FUNCTION: encrypt() // // DESCRIPTION // This function has two parameters, //
  • 4.
    // p_plain -a C- string (an array of characters with a null character at the end) co ntaining the plaintext // message. This is an input parameter. // p_cipher - a C- string containing the ciphertext. This is an output parameter. // Given the plaintext message p_plain, this function encrypts it (character by character) and returns the // ciphertext message in p_cipher. // // PSEUDOCODE // Function encrypt(In: p_plain : char[]; Out: p_cipher : char[]) Returns Nothing // Define int index // // -- This is a vary loop where we are varying the value of index so we can access the characters of p_plain // -- in order. Note the conditional expression: we continue looping until index has been advanced to point // -- to the null character that follows the last valid character of p_p lain. // For index = 0; p_plain[index] != '0'; ++index Do // Define char plain_char = p_plain[index] // Define cipher_char = encrypt_plain_char(plain_char, ind ex) // p_cipher[index] = cipher_char // End For // // -- Note, we have to place the null character at the end of p_cipher in order to form a valid C-string. // p_cipher[index] = '0'
  • 5.
    // End Function //------------------------------------------------------------------------- ------------------------------------- ??? //------------------------------------------------------------------------- ------------------------------------- //FUNCTION: main() // // DESCRIPTION // This is where execution begins. We open "plain.txt" for readi ng and "cipher.txt" for writing. For each plain- // text message in "plain.txt" we encrypt it to form a ciphertext message, which is written to "cipher.txt". // // PSEUDOCODE // Function main() Returns int // Open "plain.txt" for reading using an ifstream object name d fin // Open "cipher.txt" for writing using an ofstream object nam ed fout // // If "plain.txt" could not be opened for reading Then // Call error("Could not open plain.txt for reading") // Else If "cipher.txt" could not be opened for writing Then // Call error("Could not open cipher.txt for writing") // End If // // Define char arrays plain and cipher, both of size MAX_MS G_LEN // // -- This is an EOF loop. We read strings from fin until fin fails be cause we have reached the end of file. // While fin >> plain Do // Call encrypt(plain, cipher)
  • 6.
    // Send cipherto fout followed by endl // End While // // Close fin // Close fout // End Function //------------------------------------------------------------------------- ------------------------------------- ???