For years, the Radio Orphan Annie's Secret Society has entrusted their members with 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.
The information to be decoded is contained in a text file. The file will be processed one character at a time. Each character will be decoded by calling an appropriate function and then the decoded character will be displayed.
The message to decode and translate if you wish to download:
http://faculty.cs.niu.edu/~byrnes/csci240/pgms/encoded_quotes.txt
Rules for encoding:
The original message from Orphan Annie was encoded using the following rules. Your program will simply undo/reverse this process. The original message that was encoded will be referred to as "message" below:
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:
'0' --> )
'1' --> !
'2' --> @
'3' --> #
'4' --> $
'5' --> %
'6' --> ^
'7' --> &
'8' --> *
'9' --> (
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.
, --> '9'
" --> '8'
! --> '7'
; --> '6'
? --> '5'
' --> '4'
( --> '3'
) --> '2'
. --> '1'
- --> '0'
Two white space characters were encoded as non-standard characters:
a blank/space was encoded as an ASCII 22
a newline character was encoded as an ASCII 20
Here is what I have so far and the program is running but not displaying any output. Any help would be greatly appreciated!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
|
#include <iostream>
#include <fstream>
using namespace std;
bool isspecial(char ch) {
return ch == 20 || ch == 22;
}
char changeLower(char ch) {
return (((ch - 'a') + 27) % 26) + 'A';
}
char changeUpper(char ch) {
return (((ch - 'A') + 25) % 26) + 'a';
}
char changePunct(char ch) {
switch (ch) {
case ',':
return '9';
case '"':
return '8';
case '!':
return '7';
case ';':
return '6';
case '?':
return '5';
case '\'':
return '4';
case '(':
return '3';
case ')':
return '2';
case '.':
return '1';
case '-':
return '0';
}
return '\0';
}
char changeDigit(char ch) {
switch (ch) {
case '0':
return ')';
case '1':
return '!';
case '2':
return '@';
case '3':
return '#';
case '4':
return '$';
case '5':
return '%';
case '6':
return '^';
case '7':
return '&';
case '8':
return '*';
case '9':
return '(';
}
return '\0';
}
char changeSpecial(char ch) {
if (ch == 20) {
return '\n';
}
else if (ch == 22) {
return ' ';
}
return '\0';
}
int main()
{
ifstream infile1("file1.txt");
char ch = '\0';
if( infile1.fail()) //if the input file failed to open
{
cout << "input file did not open" << endl;
return(4) ; //stop execution of the program immediately
}
while (infile1.get(ch))
{
cout << "original character -> " << ch << " " << (int)ch << '\n';
if (isupper(ch)) {
changeUpper(ch);
}
else if (islower(ch)) {
changeLower(ch);
}
else if (isdigit(ch)) {
changeDigit(ch);
}
else if (isspecial(ch)) {
changeSpecial(ch);
}
else if (ispunct(ch)) {
changePunct(ch);
}
else
{
ch;
}
}
}
|