看完了《密码传奇》之后自己写了一个。
以下是我的代码:
#include<stdio.h>
#include<string.h>
#define num_KEY 26
#define num_WORD 1000
int check(char key[26]);
int check(char key[26])
{
char cpy[26],t;
int i,j;
strcpy(cpy,key);
for(j=0;j<25;j++)
for(i=0;i<25-j;i++)
if( cpy[i]>cpy[i+1] )
{t=cpy[i];cpy[i]=cpy[i+1];cpy[i+1]=t;}
for(i=0;i<25;i++)
{
if(cpy[i]==cpy[i+1])
{return 1;break;}
}
}
main()
{
char key[num_KEY][26],mingwen[num_WORD],ch;
int num,i,j,count;
printf("Input the number of keys:");
scanf("%d",&num);
getchar();
for(i=0;i<num;i++)
{
printf("\nInput the key%2d:",i+1);
gets(key[i]);
while( (int)strlen(key[i])!=26 || check(key[i])==1 )
{
printf("Wrong!Input this key again:");
gets(key[i]);
}
}
printf("\n\n ");
for(i=0;i<26;i++)
printf("%c ",i+97);
putchar('\n');
for(i=0;i<num;i++)
{
printf("The key%2d:",i+1);
for(j=0;j<26;j++)
printf("%c ",key[i][j]);
putchar('\n');
}
printf("\nPress [Enter] to continue.\n");
RE:
getchar();
printf("\nInput words:\n ");
gets(mingwen);
count=-1;
for(i=0;i<(int)strlen(mingwen);i++)
if(mingwen[i]>=97 && mingwen[i]<=122)
{
count++;
mingwen[i]=key[count%num][ mingwen[i]-97 ];
}
printf("\n\nMi Wen:\n %s",mingwen);
printf("\n\nAgain? Y/N :");
ch=getchar();
if(ch=='y' || ch=='Y') goto RE;
getchar();
return 0;
}
posted on 2010-01-06 18:26
lee1r 阅读(342)
评论(0) 编辑 收藏 引用 所属分类:
Programming Diary