help me translate English texting into Morse Code and visa versa using a format
ID: 3719221 • Letter: H
Question
help me translate English texting into Morse Code and visa versa using a format like this :
#include <iostream>
using namespace std;
class character
{
private:
char english;
string morse;
public:
Alphabet(char,string);
char getEnglish() const;
string getMorse() const;
}
class translator
{
private:
charactor array[36]
public:
translator()
{
array[0]=charactor('a',"-");
}
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.
Explanation / Answer
#include <iostream>
#include <cstring>
using namespace std;
void engconvert (char[50]);
int main()
{
char englstring[50];
cout <<"Enter the English text which you would like to be converted to Morse Code: ";
cin >> englstring;
engconvert(englstring);
system ("pause");
return 0;
}
void engconvert (char english[])
{
int englishstring2;
englishstring2 = strlen (english);
cout << englishstring2 << endl;
cout << "Here is the morse code translation: ";
for (int i = 0; i<englishstring2; i++)
{
if (english[i] == ' ')
cout << endl;
else if (english[i] == ',')
cout << "--..--" << endl;
else if (english[i] == '.')
cout << ".-.-.-" << endl;
else if (english[i] == '?')
cout << "..--.." << endl;
else if (english[i] == '0')
cout << "-----" << endl;
else if (english[i] == '1')
cout << ".----" << endl;
else if (english[i] == '2')
cout << "..---" << endl;
else if (english[i] == '3')
cout << "...--" << endl;
else if (english[i] == '4')
cout << "....-" << endl;
else if (english[i] == '5')
cout << "....." << endl;
else if (english[i] == '6')
cout << "-...." << endl;
else if (english[i] == '7')
cout << "--..." << endl;
else if (english[i] == '8')
cout << "---.." << endl;
else if (english[i] == '9')
cout << "----." << endl;
else if (english[i] == 'A' || english[i] == 'a')
cout << ".-" << endl;
else if (english[i] == 'B' || english[i] == 'b')
cout << "-..." << endl;
else if (english[i] == 'C' || english[i] == 'c')
cout << "-.-." << endl;
else if (english[i] == 'D' || english[i] == 'd')
cout << "-.." << endl;
else if (english[i] == 'E' || english[i] == 'e')
cout << "." << endl;
else if (english[i] == 'F' || english[i] == 'f')
cout << "..-." << endl;
else if (english[i] == 'G' || english[i] == 'g')
cout << "--." << endl;
else if (english[i] == 'H' || english[i] == 'h')
cout << "...." << endl;
else if (english[i] == 'I' || english[i] == 'i')
cout << ".." << endl;
else if (english[i] == 'J' || english[i] == 'j')
cout << ".---" << endl;
else if (english[i] == 'K' || english[i] == 'k')
cout << "-.-" << endl;
else if (english[i] == 'L' || english[i] == 'l')
cout << ".-.." << endl;
else if (english[i] == 'M' || english[i] == 'm')
cout << "--" << endl;
else if (english[i] == 'N' || english[i] == 'n')
cout << "-." << endl;
else if (english[i] == 'O' || english[i] == 'o')
cout << "---" << endl;
else if (english[i] == 'P' || english[i] == 'p')
cout << ".--." << endl;
else if (english[i] == 'Q' || english[i] == 'q')
cout << "--.-" << endl;
else if (english[i] == 'R' || english[i] == 'r')
cout << ".-." << endl;
else if (english[i] == 'S' || english[i] == 's')
cout << "..." << endl;
else if (english[i] == 'T' || english[i] == 't')
cout << "-" << endl;
else if (english[i] == 'U' || english[i] == 'u')
cout << "..-" << endl;
else if (english[i] == 'V' || english[i] == 'v')
cout << "...-" << endl;
else if (english[i] == 'W' || english[i] == 'w')
cout << ".--" << endl;
else if (english[i] == 'X' || english[i] == 'x')
cout << "-..-" << endl;
else if (english[i] == 'Y' || english[i] == 'y')
cout << "-.--" << endl;
else if (english[i] == 'Z' || english[i] == 'z')
cout << "--.." << endl;
else
{
cout << "You have not entered a character in the american";
cout << " alphabet. Please re-run the program and try again." << endl;
}
}
cout <<endl;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.