1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
|
#include <iostream>
#include <string>
#include <cctype>
#include <algorithm>
#include <functional>
using namespace std;
int main()
{
string morse[39]={".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-",".-..", "--",
"-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..",
".----", "..---", "...--", "...._", ".....", "-....", "--...", "---..", "----.", "-----",
".-.-.-", "--.--", "..--.."};
string plaintext[39]={"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", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0",
"Stop", ",", "?"};
string text, morsecode;
char answer;
int textlength, choice;
int count=0, i=0;
bool again=true;
while (again)
{
cout<<"1-3 Enter a string to convert to Morse Code.\n"
"2-3 Enter Morse Codes to convert to Text.\n"
"3-3 Quit!!!";
cout<<"\n\nEnter your choice: ";
cin>>choice;
switch(choice)
{
case 1:
cout<<"Enter a string: ";
getline(cin, text);
textlength=text.size();
cout<<"You Entered: \""<<text<<"\" with "<<textlength<<" characters.\n\nConvert to: ";
transform(text.begin(), text.end(), text.begin(), ptr_fun <int, int> (toupper ) );
for (i=0;i<textlength;i++)
{
int find_string=0;
while (find_string < 39)
{
if (text[i]==plaintext[find_string][0])
{
count=1;
cout << morse[find_string] << " ";
break;
}
find_string+=1;
}
}
if (count==0)
cout << endl << "No conversion was done due to an error" << "\a" << endl ;
break;
case 2:
cout<<"Convert Morse Code to Text";
cout<<"\n\nEnter Morse Code(sperate by space): ";
getline(cin, morsecode);
for(int i=0; i<morsecode.size(); i++)
{
int find_string=0;
while (find_string < 39)
{
if (morsecode[i]==morse[find_string][0])
{
count=1;
cout << plaintext[find_string] << " ";
break;
}
find_string+=1;
}
}
if (count==0)
cout << endl << "No conversion was done due to an error" << "\a" << endl;
break;
}
cout<<"\n\nRun Again(y/n)? ";
cin>>answer;
answer=toupper(answer);
if (answer!='y')
again=false;
}
return 0;
}
|