第4章-与文件管理有关的系统功能调用实践作业

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

第4章与文件管理有关的系统功能调用实践作业参照“强化实践能力培养课程内容”中“文件操作实践能力培养考核选例”程序,请构造一个能管理文本文件的学生成绩表的简单数据库管理系统。设文本文件的学生成绩表中每条学生成绩记录有3个字段构成:学号20个字节,姓名20个字节,成绩10个字节,字段间用空格分割对齐。简单数据库管理系统具有基本的功能有:追加一条记录,(仅允许文件主)按学号读出一条记录,按学号升序列出所有记录.(提示:可建立一个学生成绩表文件和一个以学号为主键的索引文件。)答:实验代码及说明#includemalloc.h#includestdio.h#includestdlib.h#defineLENsizeof(structscore)#defineDEBUG#includestring.htypedefstructscore{charno[20];//记录号charnumber[20];/*学号*/charname[20];/*姓名*/chargrades[10];//成绩structscore*next;//下一个节点}score;intm,n;score*load(score*head){score*p1,*p2;intm=0;charfilepn[10];FILE*fp;printf(请输入文件路径机文件名\n);scanf(%s,filepn);if((fp=fopen(filepn,r+))==NULL){printf(不能打开文件\n);exit(0);}p1=(score*)malloc(LEN);head=NULL;while(!feof(fp)){n=n+1;if(n==1)head=p1;elsep2-next=p1;p2=p1;p1=(score*)malloc(LEN);fscanf(fp,%s%s%s%s,p1-no,p1-number,p1-name,p1-grades);}p2-next=p1;p1-next=NULL;n++;fclose(fp);returnhead;}//追加score*append(score*head){score*p1,*p2,*p3;p3=(score*)malloc(LEN);printf(输入学生信息:\n);printf(记录号学号姓名成绩\n);scanf(%s%s%s%s,p3-no,p3-number,p3-name,p3-grades);p1=head;if(head==NULL){//如果链表为空head=p3;p3-next=NULL;}else{if(p1-next==NULL){p1-next=p3;p3-next=NULL;}else{while(p1-next!=NULL)//若是还没有到尾端的话{p2=p1;p1=p1-next;}p1-next=p3;p3-next=NULL;}}n++;returnhead;}score*insert(score*head){score*p1,*p2,*p3;p3=(score*)malloc(LEN);printf(输入学生信息:\n);printf(记录号学号姓名成绩\n);scanf(%s%s%s%s,p3-no,p3-number,p3-name,p3-grades);p1=head;if(head==NULL){//如果链表为空head=p3;p3-next=NULL;}else{if(p1-next==NULL){p1-next=p3;p3-next=NULL;}else{while(p1!=NULL&&strcmp(p1-no,p3-no)=0)//若是还没有到尾端的话{p2=p1;p1=p1-next;}//p1-next=p3;//p3-next=NULL;p2-next=p3;p3-next=p1;}}n++;returnhead;}score*del(score*head){score*p1,*p2,*p3;p3=(score*)malloc(LEN);printf(输入删除学生信息:\n);printf(记录号:\n);scanf(%s,p3-no);p1=head;if(head==NULL){//如果链表为空head=p3;//p3-next=NULL;printf(删除失败\n);returnhead;}else{if(p1-next==NULL){if(p1-no==p3-no)head==NULL;returnhead;//p1-next=p3;//p3-next=NULL;}else{while(p1!=NULL&&strcmp(p1-no,p3-no)!=0)//若是还没有到尾端的话{p2=p1;p1=p1-next;}//p1-next=p3;//p3-next=NULL;p2-next=p1-next;//p3-next=p1;}}returnhead;}score*search(score*head){score*p1,*p2,*p3;p3=(score*)malloc(LEN);printf(输入查询学生信息:\n);printf(记录号:\n);scanf(%s,p3-no);p1=head;if(head==NULL){//如果链表为空head=p3;//p3-next=NULL;printf(查询失败\n);returnhead;}else{if(p1-next==NULL){if(p1-no==p3-no)returnhead;//p1-next=p3;//p3-next=NULL;}else{while(p1!=NULL&&strcmp(p1-no,p3-no)!=0)//若是还没有到尾端的话{p2=p1;p1=p1-next;}//p1-next=p3;//p3-next=NULL;//p2-next=p1-next;//p3-next=p1;}}printf(%s%s%s%s\n,p1-no,p1-number,p1-name,p1-grades);returnp1;}voidsave(score*head){FILE*fp;score*p1;charfilepn[20];printf(输入文件路径以及文件名\n);scanf(%s,filepn);if((fp=fopen(filepn,wt+))==NULL){printf(文件打开失败\n);exit(0);}p1=head;while(p1!=NULL){fprintf(fp,%s%s%s%s\n,p1-no,p1-number,p1-name,p1-grades);p1=p1-next;}fclose(fp);//return0;}voidshow(score*head){score*p;p=head;printf(学生信息\n);while(p!=NULL){printf(%s%s%s%s\n,p-no,p-number,p-name,p-grades);p=p-next;}}intmain(intargc,char**argv){score*head=0;head=load(head);printf(Pleaseinputtheoperationyouwanttodo:\n\t\tI.Insertanewrecordinaformofrecord-num()stu-numstu-namestu-score\n\t\tA.Appendanewrecordinaformofrecord-num()stu-numstu-namestu-score\n\t\tS.Seadarecordbythevalueofrecord-num\n\t\tD.Deletearecordbythevalueofrecord-num\n\t\tQ.QuittheDBsystem\n);charc;intflag=1;while(flag){//return;printf(Pleaseentertheoperationtypeyouwanttodo\n);scanf(%c,&c);if(c=96)c-=32;switch(c){case'I':insert(head);break;case'A':append(head);break;case'S':search(head);break;case'D':del(head);break;case'P':show(head);break;case'Q':flag=0;break;}}save(head);return0;}

1 / 7
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功