第4章与文件管理有关的系统功能调用实践作业_第1页
第4章与文件管理有关的系统功能调用实践作业_第2页
第4章与文件管理有关的系统功能调用实践作业_第3页
第4章与文件管理有关的系统功能调用实践作业_第4页
第4章与文件管理有关的系统功能调用实践作业_第5页
免费预览已结束,剩余2页可下载查看

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、第 4 章 与文件管理有关的系统功能调用实践作业参照 “强化实践能力培养课程内容”中 “文件操作实践能力培养考核选例 ”程序, 请构造一个能管理文本文件的学生成绩表的简单数据库管理系统。 设文本文件的学生成绩表中每条学生成绩记录有3 个字段构成:学号20 个字节,姓名 20个字节,成绩10 个字节,字段间用空格分割对齐。简单数据库管理系统具有基本的功能有:追加一条记录, (仅允许文件主)按学号读出一条记录,按学号升序列出所有记录.(提示:可建立一个学生成绩表文件和一个以学号为主键的索引文件。 )答:实验代码及说明#include <malloc.h>#include <std

2、io.h>#include <stdlib.h>#define LEN sizeof(struct score)#define DEBUG#include <string.h>typedef struct scorechar no20;/ 记录号char number20;/* 学号 */char name20;/* 姓名 */char grades10;/ 成绩struct score *next;/ 下一个节点score;int m,n;score* load(score *head)score *p1,*p2;int m=0;char filepn10;FI

3、LE *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;else p2->next=p1;p2=p1;p1=(score *)malloc(LEN);fscanf(fp,"%s %s %s %s &quo

4、t;,p1->no,p1->number,p1->name,p1->grades);p2->next=p1;p1->next=NULL;n+;fclose(fp);return head;/ 追加score *append(score *head)score *p1,*p2,*p3;p3=(score *)malloc(LEN);printf(" 输入学生信息: n");printf(" 记录号 学号 姓名 成绩 n");scanf("%s %s %s %s",p3->no,p3->nu

5、mber,p3->name,p3->grades);p1=head;if(head=NULL)/ 如果链表为空head=p3;p3->next=NULL;elseif(p1->next=NULL) p1->next=p3;p3->next=NULL;elsewhile(p1->next!=NULL)/ 若是还没有到尾端的话p2=p1;p1=p1->next;p1->next=p3;p3->next=NULL;n+;return head;score * insert(score *head)score *p1,*p2,*p3;p3=(

6、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;elseif(p1->next=NULL) p1->next=p3;p3->next=NULL;elsewhile(p1!=NULL&am

7、p;&strcmp(p1->no,p3->no)<=0)/ 若是还没有到尾端的话p2=p1;p1=p1->next;/p1->next=p3;/p3->next=NULL;p2->next=p3;p3->next=p1;n+;return head;score *del(score *head)score *p1,*p2,*p3;p3=(score *)malloc(LEN);printf(" 输入删除学生信息: n");printf(" 记录号: n");scanf("%s",

8、p3->no);p1=head;if(head=NULL)/ 如果链表为空head=p3;/p3->next=NULL;printf(" 删除失败 n");return head;elseif(p1->next=NULL)if(p1->no=p3->no)head=NULL;return head;/p1->next=p3;/p3->next=NULL;elsewhile(p1!=NULL&&strcmp(p1->no,p3->no)!=0)/ 若是还没有到尾端>的话p2=p1;p1=p1->

9、next;/p1->next=p3;/p3->next=NULL;p2->next=p1->next;/p3->next=p1;return head;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->nex

10、t=NULL;printf(" 查询失败 n");return head;elseif(p1->next=NULL)if(p1->no=p3->no)return head;/p1->next=p3;/p3->next=NULL;elsewhile(p1!=NULL&&strcmp(p1->no,p3->no)!=0)/ 若是还没有到尾端>的话p2=p1;p1=p1->next;/p1->next=p3;/p3->next=NULL;/p2->next=p1->next;/p3-&

11、gt;next=p1;printf("%s %s %s %sn",p1->no,p1->number,p1->name,p1->grades);return p1;void save(score *head)FILE *fp;score *p1;char filepn20;printf(" 输入文件路径以及文件名 n");scanf("%s",filepn);if(fp=fopen(filepn,"wt+")=NULL)printf(" 文件打开失败n");exit(0)

12、;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);/return 0;void show(score *head)score *p;p=head;printf(" 学生信息 n");while(p!=NULL)printf("%s %s %s %s n",p->no,p->number,p->name,p->

13、grades);p=p->next;int main(int argc,char *argv)score *head=0;head=load(head);printf("Please input the operation you want to do:n""ttI.Insert a new record in a form of record-num() stu-num stu-name stu-scoren""ttA.Append a new record in a form of record-num() stu-num stu-n

14、ame stu-scoren""ttS.Sead a record by the value of record-numn""ttD.Delete a record by the value of record-numn""ttQ.Quit the DB systemn");char c;int flag=1;while(flag)/return;printf("Please enter the operation type you want to don");scanf("%c",&c);if(c>=96)c-=32;switch(c)cas

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论