《C语言程序设计基础》上机实验报告(模版.doc
成都信息工程学院计算机系课程实验报告实验课程:C语言程序设计2实验项目:职工管理系统指导教师: 李莉丽学生姓名:桂柯 学生学号:班 级:计科3班实验地点: 5201实验时间:2011 年 5月 11日 点 点实验成绩:评阅老师:李莉丽(说明:实验报告必须包含下面的每项内容,根据实验情况认真填写,封面必须打印或复印(A4纸),书写上机实验报告内容的纸张也用A4纸,最后从侧面装订)一【上机实验目的】通过亲自设计程序,可以令我们熟悉c语言操作,更熟练的掌握c语句。初步体会编程的过程,为将来的程序深入学习打下基础和培养兴趣。二【实验环境】TC2.0三【上机实验内容】职工信息管理系统要求:职工信息包括职工号,姓名,性别,年龄,学历,工资,住址,电话等(职工号不相等)。试设计一职工信息管理系统,使之能够提供下列功能: (1)系统以菜单方式工作 (2)职工信息录入功能(职工信息用文件保存) (3)职工信息浏览功能 (4)职工信息查询功能,查询方式: 1)按学历查询 2)按职工号查询 (5) 职工信息删除,修改功能(可选项)。思路:(1)录入并向文件里保存数据的实现思路:C语言并没有提供由键盘输入数据直接录入文件的功能,只有内存变量向文件写入数据的功能,而由键盘输入数据可以到内存变量,因此实现此部分功能时,应当由键盘将数据放入变量,再由变量写入文件。这里录入要求不采用书上例子,它是用结构体数组,我们要求只用一个结构体实现。先将一个人的信息放入结构体,将这个结构体数据写入文件后,再将下一个人的信息继续放在这个结构体中,再将这个结构体写入文件。这样节省内存空间。计算并修改文件里数据的实现思路:这部分的功能的实现应当先将文件的数据读到变量当中,在变量当中完成计算,再将数据写入文件。如果只是修改文件的数据,并不计算,则可以直接定位到文件中相应的位置,写入数据,则把原来的数据覆盖以完成修改。(2)向文件里追加数据的实现思路:文件本身提供了这项功能,只要以 “a”的方式打开就行。然后向文件写入的数据,直接放在文件末尾。(3)查找文件里是否有某项数据的实现思路:C语言并没有提供判断文件内容的功能,必须将文件内容读到变量里再进行判断。实际的查找可能是在大量的数据里的查找,高效的查找是折半查找(下学期的数据结构专门讲这一内容),折半的前提是排序,因此需先对排序后的文件读出,以折半方式查找。(这要要求,是希望巩固折半查找与排序两个重要算法,至于它的时空效率是否高,可以学完数据结构知识后自己再判断)。(4)根据要求显示文件里的某些数据或全部数据的实现思路:C语言没有提供将文件内容显示的功能,所以需要将文件内容读到变量里,再显示变量。(5)在文件里插入或删除某项数据的实现思路:C语言同样没有直接提供该项功能,因此必须借由内存变量完成。由以前的知识知道,在大量的数据里删除一个数,用数组表示不合适,因为涉及到大量的数据的移动,用链表是合适的,效率高(关于这一点,在数据结构这门课有详细的讲解)。因此完成这部分操作要求用链表实现,先将文件里的数据读出组织成链表,在链表上完成插入与删除后,再将链表中的数据写入文件。(6)按某个数据项进行排序生成排序文件的实现思路:排序是在数组里实现。因此先要将文件里的数据读到数组里,将数组排完序后,再将数据写入文件(一般写入一个新文件)。四【上机调试程序流程图】1.显示主菜单(以如下程序作为介绍)2. 添加职工信息3. 浏览职工信息。4. 查询职工信息。4(1)根据标号查询职工信息。(2) 根据姓名查询职工信息。(3) 根据年龄查询职工信息。5. 修改职工信息。浏览修改后的职工信息。6. 删除职工信息。浏览删除后的职工信息。7. 用链表添加职工信息。浏览添加后的职工信息。五【上机调试中出现的错误信息、错误原因及解决办法】1. 光条菜单的错误:刚开始只能用键盘上的英文字母控制光条上下移动,想用上下箭头的ASCII代码,结果错误不能上下移动解决方法:使用键盘扫描码。2、浏览函数 scan()在调试和链接的时候都没有出现错误提示,但在运行的时候出现了问题。写入指定位置的文件,打开后总会有乱码。与c语言课本上的例题对照后发现,我写的fopen("wenjian","ab")没有指定文件的存储类型。解决方法:在文件名wenjian后面加上.txt后即可。3.浏览函数scan()遇到的问题虽然不大,但解决起来很麻烦。理想的运行结果是美观整齐。即下面的职工信息分别与第一个printf输出的中文项目提示对齐。解决方法:不断修改空格个数,不断运行察看效果。4.程序运行后菜单界面不消失解决方法:使用清屏函数,是每次运行后界面还原。5.功能函数运行完后会跳出界面,直接退出。解决方法:在每个功能函数的后面加如返回值。六【上机调试后的源程序及还存在的问题】源程序:#include<stdio.h>#include<conio.h>#define ESC 0x1b/*键盘扫描码*/#define ENTER 0x0d#define UP 0x48#define DOWN 0x50#define LEFT 0x4b#define RIGHT 0x4dtypedef struct /*定义存放职工信息的类型*/int No;int age;char name20;WORKERBASIC;typedef struct workerWORKERBASIC workerinfo;struct worker *next;WORKER;void cleanscreen()/*清屏*/int i,j;gotoxy(1,1);textbackground(RED);for(i=1;i<=25;i+)for(j=1;j<=80;j+)cprintf(" ");clrscr();void Initial()/*定义初始化程序的函数*/int i,j;char list720="1. Add",/*菜单名*/"2. Scan","3. Edit","4. Modify","5. Delete","6. Into","7. Exit"cleanscreen();textbackground(YELLOW);for(i=1;i<=14;i+)/*画窗口*/for(j=1;j<=80;j+)if (i=1|i=14|j<=2|(j>=79)cprintf(" ");else if (i>=3&&i<=12&&j>=5&&j<=76)cprintf(" ");elseprintf(" ");gotoxy(32,4);textcolor(BLACK);cprintf("W E L C O M E !");gotoxy(31,14);cprintf("Worker System - BY GK");for(i=0;i<7;i+)/*显示菜单*/gotoxy(34,i+6);cprintf("%s",listi);void PrintPause()/*暂停*/printf("Press any key to go on . . .");getch();void Guangtiao(int flag)/*定义画光条的函数*/char list720="1. Add","2. Scan","3. Edit","4. Modify","5. Delete","6. Into","7. Exit"gotoxy(34,flag+5);textcolor(WHITE);textbackground(BLACK);cprintf("%s",listflag-1);textcolor(LIGHTGRAY);int Add()/*添加信息*/WORKERBASIC new;FILE *fp=NULL;char ch='y'while(ch='y')cleanscreen();printf("Please input the worker's No. ");scanf("%d",&new.No);getchar();printf("Please input the worker's age: ");scanf("%d",&new.age);getchar();printf("Please input the worker's name: ");scanf("%s",new.name);getchar();if(fp=fopen("wenjian.txt","ab")=NULL)fp=fopen("wenjian.txt","wb");if(fwrite(&new,sizeof(WORKERBASIC),1,fp)!=1)printf("add failed!n");PrintPause();fclose(fp);printf("whether go on(y or n):");scanf("%c",&ch);return 0;int Scan()/*查看信息*/WORKERBASIC load;FILE *fp=NULL;int n=0;cleanscreen();if(fp=fopen("wenjian.txt","rb")=NULL)printf("can not find wenjian!n");PrintPause();return 0;printf("-n");printf("No.tworker'sagetworker'namen");while(!feof(fp)if (fread(&load,sizeof(WORKERBASIC),1,fp)=1)n+;elsebreak;printf("%dt%dtt%sn",load.No,load.age,load.name);printf("-n");printf("total:%dn",n);fclose(fp);PrintPause();return 0;void exchange(WORKERBASIC *max,WORKERBASIC *min)/*调换两个职工在数组中的位置*/WORKERBASIC t;strcpy(t.name,max->name);t.No=max->No;strcpy(max->name,min->name);max->No=min->No;strcpy(min->name,t.name);min->No=t.No;int EditByNo(WORKERBASIC *F1,int n)/*按编号查找*/int i,j,k,num,high,low,mid,flag=0;for(i=0;i<n-1;i+)/*排序*/k=i;for(j=i+1;j<n;j+)if (F1+k*sizeof(WORKERBASIC)->No) > (F1+j*sizeof(WORKERBASIC)->No)k=j;if (k!=i)exchange(F1+i*sizeof(WORKERBASIC),(F1+k*sizeof(WORKERBASIC);/*此显示信息只为说明职工已按编号排序,该部分可删除*/printf("-n");printf("No.tworker'agettworker'namen");for(i=0;i<n;i+)printf("%dt%dttt%sn",(F1+i*sizeof(WORKERBASIC)->No,(F1+i*sizeof(WORKERBASIC)->age,(F1+i*sizeof(WORKERBASIC)->name);printf("-n");printf("nPlease input a number you want to search: ");scanf("%d",&num);getchar();low=0;/*折半法*/high=n-1;while(low<=high)&&(flag=0)mid=(low+high)/2;if (num>(F1+mid*sizeof(WORKERBASIC)->No)low=mid+1;else if (num<(F1+mid*sizeof(WORKERBASIC)->No)high=mid-1;else if (num=(F1+mid*sizeof(WORKERBASIC)->No)flag=1;if (flag=0)printf("can not find the worker!n");elseprintf("-n");printf("No.tworker'agettworker'namen");printf("%dt%dttt%sn",(F1+mid*sizeof(WORKERBASIC)->No,(F1+mid*sizeof(WORKERBASIC)->age,(F1+mid*sizeof(WORKERBASIC)->name);printf("-n");PrintPause();return 0;int EditByName(WORKERBASIC *F1,int n)/*按姓名查找*/char str20;int i,j,k,flag=0,low,high,mid;for(i=0;i<n-1;i+)/*排序*/k=i;for(j=i+1;j<n;j+)if (strcmp( (F1+k*sizeof(WORKERBASIC)->name, (F1+j*sizeof(WORKERBASIC)->name )>0)k=j;if (k!=i)exchange(F1+i*sizeof(WORKERBASIC),(F1+k*sizeof(WORKERBASIC);/*此显示信息只为说明职工已按名字排序,该部分可删除*/printf("-n");printf("No.tworker'agettworker'namen");for(i=0;i<n;i+)printf("%dt%dttt%sn",(F1+i*sizeof(WORKERBASIC)->No,(F1+i*sizeof(WORKERBASIC)->age,(F1+i*sizeof(WORKERBASIC)->name);printf("-n");printf("nPlease input a worker you want to exactly search: ");scanf("%s",str);getchar();low=0;/*折半法*/high=n-1;while(low<=high)&&(flag=0)mid=(low+high)/2;if (strcmp( str, (F1+mid*sizeof(WORKERBASIC)->name )>0)low=mid+1;else if (strcmp( str, (F1+mid*sizeof(WORKERBASIC)->name )<0)high=mid-1;else if (strcmp( str, (F1+mid*sizeof(WORKERBASIC)->name )=0)flag=1;if (flag=0)printf("can not find the worker!n");elseprintf("-n");printf("No.tworker'agettworker'namen");printf("%dt%dttt%sn",(F1+mid*sizeof(WORKERBASIC)->No,(F1+mid*sizeof(WORKERBASIC)->age,(F1+mid*sizeof(WORKERBASIC)->name);printf("-n");PrintPause();return 0;int EditByage(WORKERBASIC *F1,int n)/*按年龄查找*/int a,flag=0,i;printf("-n");printf("No.tworker'agettworker'namen");for(i=0;i<n;i+)printf("%dt%dttt%sn",(F1+i*sizeof(WORKERBASIC)->No,(F1+i*sizeof(WORKERBASIC)->age,(F1+i*sizeof(WORKERBASIC)->name);printf("-n");printf("nPlease input worker'age: ");scanf("%d",&a);getchar();for(i=0;i<n;i+)if (a=(F1+i*sizeof(WORKERBASIC)->age)if(flag=0)printf("-n");printf("No.tworker'agettworker'namen");flag=1;printf("%dt%dttt%sn",(F1+i*sizeof(WORKERBASIC)->No,(F1+i*sizeof(WORKERBASIC)->age,(F1+i*sizeof(WORKERBASIC)->name);if (flag=0)printf("can not find the worker!n");elseprintf("-n");PrintPause();return 0;int Edit()/*查找*/WORKERBASIC load,*F1;int n=0,key=0,i;FILE *fp=NULL;cleanscreen();if (fp=fopen("wenjian.txt","rb")=NULL)printf("can not find wenjian!n");PrintPause();return 0;while(!feof(fp)/*统计职工的数量*/if (fread(&load,sizeof(WORKERBASIC),1,fp)=1)n+;fclose(fp);if (n=0)printf("No information of worker!n");PrintPause();return 0;F1=(WORKERBASIC *)malloc(n*sizeof(WORKERBASIC);/*按职工的人数申请内存空间*/fp=fopen("wenjian.txt","rb");for(i=0;i<n;i+)if (fread(F1+i*sizeof(WORKERBASIC),sizeof(WORKERBASIC),1,fp)!=1)printf("read failed!n");PrintPause();return 0;fclose(fp);while(key!=ESC)clrscr();printf("Please select search method:n");printf("-n");printf("1t=By No.n");printf("2t=By Name.n");printf("3t=By age.n");printf("Esct=Exitn");printf("-n");printf("Press 1 2 3 or Esc?n");key=getch();switch(key)case '1':key=EditByNo(F1,n);break;case '2':key=EditByName(F1,n);break;case '3':key=EditByage(F1,n);break;free(F1);return 0;int Modify()/*修改*/WORKER *head=NULL,*t=NULL,*load=NULL;FILE *fp=NULL;char str20;int num;cleanscreen();load=(WORKER *)malloc(sizeof(WORKER);if(fp=fopen("wenjian.txt","rb")=NULL)printf("can not find wenjian!n");PrintPause();return 0;head=t=load;if (fread(load,sizeof(WORKERBASIC),1,fp)!=1)printf("load failed!n");PrintPause();return 0;while(!feof(fp)load=(WORKER *)malloc(sizeof(WORKER);t->next=load;if (fread(load,sizeof(WORKERBASIC),1,fp)!=1)break;t=load;t->next=NULL;/*创建链表完成*/fclose(fp);t=head;Scan();printf("nWhich worker do you want to modify?n");printf("Please input the No. age. or name: ");scanf("%s",str);getchar();num=atoi(str);/*把字符串转换为长整型数*/if (strcmp(t->workerinfo.name,str)=0)|(num=t->workerinfo.No)|(num=t->workerinfo.age)printf("nPlease input the new No. ");scanf("%d",&t->workerinfo.No);getchar();printf("nPlease input the new age. ");scanf("%d",&t->workerinfo.age);getchar();printf("nPlease input the new name: ");scanf("%s",t->workerinfo.name);getchar();printf("Modify Success!n");fp=fopen("wenjian.txt","wb");t=head;if (head!=NULL)fwrite(t,sizeof(WORKERBASIC),1,fp);while(t->next!=NULL)t=t->next;if(fwrite(t,sizeof(WORKERBASIC),1,fp)!=1)printf("add failed!n");PrintPause();break;free(head);fclose(fp);PrintPause();return 0;/*存入文件(目标在首地址)*/while(t->next!=NULL)t=t->next;if (strcmp(t->workerinfo.name,str)=0)|(num=t->workerinfo.No)|(num=t->workerinfo.age)printf("nPlease input the new No. ");scanf("%d",&t->workerinfo.No);getchar();printf("nPlease input the new age. ");scanf("%d",&t->workerinfo.age);getchar();printf("nPlease input the new name: ");scanf("%s",t->workerinfo.name);getchar();printf("Modify Success!n");fp=fopen("wenjian.txt","wb");t=head;if (head!=NULL)fwrite(t,sizeof(WORKERBASIC),1,fp);while(t->next!=NULL)t=t->next;if(fwrite(t,sizeof(WORKERBASIC),1,fp)!=1)printf("add failed!n");PrintPause();break;/*存入文件(当不在首地址的时候)*/free(head);fclose(fp);PrintPause();return 0;printf("Input Wrong!n");PrintPause();return 0;int Delete()/*删除*/WORKER *head=NULL,*t=NULL,*load=NULL,*b=NULL;FILE *fp=NULL;int num;char str20,ch;cleanscreen();load=(WORKER *)malloc(sizeof(WORKER);if(fp=fopen("wenjian.txt","rb")=NULL)printf("can not find wenjian!n");PrintPause();return 0;head=t=load;if (fread(load,sizeof(WORKERBASIC),1,fp)!=1)printf("load failed!n");PrintPause();return 0;while(!feof(fp)load=(WORKER *)malloc(sizeof(WORKER);t->next=load;if (fread(load,sizeof(WORKERBASIC),1,fp)!=1)break;t=load;t->next=NULL;fclose(fp);t=head;Scan();printf("nWhich worker do you want to delete?n");printf("Please input the No. age or name: ");scanf("%s",str);getchar();num=atoi(str);if (strcmp(t->workerinfo.name,str)=0)|(num=t->workerinfo.No)|(num=t->workerinfo.age)printf("nDo you really want to delete %s?(Press 'y' to sure)n",t->workerinfo.name);if (ch=getch()!='y')return 0;head=t->next;free(t);printf("Delete Success!n");fp=fopen("wenjian.txt","wb");t=head;if (head!=NULL)fwrite(t,sizeof(WORKERBASIC),1,fp);while(t->next!=NULL)t=t->next;if(fwrite(t,sizeof(WORKERBASIC),1,fp)!=1)