【维吉尼亚加密与解密】第一次发帖 请多多指教
刚刚入门学习计算机编程1个月数据结构与算法刚刚开始学习小甲鱼老师的数据结构视频刚看完线性表
自己写的代码
请各位大佬多多指教{:10_298:}
感激不尽!!!
代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
typedef struct Vigenere
{
int data;
char ciphertext;
struct Vigenere *next;
}sqvigen, *vigenere;
typedef struct Content
{
char content;
struct Content *next;
}sqlist, *linklist;
int random()//随机产生数字,作为密匙
{
int num;
srand(time(0));
num = rand()%100;
return num;
}
linklist Create()
{
int i;
linklist head = NULL;
linklist s, r;
r = head;
printf("Please input your real words: ");
getchar(); //清空输入缓冲区
while (1)
{
s = (linklist)malloc(sizeof(sqlist));
s->content = getchar();
if(s->content == '\n')
{
break;
}
if(head == NULL)
{
head = s;
}
else
{
r->next = s;
}
r = s;
}
r->next = NULL;
return head;
}
vigenere Encrypt(linklist head)
{
linklist p;
vigenere vigen = NULL;
vigenere s, r;
r = vigen;
p = head;
srand(time(0));
while (p != NULL)
{
s = (vigenere)malloc(sizeof(sqvigen));
s->data = rand()%100;
s->ciphertext = (p->content) + (s->data);
if(vigen == NULL)
{
vigen = s;
}
else
{
r->next = s;
}
r = s;
p = p->next;
}
r->next = NULL;
return vigen;
}
void decipher(vigenere top)
{
vigenere p = top;
printf("解码: \n");
while(p != NULL)
{
printf("%c", (p->ciphertext) - (p->data));
p = p->next;
}
}
int main()
{
linklist p, head;
vigenere temp, top;
int i;
while(1)
{
printf("请选择将要进行的操作: \n");
printf("1.输入明文\n2.输出明文\n3.输出密文并解码\n4.退出程序\n");
scanf("%d", &i);
switch(i)
{
case 1:
head = p = Create();
printf("\n\n");
break;
case 2:
printf("\n明文是: \n");
while(p != NULL)
{
printf("%c", p->content);
p = p->next;
}
printf("\n\n");
break;
case 3:
top = temp = Encrypt(head);
printf("\n密文是: \n");
while(temp != NULL)
{
printf("%c", temp->ciphertext);
temp = temp->next;
}
printf("\n\n");
decipher(top);
printf("\n\n");
break;
case 4:
return 0;
}
}
}
好厉害一个月就可以写了。。。
页:
[1]