#include<stdlib.h>
#include<stdio.h>
#include<string.h>
void Encry()
{
char key[100];
char ch, temp;
int L, i = 0, j = 0;
if (getchar() == “\n”)
temp = ” “;
printf(“请输入密钥:”);
gets_s(key,100);
L = strlen(key);
printf(“请输入明文:”);
while ((ch = getchar()) != “\n”)
{
if (ch = ” “)
{
i++;
continue;
}
if (ch >= “a”&&ch <= “z”)
{
printf(“%c”, (ch + key[j%L] – “a” – “a”) % 26 + “A”);
j++;
}
if (ch >= “A”&&ch <= “Z”)
{
printf(“%c”, (ch + key[j%L] – “a” – “A”) % 26 + “A”);
j++;
}
if (j%L == 0)
printf(” “);
i++;
}
putchar(ch);
}
void Decry()
{
char key[100];
char ch, temp;
int L, i = 0, j = 0;
if (getchar() == “\n”)
temp = ” “;
printf(“请输入密钥:”);
gets_s(key,100);
L = strlen(key);
printf(“请输入密文:”);
while ((ch = getchar())!= “\n”)
{
if (ch == ” “)
{
i++;
continue;
}
if (ch >= “A”&&ch <= “Z”)
{
printf(“%c”, (ch – key[j%L] – “A” + “a” + 26) % 26 + “a”);
j++;
}
if (j%L == 0)
printf(“”);
i++;
}
putchar(ch);
}
int Exit()
{
exit(0);
}
int main()
{
char ch;
for (;;)
{
printf(“请输入操作 e:加密 d:解密 q:退出 “);
ch = getchar();
if (ch == “e” || ch == “E”)
Encry();
else if (ch == “d” || ch == “D”)
Decry();
else if (ch == “q” || ch == “Q”)
Exit();
else
{
printf(“输入命令错误”);
putchar(getchar());
continue;
}
}
return 0;
}
#include<stdio.h>
#include<string.h>
void Encry()
{
char key[100];
char ch, temp;
int L, i = 0, j = 0;
if (getchar() == “\n”)
temp = ” “;
printf(“请输入密钥:”);
gets_s(key,100);
L = strlen(key);
printf(“请输入明文:”);
while ((ch = getchar()) != “\n”)
{
if (ch = ” “)
{
i++;
continue;
}
if (ch >= “a”&&ch <= “z”)
{
printf(“%c”, (ch + key[j%L] – “a” – “a”) % 26 + “A”);
j++;
}
if (ch >= “A”&&ch <= “Z”)
{
printf(“%c”, (ch + key[j%L] – “a” – “A”) % 26 + “A”);
j++;
}
if (j%L == 0)
printf(” “);
i++;
}
putchar(ch);
}
void Decry()
{
char key[100];
char ch, temp;
int L, i = 0, j = 0;
if (getchar() == “\n”)
temp = ” “;
printf(“请输入密钥:”);
gets_s(key,100);
L = strlen(key);
printf(“请输入密文:”);
while ((ch = getchar())!= “\n”)
{
if (ch == ” “)
{
i++;
continue;
}
if (ch >= “A”&&ch <= “Z”)
{
printf(“%c”, (ch – key[j%L] – “A” + “a” + 26) % 26 + “a”);
j++;
}
if (j%L == 0)
printf(“”);
i++;
}
putchar(ch);
}
int Exit()
{
exit(0);
}
int main()
{
char ch;
for (;;)
{
printf(“请输入操作 e:加密 d:解密 q:退出 “);
ch = getchar();
if (ch == “e” || ch == “E”)
Encry();
else if (ch == “d” || ch == “D”)
Decry();
else if (ch == “q” || ch == “Q”)
Exit();
else
{
printf(“输入命令错误”);
putchar(getchar());
continue;
}
}
return 0;
}
解决方案
40
#include<stdlib.h> #include<stdio.h> #include<string.h> void Encry() { char key[100]; char ch, temp; int L, i = 0, j = 0; if (getchar() == "\n") temp = " "; printf("请输入密钥:"); gets_s(key,100); //gets(key); L = strlen(key); printf("请输入明文:"); while ((ch = getchar()) != "\n") { if (ch == " ") { i++; continue; } if (ch >= "a"&&ch <= "z") { printf("%c", (ch + key[j%L] - "a" - "a") % 26 + "A"); j++; } if (ch >= "A"&&ch <= "Z") { printf("%c", (ch + key[j%L] - "a" - "A") % 26 + "A"); j++; } if (j%L == 0) printf(" "); i++; } putchar(ch); } void Decry() { char key[100]; char ch, temp; int L, i = 0, j = 0; if (getchar() == "\n") temp = " "; printf("请输入密钥:"); gets_s(key,100); //gets(key); L = strlen(key); printf("请输入密文:"); while ((ch = getchar())!= "\n") { if (ch == " ") /*此处不是赋值,而是判断 == */ { i++; continue; } if (ch >= "A"&&ch <= "Z") { printf("%c", (ch - key[j%L] - "A" + "a" + 26) % 26 + "a"); j++; } if (j%L == 0) printf(""); i++; } putchar(ch); } int Exit() { exit(0); } int main() { char ch; for (;;) { printf("请输入操作 e:加密 d:解密 q:退出 "); ch = getchar(); if (ch == "e" || ch == "E") Encry(); else if (ch == "d" || ch == "D") Decry(); else if (ch == "q" || ch == "Q") Exit(); else { printf("输入命令错误"); putchar(getchar()); continue; } } return 0; }
有一处问题,比较容易出错误。详见代码注释;