数据结构课程设计——约瑟夫环报告(含代码)

2024-07-02

数据结构课程设计——约瑟夫环报告(含代码)

1.数据结构课程设计——约瑟夫环报告(含代码) 篇一

C 语 言 课 程 设 计 实 验 报 告

实验名称:文件加密解密 院系:软件学院

学号:

年9月3日—9月17日 日期:2012

一:设计题目

1:设计图形用户界面。

2:对文件进行加密并对加密文件进行保存。3:对加密了的文件进行解密。

二:设计过程

设计过程中遇到的困难和解决方法: 1:不能很好地理解题意(通过老师的讲解)。

2:不知道如何设计加密解密程序(通过翻阅书籍和上网查找资料)过程:

首先通过学习老师提供的资料了解大致的设计过程并懂得运用一些以前没有学习过的c语言。先利用文本文件设计出加密解密的主要过程并能运行。知道如何运用fopen将原文件打开并用fread将原文件内容读出来,然后进行加密设计并将加密的数据用fwrite写进指定的文件中并保存。然后读出加密的文件并解密并保存。最后在写出的程序中加入图形用户界面,运用window,box,gotoxy等进行设计。

三:源代码

#include /* 标准输入、输出函数 */ #include /* 标准库函数 */ #include //*字符串处理函数 */ #include /* 字符操作函数 */ #include #include #define key_down 80 #define key_up 72 #define key_esc 1 #define key_enter 28 #define SIZE 1 void box(int startx,int starty,int high,int width);int get_key();char buf[20*20*4];/*/////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////加密解密 */ void fun(char *list,char *sd)/*加密过程*/ {

FILE *fp1,*fp2;char buf[1000];/*文件临时存放处*/ register int ch;fp1=fopen(“e:list.txt”,“r”);/*用可读方式打开文件*/ fp2=fopen(“e:sd.txt”,“w”);/*用可写方式创建一个文件*/ if(fp1==NULL){ printf(“cannot open filen”);exit(1);} if(fp2==NULL){ printf(“cannot build filen”);exit(1);} ch=fgetc(fp1);/*读出打开文件的光标处的一个字符*/ while(!feof(fp1))/*读出的字符不是最后的字符*/ { ch=ch<<1;/*加密方法*/ fputc(ch,fp2);/*加密的字符存放在指定的地方*/ ch=fgetc(fp1);

} rewind(fp2);/*将光标移动到第一个字符前面*/ fread(buf,sizeof(buf),1,fp2);/*从文件的当前位置开始中读取buf中存放的数据*/ printf(“%s”,buf);/*fclose(fp1);fclose(fp2);*/ }

void man(char *sd,char *ds)/*解密过程*/ { /*int n=0;*/ FILE *fp2,*fp3;register int fh;char buf1[1000];

fp2=fopen(“e:sd.txt”,“rb”);/*用可读方式打开文件*/ fp3=fopen(“e:ds.txt”,“wb”);/*用可写方式创建一文件*/ if(fp2==NULL){ printf(“cannot open filen”);exit(1);} if(fp3==NULL){ printf(“cannot build filen”);exit(1);} fh=fgetc(fp2);/*从光标处读出一个字符*/ while(!feof(fp2))/*当读出的字符到达最后一个则停止*/ { fh=fh>>1;/*解密方式*/

fputc(fh,fp3);/*解密的字符存放在指定的地方*/ fh=fgetc(fp2);} fread(buf1,sizeof(buf1),1,fp3);/*读出buf1中所存放的数据*/ printf(“%s”,buf1);}

