2022年C语言大作业 2.pdf

上传人:C****o 文档编号:39722796 上传时间:2022-09-07 格式:PDF 页数:14 大小:80.94KB
返回 下载 相关 举报
2022年C语言大作业 2.pdf_第1页
第1页 / 共14页
2022年C语言大作业 2.pdf_第2页
第2页 / 共14页
点击查看更多>>
资源描述

《2022年C语言大作业 2.pdf》由会员分享,可在线阅读,更多相关《2022年C语言大作业 2.pdf(14页珍藏版)》请在得力文库 - 分享文档赚钱的网站上搜索。

1、#include stdafx.h#include stdio.h#include stdlib.h#include string.h#include malloc.h#define LEN sizeof(struct student)#define NULL 0 int n,m;int k=1;struct student char num20;char name20;int age;char profession50;int maths;int english;int C_program;float ave;struct student*next;float ave(struct stud

2、ent*p1)float ave;ave=(float)(p1-maths+p1-english+p1-C_program)/3;return ave;void save(struct student*head)struct student*p1=head;FILE*outFile;outFile=fopen(stu.bin,wb);if(outFile=NULL)printf(nFile error.);else 名师资料总结-精品资料欢迎下载-名师精心整理-第 1 页,共 14 页 -while(p1!=NULL)fwrite(p1,sizeof(struct student),1,out

3、File);p1=p1-next;fclose(outFile);/将数据存入文档中void check(struct student*head,struct student*p1)struct student*h=head;for(m=0;mnext)if(strcmp(h-num,p1-num)=0)printf(输入号码重复 n);printf(请重新输入学号:);scanf(%s,p1-num);getchar();check(head,p1);/检验学号是否重复struct student*creat()struct student*head;struct student*p1,*p

4、2;n=0;p1=p2=(struct student*)malloc(LEN);printf(学号:n);scanf(%s,p1-num);getchar();printf(姓名:n);gets(p1-name);printf(年龄:n);scanf(%d,&p1-age);getchar();printf(专业:n);名师资料总结-精品资料欢迎下载-名师精心整理-第 2 页,共 14 页 -gets(p1-profession);printf(数学成绩:n);scanf(%d,&p1-maths);printf(英语成绩:n);scanf(%d,&p1-english);printf(C语

5、言成绩:n);scanf(%d,&p1-C_program);head=NULL;while(strcmp(p1-num,0)!=0)n=n+1;if(n=1)head=p1;else p2-next=p1;p2=p1;p1=(struct student*)malloc(LEN);printf(学号:n);scanf(%s,p1-num);getchar();check(head,p1);/检查输入的学号是不是重复且是不是重复输错 printf(姓名:n);gets(p1-name);printf(年龄:n);scanf(%d,&p1-age);getchar();printf(专业:n);

6、gets(p1-profession);printf(数学成绩:n);scanf(%d,&p1-maths);printf(英语成绩:n);scanf(%d,&p1-english);printf(C语言成绩:n);scanf(%d,&p1-C_program);p2-next=NULL;save(head);return(head);/录入数据并将其保存至文档void print(struct student*head)struct student*p;printf(n这%d个学生的成绩为:n,n);printf(学号姓名年龄专业数学英语 C语言平均成绩 n);名师资料总结-精品资料欢迎下载

7、-名师精心整理-第 3 页,共 14 页 -p=head;if(head=NULL)printf(The info errorn);if(head!=NULL)while(p!=NULL)p-ave=ave(p);printf(%s%8s%8d%13s%9d%10d%9d%10.2f,p-num,p-name,p-age,p-profession,p-maths,p-english,p-C_program,p-ave);printf(n);p=p-next;/输出链表/struct student*read()struct student*head=NULL;struct student*p2

