CS10 F02 Assignment 5

advertisement
CIS 61 – C++ Language Programming
Assignment 6 – due by 11:59 p.m. Fri 12/7
Fall - 2012
20 Points
Caesar Encoding
You MUST submit your work by email to cpolen@shastacollege.edu by the due date. Late assignments are
NOT accepted after the due date. You may turn in your assignment as early as you would like.
During World War I and World War II it became common for each side to come up with their own way to encode
messages to send to other allies. The Germans came up with a very good encoding/decoding algorithm that took
a special machine to encode and decode. In this program we are going to make our own encoding/decoding
program using a very simple algorithm called the Caesar Encoding.
The Caesar Encoding is simply just to take each letter in the message and add or subtract some number (called
the “cipher shift”) to the value of each character. In other words, if the cipher shift was 1, then A would be B, Z
would be A, m would be n, etc. To decode this simple encoding you just need to subtract the cipher shift, so C
would be B, A would be Z, x would be w, etc. All other characters that are not a to z or A to Z will be ignored.
In this program the application will ask the user if they would like to encode, decode or quit, then ask the user for
a string of characters and ask for an integer cipher shift to use to convert this string using either an encoding or
decoding algorithm and print back out the encoded/decoded string. If the user wants to quit the program should
immediately quit.
In this program you must use a class that will hold a string that you use to store the encoded/decoded string.
Your class must use member functions: one function to print the private member data string, one member
function for encoding strings, another member function to decode strings and a member function to ask the user
what the cipher shift should be. You will ask the user if they would like to encode, decode or quit in your member
function, store their answer in a string object, call a member function to get the cipher shift and then call the
appropriate object member function to encode or decode. In your encoding or decoding member function you will
ask the user for the string to encode/decode and then use your algorithm to do the appropriate conversion to the
string. Lastly, print out the encoded or decoded string to the user using a member function. Then ask the user if
they would like to encode/decode another string. If the user wants to go again, clear the screen using
system(“cls”); and loop through your whole program again. (See sample output for 3 sample runs.)
In this program you will just encode/decode characters in the range of A to Z and a to z. Note that in C++
uppercase and lowercase is completely different. You must also provide “wrap around” in other words adding one
character to Z is A, subtracting one from A is Z.
Please look in the book for ideas on what built in member functions to use with your string objects. We are now
using strings for user input so you must have the user type in strings and be sure to check what the user types in
to make sure that what the user typed in was valid input. For example, this means that you will need to allow the
user to type in “Yes” instead of typing in just Y. Some useful string member functions are getline() and at().
Sample Output 1:
Welcome to the Caesar Encoder/Decoder!
Would you like to Encode or Decode or Quit? Encode
Please enter a string that you would like to encode:
(Please end your string with a ~ symbol and then press the enter key)
This is a test string to encode!~
What integer cipher shift key will you use? (0-20) 5
Your encoded string is:
Ymnx nx f yjxy xywnsl yt jshtij!
Would you like to encode/decode another string? (Yes/No): No
Sample Output 2:
Welcome to the Caesar Encoder/Decoder!
Would you like to Encode or Decode or Quit? Decode
Please enter a string that you would like to decode:
(Please end your string with a ~ symbol and then press the enter key)
Drsc sc wi myno.~
What integer cipher shift key will you use? (0-20) 10
Your decoded string is:
This is my code.
Would you like to encode/decode another string? (Yes/No): No
Sample Output 3:
Welcome to the Caesar Encoder/Decoder!
Would you like to Encode or Decode or Quit? Encode
Please enter a string that you would like to encode:
(Please end your string with a ~ symbol and then press the enter key)
Test this string...~
What integer cipher shift key will you use? (0-20) 21
What integer cipher shift key will you use? (0-20) -2
What integer cipher shift key will you use? (0-20) 0
Your encoded string is:
Test this string...
Would you like to encode/decode another string? (Yes/No): No
Download