用户:AlanTuring查看:2 回复:2 评论:2 创建时间:2020-08-19T12:53:30
C++制作,凯撒密码生成/破译器(选项1和2弄反了)
附代码:
#include<iostream>
#include<string>
#include<cstring>
using namespace std;
void tips()
{
cout<<"##############################"<<endl;
cout<<"########1.左移获取密码########"<<endl;
cout<<"########2.右移获取密码########"<<endl;
cout<<"########3.左移破译密码########"<<endl;
cout<<"########4.右移破译密码########"<<endl;
cout<<"########5.退出凯撒密码########"<<endl;
cout<<"##############################"<<endl;
}
void PasswordLeft(string &mcode)
{
for(int i=0;i<mcode.size();i++)
{
if(mcode[i]!='z')
{
mcode[i]++;
}
else
{
mcode[i]=97;
}
}
}
void PasswordRight(string &mcode)
{
for(int i=0;i<mcode.size();i++)
{
if(mcode[i]!='a')
{
mcode[i]--;
}
else
{
mcode[i]=122;
}
}
}
void CodeLeft(string &mcode)
{
for(int i=0;i<mcode.size();i++)
{
if(mcode[i]!='a')
{
mcode[i]--;
}
else
{
mcode[i]=122;
}
}
}
void CodeRight(string &mcode)
{
for(int i=0;i<mcode.size();i++)
{
if(mcode[i]!='z')
{
mcode[i]++;
}
else
{
mcode[i]=97;
}
}
}
int main()
{
string password,code;
int choice=1;
while(choice==1 or choice==2 or choice==3 or choice==4)
{
tips();
cout<<"请输入选择";
cin>>choice;
if(choice==1)
{
cout<<"请输入密码";
cin>>code;
cout<<"正在转换!"<<endl;
PasswordLeft(code);
cout<<code<<endl;
cout<<"转换成功!"<<endl;
}
else if(choice==2)
{
cout<<"请输入密码";
cin>>code;
cout<<"正在转换!"<<endl;
PasswordRight(code);
cout<<code<<endl;
cout<<"转换成功!"<<endl;
}
else if(choice==3)
{
cout<<"请输入密码";
cin>>code;
cout<<"正在破译!"<<endl;
CodeLeft(code);
cout<<code<<endl;
cout<<"破译成功!"<<endl;
}
else if(choice==4)
{
cout<<"请输入密码";
cin>>code;
cout<<"正在破译!"<<endl;
CodeRight(code);
cout<<code<<endl;
cout<<"破译成功!"<<endl;
}
else
{}
system("pause");
system("cls");
}
cout<<"退出成功"<<endl;
system("pause");
return 0;
}