-
Notifications
You must be signed in to change notification settings - Fork 0
/
morse.cpp
88 lines (85 loc) · 2.01 KB
/
morse.cpp
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 <fstream>
#include <unordered_map>
using namespace std;
void s2c(){
string arr[36];
ifstream code;
string temp;
code.open("morseCode.txt");
for(int i=0 ; i<36 ; i++){
getline(code,temp);
arr[i]=temp;
}
code.close();
cout<<"Enter a string to convert to morse code :\n";
getline(cin>>ws,temp);
int n=temp.size();
string ans="";
for(int i=0 ; i<n ; i++){
if('A'<=temp[i]&&'Z'>=temp[i]) temp[i]=temp[i]+'a'-'A';
if('a'<=temp[i]&&'z'>=temp[i]){
ans+=arr[temp[i]-'a'];
ans+="000";
}else if(temp[i]==' '){
ans+="0000";
}else{
ans+=arr[temp[i]-'0'+26];
ans+="000";
}
}
ans=ans.substr(0,ans.size()-3);
cout<<ans;
return;
}
void c2s(){
unordered_map<string,char> mp;
ifstream code;
string temp,temp2;
code.open("morseCode.txt");
for(int i=0 ; i<26 ; i++){
getline(code,temp);
mp[temp]='a'+i;
}
for(int i=26 ; i<36 ; i++){
getline(code,temp);
mp[temp]='0'+i;
}
code.close();
cout<<"Enter a code to convert to string :\n";
getline(cin>>ws,temp);
int n=temp.size();
string ans="";
for(int i=0 ; i<n ; i++){
if(temp[i]=='0'&&temp[i+1]=='0'){
ans+=mp[temp2];
if(temp[i+5]=='0'){
ans+=' ';
i+=6;
}else{
i+=2;
}
temp2.clear();
continue;
}
temp2+=temp[i];
}
ans+=mp[temp2];
cout<<ans;
return;
}
int main(){
cout<<"Welcome to Morse Writer and Interpreter\n\n";
cout<<"Enter 0 to convert message to morse code or 1 to interpret a morse coded message : ";
int x;
cin>>x;
if(x==0) {
s2c();
}else if(x==1) {
c2s();
}else {
cout<<"fuck you!!";
}
return 0;
}