C语言大作业(全).doc

上传人:小** 文档编号:90645169 上传时间:2019-06-14 格式:DOC 页数:21 大小:19.30KB
返回 下载 相关 举报
C语言大作业(全).doc_第1页
第1页 / 共21页
C语言大作业(全).doc_第2页
第2页 / 共21页
C语言大作业(全).doc_第3页
第3页 / 共21页
C语言大作业(全).doc_第4页
第4页 / 共21页
C语言大作业(全).doc_第5页
第5页 / 共21页
点击查看更多>>
资源描述

《C语言大作业(全).doc》由会员分享,可在线阅读,更多相关《C语言大作业(全).doc(21页珍藏版)》请在金锄头文库上搜索。

1、#include stdafx.h#include stdio.h#include stdlib.h#include string.h#include malloc.h#define LEN sizeof(struct student)#define NULL 0int n,m;int k=1;struct studentchar 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 while(p1 != NULL)fwrite(p1, sizeof(struct student), 1, outFile);p1 = p1-nex

3、t;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,*p2;n=

4、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); gets(p1-profession); printf(数学成绩:n); scanf(%d,&p1-maths); printf(英语成绩:n); scanf(%d,&p1-english); printf(C语言成绩:n); scanf(%d,&p1-C_pro

5、gram); 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); gets(p1-profes

6、sion); 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);p=head;if(head=

7、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 = head;struct student record;FI

8、LE *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 *)malloc(sizeof(

9、struct student);memcpy(p2-next, &record, sizeof(record); /复制结构体数据 p2 = p2-next; n=n+1;p2-next=NULL;fclose(infile);return head;/从文件中读取数据struct student * search_num(struct student * head,char array20)struct student * p1;p1=head;if(head=NULL)printf(打开错误);return(head);while(strcmp(array,p1-num)!=0)&(p1-

10、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_name(struct student * head,char

11、 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,p1-english,p1-C_progr

12、am);printf(n);else printf(此姓名不存在n);return(head); /按姓名查询数据/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; /找到相应的学号 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-

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

当前位置:首页 > 商业/管理/HR > 管理学资料

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号