-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathCaesar.cpp
More file actions
63 lines (63 loc) · 1.12 KB
/
Copy pathCaesar.cpp
File metadata and controls
63 lines (63 loc) · 1.12 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
#include <string>
#include <iostream>
using namespace std;
int main()
{
int i,j,k;
char m[10000];
cout<<"please input simple as m:";
gets(m);
cout<<"请选择你是加密还是解密,0代表加密,1代表解密:";
cin>>j;
cout<<"你选择的是"<<j<<endl;
if(j==0)
{
cout<<"未加密时的明文为:"<<m<<endl;
cout<<"请输入加密密钥:";
cin>>k;
cout<<"加密密钥为:"<<k<<endl;
for(i=0;i<strlen(m);i++)
{
if(m[i]<='z'&&m[i]>='a')
{
cout<<(char)((m[i]-'a'+k)%26+'a');
}
else if(m[i]<='Z'&&m[i]>='A')
{
cout<<(char)((m[i]-'A'+k)%26+'A');
}
else if(m[i]<=9&&m[i]>=0)
{
cout<<((m[i]+k)%26);
}
else
cout<<m[i];
}
cout<<endl;
}
else
{
cout<<"加密后的密文为:"<<m<<endl;
cout<<"请输入解密密钥:";
cin>>k;
cout<<"解密密钥为:"<<k<<endl;
for(i=0;i<strlen(m);i++)
{
if(m[i]<='z'&&m[i]>='a')
{
cout<<(char)((m[i]-'a'+26-k)%26+'a');
}
else if(m[i]<='Z'&&m[i]>='A')
{
cout<<(char)((m[i]-'A'+26-k)%26+'A');
}
else if(m[i]<=9&&m[i]>=0)
{
cout<<((m[i]-k)%26);
}
else
cout<<m[i];
}
cout<<endl;
}
}