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
|
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <math.h>
#include <cctype>
#include <string>
using namespace std;
string texttomorse(char l)
{
string text = "abcdefghijklmnopqrstuvwqyz 1234567890"; //each letter of the alphabet, a space, and numbers 0-9
string morse[] ={
" .-"," -..."," -.-."," -..", " .", " ..-.", " --.",
" ....", " ..", " .---", " -.-", " .-..", " --",
" -.", " ---", " .--.", " --.-", " .-.", " ...", " -",
" ..-", " ...-", " .--", " -..-", " -.--", " --.."," ",
" .----", " ..---", " ...--", " ....-", " .....", " -....",
" --...", " ---..", " ----.", " -----",// Each letter and number in Morse
};
int index = text.find(l);//gets character from input and finds its Morse equivalent
if(index!=-1)
return morse[index];
else
return " ";
}
string morsetotext(char l)
{
string text[] = {"a","b","c","d", "e", "f", "g", "h", "i", "j", "k", "l", "m","n","o","p","q","r","s","t","u","v","w","q","y","z"," ","1","2","3","4","5","6","7","8","9","0"}; //each letter of the alphabet, a space, and numbers 0-9
string morse={
".-/ -.../ -.-./ -../ ./ ..-./ --./ ..../ ../ .---/ -.-/ .-../ --/ -./ ---/ .--./ --.-/ .-./ .../ -/ ..-/ ...-/ .--/ -..-/ -.--/ --../ / .----/ ..---/ ...--/ ....-/ ...../ -..../ --.../ ---../ ----./ -----"// Each letter and number in Morse
};
int index = morse.find(l);//gets character from input and finds its Morse equivalent
if(index!=-1)
return text[index];
else
return " ";
}
int main ()
{
int c;
string userinput;
cout<<"Enter 1 for text to morse. 2 for morse to text\n";
cout<<"Choice: ";
cin>>c;
while (c==1)
{
getline(cin, userinput);
string out="";
for(int i=0; i<userinput.length(); i++)
{
out+= texttomorse(userinput[i]);
}
cout<< out << endl;
}
while (c==2)
{
getline(cin, userinput);
string out="";
for(int i=0; i<userinput.length(); i++)
{
out+= morsetotext(userinput[i]);
}
cout<< out << endl;
}
return 0;
}
|