-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBaconian-cipher.cpp
More file actions
88 lines (76 loc) · 2.2 KB
/
Baconian-cipher.cpp
File metadata and controls
88 lines (76 loc) · 2.2 KB
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 <bits/stdc++.h>
using namespace std;
//todo:solve cin problem
int main(){
string decoded,coded,decider;
while(true)
{
cout<<"\n1-cipher a message\n"<<"2-decipher a message\n"<<"3-end\n";
cin>>decider;
if(decider=="1")
{
cout<<"enter text to encode: ";
cin.ignore();
getline(cin,decoded);
//todo:check for alphabet only and not empty and trim
transform(decoded.begin(),decoded.end(),decoded.begin(),::tolower);
string ans = "";
for(int i=0 ; i<decoded.size() ; ++i)
{
if(decoded[i]==' ')
{
ans+=" ";
continue;
}
int k = decoded[i]-'a';
string coding="";
while(k>0 || coding.size()<5)
{
coding = char(k%2 + 65) + coding;
k/=2;
}
ans += coding;
ans += " ";
}
cout<<ans<<"\n";
}
else if(decider=="2")
{
cout<<"enter text to decode: ";
cin.ignore();
getline(cin,coded);
//todo:check for alphabet only and not empty and trim
transform(decoded.begin(),decoded.end(),decoded.begin(),::toupper);
string ans="";
for (int i=0 ; i<coded.size() ;)
{
int exponent = 16;
int letter = 0;
while(coded[i]==' ')
{
ans+=" ";
++i;
}
for (int j=0 ; j<5 ; ++j)
{
letter += int(coded[i]-65)*exponent;
exponent = exponent>>1;
++i;
}
ans += char(letter+97);
++i;
}
cout<<ans<<"\n";
}
else if(decider=="3")
{
cout<<"\nGoodbye\n";
break;
}
else
{
cout<<"\nEnglish motherfucker do you speak it?\n";
}
}
return 0;
}