本帖最后由 jackz007 于 2022-9-6 10:44 编辑
凯撒密码而已,算法很简单#include <stdio.h>
char crypt(char ch , int e)
{
char b ;
if(ch >= 'A' && ch <= 'Z' || ch >= 'a' && ch <= 'z') {
b = (ch >= 'a' && ch <= 'z') ? 'a' : 'A' ;
ch = b + (26 + ch - b + e) % 26 ;
}
return ch ;
}
int main(void)
{
char ch ;
int e = 3 ;
printf("请输入明文:") ;
while ((ch = getchar()) != '\n') putchar(crypt(ch , e)) ;
printf("\n") ;
printf("请输入密文:") ;
while ((ch = getchar()) != '\n') putchar(crypt(ch , -e)) ;
printf("\n") ;
}
编译、运行实况:D:\[00.Exerciese.2022]\C>g++ -o x x.c
D:\[00.Exerciese.2022]\C>x
请输入明文:Hello,World!
Khoor,Zruog!
请输入密文:Khoor,Zruog!
Hello,World!
D:\[00.Exerciese.2022]\C>
|