2022年C语言大作业 2.pdf
《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
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 2022年C语言大作业 2022 语言 作业
限制150内