C语言大作业(全).doc
#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 student * 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->next;fclose(outFile);/将数据存入文档中void check(struct student * head,struct student * p1)struct student *h=head;for(m=0;m<n;m+,h=h->next )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=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_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"); 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");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 = 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 *)malloc(sizeof(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->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 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_program);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-