|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 chinggggg 于 2018-10-2 13:39 编辑
生成随机密码和输入都正常,然而输出不了加密的文本。。
主要问题应该在password这个函数。。
- #include <stdio.h>
- #include <stdlib.h>
- #define letternum 26
- typedef char Elemtype;
- //字母结点
- typedef struct Letter
- {
- Elemtype let;
- struct Letter *next;
- } letter;
- //数字密码结点
- typedef struct Passwords
- {
- int passw;
- struct passwords *next;
- } pw;
- //初始化26个字母,循环链表
- void Init(letter **L)
- {
- int i;
- *L = (letter*)malloc(sizeof(letter));
- letter *p = *L;
- p->let = 65;
- letter *q;
- for(i = 2; i <= letternum; ++i)
- {
- q = (letter*)malloc(sizeof(letter));
- q->let = i+64;
- p->next = q;
- p = q;
- }
- p->next = *L;
- }
- //创建密码,根据输入的字符长度,一个字符对应一个100以内的数字
- void random(pw **L,int n)
- {
- pw *p,*q;
- int i;
- srand(time(0));
- *L = (pw*)malloc(sizeof(pw));
- p = *L;
- p->passw = rand()%100 + 1;
- p->next = NULL;
- for(i = 1; i < n; ++i)
- {
- q = (pw*)malloc(sizeof(pw));
- q->passw = rand()%100 +1;
- p->next = q;
- p = q;
- }
- p->next = NULL;
- }
- //加密文本
- void password(letter *L,pw *P,char *head)
- {
- int i;
- pw *a = P;
- letter *x = L;
- while((*head)!= '\n') //检测是否输入回车
- {
- if((*head)!= ' ') //检测是否空格(假定输入的只有空格与字母,不考虑其他符号)
- {
- while((x->let)!= (*head)) //在创建的字母循环链表中找到对应的字母
- {
- x = x->next;
- }
- for(i = 1;i <= (a->passw)%26;++i) //根据对应密码找到明文字母的加密字母
- {
- x = x->next;
- }
- printf("%c",x->let); //输出加密字母
- }
- else if((*head)== ' ') //假如是空格则直接输出空格
- printf(" ");
- ++head;
- a = a->next;
- }
- }
- //输出每个密码
- void print(pw *L,int n)
- {
- int i;
- pw *p = L;
- for(i = 0; i < n; ++i)
- {
- printf("%d ",p->passw);
- p = p->next;
- }
- printf("\n");
- }
- int main()
- {
- int n,i;
- pw *p = NULL;
- char plain[50];
- letter *L = NULL;
- Init(&L);
- printf("输入字母:");
- gets(plain);
- i = strlen(plain); //输入明文的长度
- random(&p,i); //创造出对应的密码
- print(p,i); //打印出密码
- password(L,p,&plain); //打印加密文本
- return 0;
- }
复制代码 |
|