Functions switch and Character Processing EVERYTHING LISTED
Functions, switch, and Character Processing
EVERYTHING LISTED BELOW IS THE REQUIREMENTS FOR THE PROGRAM. FOLLOW STEP BY STEP. C++ ONLY. MAKE SURE YOU DOCUMENT EVERYTHING YOU DO WITH //. BASIC C++ PLEASE FOLLOW DIRECTIONS!! THANK YOU!
Overview
For years, the Radio Orphan Annie\'s Secret Society has entrusted their members with a special Little Orphan Annie decoder pins so that they can decode secret messages from Orphan Annie. For this program, the system of decoding messages will be modernized so that a C++ program can do the decoding rather than having to rely on a physical pin.
Using an Input File in a CPP program
Rather than having the user type in the encoded message, the information has been placed in a text file. This means that the program will be getting its input from the file rather than the keyboard.
The file with the input data is included that you need to start the program. It is called encoded_quotes.txt . It is at the very bottom of this page.
In order to read from a file, create an input file stream variable and \"connect\" it with the text file. The \"connection\" is created by opening the file:
The open statement will open a file named encoded_quotes.txt. The file must be located in the same directory as the CPP file. (For Mac users, you will probably have to put in the set of double quotes(\"\"), find where the file has been saved on your computer, and then drag and drop the file in between the quotes. It should place the name of the file, including the complete path of where it is located on your computer, between the quotes.)
Once the file has been opened, make sure that it opened correctly. This is an important step because a file cannot be processed if it doesn\'t exist or open correctly. To test if the file opened correctly:
In order to read a character from a file, the input operator can be used in the same manner that it\'s used with a cin statement. So:
Since the data is coming from a file, there has to be some way to determine when there is no more data. One way to do that is to use the input file stream variable as a boolean variable:
As long as there is information in the file, the input file stream variable will remain valid (or true). Once the end of the data has been reached, the stream will become invalid (or false). Note: this test is only successful AFTER an attempt has been made to read data from the file. This means that a standard read loop pattern should be followed.
When a file is done being processed, it should be closed.
Processing
The text file (encoded_quotes.txt that was mentioned above) will be processed one character at a time. Each character will be decoded by calling an appropriate function and then displayed.
The basic layout for the program is as follows:
The Encoding Process
The original message from Orphan Annie was encoded using the following rules. Your program will simply undo this process. The original message that was encoded will be referred to as \"message\" below:
a blank/space was encoded as an ASCII 22
a newline character was encoded as an ASCII 20
Any character in message that was an uppercase character was converted to lowercase and then had 1 subtracted from it. So \'B\' became \'a\', \'C\' became \'b\', ... The first uppercase letter was the exception to the rule. The \'A\' was simply wrapped around to the end of the alphabet so that it became \'z\'.
Any character in message that was a lowercase character was converted to uppercase and then had 1 added it. So \'a\' became \'B\', \'b\' became \'C\', ... The last lowercase letter was the exception to the rule. The \'z\' was simply wrapped around to the beginning of the alphabet so that it became \'A\'.
Any character in message that was a digit (\'0\' ... \'9\') was converted as follows:
Any character in message that was one of the following punctuation characters was encoded as shown below. Any punctuation that is not shown here was not altered.
Two white space characters were encoded as non-standard characters:
Note again: any character that does not fit the criteria listed above in 1 - 5 was not altered in the encoding.
The Functions
This program will accomplish the decoding by using some pre-written library functions and ones that you design yourself.
Library Functions
The following set of functions are contained in the cctype library and can simply be called in the program (ie. they do not have to be written): isalpha, isupper(), islower(), ispunct(), isdigit().
Each of the \"is\" functions take a character as an argument and return a true or false value:
isalpha() will return true if the passed in character is an alphabetic character; and false if the passed in character is not an alphabetic character
isupper() will return true if the passed in character is an uppercase character; and false if the passed in character is not an uppercase character
islower() will return true if the passed in character is a lowercase character; and false if the passed in character is not a lowercase character
ispunct() will return true if the passed in character is a punctuation character; and false if the passed in character is not a punctuation character
isdigit() will return true if the passed in character is a digit; and false if the passed in character is not a digit
The return value for the \"is\" functions allows for the functions to be called as the condition in an if statement:
Other Functions
The functions listed below will be used to decode a character (these are the functions that MUST be written and called in the program).
bool isspecial( char ch )
This function will be called to test if the passed in character is a special character (ie. is the ASCII value for the character 20 or 22?). It takes a single character as its argument: the character to be tested. It returns a boolean value. If the passed in character is one of the two special characters, the function should return true. If the passed in character is not one of the two special characters, the function should return false.
char changeLower( char ch )
This function will be called for any character that is a lowercase character. It takes a single character as its argument: the character to be decoded. It returns a character: the decoded character.
The function should reverse the process that was detailed in #1 of the Encoding Process above by converting the passed in character to uppercase and adding 1, so that, for example, \'d\' becomes \'E\'. However, there is the one exception to the rule: \'z\' should become \'A\'.
DO NOT have a decision statement with 26 separate tests. Take advantage of the fact that characters are stored as integer values.
char changeUpper( char ch )
This function will be called for any character that is an uppercase character. It takes a single character as its argument: the character to be decoded. It returns a character: the decoded character.
The function should reverse the process that was detailed in #2 of the Encoding Process above by converting the passed in character to lowercase and subtracting 1, so that, for example, \'D\' becomes \'c\' and \'E\' becomes \'d\'. However, there is the one exception to the rule: \'A\' should become \'z\'.
DO NOT have a decision statement with 26 separate tests. Take advantage of the fact that characters are stored as integer values.
char changePunct( char ch )
This function will be called for any character that is a punctuation symbol (in C++, that is all type-able characters that are not letters, digits, or whitespace). It takes a single character as its argument: the character to possibly be decoded. It returns a character: the decoded character or the original passed in character.
The function should use a switch statement to check for each of the punctuation symbols that were detailed in #3 of the Encoding Process above and convert the passed in character to its original version, so that, for example, \'&\' becomes \'7\' or \'@\' becomes \'2\'. Don\'t forget that if the punctuation symbol is not listed above, it should be returned unchanged.
char changeDigit( char ch )
This function will be called for any character that is a digit character. It takes a single character as its argument: the character to be decoded. It returns a character: the decoded character.
The function should reverse the process that was detailed in #4 of the Encoding Process above by converting the passed in character to its original value, so that, for example, \'6\' becomes \';\' and \'2\' becomes \')\'.
char changeSpecial( char ch )
This function will be called for any character that is not an uppercase, lowercase, digit, or punctuation character. It takes a single character as its argument: the character to possibly be decoded. It returns a character: the decoded character or the original passed in character.
The function should reverse the process that was detailed in #5 of the Encoding Process above by only changing the passed in character if it has ASCII value 20 or 22. If the passed in character has an ASCII value of 20, then it will return a newline character. If the passed in character has ASCII value 22, then it will return a space character. Anything else will be returned unchanged.
Notes
This program can be developed incrementally if you use your own data file. Start by writing the program so that it handles lowecase characters. Create a text file with the line zabcdef. Run your program using this file as input. It should produce ABCDEFG on the screen as its result.
Then add code to handle uppercase characters and add uppercase characters to your text file. Run the program again and verify the output. Add in punctuation, digits, etc...
The one test that will be difficult to add in is the testing for the special characters. This is where you can switch to working with the original input file since all of the other characters likely work at this point. If you want something that is a little smaller to work with, try using just the 1st 50 characters from the original file. There should be enough of the special characters to test if your program can handle them properly.
When you\'re done testing the characters individually, make sure to run your code with original file, unchanged.
Above, it says that isdigit() and the other \"is\" functions return a true or false value. That\'s part of the truth. If you look up the functions, you will see that they actually return an integer. As mentioned in lecture, any non-zero value is considered true; whereas 0 is considered false.
Output
For this assignment, to verify your output, just make sure that you can read the decoded message.
Encoded Quotes to start program
8aFTVSFUPESJOLZPVSnWBMUJOF188zVOUbMBSBIBEGPSZFBSTMBCPSFEVOEFSUIFEFMVTJPOUIBUhXBTOPUPOMZQFSQFUVBMMZ$ZFBSTPME9CVUBMTPBHJSM188hNNFEJBUFMZ9NZGFFUCFHBOUPTXFBUBTUIPTFUXPGMVGGZMJUUMFCVOOJFTXJUIBCMVFCVUUPOFZFTUBSFETBQQJMZVQBUNF188gFMPPLTMJLFBEFSBOHFEdBTUFSaVOOZ188gn1gn1gn188sIFTOBQPGBGFXTQBSLT9BRVJDLXIJGGPGPAPOF9BOEUIFMBNQCMBAFEGPSUIJOVOQBSBMMFMFEHMPSZ188nI9MPPLBUUIBU7vJMMZPVMPPLBUUIBU5hTO4UUIBUHMPSJPVT5hU4T111JU4T111JU4TJOEFTDSJCBCMZCFBVUJGVM7hUSFNJOETNFPGUIFePVSUIPGiVMZ788qBOEZ9IPXEPUIFMJUUMFQJHHJFTHP588hUJTBMBNQ9ZPVOJODPNQPPQ9CVUJU4TBlBKPSzXBSE1hXPOJU788xFBI9NJOEQPXFS9rXFEF6NJOEQPXFS188sIFMJOFXBJUJOHUPTFFrBOUBbMBVTTUSFUDIFEBMMUIFXBZCBDLUPsFSSFgBVUF1zOEhXBTBUUIFFOEPGJU188ePPUCBMM5ePPUCBMM5vIBU4TBGPPUCBMM5vJUIVODPOTDJPVTXJMMNZWPJDFTRVFBLFEPVU4GPPUCBMM4188nLBZ9HFUIJNPVUPGIFSF188zGPPUCBMM5nIOP9XIBUXBThEPJOH5vBLFVQ9rUVQJE7vBLFVQ788mP7mP7hXBOUBOnGGJDJBMqFEqZEFSbBSCJOF0zDUJPOsXP0gVOESFE0rIPUqBOHFlPEFMzJSqJG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