Question: help me translating text into Morse Code Requirements 1. The approach should be Object Oriented. I don't want to see parallel arrays or a long
help me translating text into Morse Code
Requirements
1. The approach should be Object Oriented. I don't want to see parallel arrays or a long if/else if/else or switch statement. 2. Prompt the user to enter a sentence. This means you must support whitespace in the string input. The sentence can have a maximum of 50 characters. 3. Output the sentence character by character by translating the character into it's Morse Code equivalent. You can print each on an individual line or on one line separated by a space. 4. Use the Morse Code table as shown on Wikipedia. Represent with periods and hyphens. For example the character 'B' would be output as string "-..." 5. Morse Code is case insensitive. 'a' and 'A' are the same character. You will need to handle this in your code. 6. You can ignore any character in the sentence that is not a letter or number. 7. I will not only be grading program correctness, I'm also going to be grading your Object Oriented approach. So please take the time to think about the design of the program before implementing.
Note: if you also support translating Morse Code into English Alphabet characters. You can ask the user to separate the Morse codes with a space. Same rules apply as above, except you'll expect Morse codes as input and output the sentence. You must support more than one character. The sentence you output can be all upper or lower case characters. You do not have to be case sensitive in your output.
#include
using namespace std;
struct TREENODE {
char letter;
TREENODE *left;
TREENODE *right;
TREENODE() { // Constructor
letter = '*'; //To be replaced by a letter later.
left = 0;
right = 0;
}
};
struct MORSECODE { //each morsecode object has
char letter; //English letters.
char code[20]; //Dots + dashes
};
class TELEGRAPH { //The binary (tree)
private:
static MORSECODE table[40];
static TREENODE * root;
static void destroyTree(TREENODE *node);
public:
TELEGRAPH() {
root = NULL;
}
static void buildTree();
static void destroyTree();
void Encode(char text[], char morse[]);
void Decode(char morse[], char text[]);
};
MORSECODE TELEGRAPH::table[40] = {
{ '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', "----." },
{ '.', ".-.-.-" },{ ',', "--..--" },{ '?', "..--.." },
{ '\0', "END" }
};
TREENODE* TELEGRAPH::root = 0;
void TELEGRAPH::Decode(char morse[], char text[]) {
char *morsePtr;
TREENODE *node;
node = root;
cout << "Decode called." << endl;
for (morsePtr = morse; *morsePtr; morsePtr++) {
if (*morsePtr != ' ') {
if (*morsePtr == '.') {
node = node->left;
}
else if (*morsePtr == '-') {
node = node->right;
}
}
continue;
}
*text++ = node->letter;
return;
}
void TELEGRAPH::Encode(char text[], char morse[]) {
int i;
char c, *t, *morsePtr;
cout << "Encode called" << endl;
cout << " Sending >>> ";
for (t = text; *t; t++) {
c = toupper(*t);
if (c == ' ') {
*morse++ = ' ';
continue;
}
for (i = 0; table[i].letter; i++) {
if (table[i].letter == c) break;
}
if (!table[i].letter) {
continue;
}
morsePtr = table[i].code;
while (*morsePtr) {
*morse++ = *morsePtr++;
}
*morse++ = ' ';
}
}
void TELEGRAPH::buildTree() {
TREENODE *node, *nextNode;
char *morsePtr; //Points to the dots and dashes in the table.
root = new TREENODE;
if (!root) {
return;
}
root->letter = ' ';
cout << "Alphabet in Morse:";
for (int i = 0; table[i].letter; i++) {
node = root;
for (morsePtr = table[i].code; *morsePtr; morsePtr++) { //goes through the morse code for that letter/symbol.
if (*morsePtr == '-') {
cout << *morsePtr;
nextNode = new TREENODE;
node->right = nextNode;
node = node->right;
}
else if (*morsePtr == '.') {
cout << *morsePtr;
nextNode = new TREENODE;
node->left = nextNode;
node = node->left;
}
}
}
}
int main(){ TELEGRAPH station; char text[80], morse[600]; station.buildTree(); cout << " Enter telegram (in English): "; cin.getline(text, 80); station.Encode(text, morse); cout << morse; cout << " >>> Received "; station.Decode(morse, text); cout << "Message sent: " << text << endl; station.destroyTree(); } Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
