Will award the person who helps me with a lifesaver rating!! Really could use so
ID: 3625781 • Letter: W
Question
Will award the person who helps me with a lifesaver rating!! Really could use some help.
Sorry if this looks like a lot but I just threw in the code ive been working with at the end.
C++
Morse Code Tree class: This class uses a Binary Tree to store a Morse code tree.
Data Members:
1. Binary_Tree tree;
tree holds the Morse code tree
2. string codes[26];
codes holds the Morse code table
Function Members:
1. constructor
When a Morse Code Tree object is created in the main program, the constructor should perform
the following operations.
Read the input file Morse_Code.txt, and use it to build tree. The first few lines of the file are as follows:
*
e
i
s
h
NULL
NULL
v
NULL
NULL
u
f
NULL
NULL
NULL
a
r
l
NULL
NULL
NULL
w
p
NULL
NULL
j
NULL
NULL
t
n
d
b
NULL
NULL
x
NULL
NULL
k
c
NULL
NULL
y
NULL
NULL
m
g
z
NULL
NULL
q
NULL
NULL
o
NULL
NULL
The class Binary Tree has a member function read_binary_tree() that performs this task.
• From the Morse Code Tree created above, build a Morse code table and store it in the data member codes. The table has 26 entries, one entry for each letter. The entry contains the Morse code for the letter.
2. string encode(const string& message);
This function has an input parameter string message, which contains a sequence of letters (the letters can be either upper or lower case.) The function converts it into the corresponding Morse code.
3. string decode(const string& mcode);
This function has an input parameter string mcode, which contains the Morse code. The function converts it back to the original message.
Note: The original message may contain both lower and upper case letters. However, Morse code table does not has separate code for lower and upper case letters. (For example both letter A and a are coded as .-.) Thus, the string converted by the decode function contains lower case letters only.
This is what I've gotten done so far. I'm pretty sure most of it is correct I want someone to look at it and tell me if I'm doing something different from what the instructions are telling me to do cause theyre a little vague I feel.
Morse_Code_Tree.h
#ifndef MORSE_CODE_TREE_H_
#define MORSE_CODE_TREE_H_
#include
#include
#include
#include "Binary_Tree.h"
#include "BTNode.h"
using namespace std;
class Morse_Code_Tree {
public:
Morse_Code_Tree();
string encode(const string& message);
string decode(const string& mcode);
private:
Binary_Tree tree;
string codes[26];
};
#endif
Morse_Code_Tree.cpp
#include "Morse_Code_Tree.h"
using namespace std;
Morse_Code_Tree::Morse_Code_Tree(){
// char* file_name = "Morse_Code.txt";
// ifstream in(file_name);
ifstream in("Morse_Code.txt");
Binary_Tree tree = Binary_Tree::read_binary_tree(in);
/*a*/ codes[0] = ".-";
/*b*/ codes[1] = "-...";
/*c*/ codes[2] = "-.-.";
/*d*/ codes[3] = "-..";
/*e*/ codes[4] = ".";
/*f*/ codes[5] = "..-.";
/*g*/ codes[6] = "--.";
/*h*/ codes[7] = "....";
/*i*/ codes[8] = "..";
/*j*/ codes[9] = ".---";
/*k*/ codes[10] = "-.-";
/*l*/ codes[11] = ".-..";
/*m*/ codes[12] = "--";
/*n*/ codes[13] = "-.";
/*o*/ codes[14] = "---";
/*p*/ codes[15] = ".--.";
/*q*/ codes[16] = "--.-";
/*r*/ codes[17] = ".-.";
/*s*/ codes[18] = "...";
/*t*/ codes[19] = "-";
/*u*/ codes[20] = "..-";
/*v*/ codes[21] = "...-";
/*w*/ codes[22] = ".--";
/*x*/ codes[23] = "-..-";
/*y*/ codes[24] = "-.--";
/*z*/ codes[25] = "--..";
}
string Morse_Code_Tree::encode(const string& message){
int length = message.size();
for (int i = 0; i
{
switch (message[i])
{
case 'a':
return codes[0];
break;
case 'A':
return codes[0];
break;
case 'b':
return codes[1];
break;
case 'B':
return codes[1];
break;
case 'c':
return codes[2];
break;
case 'C':
return codes[2];
break;
case 'd':
return codes[3];
break;
case 'D':
return codes[3];
break;
case 'e':
return codes[4];
break;
case 'E':
return codes[4];
break;
case 'f':
return codes[5];
break;
case 'F':
return codes[5];
break;
case 'g':
return codes[6];
break;
case 'G':
return codes[6];
break;
case 'h':
return codes[7];
break;
case 'H':
return codes[7];
break;
case 'i':
return codes[8];
break;
case 'I':
return codes[8];
break;
case 'j':
return codes[9];
break;
case 'J':
return codes[9];
break;
case 'k':
return codes[10];
break;
case 'K':
return codes[10];
break;
case 'l':
return codes[11];
break;
case 'L':
return codes[11];
break;
case 'm':
return codes[12];
break;
case 'M':
return codes[12];
break;
case 'n':
return codes[13];
break;
case 'N':
return codes[13];
break;
case 'o':
return codes[14];
break;
case 'O':
return codes[14];
break;
case 'p':
return codes[15];
break;
case 'P':
return codes[15];
break;
case 'q':
return codes[16];
break;
case 'Q':
return codes[16];
break;
case 'r':
return codes[17];
break;
case 'R':
return codes[17];
break;
case 's':
return codes[18];
break;
case 'S':
return codes[18];
break;
case 't':
return codes[19];
break;
case 'T':
return codes[19];
break;
case 'u':
return codes[20];
break;
case 'U':
return codes[20];
break;
case 'v':
return codes[21];
break;
case 'V':
return codes[21];
break;
case 'w':
return codes[22];
break;
case 'W':
return codes[22];
break;
case 'x':
return codes[23];
break;
case 'X':
return codes[23];
break;
case 'y':
return codes[24];
break;
case 'Y':
return codes[24];
break;
case 'z':
return codes[25];
break;
case 'Z':
return codes[25];
break;
}
}
return "";
}
string Morse_Code_Tree::decode(const string& mcode){
if (mcode == codes[0]) {
return "a";
}
else if (mcode == codes[1]) {
return "b";
}
else if (mcode == codes[2]) {
return "c";
}
else if (mcode == codes[3]) {
return "d";
}
else if (mcode == codes[4]) {
return "e";
}
else if (mcode == codes[5]) {
return "f";
}
else if (mcode == codes[6]) {
return "g";
}
else if (mcode == codes[7]) {
return "h";
}
else if (mcode == codes[8]) {
return "i";
}
else if (mcode == codes[9]) {
return "j";
}
else if (mcode == codes[10]) {
return "k";
}
else if (mcode == codes[11]) {
return "l";
}
else if (mcode == codes[12]) {
return "m";
}
else if (mcode == codes[13]) {
return "n";
}
else if (mcode == codes[14]) {
return "o";
}
else if (mcode == codes[15]) {
return "p";
}
else if (mcode == codes[16]) {
return "q";
}
else if (mcode == codes[17]) {
return "r";
}
else if (mcode == codes[18]) {
return "s";
}
else if (mcode == codes[19]) {
return "t";
}
else if (mcode == codes[20]) {
return "u";
}
else if (mcode == codes[21]) {
return "v";
}
else if (mcode == codes[22]) {
return "w";
}
else if (mcode == codes[23]) {
return "x";
}
else if (mcode == codes[24]) {
return "y";
}
else if (mcode == codes[25]) {
return "z";
}
else {
return "";
}
}
Explanation / Answer
Alright well first off there are a couple of problems that i see. The first is in your encode. The problem with this is that you are not going through the entire string, but simply the very first letter of each word, where upon you return the first letter in morris code. I would say to get around this to change the header of this function to read in a char and return a string (aka make this a private helper method), which you can then use to return each and every one. Second is with the decode, there is no == operator assigned to string, you need to use compare instead, as the == is comparing the memory locations of string instead of what you want. string Morse_Code_Tree::encode(const string& message) { string returnThis; int length =message.size(); for(int i=0;iRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.