|
| 1 | +//C++ implemention of morse code decoding |
| 2 | +#include <iostream> |
| 3 | +#include<string> |
| 4 | +#include <iomanip> |
| 5 | +#include <map> //For map data structure |
| 6 | +using namespace std; |
| 7 | + |
| 8 | +//function for creating map structure |
| 9 | +map<string,string> createMap() |
| 10 | +{ |
| 11 | + // Decalaring map structure and Defination of map |
| 12 | + map<string, string> decrypt; |
| 13 | + decrypt[".-"] = "A"; |
| 14 | + decrypt["-..."] = "B"; |
| 15 | + decrypt["-.-."] = "C"; |
| 16 | + decrypt["-.."] = "D"; |
| 17 | + decrypt["."] = "E"; |
| 18 | + decrypt["..-."] = "F"; |
| 19 | + decrypt["--."] = "G"; |
| 20 | + decrypt["...."] = "H"; |
| 21 | + decrypt[".."] = "I"; |
| 22 | + decrypt[".---"] = "J"; |
| 23 | + decrypt["-.-"] = "K"; |
| 24 | + decrypt[".-.."] = "L"; |
| 25 | + decrypt["--"] = "M"; |
| 26 | + decrypt["-."] = "N"; |
| 27 | + decrypt["---"] = "O"; |
| 28 | + decrypt[".--."] = "P"; |
| 29 | + decrypt["--.-"] = "Q"; |
| 30 | + decrypt[".-."] = "R"; |
| 31 | + decrypt["..."] = "S"; |
| 32 | + decrypt["-"] = "T"; |
| 33 | + decrypt["..-"] = "U"; |
| 34 | + decrypt["...-"] = "V"; |
| 35 | + decrypt[".--"] = "W"; |
| 36 | + decrypt["-..-"] = "X"; |
| 37 | + decrypt["-.--"] = "Y"; |
| 38 | + decrypt["--.."] = "Z"; |
| 39 | + decrypt[".----"] = "1"; |
| 40 | + decrypt["..---"] = "2"; |
| 41 | + decrypt["...--"] = "3"; |
| 42 | + decrypt["....-"] = "4"; |
| 43 | + decrypt["....."] = "5"; |
| 44 | + decrypt["-...."] = "6"; |
| 45 | + decrypt["--..."] = "7"; |
| 46 | + decrypt["---.."] = "8"; |
| 47 | + decrypt["---."] = "9"; |
| 48 | + decrypt["-----"] = "0"; |
| 49 | + return decrypt; |
| 50 | +} |
| 51 | + |
| 52 | +map<string, string> decrypt=createMap(); |
| 53 | +// decryption Function |
| 54 | +void decryption(string msg) |
| 55 | +{ |
| 56 | + string plain = "", tempPlain; |
| 57 | + for (int i = 0; i < msg.size(); i++) |
| 58 | + { |
| 59 | + if (msg[i] != ' ' && msg[i] != '\t' && msg[i] != '\n') |
| 60 | + { |
| 61 | + tempPlain.append(1, msg[i]); |
| 62 | + } |
| 63 | + if ( msg[i] == ' ' || i==msg.size()-1) |
| 64 | + { |
| 65 | + plain=plain +decrypt[tempPlain]; |
| 66 | + tempPlain=""; |
| 67 | + } |
| 68 | + if(msg[i] == '\t' || msg[i] =='\n') |
| 69 | + { |
| 70 | + plain=plain +decrypt[tempPlain]; |
| 71 | + tempPlain=""; |
| 72 | + plain += " "; |
| 73 | + |
| 74 | + } |
| 75 | + } |
| 76 | + cout <<"Decrypted Code=:"<< plain<<endl; |
| 77 | +} |
| 78 | +//Driver program |
| 79 | +int main() |
| 80 | +{ |
| 81 | + string input = ""; |
| 82 | + char c; |
| 83 | + cout<<"Keep spacing as follows and end morse code 'q' character.\n"; |
| 84 | + cout<<"----------------------------------------------------------------\n"; |
| 85 | + cout<<setw(20)<<" |"<<setw(20)<<" Morse Spacing |"<<setw(25)<<"This Program Spacing |\n"; |
| 86 | + cout<<"----------------------------------------------------------------\n"; |
| 87 | + |
| 88 | + cout<<setw(20)<<"Letter to Letter |"<<setw(20)<<"Three dits |"<<setw(25)<<"Single Space |\n"; |
| 89 | + cout<<setw(20)<<"Word to Word |"<<setw(20)<<"Five dits |"<<setw(25)<<"Tab(key) |\n"; |
| 90 | + cout<<"----------------------------------------------------------------\n"; |
| 91 | + |
| 92 | + cout<<"Enter Morse Code: "; |
| 93 | + while (cin.get(c) && c != 'q') |
| 94 | + { |
| 95 | + input.append(1, c); |
| 96 | + } |
| 97 | + cout << "\nInput=" << input << endl; |
| 98 | + //Calling Function |
| 99 | + decryption(input); |
| 100 | + return 0; |
| 101 | +} |
0 commit comments