Morse Code Problem

advertisement
Assignment 7 - Morse Code Problem
Page 1
CIS 501
Spring 2003
Assignment Number 08 - Trees and Hashing: The Morse Code Problem
Introduction (You can work in pairs or threes if you like):
The Morse Code is a code on which combinations of dots and dashes are used to
represent the letters of the alphabet, numerals, and punctuation marks. The
code was invented by Samuel F. B. Morse for use with his telegraph. Although
Morse’s code is still employed on a few land lines in the U. S. and Canada, a
simplification, called the International or Continental Code is employed much
more widely throughout the world.
The table below shows the Continental (International) Morse Code representations for the upper case letters of the alphabet, numbers, and a handful
of special characters.
Continental Morse Code
A
B
C
D
E
F
G
.-...
-.-.
-..
.
..-.
--.
H
I
J
K
L
M
N
....
..
.---..-..
--.
O
P
Q
R
S
T
U
--.--.
--..-.
...
..-
V
W
X
Y
Z
0
1
..._
.--..-.---..
----.----
2
3
4
5
6
7
8
..--...-.........
-....
--...
---..
9 ----.
period
comma
colon
semicolon
.-.-.--..----...
-.-.-.
In International Code, a dash is three times as long as a dot. The space, or
silence between two parts of a single character, is the length of a dot, while
the space between two characters is the length of a dash. The space between
two words is six times the length of a dot, or twice the length of a dash.
Reading: Complete the readings for Section G in the Syllabus.
Section 10.2 on Hashing.
Also read
Programming: Using Continental Morse Code table shown above, write a C++
program to translate an English message to Morse Code and then translate the
Morse Code message back to English. The initial English message will be stored
on your CIS 068 “board” in the input data file asgn07.dat.
Solution Hints: The key to solving this problem involves choosing the right
data structures to aid in the translation. We will use a binary search tree
to translate from English to Morse Code and a simple hash table to translate
from Morse Code to English. Each will be implemented as a separate class.
 Store the above table in a file (asgn08i.dat – we will give this to you)
with each character and its Morse Code on a separate line. For
simplicity, we will put an asterisk at the end of each line (to mark the
end of the Morse Code character sequence.
 The constructors for these two classes will read data from the file
create the lookup structure, and when done, display the contents of the
structure and rewind the input file.
 When you read a line, first convert the letter to its ASCII code
equivalent (using the casting operator char) and use this ASCII value to
place the character and its associated Morse Code representation in a
binary search tree (in the right place).
Assignment 7 - Morse Code Problem
Page 2

Next compute the hash value for the Morse Code character. Do this as
follows.
o Initialize hash_value to 0. Examine the Morse Code sequence left
to right from position 0 (the left-most position) to position 5
(maximum) the right-most position. Compute the hash value as
follows:
If position i (0<=i<=5) contains a dot, add the value 1*2i to
the hash_value. If position i contains a dash add the value
2i+1 to the hash value being computed. (Stop adding values when
you see white space (or an end-of-file). For example -..- (an
X) would translate to the value 2*20 + 1*21 + 1*22 + 2*23 = 24.
o Convince yourself a) that each of the Morse Code characters in the
table converts to a unique integer and b) that the values range
from 1 (for an E) to
2*20+2*21+2*22+2*23+2*24+2*25=126
(not represented in this assignment).
o The value you get for each Morse Code sequence is called an
initial hash value, h. Store the the character correspond-ing to
the Morse Code sequence in a hash table (an array) at the position
with index = h. Thus, hash_table[1] = E, and hash_table[118] = :
(the largest value of h we will have). Also, hash_table[63] = 5.
The rest is now pretty easy. Your program should
1. Determine and print the height of the tree and the total number of nodes
in the tree. Find out if the tree is balanced or not
2. Then, read a English message from file asgn07b.dat, translate the
message, and print the translated result and write the translated result
out to a file asgn7op.dat.
3. Translate the new Morse code message back to English.
NOTE A: For each of steps 2. and 3., count the number of characters and the
number of words processed.
NOTE B: For simplicity, use the following conventions: For the Morse Code
message, assume there is one space after of Morse Code character and three
spaces after each word or punctuation mark. (Leave one space in your
translated message between words, no space between a word and a punctuation
mark, and one space after a punctuation mark.)
NOTE C: You never need to read more than once character at a time from either
your original English message or your created Morse Code message.
NOTE D: You will want to use a hash_table of size 128 to store your hashed
Morse Codes. This is wasteful since there are only 40 codes. However, it
does give us a speedy lookup with no searching involved.
Due: To be determined. Turn in to the lab assistant (one copy per group) a
behavior diagram, the descriptions for your two classes, and the final working
code. Also turn in to me (one copy per person) a one-page summary that 1)
outlines your group’s approach to this problem, 2) your contribution to the
work, and 3) what you did to ensure that everyone in the group completely
understood what everyone else was doing.
Download