Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C++ no global variables (1) Using command line arguments for an input file and output file name (see command line argument slides and page 3),

C++

no global variables

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

(1) Using command line arguments for an input file and output file name (see command line argument slides and page 3), print out the command line input file name (see the sample solution for the output to use) and open the input file provided. Verify that the file opened successfully (file stream is valid). If it did not open successfully, enter a while loop that outputs an error message, resets the input file stream being opened and prompts the user to enter another input file name. This file name is read, echo printed and opened. The loop continues until the input file is successfully opened. (same procedure as used in project 6). Be sure to test for correct number of arguments as the first action in main. (2) If the input file is successfully opened, print out the command line output file name and open the output file provided. Verify that the output file opened successfully. If it did not open successfully, enter a while loop that outputs an error message, resets the output file stream being opened and prompts the user to enter another output file name. This file name is read, echo printed and opened. The loop continues until the output file is successfully opened. Use the filename "Bad/file" to cause the open function to fail for the output file (3) The input file contains multiple lines the first line contains a single character E or D for encode or decode. After reading this character test the status of the file stream to see if the end-of-file has been reached. If it has, then the input file is empty. Print out an empty file message to the terminal only and terminate the program. If a character was successfully read, verify that it is E or D. If it is any other character, output an error message to the terminal and terminate the program. (4) The next line in the file contains an integer offset value between 0 and 25 inclusive. There are two possible errors to check for after this read. The first error is if the input file does not contain an offset value or any other data after the E or D that was read from the first line. For this case output the appropriate error message to the terminal and terminate the program. present, but it is not an integer. In this case output the appropriate error message to the terminal and terminate the program. The second error is if the offset is (5) After successfully reading the offset, the rest of the file is read character by character. For each character read, it is either (E)ncoded or (D)ecoded according to the following rules If the character read from the file is a letter(upper or lower case), it is changed based on the offset value read. For (E)ncoding the offset is added to the integer value of the character. For (D)ecoding the offset is subtracted from the integer value of the character. A new letter is obtained from this value and written to the output file. See page 4 for more information on the character conversion. If the character read from the input file is not a letter, then it is written to the output file as is (no modification by the offset) a. b. (6) Continue to read all characters from the input file until the end of the file is reached which ends the program 1. Testing for an empty input file is performed by testing the status of the file stream and the EOF bit out an appropriate message to the terminal and terminate the program. 2. There are 5 types of possible problems with the input file that need to be tested: Empty file condition, Invalid code symbol-a character other than E or D, an Invalid offset- only have to verify that it was not an integer (if it is an integer, it will be between 0 and 25), a missing offset value -for these file the only data in the file is the code symbol of E or D, No data to convert- input file contains no data after the offset(see hint #3) 3. After reading the offset value, ignore the newline character that comes after it before you start after the newline character associated with the offset value. The get function must be used to read the data that is to be encoded or decoded. 4. The offset value has 3 possibilities: it will be missing, it will be an invalid character (non-digit) or it will be an integer between 0 and 25 inclusive. 5. 6. Only letters are encoded or decoded. All other characters are written to the output file as they appear. 7. Look at project 6 for the test for the correct number of command line arguments. The test needs to be the first lines in your main function Project 7 Letter Encoding and Decodin For this project letters will be encoded or decoded by using the Cesar Cipher algorithm. This algorithm adds an offset to a letter for encoding and subtracts an offset for decoding. If the offset value is 1, then for encoding, A B. B ZA. l the offset value is 1, then for decoding, AZ. B, B ZY Note that the letters wrap around when you reach either end of the alphabet. To perform the encoding or decoding of letters, a few functions from the cctype header file are needed These functions are isalpha(char), islower(char) and isupper(char) (char here represents a character variable that must be used as the argument in the function call). All three of these functions return a boolean value of false if the character passed in is not a letter, not lower case or not upper case respectively. Likewise, these functions return a Boolean value of true if the character passed in is a letter, a lower case letter or an upper case letter respectively. These functions can be used to determine what is to be done with the character being tested. For characters that are letters, a simple algorithm is used to modify the letter to its new value. The algorithm has to be written twice once for upper case letters and once for lower case letters. If a character read is a letter, the procedure for a lower case letter is as follows 1 Convert the character to an integer value between 0 and 25-0 representing 'a' and 25 representing ' (same can be done with upper case letters). To do this conversion, use intVar- charRead- a' (you can use whatever variable names you want. intVar is an integer variable and charRead is a character variable) 2) For encoding the offset is added to intVar. For decoding the offset is subtracted from intVar 3) This new integer value must then be converted to a number between 0 and 25 inclusive. For example if the offset is 2 and the letter to encode is 'z', then you have intVar 25+ 2 27. This value of 27 must be converted to a number that will represent the letter that 'z' with an offset of 2 should be converted to. In this case, the number to convert to is 1(there are 2 ways to get this value) which will represent b'. Once the number has been obtained in step 3, it must be converted back to the correct integer value for the letter it is to represent. That is easily done by taking newCharVar 'aintVar; Just add the integer value calculated(which must be between 0 and 25) to the integer value representing 'a" 4) 5) Same procedure is done for encoding upper case letters except 'A' is used instead of 'a' negative numbers, they need to be converted back to positive values that represent the end of the alphabet (the end not the'a, end). For example if the offset is 2 and the letter to decode is a', then we have intVar 0-2 -2. This value of -2 must be converted to a number that represents the letter 'a' decoded to with an offset of 2. In this case that value is 24 which represents the letter y. Once the corrected integer value is obtained it is used to obtain the newCharVar as described in step 4 7) Encoding and decoding examples: using an offset of 5 a. Encoding: AF, XC,YAD,29 E c. Encoding: AbcdefghijklmnopqrstuvwxyzFghjkimnopgrstuvwxyzabcde d. Decoding: ABCDEFGHlJKLMNOPQRSTUVWxyzVWXYZABCDEFGHlJKLMNOPQRstu Proiect 7 Letter Encoding and Decodin For this project letters will be encoded or decoded by using the Cesar Cipher algorithm. This algorithm adds an offset to a letter for encoding and subtracts an offset for decoding. If the offset value is 1, then for encoding, A B, BC ZA lf the offset value is 1, then for decoding, AZ, BA, CB ZY Note that the letters wrap around when you reach either end of the alphabet. One way to look at the encoding or decoding is to look at two rows of the alphabet. The top row will be the character to encode or decode and the bottom row will show the letter to use for the encoding or decoding The bottom row will show the alphabet three times to make it easier to see what happens when encoding is used top row shifts right) or decoding is used (top row shifts left) Here is the initial start point with offset-0 abcdefghijklmnopqrstuvwxyzabcdefghijklmnoparstuvwxyzabcdefghijklmnopqrstuvwxyz Here is what happens for encoding with a shift of 7 (a becomes h, b becomes i, etc) abcdefghijklmnopqrstuvwxyzabcdefghijklmnoparstuvwxyzabcdefghijklmnopqrstuvwxyz Here is what happens for decoding with a shift of 7 (h becomes a, i becomes b, etc) abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz abcdefghijklmnopqrstuvwxyz abcdefghijklmnopqrstuvwxyz abcdefghijklmnopgrstuvwxyz Lower case letters will be used for the rest of the information, although it is simple to change the same algorithm for upper case letters First, read a character and determine if it is a letter. If it is a letter, then determine if it is upper case or lower case (use functions isalpha(char), islower(char) and isupper(char) from the cctype header file) The following applies for lower case letters Let charRead be the character variable holding the lower case letter read. For the ascii character set, the integer value representing the lower case letters are shown below: 97 98 106 107 108 115 116 118 119 m 109 v 101 f 102 103 104 105 110w 111 x 120 121 122 112 113 114 These numeric values can be changed to 0 to 25 by subtracting 'a' from charRead as shown below 18 19 20 21 10 12 13 14 15 16 17 23 24 25 infVar is an integer variable, and it can be assigned a value between 0 and 25 by the following equation intVarcharRead 'a If charRead contains 'g' then charRead 'a' is done using the integer variables representing 'g' and 'a' or 103-97-6 When you add (encode) or subtract (decode) the offset from this value, you get a new integer value that may still be in the range of 0 to 25 or it may have to be converted back to a range of 0 to 25 For example charRead is 'g' as above so intVar contains 6. If the offset value is 22, then we have the following possibilities For encoding: newintar invar + 22 28 For decoding: newlntVar intar-22 =-16 Both of these numbers are outside the range 0 to 25, so they have to be converted back to that range. You can look at the shifting of the alphabet on page 1 to get an idea of what you will have to do. Once newintVar is in the range of 0 to 25, that value can be added to 'a' to obtain the character to write (newchar) to the output file For example, if charRead is 'g' and the offset is 22 and we are encoding, newintVar 28, and this value has to be converted to the value between 0 and 25 that represents the character we want to encode to. The letter 'g' encoded with offset of 22 should be converted to c' which means newintVar needs to be modified to a value of 2 (it is a simple math formula to do this conversion). So newChar'a'newintVar is assigning the numeric value of 97 2 99 to newChar and a character variable assigned an integer value of 99 is the letter c Likewise, for decoding with the above example 'g'would be decoded to 'k' which means newintVar needs to be modified from -16 to 10. Once newintVar has a numeric value between 0 and 25, its value is added to a' to obtain the ascii character represented by that integer value - in this case 107 which is the letter k. Same procedure is done with upper case letters -just replace 'a' with A' and the ascii character integer values range from 65 (A) to 90 (Z) (1) Using command line arguments for an input file and output file name (see command line argument slides and page 3), print out the command line input file name (see the sample solution for the output to use) and open the input file provided. Verify that the file opened successfully (file stream is valid). If it did not open successfully, enter a while loop that outputs an error message, resets the input file stream being opened and prompts the user to enter another input file name. This file name is read, echo printed and opened. The loop continues until the input file is successfully opened. (same procedure as used in project 6). Be sure to test for correct number of arguments as the first action in main. (2) If the input file is successfully opened, print out the command line output file name and open the output file provided. Verify that the output file opened successfully. If it did not open successfully, enter a while loop that outputs an error message, resets the output file stream being opened and prompts the user to enter another output file name. This file name is read, echo printed and opened. The loop continues until the output file is successfully opened. Use the filename "Bad/file" to cause the open function to fail for the output file (3) The input file contains multiple lines the first line contains a single character E or D for encode or decode. After reading this character test the status of the file stream to see if the end-of-file has been reached. If it has, then the input file is empty. Print out an empty file message to the terminal only and terminate the program. If a character was successfully read, verify that it is E or D. If it is any other character, output an error message to the terminal and terminate the program. (4) The next line in the file contains an integer offset value between 0 and 25 inclusive. There are two possible errors to check for after this read. The first error is if the input file does not contain an offset value or any other data after the E or D that was read from the first line. For this case output the appropriate error message to the terminal and terminate the program. present, but it is not an integer. In this case output the appropriate error message to the terminal and terminate the program. The second error is if the offset is (5) After successfully reading the offset, the rest of the file is read character by character. For each character read, it is either (E)ncoded or (D)ecoded according to the following rules If the character read from the file is a letter(upper or lower case), it is changed based on the offset value read. For (E)ncoding the offset is added to the integer value of the character. For (D)ecoding the offset is subtracted from the integer value of the character. A new letter is obtained from this value and written to the output file. See page 4 for more information on the character conversion. If the character read from the input file is not a letter, then it is written to the output file as is (no modification by the offset) a. b. (6) Continue to read all characters from the input file until the end of the file is reached which ends the program 1. Testing for an empty input file is performed by testing the status of the file stream and the EOF bit out an appropriate message to the terminal and terminate the program. 2. There are 5 types of possible problems with the input file that need to be tested: Empty file condition, Invalid code symbol-a character other than E or D, an Invalid offset- only have to verify that it was not an integer (if it is an integer, it will be between 0 and 25), a missing offset value -for these file the only data in the file is the code symbol of E or D, No data to convert- input file contains no data after the offset(see hint #3) 3. After reading the offset value, ignore the newline character that comes after it before you start after the newline character associated with the offset value. The get function must be used to read the data that is to be encoded or decoded. 4. The offset value has 3 possibilities: it will be missing, it will be an invalid character (non-digit) or it will be an integer between 0 and 25 inclusive. 5. 6. Only letters are encoded or decoded. All other characters are written to the output file as they appear. 7. Look at project 6 for the test for the correct number of command line arguments. The test needs to be the first lines in your main function Project 7 Letter Encoding and Decodin For this project letters will be encoded or decoded by using the Cesar Cipher algorithm. This algorithm adds an offset to a letter for encoding and subtracts an offset for decoding. If the offset value is 1, then for encoding, A B. B ZA. l the offset value is 1, then for decoding, AZ. B, B ZY Note that the letters wrap around when you reach either end of the alphabet. To perform the encoding or decoding of letters, a few functions from the cctype header file are needed These functions are isalpha(char), islower(char) and isupper(char) (char here represents a character variable that must be used as the argument in the function call). All three of these functions return a boolean value of false if the character passed in is not a letter, not lower case or not upper case respectively. Likewise, these functions return a Boolean value of true if the character passed in is a letter, a lower case letter or an upper case letter respectively. These functions can be used to determine what is to be done with the character being tested. For characters that are letters, a simple algorithm is used to modify the letter to its new value. The algorithm has to be written twice once for upper case letters and once for lower case letters. If a character read is a letter, the procedure for a lower case letter is as follows 1 Convert the character to an integer value between 0 and 25-0 representing 'a' and 25 representing ' (same can be done with upper case letters). To do this conversion, use intVar- charRead- a' (you can use whatever variable names you want. intVar is an integer variable and charRead is a character variable) 2) For encoding the offset is added to intVar. For decoding the offset is subtracted from intVar 3) This new integer value must then be converted to a number between 0 and 25 inclusive. For example if the offset is 2 and the letter to encode is 'z', then you have intVar 25+ 2 27. This value of 27 must be converted to a number that will represent the letter that 'z' with an offset of 2 should be converted to. In this case, the number to convert to is 1(there are 2 ways to get this value) which will represent b'. Once the number has been obtained in step 3, it must be converted back to the correct integer value for the letter it is to represent. That is easily done by taking newCharVar 'aintVar; Just add the integer value calculated(which must be between 0 and 25) to the integer value representing 'a" 4) 5) Same procedure is done for encoding upper case letters except 'A' is used instead of 'a' negative numbers, they need to be converted back to positive values that represent the end of the alphabet (the end not the'a, end). For example if the offset is 2 and the letter to decode is a', then we have intVar 0-2 -2. This value of -2 must be converted to a number that represents the letter 'a' decoded to with an offset of 2. In this case that value is 24 which represents the letter y. Once the corrected integer value is obtained it is used to obtain the newCharVar as described in step 4 7) Encoding and decoding examples: using an offset of 5 a. Encoding: AF, XC,YAD,29 E c. Encoding: AbcdefghijklmnopqrstuvwxyzFghjkimnopgrstuvwxyzabcde d. Decoding: ABCDEFGHlJKLMNOPQRSTUVWxyzVWXYZABCDEFGHlJKLMNOPQRstu Proiect 7 Letter Encoding and Decodin For this project letters will be encoded or decoded by using the Cesar Cipher algorithm. This algorithm adds an offset to a letter for encoding and subtracts an offset for decoding. If the offset value is 1, then for encoding, A B, BC ZA lf the offset value is 1, then for decoding, AZ, BA, CB ZY Note that the letters wrap around when you reach either end of the alphabet. One way to look at the encoding or decoding is to look at two rows of the alphabet. The top row will be the character to encode or decode and the bottom row will show the letter to use for the encoding or decoding The bottom row will show the alphabet three times to make it easier to see what happens when encoding is used top row shifts right) or decoding is used (top row shifts left) Here is the initial start point with offset-0 abcdefghijklmnopqrstuvwxyzabcdefghijklmnoparstuvwxyzabcdefghijklmnopqrstuvwxyz Here is what happens for encoding with a shift of 7 (a becomes h, b becomes i, etc) abcdefghijklmnopqrstuvwxyzabcdefghijklmnoparstuvwxyzabcdefghijklmnopqrstuvwxyz Here is what happens for decoding with a shift of 7 (h becomes a, i becomes b, etc) abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz abcdefghijklmnopqrstuvwxyz abcdefghijklmnopqrstuvwxyz abcdefghijklmnopgrstuvwxyz Lower case letters will be used for the rest of the information, although it is simple to change the same algorithm for upper case letters First, read a character and determine if it is a letter. If it is a letter, then determine if it is upper case or lower case (use functions isalpha(char), islower(char) and isupper(char) from the cctype header file) The following applies for lower case letters Let charRead be the character variable holding the lower case letter read. For the ascii character set, the integer value representing the lower case letters are shown below: 97 98 106 107 108 115 116 118 119 m 109 v 101 f 102 103 104 105 110w 111 x 120 121 122 112 113 114 These numeric values can be changed to 0 to 25 by subtracting 'a' from charRead as shown below 18 19 20 21 10 12 13 14 15 16 17 23 24 25 infVar is an integer variable, and it can be assigned a value between 0 and 25 by the following equation intVarcharRead 'a If charRead contains 'g' then charRead 'a' is done using the integer variables representing 'g' and 'a' or 103-97-6 When you add (encode) or subtract (decode) the offset from this value, you get a new integer value that may still be in the range of 0 to 25 or it may have to be converted back to a range of 0 to 25 For example charRead is 'g' as above so intVar contains 6. If the offset value is 22, then we have the following possibilities For encoding: newintar invar + 22 28 For decoding: newlntVar intar-22 =-16 Both of these numbers are outside the range 0 to 25, so they have to be converted back to that range. You can look at the shifting of the alphabet on page 1 to get an idea of what you will have to do. Once newintVar is in the range of 0 to 25, that value can be added to 'a' to obtain the character to write (newchar) to the output file For example, if charRead is 'g' and the offset is 22 and we are encoding, newintVar 28, and this value has to be converted to the value between 0 and 25 that represents the character we want to encode to. The letter 'g' encoded with offset of 22 should be converted to c' which means newintVar needs to be modified to a value of 2 (it is a simple math formula to do this conversion). So newChar'a'newintVar is assigning the numeric value of 97 2 99 to newChar and a character variable assigned an integer value of 99 is the letter c Likewise, for decoding with the above example 'g'would be decoded to 'k' which means newintVar needs to be modified from -16 to 10. Once newintVar has a numeric value between 0 and 25, its value is added to a' to obtain the ascii character represented by that integer value - in this case 107 which is the letter k. Same procedure is done with upper case letters -just replace 'a' with A' and the ascii character integer values range from 65 (A) to 90 (Z)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Visual Basic Net Database Programming

Authors: Rod Stephens

1st Edition

0789726815, 978-0789726810

More Books

Students also viewed these Databases questions