这个代码在VC6.0上可以直接使用,在vs2013上只需要加一个宏定义#define _CRT_SECURE_NO_WARNINGS就看可以使用,但是要在源文件下面加上一个(学生信息系统.txt)这样的一个文件,学生系统才能使用!!!
#include"stdio.h"
#include"stdlib.h"
#include"string.h"
#include"conio.h"
#define N 100
struct student
{
int num,math,english,computer;
char name[N];
double average;
};
struct node
{
struct student date;
struct node *next;
};
struct node *find(struct node *first,int num)//搜索学号;
{
struct node *p;
for(p=first;p!=NULL;p=p->next)
{
if(p->date.num==num)
{
return p;
}
}
return NULL;
}
void search(struct node *first)//查找学生信息;
{
int num;
struct node *p=NULL;
printf("请输入你想要搜索的学生的学号:");
scanf("%d",&num);
p=find(first,num);
if(p==NULL)
{
printf("没有搜索到该学生!!!\n");
}
else
{
printf("学号\t姓名\t数学\t英语\t计算机\t平均成绩\t\n");
printf("%d\t%s\t%d\t%d\t%d\t%.2f\t\n",p->date.num,p->date.name,p->date.math,p->date.english,p->date.computer,p->date.average);
}
}
struct node *update(struct node *first)//修改学生成绩;
{
int num,c,score;
struct node *p=NULL;
do
{
printf("请输入你想要修改的学生的学号:");
scanf("%d",&num);
p=find(first,num);
if(p==NULL)
{
printf("没有找到该学生的信息!!!\n");
}
else
{
break;
}
}while(1);
printf("请选择你想要修改的科目:\n");
printf("1.数学\n2.英语\n3.计算机\n");
scanf("%d",&c);
printf("请输入修改后的成绩:");
scanf("%d",&score);
switch(c)
{
case 1:p->date.math=score;break;
case 2:p->date.english=score;break;
case 3:p->date.computer=score;break;
default:printf("输入错误!!!\n");
}
p->date.average=(p->date.math+p->date.english+p->date.computer)/3.0;
return first;
}
struct node *cut(struct node *first,int *n)//删除学生信息;
{
int num;
struct node *cur,*prev;
do{
printf("请输入你所要删除学生信息的学号:");
scanf("%d",&num);
for(cur=first,prev=NULL;cur!=NULL&&cur->date.num!=num;prev=cur,cur=cur->next);
if(cur==NULL)
{
printf("没有找到该学生的信息!!!\n");
}
else
{
break;
}
}while(1);
if(prev==NULL)
{
first=first->next;
}
else
{
prev->next=cur->next;
}
free(cur);
printf("删除成功!!!\n");
*n=*n-1;
return first;
}
struct node *add(struct node *first,int *n)//添加学生信息;
{
int num;
struct node *new_stu,*p;
struct node *last,*cot;
cot=first;
new_stu=(struct node *)malloc(sizeof(struct node));
if(new_stu==NULL)
{
printf("添加失败!!!\n");
exit(-1);
}
for(;;)
{
printf("请输入需要添加的学号:");
scanf("%d",&num);
p=find(first,num);
if(p==NULL)
{
break;
}
printf("你所输入的学号已经存在!!!\n");
printf("请重新输入!!!\n");
}
new_stu->date.num=num;
printf("请输入学生的名字,数学,英语,计算机成绩:\n");
scanf("%s%d%d%d",new_stu->date.name,&new_stu->date.math,&new_stu->date.english,&new_stu->date.computer);
new_stu->date.average=(new_stu->date.math+new_stu->date.english+new_stu->date.computer)/3.0;
if(first==NULL)
{
new_stu->next=NULL;
first=new_stu;
*n=*n+1;
return first;
}
while(new_stu->date.averagedate.average&&cot->next!=NULL)
{
last=cot;
cot=cot->next;
}
if(new_stu->date.average>=cot->date.average)
{
if(cot==first)
{
new_stu->next=first;
first=new_stu;
*n=*n+1;
return first;
}
else
{
new_stu->next=cot;
last->next=new_stu;
*n=*n+1;
return first;
}
}
else
{
cot->next=new_stu;
new_stu->next=NULL;
*n=*n+1;
return first;
}
}
struct node *read(struct node *first)//读取历史学生信息;
{
int num,math,english,computer;
char name[20];
double average;
FILE *fp;
struct node *p,*end;
if(!(fp=fopen("学生信息文件.txt","r")))
{
printf("文件打开失败!!!\n");
exit(0);
}
while((fscanf(fp,"%d %s %d %d %d %lf\n",&num,name,&math,&english,&computer,&average))!=EOF)
{
p=(struct node*)malloc(sizeof(struct node));
p->date.num=num;
strcpy(p->date.name,name);
p->date.math=math;
p->date.english=english;
p->date.computer=computer;
p->date.average=average;
if(first==NULL)
{
first=p;
first->next=NULL;
end=first;
}
else
{
end->next=p;
p->next=NULL;
end=p;
}
}
fclose(fp);
return first;
}
void print(struct node *first)//输出学生信息;
{
struct node *p;
printf("学号\t姓名\t数学\t英语\t计算机\t平均成绩\t\n");
for(p=first;p!=NULL;p=p->next)
{
printf("%d\t%s\t%d\t%d\t%d\t%.2f\t\n",p->date.num,p->date.name,p->date.math,p->date.english,p->date.computer,p->date.average);
}
printf("\n");
}
void write(struct node *first)//写入文件函数;
{
FILE *fp;
struct node *p;
if((fp=fopen("学生信息文件.txt","w"))==NULL)
{
printf("文件打开失败!!!\n");
exit(0);
}
for(p=first;p!=NULL;p=p->next)
{
fprintf(fp,"%d %s %d %d %d %lf\n",p->date.num,p->date.name,p->date.math,p->date.english,p->date.computer,p->date.average);
}
fclose(fp);
}
void meun()//主菜单;
{
printf("\n\t********************学生信息管理系统********************\n");
printf("\n\t*** 1.添加学生信息 2.删除学生信息 ***\n");
printf("\n\t*** 3.输出学生信息 4.查找学生信息 ***\n");
printf("\n\t*** 5.学生成绩排序 6.输出学生人数 ***\n");
printf("\n\t*** 7.修改学生信息 8.退出信息系统 ***\n");
printf("\n\t********************>>>>>>>><<<<<<<<********************\n");
}
void print_n(int *n)//输出学生总人数;
{
printf("学生总人数为:%d\n",*n);
}
void Exit()//退出程序;
{
char c;
printf("你即将退出学生信息系统,请确认你的选择:\n");
printf("====Y.确认退出\n====N.返回系统\n");
switch(c=getch())
{
case 'y':exit(0);break;
case 'n':break;
default:printf("输入错误!!!\n");
}
getchar();
}
int main(void)//主函数;
{
int c,n=0;
struct node *first=NULL;
first=read(first);
do{
meun();
printf("请选择:");
scanf("%d",&c);
switch(c)
{
case 1:
{
first=add(first,&n);
write(first);
break;
}
case 2:
{
first=cut(first,&n);
write(first);
break;
}
case 3:
{
print(first);
break;
}
case 4:
{
search(first);
break;
}
case 5:
{
/*first=sort(first);*/
break;
}
case 6:
{
print_n(&n);
break;
}
case 7:
{
first=update(first);
break;
}
case 8:
{
Exit();
break;
}
default:
{
printf("输入错误!!!\n");
}
}
printf("按任意键继续!!!\n");
getch();
system("cls");
}while(1);
return 0;
}