#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void display();
struct score
{
int math_score;
int English_score;
int c_score;
};
struct student
{
long sum;
char name[30];
char sex[10];
char birth[20];
struct score score1;
struct student *next;
};
typedef struct student stu;
void meum(void)
{
printf("1输入学生信息\n");
printf("2查询学生信息\n");
printf("3排序\n");
printf("4删除信息\n");
printf("5退出\n");
}
struct student* cz1()
{
stu *p1,*p2,*head;
int n=0,m;
p1=p2=(struct student*)malloc(sizeof(struct student));
if(p1!=NULL)
{
printf("请输入学号:\n");
scanf("%ld",&p1->sum);
printf("请输入姓名:\n");
scanf("%s",&p1->name);
printf("请输入性别:\n");
scanf("%s",&p1->sex);
printf("请输入出生日:\n");
scanf("%s",&p1->birth);
printf("请输入数学成绩:\n");
scanf("%d",&p1->score1.math_score);
printf("请输入英语成绩:\n");
scanf("%d",&p1->score1.English_score);
printf("请输入c语言成绩:\n");
scanf("%d",&p1->score1.c_score);
printf("%-15ld%-10s%-10s%-20s%-5d%-5d%-5d\n",p1->sum,p1->name,p1->sex,p1->birth,p1->score1.math_score,p1->score1.English_score,p1->score1.c_score);
}
do
{
if(n==0)
{
head=p1;
n+=1;
}
else
{
p2->next=p1;
p2=p1;
}
printf("是否继续添加,“是”请按“1”,“否”请按“2”");
scanf("%d",&m);
do
{
printf("请重新输入");
scanf("%d",&m);
}while(m!=1&&m!=2);
if(m==2)break;
if(m==1)
{
p1=(struct student*)malloc(sizeof(struct student));
printf("请输入学号:\n");
scanf("%ld",&p1->sum);
printf("请输入姓名:\n");
scanf("%s",&p1->name);
printf("请输入性别:\n");
scanf("%s",&p1->sex);
printf("请输入出生日:\n");
scanf("%s",&p1->birth);
printf("请输入数学成绩:\n");
scanf("%d",&p1->score1.math_score);
printf("请输入英语成绩:\n");
scanf("%d",&p1->score1.English_score);
printf("请输入c语言成绩:\n");
scanf("%d",&p1->score1.c_score);
}
}while(1);
p2->next=NULL;
return head;
}
void display(struct student *head)
{
struct student *p;
p=head;
printf("%-15s%-10s%-10s%-20s%-5s%-5s%-5s\n","学号","姓名","性别","出生年月","数学","英语","c语言");
do
{
printf("%-15ld%-10s%-10s%-20s%-5d%-5d%-5d\n",p->sum,p->name,p->sex,p->birth,p->score1.math_score,p->score1.English_score,p->score1.c_score);
p=p->next;
}while(p!=NULL);
}
void save(struct student *head)
{
FILE *fp;
stu *p1=head;
if((fp=fopen("date.dat","a"))==NULL)
{
printf("无法打开文件");
exit(0);
}
while(p1!=NULL)
{
fwrite (p1,sizeof(struct student),1,fp);
p1=p1->next;
}
fclose(fp);
printf("*********************保存成功**********************\n");
}
void cx1()
{
long sum1;
int n=0;
FILE *fp;
stu p1;
printf("请输入学号");
scanf("%ld",&sum1);
if((fp=fopen("date.dat","rb"))==NULL)
{
printf("无法打开文件");
exit(0);
}
while(!feof(fp))
{
fread(&p1,sizeof(struct student),1,fp);
if(sum1==p1.sum)
{
printf("%-15ld%-10s%-10s%-20s%-5d%-5d%-5d\n",p1.sum,p1.name,p1.sex,p1.birth,p1.score1.math_score,p1.score1.English_score,p1.score1.c_score);
n=1;
}
}
if(sum1!=p1.sum&&n==0)
printf("查无此人\n");
fclose(fp);
}
void load()
{
int n=0;
FILE *fp;
stu p,*p1,*p2,*head1;
if((fp=fopen("date.dat","rb"))==NULL)
{
printf("无法打开文件");
exit(0);
}
while(!feof(fp))
{
p1=(struct student*)malloc(sizeof(struct student));
fread(&p,sizeof(struct student),1,fp);
*p1=p;
if(n==0)
{
head1=p1;
p2=p1;
n+=1;
}
else
{
p2->next=p1;
p2=p1;
}
}
fclose(fp);
p2->next=NULL;
display(head1);
printf("*******************************读取数据*****************************************");
}
void del()
{
long sum;
printf("请输入删除学生学号");
scanf("%l",&sum);
}
int main()
{
struct student *head;
load();
meum();
head=cz1();
save(head);
display(head);
cx1();
return 0;
}
这是半成品的代码,您可以看一下