8、=head;struct student record;FILE*infile;infile=fopen(stu.bin,rb);n=1;if(infile=NULL)printf(nFile error.);else head=(struct student*)malloc(sizeof(struct student);fread(head,sizeof(*head),1,infile);p2=head;while(fread(&record,sizeof(record),1,infile)/fread返回数据个数,结束和出错返回0;p2-next=(struct student*)mall

9、oc(sizeof(struct student);memcpy(p2-next,&record,sizeof(record);/复制结构体数据 p2=p2-next;n=n+1;p2-next=NULL;fclose(infile);return head;/从文件中读取数据名师资料总结-精品资料欢迎下载-名师精心整理-第 4 页,共 14 页 -struct student*search_num(struct student*head,char array20)struct student*p1;p1=head;if(head=NULL)printf(打开错误);return(head);

10、while(strcmp(array,p1-num)!=0)&(p1-next!=NULL)p1=p1-next;/找到相应的学号if(strcmp(array,p1-num)=0)printf(学号姓名年龄专业数学英语 C语言 n);printf(%s%10s%10d%15s%11d%12d%11d,p1-num,p1-name,p1-age,p1-profession,p1-maths,p1-english,p1-C_program);printf(n);else printf(此学号不存在 n);return(head);/按学号查询数据/struct student*search_na

11、me(struct student*head,char array20)struct student*p1;p1=head;if(head=NULL)printf(打开错误);return(head);while(strcmp(array,p1-name)!=0)&(p1-next!=NULL)p1=p1-next;/找到相应的学号if(strcmp(array,p1-name)=0)printf(学号姓名年龄专业数学英语 C语言 n);printf(%s%10s%10d%15s%11d%12d%11d,p1-num,p1-name,p1-age,p1-profession,p1-maths,

12、p1-english,p1-C_program);printf(n);else printf(此姓名不存在 n);return(head);名师资料总结-精品资料欢迎下载-名师精心整理-第 5 页,共 14 页 -/按姓名查询数据/struct student*change(struct student*head,char array20)struct student*p1;p1=head;if(head=NULL)printf(打开错误!);return(head);while(strcmp(array,p1-num)!=0)&(p1-next!=NULL)p1=p1-next;/找到相应的

13、学号 if(strcmp(array,p1-num)=0)printf(请输入更改的学号:n);printf(学号:n);scanf(%s,p1-num);getchar();printf(姓名:n);gets(p1-name);printf(年龄:n);scanf(%d,&p1-age);getchar();printf(专业:n);gets(p1-profession);printf(数学成绩:n);scanf(%d,&p1-maths);printf(英语成绩:n);scanf(%d,&p1-english);printf(C语言成绩:n);scanf(%d,&p1-C_program)

14、;save(head);else printf(所查学号不存在 n);return(head);/修改数据/struct student*del(struct student*head,char array20)struct student*p1,*p2;if(head=NULL)名师资料总结-精品资料欢迎下载-名师精心整理-第 6 页,共 14 页 -printf(nlist null!n);return head;p1=head;while(strcmp(array,p1-num)!=0)&(p1-next!=NULL)p2=p1;p1=p1-next;/找到相应的学号、if(strcmp

15、(array,p1-num)=0)if(strcmp(array,head-num)=0)head=p1-next;else p2-next=p1-next;else printf(你查询的学号(%s)不存在!,p1-num);save(head);return(head);void add(struct student*head)struct student*p1;printf(n请输入添加学生的信息:n);p1=(struct student*)malloc(LEN);printf(学号:n);scanf(%s,p1-num);getchar();check(head,p1);printf

16、(姓名:n);gets(p1-name);printf(年龄:n);scanf(%d,&p1-age);getchar();printf(专业:n);gets(p1-profession);printf(数学成绩:n);scanf(%d,&p1-maths);printf(英语成绩:n);scanf(%d,&p1-english);printf(C语言成绩:n);名师资料总结-精品资料欢迎下载-名师精心整理-第 7 页,共 14 页 -scanf(%d,&p1-C_program);FILE*outFile;outFile=fopen(stu.bin,ab);if(outFile=NULL)p

17、rintf(n打开错误.);fwrite(p1,sizeof(struct student),1,outFile);fclose(outFile);void sta(struct student*head,char array20,int max,int min)struct student*p1=head;int k=0;if(strcmp(array,maths)=0)while(p1!=NULL)if(p1-maths=min&p1-mathsnext;printf(数学科目在%d到%d之间的人数为:%dn,min,max,k);else if(strcmp(array,english)

18、=0)while(p1!=NULL)if(p1-english=min&p1-englishnext;printf(英语科目在%d到%d之间的人数为:%dn,min,max,k);else if(strcmp(array,C_program)=0)while(p1!=NULL)if(p1-C_program=min&p1-C_programnext;printf(C语言科目在%d到%d之间的人数为:%dn,min,max,k);void exit()printf(tt);printf(*n);printf(tt);printf(*感谢使用*n);printf(tt);printf(*n);k=

19、0;void Chooseproject(int i)struct student*head=NULL;if(i=1)head=read();print(head);else if(i=2)int p=1 ;printf(请选择按学号查询(1)还是按姓名查询(0):);scanf(%d,&p);while(p!=1&p!=0)printf(输入错误!);printf(请选择学号查询(1)还是姓名查询(0));scanf(%d,&p);if(p=1)head=read();char num20;printf(请输入查询的学号:n);scanf(%s,num);head=search_num(he

20、ad,num);while(strcmp(num,0)!=0)printf(是否继续查询(是就输学号,否就输0):n);名师资料总结-精品资料欢迎下载-名师精心整理-第 9 页,共 14 页 -scanf(%s,num);if(strcmp(num,0)=0)break;head=search_num(head,num);else if(p=0)head=read();char name20;printf(请输入查询的学号:n);scanf(%s,name);head=search_name(head,name);while(strcmp(name,0)!=0)printf(是否继续查询(是就

21、输学号,否就输0):n);scanf(%s,name);if(strcmp(name,0)=0)break;head=search_name(head,name);else if(i=3)head=read();char num20;printf(请输入要更改的学号:n);scanf(%s,num);getchar();head=change(head,num);print(head);while(strcmp(num,0)!=0)printf(是否继续更改(是就输学号,否就输0):n);scanf(%s,num);getchar();if(strcmp(num,0)=0)break;head

22、=change(head,num);print(head);else if(i=4)名师资料总结-精品资料欢迎下载-名师精心整理-第 10 页,共 14 页 -head=read();add(head);else if(i=5)char num20;head=read();printf(请输入要删除的学号:n);scanf(%s,num);getchar();head=del(head,num);print(head);else if(i=6)head=read();int i;printf(tt);printf(*1.学号排序;*n);printf(tt);printf(*2.平均成绩排序;

23、*n);printf(tt);printf(*3.数学成绩排序;*n);printf(tt);printf(*4.英语成绩排序;*n);printf(tt);printf(*5.C语言成绩排序;*n);printf(请输入相应的排序代号:);scanf(%d,&i);sort_down(head,i);else if(i=7)head=read();int min,max;char subject20;printf(tt);printf(*数学(maths);*n);printf(tt);printf(*英语(english);*n);printf(tt);printf(*C语言(C_prog

24、ram);*n);printf(请选择要统计的项目:n);scanf(%s,subject);getchar();while(strcmp(subject,maths)!=0&strcmp(subject,english)!=0&strcmp(subject,C_program)!=0)名师资料总结-精品资料欢迎下载-名师精心整理-第 11 页,共 14 页 -printf(输入错误!);printf(tt);printf(*数学(maths);*n);printf(tt);printf(*英语(english);*n);printf(tt);printf(*C语言(C_program);*n

25、);printf(请选择要统计的项目:n);scanf(%s,subject);getchar();printf(请输入分数段的区间:);printf(区间上限:);scanf(%d,&max);printf(区间下限:);scanf(%d,&min);while(min=max)printf(输入的下限大于等于上限,错误!);printf(请重新输入分数段的区间:);printf(区间上限:);scanf(%d,max);printf(区间下限:);scanf(%d,min);sta(head,subject,max,min);else if(i=8)printf(tt);printf(*n

26、);printf(tt);printf(*感谢使用*n);printf(tt);printf(*n);k=0;/选择功能void menu()int i;printf(tt);printf(*1.信息浏览;*n);printf(tt);printf(*2.信息查询;*n);printf(tt);printf(*3.信息修改;*n);printf(tt);printf(*4.信息添加;*n);名师资料总结-精品资料欢迎下载-名师精心整理-第 12 页,共 14 页 -printf(tt);printf(*5.信息删除;*n);printf(tt);printf(*6.信息排序;*n);print

27、f(tt);printf(*7.信息统计;*n);printf(tt);printf(*8,退出系统;*n);printf(请选择项目:);scanf(%d,&i);while(i8)printf(输入错误,请重新输入:n);printf(tt);printf(*1.信息浏览;*n);printf(tt);printf(*2.信息查询;*n);printf(tt);printf(*3.信息修改;*n);printf(tt);printf(*4.信息添加;*n);printf(tt);printf(*5.信息删除;*n);printf(tt);printf(*6.信息统计;*n);printf(

28、tt);printf(*7,退出系统;*n);printf(请重新选择项目:);scanf(%d,&i);Chooseproject(i);int _tmain(int argc,_TCHAR*argv)struct student*head=NULL;printf(tt);printf(*n);printf(tt);printf(*学生管理系*n);printf(tt);printf(*n);printf(是否已经录入信息?n);char c;printf(请输入 y 或 n:);c=getchar();getchar();while(c!=n&c!=y)printf(输入错误,请重新输入(y 或 n):n);c=getchar();getchar();if(c=n)printf(请输入学生成绩:n);head=creat();名师资料总结-精品资料欢迎下载-名师精心整理-第 13 页,共 14 页 -while(k!=0)menu();system(pause);return 0;名师资料总结-精品资料欢迎下载-名师精心整理-第 14 页,共 14 页 -

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 教育专区 > 高考资料

本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知得利文库网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

工信部备案号:黑ICP备15003705号-8 |  经营许可证:黑B2-20190332号 |   黑公网安备:91230400333293403D

© 2020-2023 www.deliwenku.com 得利文库. All Rights Reserved 黑龙江转换宝科技有限公司 

黑龙江省互联网违法和不良信息举报
举报电话:0468-3380021 邮箱:hgswwxb@163.com