📄 2410311_wa.c
字号:
#include <stdio.h>
#include <ctype.h>
#include <string.h>
int f(char a)
{
return a<='Z' ? a-'A':a-'a';
}
int v[27];
char txt[10002], output[10002];
char com[100], cip[27], con[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
void get_v()
{
int i;
for(i = 0; i < 26; i++)
v[f(cip[i])] = i;
}
int main()
{
int i, mark[27], t, tmp;
strcpy(cip,con);
while(scanf("%s",com)==1)
{
if(strlen(com)>7)
goto there;
getchar();
gets(txt);
for(i = 0; com[i]!='\0'; i++)
com[i] = toupper(com[i]);
if(strcmp(com,"CIPHER")==0)
{
memset(mark,0,sizeof(mark));
for(i = 0; txt[i]!='\0'; i++)
if(!isalpha(txt[i]))
{
strcpy(&txt[i],&txt[i+1]);
i--;
}
else
{
txt[i] = toupper(txt[i]);
mark[txt[i]-'A']++;
}
for(i = 0; i < 26; i++)
if(mark[i]!=1)
{
puts("Bad cipher. Using default.");
strcpy(cip,con);
goto ed;
}
printf("Good cipher. Using %s.\n",txt);
strcpy(cip,txt);
ed:
continue;
}
if(strcmp(com,"ENCRYPT")==0)
{
get_v();
for(i = 0; txt[i]!='\0'; i++)
{
if(!isalpha(txt[i]))
output[i] = txt[i];
else
{
if(!i||!isalpha(txt[i-1]))
t = v[f(txt[i])]-v[f('A')];
else
t = v[f(txt[i])] - v[f(txt[i-1])];
if(t<0)
t += 26;
output[i] = isupper(txt[i])?cip[t]:cip[t]+' ';
}
}
printf("RESULT: ");
puts(output);
continue;
}
if(strcmp(com,"DECRYPT")==0)
{
get_v();
for(i = 0; txt[i]!='\0'; i++)
{
if(!isalpha(txt[i]))
output[i] = txt[i];
else
{
for(t = 0; t < 26; t++)
if(f(cip[t])==f(txt[i]))
break;
if(!i||!isalpha(txt[i-1]))
tmp = t+v[f('A')];
else
tmp = t+v[f(output[i-1])];
tmp %= 26;
output[i] = isupper(txt[i])?cip[tmp]:cip[tmp]+' ';
}
}
printf("RESULT: ");
puts(output);
continue;
}
there:
puts("Command not understood.");
}
return 1;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -