其他分享
首页 > 其他分享> > 1.1凯撒密码

1.1凯撒密码

作者:互联网

 1 #include <iostream>
 2 #include <string>
 3 using namespace std;
 4 string encoder(string text,int offset)//加密 
 5 {
 6     string encoded_text=text;
 7     for(auto &i:encoded_text)
 8     {
 9         if(i>='a'&&i<='z')
10         i=(i-'a'+offset)%26+'a';
11         if(i>='A'&&i<='Z')
12         i=(i-'A'+offset)%26+'A';
13     }
14     return encoded_text;
15 }
16 string decoder(string text,int offset)//解密 
17 {
18     string decoded_text=text;
19     for(auto &i:decoded_text)
20     {
21         if(i>='a'&&i<='z')
22         i=(i-'a'-offset+26)%26+'a';
23         if(i>='A'&&i<='Z')
24         i=(i-'A'-offset+26)%26+'A';
25     }
26     return decoded_text;
27 }
28 int main()
29 {
30     string text,encoded_text,decoded_text;
31     cout<<"请输入偏移量:"<<endl;
32     int offset;//偏移量
33     cin>>offset;
34     cout << "输入英文文本: "<<endl;
35     char c=getchar();//吞掉回车 !!!!!!!!不然读入text是回车 
36     while(getline(cin,text))
37     {
38         cout<<"加密(输入:e) or 解密(输入:d) ?"<<endl;
39         char flag;
40         cin>>flag;
41         if(flag=='e')
42         {
43             encoded_text = encoder(text,offset);        
44             cout << "加密后英文文本:\t" << encoded_text << endl;
45         }
46         else
47         {
48             decoded_text = decoder(text,offset);
49             cout << "解密后英文文本:\t" << decoded_text << endl;
50         }
51         
52         cout<<"请输入偏移量:"<<endl;
53         int offset;//偏移量
54         cin>>offset;
55         cout << "输入英文文本: "<<endl;
56         char c=getchar();
57     }
58 }

标签:1.1,text,密码,&&,offset,encoded,凯撒,string,cout
来源: https://www.cnblogs.com/zhouruohong/p/16080276.html