void main(){ int k;char *f[]={“jiami”,“jiemi”};/**界面的形式/ int key,y;int j,q;char list[300];char sd[300];char ds[300];char ch,fh;char buf[1000];char buf1[1000];FILE *fp1;FILE *fp2;int l1,l2;window(1,1,80,25);/*left,top,right,bottom,相对于屏幕的字符坐标,屏幕原点在左上角*/ gettext(20,10,40,14,buf);/*保存矩形屏幕上的字符*/

textbackground(7);/*背景颜色*/ textcolor(0);/*字体颜色*/ clrscr();/*清除矩形屏幕上的所有字符*/ gotoxy(24,10);/*将当前字符屏幕的光标位置移动到x,y的坐标位子*/ printf(“%s”,f[0]);gotoxy(24,14);printf(“%s”,f[1]);gettext(10,8,60,16,buf);box(22,9,3,30);/*建立一个小窗口*/ key=0;while(1){ while(bioskey(1)==0);/*读取键盘值查询键盘是否按下*/ key=get_key();/*按下了什么键盘*/

if(key==key_up||key==key_down){ y=wherey();/*得到字符模式下窗口光标的x坐标数值*/ if(key==key_up)y=y==10? y+4:10;/*当y=10光标向下移动四个位置否则将光标移动到y=10处*/ if(key==key_down)y=y==14? y-4:14;/*当y=14光标向下移动四个位置否则将光标移动到y=14处*/

puttext(10,8,60,16,buf);/*将gettext函数保存的字符恢复到屏幕上 */

gotoxy(24,y);

if(y==10){ textbackground(7);textcolor(0);box(22,9,3,30);textbackground(3);textcolor(15);gotoxy(24,y);cprintf(“%s”,f[0]);} else { textbackground(7);textcolor(0);box(22,13,3,30);textbackground(3);textcolor(15);gotoxy(24,y);cprintf(“%s”,f[1]);} }

if(key==key_enter&&y==10)且光标在y=10处 /*当按下enter键且光标在y=10处进行下步*/ { clrscr();textbackground(3);textcolor(15);/*clrscr();*/ gotoxy(24,5);printf(“input the file name for jiamin”);/*用户给需要加密的文件加密 */ l1=strlen(“input the file name for jiami:”);/*待求长度的字符串指针*/ gotoxy(24+l1,5);scanf(“%s”,list);gotoxy(24,10);printf(“input file name for saven”);/*给加密后的文件命名,并保存*/ l2=strlen(“input file name for save:”);gotoxy(24+l2,10);scanf(“%s”,sd);fun(list,sd);fp1=fopen(“e:sd.txt”,“rb”);fread(buf1,sizeof(buf1),1,fp1);gotoxy(10,15);printf(“%sn”,buf1);getch();printf(“file haven jiami ,save now”);getche();break;} if(key==key_enter&&y==14){ clrscr();textbackground(3);textcolor(15);gotoxy(24,5);

printf(“input the file name for jiemi n”);/*用户给需要解密的文件解密 */ l1=strlen(“input the file name for jiemi: ”);gotoxy(24+l1,5);scanf(“%s”,sd);gotoxy(24,10);printf(“input file name for save:n”);/*对解密的文件系统又可以提供保存路径 */ l2=strlen(“input file name for save: ”);gotoxy(24+l2,10);scanf(“%s”,ds);man(sd,ds);fp2=fopen(“e:ds.txt”,“rb”);fread(buf1,sizeof(buf1),1,fp2);gotoxy(10,15);printf(“%sn”,buf1);getch();

printf(“file haven jiemi,save now”);getche();break;}

}

window(1,1,80,25);gettext(20,10,40,14,buf);

textbackground(7);textcolor(0);clrscr();gotoxy(24,10);printf(“%s”,f[0]);gotoxy(24,14);printf(“%s”,f[1]);gettext(10,8,60,16,buf);box(22,9,3,30);key=0;while(1){ while(bioskey(1)==0);key=get_key();

if(key==key_up||key==key_down){ y=wherey();if(key==key_up)y=y==10? y+4:10;if(key==key_down)y=y==14? y-4:14;puttext(10,8,60,16,buf);

gotoxy(24,y);

if(y==10)/*光标在10处的窗口*/ { textbackground(7);textcolor(0);box(22,9,3,30);textbackground(3);textcolor(15);gotoxy(24,y);cprintf(“%s”,f[0]);} else { textbackground(7);textcolor(0);box(22,13,3,30);textbackground(3);textcolor(15);gotoxy(24,y);cprintf(“%s”,f[1]);} }

if(key==key_enter&&y==10){ clrscr();textbackground(3);textcolor(15);/*clrscr();*/ gotoxy(24,5);printf(“input the file name for jiamin”);/*用户给需要加密的文件加密 */ l1=strlen(“input the file name for jiami:”);gotoxy(24+l1,5);scanf(“%s”,list);gotoxy(24,10);printf(“input file name for saven”);/*给加密后的文件命名,并保存*/ l2=strlen(“input file name for save:”);gotoxy(24+l2,10);scanf(“%s”,sd);fun(list,sd);fp1=fopen(“e:sd.txt”,“rb”);fread(buf1,sizeof(buf1),1,fp1);gotoxy(10,15);printf(“%sn”,buf1);getch();printf(“file haven jiami ,save now”);getche();} if(key==key_enter&&y==14){ clrscr();textbackground(3);textcolor(15);gotoxy(24,5);

printf(“input the file name for jiemi n”);/*用户给需要解密的文件解密 */ l1=strlen(“input the file name for jiemi: ”);gotoxy(24+l1,5);scanf(“%s”,sd);gotoxy(24,10);printf(“input file name for save:n”);/*对解密的文件系统又可以提供保存路径 */ l2=strlen(“input file name for save: ”);gotoxy(24+l2,10);scanf(“%s”,ds);man(sd,ds);fp2=fopen(“e:ds.txt”,“rb”);fread(buf1,sizeof(buf1),1,fp2);gotoxy(10,15);printf(“%sn”,buf1);getch();

printf(“file haven jiemi,save now”);getche();break;}

}

}

int get_key(){ union REGS rg;rg.h.ah=0;int86(0x16,&rg,&rg);return rg.h.ah;getchar();} void box(int startx,int starty,int high,int width)/*的建立*/ { int i;gotoxy(startx,starty);putch(0xda);for(i=startx+1;i

for(i=starty+1;i

屏幕 } gotoxy(startx,starty+high-1);putch(0xc0);gotoxy(startx+1,starty+high-1);for(i=startx+1;i

通过这次的作业我觉得最大的收获是不仅把平时学习到的知识理解的更加透彻,而且使知识更加系统化,同时还把有些平时不太注意的小问题发现了出来,这不但有利于我学习C语言,而且对于我学习任何一门课程都是很有益处的。总之,做这份作业对于我们学习C语言有很大的帮助。

在做课程设计时,由于运用了很多新知识,新的方法,还有题目更加复杂,应用性更强,在编写过程中遇到了很多困难,从而使自己能够学习到更多以前不懂,难懂的东西。

上一篇:信用卡竞聘稿下一篇:坚持以人为本执政为民理念发扬密切联系群众优良作风民主生活会发言提纲