c语言学生管理系统_第1页
c语言学生管理系统_第2页
已阅读5页,还剩13页未读 继续免费阅读

下载本文档

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

文档简介

1、c语言学生管理系统 同学信息的录入,查找,删除,修改等操作 #includestdio.h #includestdlib.h#includestring.htypedef struct pnode /结构体定义 用于存放同学信息的节点char name 20; /姓名char xh20; /学号char fh30; /房号personnode;char filename20; /文件名file *fp; /指向文件的指针void creat() /创建新数据文件的函数 创建一个二进制文件 用于存放同学数据personnode *person;person=(personnode *)mallo

2、c(sizeof(personnode); /为节点安排内存printf(n please enter the filename:n);scanf(%s,filename);if(fp=fopen(filename,w+)=null)printf(n you have no enter the filename,can not font the file);exit(0);printf(n please enter the name,studentnumbeer (the same length) and roomnumber,spare with space,endwith #n);scan

3、f(%s,person-name);while(strcmp(person-name,#) /该循环用于掌握 同学信息的录入 当输入#时 同学信息录入完毕scanf(%s %s,person-xh,person-fh);fprintf(fp,%-10s%-20s%-50sn,person-name,person-xh,person-fh);scanf(%s,person-name);fclose(fp);void readfile() /文件读取函数 用于打开已有的二进制数据文件printf(n please enter the fileroad:n);scanf(%s,filename);

4、/此处输入为文件的路径 如c:zhangif(fp=fopen(filename,r+)=null)printf(n can't open thefile:n);exit(0);fclose(fp);void output() /输出函数 用于输出文件的全部信息personnode *person;long offset1,offset2;char name18,name28,name38;char xh116,xh216,xh316;char fh130,fh230,fh330;person=(personnode *)malloc(sizeof(personnode);if(fp=

5、fopen(filename,r)=null)printf(n can't open the file);exit(0);while(!feof(fp) /此循环用于 对文件数据中关键字学号进行从小到大冒泡排序int a,b,c,d; /a为循环结束判定变量 b,c,d用于起泡排序时学号交换的替换if(fp=fopen(filename,r+)=null)printf(n can't open the file);exit(0);while(!feof(fp) / 从文件头开头遍历while(!feof(fp) /对两组数据进行排序offset1=ftell(fp); /猎取文

6、件内部当前指针位置fscanf(fp,%s%s%sn,person-name,person-xh,person-fh);strcpy(name1,person-name);strcpy (xh1,person-xh);strcpy(fh1,person-fh);if(feof(fp) break; /文件结束 跳出循环offset2=ftell(fp); /猎取文件内部下一指针位置 同学信息的录入,查找,删除,修改等操作 fscanf(fp,%s%s%sn,person-name,person-xh,person-fh); strcpy (name2,person-name);strcpy(x

7、h2,person-xh);strcpy(fh2,person-fh);b=strlen(xh1);c=strlen(xh2);d=strcmp(xh1,xh2);if(b=cd0) /假如 学号1大于等于学号2 怎交换全部数据strcpy(name3,name1);strcpy(name1,name2);strcpy(name2,name3);strcpy(xh3,xh1);strcpy(xh1,xh2);strcpy(xh2,xh3);strcpy(fh3,fh1);strcpy(fh1,fh2);strcpy(fh2,fh3);fseek(fp,offset1,seek_set); /将

8、文件指针移动offset1个字节strcpy(person-name,name1);strcpy(person-xh,xh1);strcpy(person-fh,fh1);fprintf(fp,%-10s%-20s%-50sn,person-name,person-xh,person-fh);/排序后写入文件strcpy(person-name,name2);strcpy(person-xh,xh2);strcpy( person-fh,fh2);fprintf(fp,%-10s%-20s%-50sn,person-name,person-xh,person-fh);/排序后写入文件fseek

9、(fp,offset2,seek_set); /将文件位置指针从文件头向前移动offset2个字节rewind(fp); /指向头文件while(!feof(fp) /此循环用于推断学号是否有序offset1=ftell(fp);fscanf(fp,%s%s%sn,person-name,person-xh,person-fh);strcpy (xh1,person-xh);if(feof(fp) a=1; break;offset2=ftell(fp);fscanf(fp,%s%s%sn,person-name,person-xh,person-fh);strcpy(xh2,person-x

10、h);b=strlen(xh1);c=strlen(xh2);d=strcmp(xh1,xh2);if(b=cd=0) a=1; /若a为1 则有序else a=0; /a为0怎无序break;fseek(fp,offset2,seek_set);if(a) break; /a为1 说明学号已经有序 跳出循环rewind(fp);if(a) break; /若a为1,则学号已有序排列,跳出总循环,输出文件rewind(fp);printf(n*n);printf(%35sn,thefile(thefile hava printf as studentnumber);printf(%-10s%-

11、20s%-50sn,name,studentnumber,roomnumber);while(!feof(fp) /次循环用于输出文件fscanf(fp,%s%s%sn,person-name,person-xh,person-fh);printf(%-10s%-20s%-50sn,person-name,person-xh,person-fh);fclose(fp);printf(*nn);void search1() / 按姓名搜寻函数int k=0;char namekey8; 同学信息的录入,查找,删除,修改等操作 personnode *person; person=(personn

12、ode *)malloc(sizeof(personnode);printf(n please enter the namehead you need :);scanf(%s,namekey);if(fp=fopen(filename,rb)=null)printf(n can't open thefile);exit(0);while(!feof(fp)fscanf(fp,%s %s %sn,person-name,person-xh,person-fh);if(!strcmp(namekey,person-name) /比较是否相同 相同则输出结果 否则输出未找到记录printf(

13、nn have serched,the courrent is:);printf(%-10s%-20s%-50sn,person-name,person-xh,person-fh);k=1;if(!k) printf(nn sorry,there is not the current of thiss people n);fclose(fp);void search2() /按学号搜寻函数int k=0;char xhkey16;personnode *person;person=(personnode *)malloc(sizeof(personnode);printf(n please e

14、nter the roomnumber you want to serch:);scanf(%s,xhkey);if(fp=fopen(filename,rb)=null)printf(n can't open the file );exit(0);while(!feof(fp)fscanf(fp,%s %s %sn,person-name,person-xh,person-fh);if(!strcmp(xhkey,person-xh) /比较是否相同 相同则输出结果 否则输出未找到记录printf(nn have got it,the current is:);printf(%-10

15、s%-20s%-50sn,person-name,person-xh,person-fh);k=1;if(!k) printf(nn sorry,there is not the current of thiss people n);fclose(fp);void search3() /按房号搜寻函数int k=0;char fhkey30;personnode *person;person=(personnode *)malloc(sizeof(personnode);printf(n please enter the roomnumber you want to serch:);scanf

16、(%s,fhkey);if(fp=fopen(filename,rb)=null)printf(n can't open thefile);exit(0);while(!feof(fp)fscanf(fp,%s %s %sn,person-name,person-xh,person-fh);if(!strcmp(fhkey,person-fh) /比较是否相同 相同则输出结果 否则输出未找到记录printf(nn have got it,the current is:);printf(%-10s%-20s%-50sn,person-name,person-xh,person-fh);k

17、=1;if(!k) printf(nn sorry,there is not the current of thiss peoplen);fclose(fp);void append() /插入函数 用于像已有文件插入一条新的同学信息记录personnode *person;person=(personnode *)malloc(sizeof(personnode);if(fp=fopen(filename,a)=null)printf(n can't open the file);exit(0);printf(n please enter the name,studentnumber

18、 and roomnumber n);scanf(%s %s %s,person-name,person-xh,person-fh);fprintf(fp,%-10s%-20s%-50sn,person-name,person-xh,person-fh);fclose(fp);void modify() /更新函数 用 同学信息的录入,查找,删除,修改等操作 于修改指定同学姓名的记录 int k=0;long offset;char namekey8;personnode *person;person=(personnode *)malloc(sizeof(personnode);printf

19、(n please enter the studentname you want to updata :);scanf(%s,namekey);if(fp=fopen(filename,r+)=null)printf(n can't open thefile);exit(0);while(!feof(fp)offset=ftell(fp);fscanf(fp,%s %s %sn,person-name,person-xh,person-fh);if(!strcmp(namekey,person-name) /比较是否相同 如k=1;break;if(k) /相同 输出记录并进行修改pr

20、intf(n hava got it,the current is:);printf(%-10s%-20s%-50sn,person-name,person-xh,person-fh);printf(n please enter the new studentname,studentnumber and roomnumber :);scanf(%s %s %s,person-name,person-xh,person-fh);fseek(fp,offset,seek_set);fprintf(fp,%-10s%-20s%-50sn,person-name,person-xh,person-fh

21、);else printf(n sorry,there is not the current of this people n);fclose(fp);void deleted() /删除函数 用于删除指定同学姓名的记录int k=0;char m;long offset;char namekey8;personnode *person;person=(personnode *)malloc(sizeof(personnode);printf(n please enter the name you want to delete :);scanf(%s,namekey);if(fp=fopen(

22、filename,r+)=null)printf(n can't open the file );exit(0);while(!feof(fp) /此循环遍历整个文件 查找需要删除的记录offset=ftell(fp);fscanf(fp,%s%s%sn,person-name,person-xh,person-fh);if(!strcmp(namekey,person-name)k=1;break;if(k)printf(n hava got it,the current is:);printf(%-10s%-20s%-50sn,person-name,person-xh,person-fh);printf(n are you sure to delete it?y/n?);scanf(%s,m);if(m='y') /删除确认按钮fseek(fp,offset,seek_set); /删除记录fprintf(fp,%-10s%-20s%-50sn,);else rewind(fp);else pr

温馨提示

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

评论

0/150

提交评论