Assignment 3: Emotion Decoder
Write a program that converts a sequence of bits into messages (this process is known as decoding, and the reverse process is called encoding). There are five types of bit encoding: 00, 01, 100, 1010, 1011. The corresponding message for each bit encoding is shown below.
00 -> Happy
01 -> Surprise
100 -> Fear
1010 -> Anger
1011 -> Sad
Example 1 (click to see the figure):
Enter a sequence of bits: 10000101001001011
Fear Happy Anger Surprise Happy Sad
In Example 1,
program input: 10000101001001011
program output: Fear Happy Anger Surprise Happy Sad
More input-output samples.
The input sequence of bits is decoded from left to right.
You can assume that all the input sequences are leagal.
The last output character can be a space. That is, the following program output for Example 1 is also legal:
Fear Happy Anger Surprise Happy Sad
It is recommended that your solution is written based on the code template.
Do not output any line breaks except the one in the code template.
Do not modify the cout statements in the code template.
To check whether an input bit is 0, you can use code[i] == '0'
You must ensure that the array index is within the legal range.
If you use code[i], you need to ensure that i >= 0 and i <= code.length()-1
If you use code[i+1], you need to ensure that i+1 >= 0 and i+1 <= code.length()-1
If the array index is not within the legal range, your program will cause runtime error or unknown results.
If any runtime error occurs, the accuracy of your program is ZERO.
If you are interested in the story behind this assignment, read Demystify Entropy for the motivation of encoding.