# include<iostream>
# include<cstring>
# include<string>
using namespace std;
int main( )
{  
	cout<<"\n";
	cout<<"              ---------------凯撒加密算法--------------"<<endl<<endl;
	char a[26];
	int i;
	for(i=0;i<26;i++)  
		a[i]=char(65+i);
	cout<<"密码表为："<<endl;
	for(i=0;i<26;i++)
		cout<<i<<"------"<<a[i]<<'\t';
	cout<<endl;                                        
 //以上为密码表的定义；
	char plain[64],cipher[64];   // 明文、密文字符串
     //==================================================================
     //加密阶段 
	cout<<"\n***加密***\n"<<endl;
while(1)
	{
		cout<<"请输入明文(明文需用连续的大写字母):"<<"\n";
		cin>>plain;
		for( i=0;i<strlen(plain);i++)
		{
			if(plain[i]<'A'||plain[i]>'Z')  break;
		}
		if(i>=strlen(plain))  break;
	}
cout<<"您输入的明文为："<<plain<<endl;                //明文的输入；
	int key;
	cout<<"请输入您的密钥(必需是整数):";
	cin>>key;
	cout<<"您输入的密钥为:"<<key<<endl;
	cout<<"所得的凯撒密码为:";
	for(i=0;i<strlen(plain);i++)        //明文转换为凯撒密码；
	{
		int c,j;
		c=int(plain[i]);
		j=((c-65)+key)%26;
		cipher[i]=a[j];                 //存储到密文字符串；
	}
	cipher[i]=0;
	cout<<cipher;
	cout<<endl;
//==================================================================
//解密阶段
	cout<<"\n***解密***\n"<<endl;
	cout<<"请输入您的凯撒密码(需用大写连续英文字母):";
	cin>>cipher;
	cout<<"您输入的凯撒密码为:"<<cipher<<endl;
    cout<<"请输入您的密钥(必需是整数):";
	cin>>key;
    cout<<"您输入的密钥为:"<<key<<endl;
	cout<<"解密后的明文为:";
	for(i=0;i<strlen(cipher);i++)           //凯撒密码转换为明文；
	{
		int c,j;
		c=int(cipher[i]);
		j=((c-65)-key+26)%26;
		plain[i]=a[j];
	}
	plain[i]=0;
	cout<<plain;
    cout<<endl;                                    
}
