Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

----I dont receive any errors or warnings ----Im using visual studio ----the program translates english to morse fine, but when trying to translate morse to

----I dont receive any errors or warnings ----Im using visual studio ----the program translates english to morse fine, but when trying to translate morse to english, the program just spits back out the morse that was suppose to be translated to english code:

#include //headerfile #include //headerfile #include //headerfile #include //headerfile #include //headerfile #include //headerfile

using namespace std;

class BST //binary search tree class { private://private class struct Node { // initializing struct data members using uniform initialization string letter; string code; Node* left{ nullptr }; Node* right{ nullptr }; }; Node* root;//points root node memory location public:// public class BST() { root = NULL;//empty tree } void Insert(Node*& r, string letter, string code)//insert english and morse string into tree { if (r == NULL)//if node is empty { r = new Node;//assigns r to new node r->letter = letter;//r accesses string letter r->left = r->right = NULL; r->code = code; } if (r->letter > letter) Insert(r->left, letter, code);//accesses the property of left node/// if (r->letter < letter) Insert(r->right, letter, code);//accesses the property of right node//// } void Insert(string letter, string code)//insert english and morse string into tree { Insert(root, letter, code);//insert english and morse string into root node } void DisplayPreOrder(Node* r)//display tree traversal { if (r != NULL)//if node is not empty { cout << r->letter << "\t";//display letter node DisplayInOrder(r->left);//display tree traversal DisplayInOrder(r->right);//display tree traversal } } void DisplayInOrder(Node* r)//display tree traversal { if (r != NULL)//if node is not empty { DisplayInOrder(r->left);//display tree traversal of left traversal cout << r->letter << "\t"; DisplayInOrder(r->right);//display tree traversal of right traversal } } // Overrides DisplayInOrder(Node * ) void DisplayInOrder()//display tree traversal functions { DisplayInOrder(root);//display tree traversal functions } void DisplayPreOrder()//display tree traversal functions { DisplayPreOrder(root);//display tree traversal functions } void DisplayPostOrder(Node* r)//display tree traversal functions { if (r != NULL)//if node is not empty { DisplayPostOrder(r->left);//display tree traversal of left traversal DisplayPostOrder(r->right);//display tree traversal of right traversal cout << r->letter << "\t"; } } void Encode(char x)//function to encode morsecode string { Node* r = SearchAndReturn(root, x);//points to value returned x if (r != NULL) //if node is not empty cout << r->code; else cout << "Error.";//display error } void Decode(string x)//functiont to decode english string { Node* r = SearchAndReturnString(root, x);//search node r and return string if (r->code == x) cout << r->letter; else cout << r->code << " with x being " << x << endl;//displays the strings translated to english cout << "Error.";//display error to user } Node* SearchAndReturn(Node* r, char x)//function to search and retreive value in tree { if (r != NULL)//if node is not empty { if (r->letter[0] == x) { return r; } else if (r->letter[0] > x) { SearchAndReturn(r->left, x);//search and return left node } else { SearchAndReturn(r->right, x);//search and return right node } } else return NULL;//return empty } Node* SearchAndReturnString(Node* r, string x)//function to search and retreive value in tree { if (r != NULL)//if node is not empty { if (r->code == x) { cout << r->code << " matches " << x << endl; return r; } else if (r->code > x) { SearchAndReturnString(r->left, x); }//function to search and retreive value in tree else { SearchAndReturnString(r->right, x); }//function to search and retreive value in tree } else return NULL;//return empty } }; struct alphaTree { string letter; string code; }; int main() { time_t u; time(&u); cout << ctime(&u) << endl;//function to display current time BST t; string message; char* morseCode = new char; char ans; alphaTree array[27] = { {"E", ". "}, {"T", "- "}, {"I", ".. "}, {"A", ".- "}, {"N", "-. "}, {"M", "-- "},//tree containing character and their morsecode translation {"S", "... "}, {"U", "..- "}, {"R", ".-. "}, {"W", ".-- "}, {"D", "-.. "}, {"K", "-.- "},//tree containing character and their morsecode translation {"G", "--. "}, {"O", "--- "}, {"H", ".... "}, {"V", "...- "}, {"F", "..-. "}, {"L", ".-.. "},//tree containing character and their morsecode translation {"P", ".--. "}, {"J", ".--- "}, {"B", "-... "}, {"X", "-..- "},//tree containing character and their morsecode translation {"C", "-.-. "}, {"Y", "-.-- "}, {"Z", "--.. "}, {"Q", "--.- "}, {" ", "---- "} };//tree containing character and their morsecode translation t.Insert("0", ""); for (int i = 0; i < 27; i++)//for loop for character insertions into tree { t.Insert(array[i].letter, array[i].code);//insert function into tree } do { cout << "Enter your message: ";//ask the user to enter their message to be decoded getline(cin, message);//extracts characters from string int len = message.length();//length method call of string length for (int i = 0; i < len; i++)//convert string to upper case for loop { message[i] = toupper(message[i]);//convert string to upper case for loop char c = message[i]; if (c == ' ') cout << "/ "; t.Encode(c);//function to encode morsecode } cout << endl; cout << "Enter your Morse code, separated by /, ended by *: ";//ask user to enter morsecode to be encoded cin.getline(morseCode, 100, '*');//extracts characters from string char* token = strtok(morseCode, "/"); while (token != NULL)//while token is not empty { cout << endl << "Decoding: " << token << endl; string newCode = token; t.Decode(newCode);//decode function token = strtok(NULL, "/"); } cout << "Continue: "; cin >> ans; cin.ignore(); cout << endl; } while (ans == 'y'); return 0; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Introduction to Wireless and Mobile Systems

Authors: Dharma P. Agrawal, Qing An Zeng

4th edition

1305087135, 978-1305087132, 9781305259621, 1305259629, 9781305537910 , 978-130508713

More Books

Students also viewed these Programming questions