#include <stdio.h>
#include <windows.h>
#include <malloc.h>
typedef struct stu
{
int num;
char name[50];
char sex[50];
int score;
struct stu *next;
}student;
int operate(void)//菜单
{
system("cls");
printf("\t\t\t1录入新的学生成绩\n");
printf("\t\t\t2删除学生成绩\n");
printf("\t\t\t3修改学生成绩\n");
printf("\t\t\t4查找学生成绩\n");
printf("\t\t\t5显示所有学生信息\n");
printf("\t\t\t0退出程序\n");
printf("\t\t\t输入编号:");
return 0;
}
student* input(student *p3)//输入
{
int a;
int n=0;
student *head,*p1,*p2,*p4;
system("cls");
p4=p3;
while(p3!=NULL)
{
p2=p3;
p3=p3->next;
if(p3==NULL)
{
p1=(student *)malloc(sizeof(student));
p2->next=p1;
head=p4;
goto l1;
}
}
do{
p1=(student *)malloc(sizeof(student));
if(n==0)
{
head=p1;
p2=p1;
}
l1:printf("学号:");
scanf("%d",&p1->num);
printf("姓名:");
scanf("%s",p1->name);
printf("性别男/女:");
scanf("%s",p1->sex);
do
{ printf("成绩(0-100):");
scanf("%d",&p1->score);
if(p1->score>100||p1->score<0)
printf("输入错误请重新输入:\n");
}while(p1->score>100||p1->score<0);
do{
printf("是否继续输入1/0:");
scanf("%d",&a);
if(a!=1&&a!=0)
printf("输入错误请重新输入!\n");
}while(a!=0&&a!=1);
p2->next=p1;
p2=p1;
n++;
}while(a==1);
p2->next=NULL;
return (head);
}
void prin(student *head)/*显示学生信息*/
{
int b;
student *p;
p=head;
printf("\t\t学号\t\t姓名\t\t性别\t\t成绩\n");
if(head!=NULL)
do
{
printf("\t\t%d\t\t%s\t\t%s\t\t%d\n",p->num,p->name,p->sex,p->score);
p=p->next;
}while(p!=NULL);
do{
printf("1返回:");
scanf("%d",&b);
if(b!=1)
printf("输入错误请重新输入\n");
}while(b!=1);
}
int find(student *head)//查找学生信息
{
int a,b;
student *p;
do{
do
{ p=head;
printf("\t\t请输入要查找的学生学号:");
scanf("%d",&a);
while(p->num!=a)
{
p=p->next;
if(p==NULL)
{
printf("\t\t没有找到该学生信息请重新输入\n");
break;
}
}
}while(p==NULL);
printf("\t\t学号\t\t姓名\t\t性别\t\t成绩\n");
printf("\t\t%d\t\t%s\t\t%s\t\t%d\n",p->num,p->name,p->sex,p->score);
do{
printf("是否继续查询1/0:");
scanf("%d",&b);
if(b!=0&&b!=1)
printf("输入错误请重新输入\n");
}while(b!=1&&b!=0);
if(b==0)
return 0;
}while(b==1);
return 0;
}
void alter(student *head)//修改学生信息
{
int a;
student *p;
do
{
p=head;
printf("\t\t请输入要修改的学生学号:");
scanf("%d",&a);
while(p->num!=a)
{
p=p->next;
if(p==NULL)
{
printf("\t\t没有找到该学生请重新输入\n");
break;
}
}
}while(p==NULL);
printf("学号:");
scanf("%d",&p->num);
printf("姓名:");
scanf("%s",p->name);
printf("性别男/女:");
scanf("%s",p->sex);
do
{ printf("成绩(0-100):");
scanf("%d",&p->score);
if(p->score>100||p->score<0)
printf("输入错误请重新输入:\n");
}while(p->score>100||p->score<0);
}
student* omit(student *head)//删除学生信息
{
int a;
student *p,*p1;
do
{
p=head;
printf("\t\t请输入要删除的学生学号:");
scanf("%d",&a);
while(p->num!=a)
{
p1=p;
p=p->next;
if(p==NULL)
{
printf("\t\t没有找到该学生,请重新输入\n");
}
}
}while(p==NULL);
if(p==head)
head=p->next;
else if(p->next==NULL)
p1->next=NULL;
else
p1->next=p->next;
free(p);
return (head);
}
int save(FILE *fp,student *head)//向D盘写入数据
{
student *p;
p=head;
rewind(fp);
while(p!=NULL)
{
if(p==NULL)
break;
fwrite(p,sizeof(student),1,fp);
p=p->next;
}
return 0;
}
int main()
{
int a;
FILE *fp;
student *head=NULL,*p1,*p2;
if((fp=fopen("d:\\student.dat","rb+"))==NULL)
fp=fopen("d:\\student.dat","wb+");
else
{
p1=(student *)malloc(sizeof(student) );
fread(p1,sizeof(student),1,fp);
head=p1;
p2=p1;
while((feof(fp))==NULL)
{
p1=(student *)malloc(sizeof(student) );
fread(p1,sizeof(student),1,fp);
p2->next=p1;
if((feof(fp))==NULL)
p1->next=NULL;
p2=p1;
}
fclose(fp);
}
do
{
operate();//显示菜单
do
{
scanf("%d",&a);
if(a<0||a>6)
printf("输入错误请重新输入:");
}while(a<0||a>6);
switch(a)
{
case 1:head=input(head);break;//新建学生信息
case 2:head=omit(head);break;// 删除学生信息
case 3:alter(head);break;//修改学生信息
case 4:find(head);break;//查找学生信息
case 5: prin(head);break;//显示全部学生信息
default:break;
}
}while(a!=0);
printf("数否保存文件到D盘1/0:");
scanf("%d",&a);
if(a==1)
{ fp=fopen("d:\\student.dat","wb+");
save(fp,head);//保存数据到D盘
fclose(fp);
printf("\t\t\t本程序由H U C ls701110编写用于交流学习\n");
return 0;
}
printf("\t\t\t本程序由H U C ls701110编写用于交流学习\n");
return 0;
}