#include
#include
int main ()
{
char small[26] ={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
char big[26] ={'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
char text[1000],text2[1000];
int c,j =0,key,temp;
char function;
printf("请输入明文或密文:");
c=getchar();
while(1)
{
if(c == '\n')
break;
text[j]=c;
printf("%c",text[j]);
j++;
c=getchar();
}
printf("\n");
printf("加密或者解密? E or D :");
scanf("%c",&function);
if (function == 'E')
{
printf("请输入密钥 :" );
scanf("%d",&key);
for(int i=0;i
{
if(text[i]>='A'&&text[i]<='Z')
{
text2[i]=big[((text[i]-'A')+key)%26];
}
else if (text[i]>='a'&&text[i]<='z')
{
text2[i]=small[((text[i]-'a')+key)%26];
}
else {
text2[i]=text[i];
}
printf("%c",text2[i]);
}
}
else
{
printf("请输入密钥 :" );
scanf("%d",&key);
for(int i=0;i
if(text[i]>='A'&&text[i]<='Z')
{
temp=((text[i]-'A')-key);
while(temp<0)temp+=26;
text2[i]=big[temp];
}
else if (text[i]>='a'&&text[i]<='z')
{
temp=((text[i]-'a')-key);
while(temp<0)temp+=26;
text2[i]=small[temp];
}
else text2[i]=text[i];
printf("%c",text2[i]);
}
printf("\n");
}
return 0;
}