⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 2410362_ac_0ms_48k.c

📁 北大大牛代码 1240道题的原代码 超级权威
💻 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]+' ';
				}	
			}
			output[i] = '\0';
			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]+' ';
				}
			}
			output[i] = '\0';
			printf("RESULT:  ");
			puts(output);
			continue;
		}
there:
		puts("Command not understood.");
	}
	return 1;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -