#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct student
{
double NO;
char name[20];
double math;
double English;
double C;
double Average;
};
struct student Sinformation[1000];
int format() //格式函数
printf("
");
printf("^_^请根据目录继续选择您想要进行的操作:
");
printf(" **********学生管理系统**********
");
printf(" *********【0】 退出程序*********
");
printf(" *********【1】 添加学生*********
");
printf(" *********【2】 显示学生*********
");
printf(" *********【3】 修改学生*********
");
printf(" *********【4】 删除学生*********
");
printf(" *********【5】 成绩排序*********
");
printf(" *********【6】 查找学生*********
");
printf(" *********【7】 清空数据*********
");
printf("选择操作:");
}
int add() //添加学生
{
system("cls");//清屏
int number,n=1,i=0;
FILE *pt;
while(n==1)
{
printf("请输入学生的学号:
");
scanf("%lf",&Sinformation[i].NO) ;
printf("请输入学生的名字:
");
scanf("%s",Sinformation[i].name) ;
printf("请输入学生的高数成绩:
");
scanf("%lf",&Sinformation[i].math);
while(!(Sinformation[i].math>=0&&Sinformation[i].math<=100))
{
printf("输入错误!请重新输入:
");
scanf("%lf",&Sinformation[i].math);
}
printf("请输入学生的英语成绩:
");
scanf("%lf",&Sinformation[i].English) ;
while(!(Sinformation[i].English>=0&&Sinformation[i].English<=100))
{
printf("输入错误!请重新输入:
");
scanf("%lf",&Sinformation[i].English) ;
}
printf("请输入学生的C语言成绩:
");
scanf("%lf",&Sinformation[i].C) ;
while(!(Sinformation[i].C>=0&&Sinformation[i].C<=100))
{
printf("输入错误!请重新输入:
");
scanf("%lf",&Sinformation[i].C) ;
}
pt=fopen("学生管理系统文件.txt","a+");
fprintf(pt,"%.0lf ",Sinformation[i].NO);
fprintf(pt,"%s ",Sinformation[i].name);
fprintf(pt,"%.2lf ",Sinformation[i].math);
fprintf(pt,"%.2lf ",Sinformation[i].English);
fprintf(pt,"%.2lf ",Sinformation[i].C);
fclose(pt);
i++;
FILE *pa;
pa=fopen("计数文件.txt","r+");
fscanf(pa,"%d",&number);
fclose(pa);
number++;
pa=fopen("计数文件.txt","w");
fclose(pa);
pa=fopen("计数文件.txt","r+");
fprintf(pa,"%d",number);
fclose(pa);
printf("如果要继续输入请按1,结束输入请按0:");
scanf("%d",&n);
}
format();
}
int display() //显示学生
{
system("cls"); //清屏
printf("学生信息如下:
");
printf("------------------------------------------------
");
int number,i;
FILE *pa;
pa=fopen("计数文件.txt","r+");
fscanf(pa,"%d",&number);
fclose(pa);
FILE *pb;
pb=fopen("学生管理系统文件.txt","r+");
printf("学号 姓名 高数成绩 英语成绩 C语言成绩
");
for(i=0;i<number;i++)
{
fscanf(pb,"%lf",&Sinformation[i].NO);
fscanf(pb,"%s",Sinformation[i].name);
fscanf(pb,"%lf",&Sinformation[i].math);
fscanf(pb,"%lf",&Sinformation[i].English);
fscanf(pb,"%lf",&Sinformation[i].C);
}
fclose(pb);
for(i=0;i<number;i++)
{
printf("%.0lf ",Sinformation[i].NO);
printf("%s ",Sinformation[i].name);
printf("%.2lf ",Sinformation[i].math);
printf("%.2lf ",Sinformation[i].English);
printf("%.2lf",Sinformation[i].C);
printf("
");
}
format();
}
//修改学生
int modify()
{
system("cls");//清屏
printf("
");
int number,n=1,i,m=-1,k;
double change;
double NO,math,English,C;
printf("请问有必要显示所有的学生信息吗? 如果有必要请按1,否则请按0:");
scanf("%d",&k);
if(k==1)
display();
FILE *pa;
pa=fopen("计数文件.txt","r+");
fscanf(pa,"%d",&number);
fclose(pa);
FILE *pb;
pb=fopen("学生管理系统文件.txt","r+");
for(i=0;i<number;i++)
{
fscanf(pb,"%lf",&Sinformation[i].NO);
fscanf(pb,"%s",Sinformation[i].name);
fscanf(pb,"%lf",&Sinformation[i].math);
fscanf(pb,"%lf",&Sinformation[i].English);
fscanf(pb,"%lf",&Sinformation[i].C);
}
fclose(pb);
while(n==1)
{
printf("请输入要修改学生的学号
");
scanf("%lf",&change);
for(i=0;i<number;i++)
if(Sinformation[i].NO==change)
{
m=i;
break;
}
if(m==-1)
printf("您输入的数据有误!文件中没有此学生的信息!
");
if(m==-1)
break;
int z;
printf("是否要修改学号,是输入1,否输入0
");
scanf("%d",&z);
if(z==1)
{
printf("请输入修改后的学号
");
scanf("%lf",&NO);
Sinformation[m].NO=NO;
}
printf("是否要修改姓名,是输入1,否输入0
");
scanf("%d",&z);
if(z==1){
printf("请输入修改后的姓名
");
scanf("%s",Sinformation[m].name);
}
printf("是否要修改高数成绩,是输入1,否输入0
");
scanf("%d",&z);
if(z==1)
{
printf("请输入修改后学生的高数成绩
");
scanf("%lf",&math);
while(!(math>=0&&math<=100))
{
printf("输入错误!请重新输入:
");
scanf("%lf",&math);
}
Sinformation[m].math=math;
}
printf("是否要修改英语成绩,是输入1,否输入0
");
scanf("%d",&z);
if(z==1)
{
printf("请输入修改后学生的英语成绩
");
scanf("%lf",&English);
while(!(English>=0&&English<=100))
{
printf("输入错误!请重新输入:
");
scanf("%lf",&English);
}
Sinformation[m].English=English;
}
printf("是否要修改C成绩,是输入1,否输入0
");
scanf("%d",&z);
if(z==1)
{
printf("请输入修改后学生的C语言成绩
");
scanf("%lf",&C);
while(!(C>=0&&C<=100))
{
printf("输入错误!请重新输入:
");
scanf("%lf",&C);
}
Sinformation[m].C=C;
}
printf("如果继续修改请按1,否则按0
");
scanf("%d",&n);
}
FILE *pc;
pc=fopen("学生管理系统文件.txt","w");
fclose(pc);
pc=fopen("学生管理系统文件.txt","r+");
for(i=0;i<number;i++)
{
fprintf(pc,"%.0lf ",Sinformation[i].NO);
fprintf(pc,"%s ",Sinformation[i].name);
fprintf(pc,"%.2lf ",Sinformation[i].math);
fprintf(pc,"%.2lf ",Sinformation[i].English);
fprintf(pc,"%.2lf ",Sinformation[i].C);
}
fclose(pc);
format();
}
//删除学生
int Delete()
{
system("cls");//清屏
display();
int m=-1,i,number,n=1;
double except;
int de=0;
FILE *pa;
pa=fopen("计数文件.txt","r+");
fscanf(pa,"%d",&number);
fclose(pa);
FILE *pb;
pb=fopen("学生管理系统文件.txt","r+");
for(i=0;i<number;i++)
{
fscanf(pb,"%lf",&Sinformation[i].NO);
fscanf(pb,"%s",Sinformation[i].name);
fscanf(pb,"%lf",&Sinformation[i].math);
fscanf(pb,"%lf",&Sinformation[i].English);
fscanf(pb,"%lf",&Sinformation[i].C);
}
fclose(pb);
while(n==1)
{
printf("请输入要删除学生的学号
");
scanf("%lf",&except);
for(i=0;i<number;i++)
if(Sinformation[i].NO==except)
{
m=i;
break;
}
if(m==-1)
printf("您输入的数据有误!文件中没有此学生的信息!
");
if(m==-1)
break;
printf("您要删除的学生信息如下:
");
printf("学号 姓名 高数成绩 英语成绩 C语言成绩
");
printf("%.0lf ",Sinformation[m].NO);
printf("%s ",Sinformation[m].name);
printf("%.2lf ",Sinformation[m].math);
printf("%.2lf ",Sinformation[m].English);
printf("%.2lf",Sinformation[m].C);
printf("
");
de++;//未删除计数做准备 ,删几次,就有几个
for(i=m;i<number;i++)
{
Sinformation[i].NO=Sinformation[i+1].NO;
strcpy(Sinformation[i+1].name,Sinformation[i].name);
Sinformation[i].math=Sinformation[i+1].math;
Sinformation[i].English=Sinformation[i+1].English;
Sinformation[i].C=Sinformation[i+1].C;
}
printf("如果要继续删除其他学生信息请按1,否则按0:");
scanf("%d",&n);
}
FILE *pd;
pd=fopen("计数文件.txt","r+");
fscanf(pd,"%d",&number);
fclose(pd);
pd=fopen("计数文件.txt","w");
fclose;
number=number-de;
pd=fopen("计数文件.txt","r+");
fprintf(pd,"%d",number);
fclose(pd);
FILE *pc;
pc=fopen("学生管理系统文件.txt","w");
fclose(pc);
pc=fopen("学生管理系统文件.txt","r+");
for(i=0;i<number;i++)
{
fprintf(pc,"%.0lf ",Sinformation[i].NO);
fprintf(pc,"%s ",Sinformation[i].name);
fprintf(pc,"%.2lf ",Sinformation[i].math);
fprintf(pc,"%.2lf ",Sinformation[i].English);
fprintf(pc,"%.2lf ",Sinformation[i].C);
}
fclose(pc);
if(m!=-1)
printf(" 学生信息删除成功
");
format();
}
//成绩排序
int sort()
{
system("cls");//清屏
char middle[20]={' '};
int temp;
int number,i,y,z;
FILE *pa;
pa=fopen("计数文件.txt","r+");
fscanf(pa,"%d",&number);
fclose(pa);
FILE *pb;
pb=fopen("学生管理系统文件.txt","r+");
for(i=0;i<number;i++)
{
fscanf(pb,"%lf",&Sinformation[i].NO);
fscanf(pb,"%s",Sinformation[i].name);
fscanf(pb,"%lf",&Sinformation[i].math);
fscanf(pb,"%lf",&Sinformation[i].English);
fscanf(pb,"%lf",&Sinformation[i].C);
}
fclose(pb);
for(i=0;i<number;i++)
Sinformation[i].Average=(Sinformation[i].math+Sinformation[i].English+Sinformation[i].C)/3.0;
printf(" 排序前的成绩为:
");
printf("学号 姓名 高数成绩 英语成绩 C语言成绩 平均成绩
");
for(i=0;i<number;i++)
{
printf("%.0lf ",Sinformation[i].NO);
printf("%s ",Sinformation[i].name);
printf("%.2lf ",Sinformation[i].math);
printf("%.2lf ",Sinformation[i].English);
printf("%.2lf ",Sinformation[i].C);
printf("%.2lf",Sinformation[i].Average);
printf("
");
}
for(y=0;y<number-1;y++)
for(z=0;z<number-y-1;z++)
if(Sinformation[z].Average<Sinformation[z+1].Average) //排序
{
temp=Sinformation[z].NO;
Sinformation[z].NO=Sinformation[z+1].NO;
Sinformation[z+1].NO=temp;
strcpy(middle,Sinformation[z].name);
strcpy(Sinformation[z].name,Sinformation[z+1].name);
strcpy(Sinformation[z+1].name,middle);
temp=Sinformation[z].math;
Sinformation[z].math=Sinformation[z+1].math;
Sinformation[z+1].math=temp;
temp=Sinformation[z].English;
Sinformation[z].English=Sinformation[z+1].English;
Sinformation[z+1].English=temp;
temp=Sinformation[z].C;
Sinformation[z].C=Sinformation[z+1].C;
Sinformation[z+1].C=temp;
temp=Sinformation[z].Average;
Sinformation[z].Average=Sinformation[z+1].Average;
Sinformation[z+1].Average=temp;
}
printf("
");
printf(" 排序后的成绩为:
");
printf("学号 姓名 高数成绩 英语成绩 C语言成绩 平均成绩 排名
");
for(i=0;i<number;i++)
{
printf("%.0lf ",Sinformation[i].NO);
printf("%s ",Sinformation[i].name);
printf("%.2lf ",Sinformation[i].math);
printf("%.2lf ",Sinformation[i].English);
printf("%.2lf ",Sinformation[i].C);
printf("%.2lf ",Sinformation[i].Average);
printf("%d",i+1);
printf("
");
}
format();
}
//查找学生
int search()
{
system("cls");//清屏
int number,i,m,n=1;
double fate;
FILE *pa;
pa=fopen("计数文件.txt","r+");
fscanf(pa,"%d",&number);
fclose(pa);
FILE *pb;
pb=fopen("学生管理系统文件.txt","r+");
for(i=0;i<number;i++)
{
fscanf(pb,"%lf",&Sinformation[i].NO);
fscanf(pb,"%s",Sinformation[i].name);
fscanf(pb,"%lf",&Sinformation[i].math);
fscanf(pb,"%lf",&Sinformation[i].English);
fscanf(pb,"%lf",&Sinformation[i].C);
}
fclose(pb);
while(n==1)
{
m=-1;
printf("请输入要查找学生的学号:
");
scanf("%lf",&fate);
for(i=0;i<number;i++)
if(Sinformation[i].NO==fate)
{
m=i;
break;
}
else if(i==number-1)
{
printf(" ⊙﹏⊙您输入的数据有误,文件中没有此同学的资料! ! !
");
}
if(m!=-1)
{
printf("学号 姓名 高数成绩 英语成绩 C语言成绩
");
printf("%.0lf ",Sinformation[m].NO);
printf("%s ",Sinformation[m].name);
printf("%.2lf ",Sinformation[m].math);
printf("%.2lf ",Sinformation[m].English);
printf("%.2lf ",Sinformation[m].C);
printf("
");
printf("
");
}
printf("请问还要继续查找吗?如果是请按1,否则按0:");
scanf("%d",&n);
}
format();
}
//清空数据
int empty()
{
char confirm[20];
printf("您进行的操作将会将相关文件格式化:
");
printf("如果确定格式化,请输入:确定操作
") ;
printf("否则输入其他字符结束本次操作:") ;
scanf("%s",confirm);
if(strcmp(confirm,"确定操作")==0)
{
FILE *pa;
pa=fopen("学生管理系统文件.txt","w");
fclose(pa) ;
FILE *pb;
pb=fopen("计数文件.txt","w");
fclose(pb);
printf("删除完成!
");
}
else
printf("操作已结束,未进行文件格式化!
");
}
int main()
{
printf(" **********学生管理系统**********
");
printf(" *********【0】 退出程序*********
");
printf(" *********【1】 添加学生*********
");
printf(" *********【2】 显示学生*********
");
printf(" *********【3】 修改学生*********
");
printf(" *********【4】 删除学生*********
");
printf(" *********【5】 成绩排序*********
");
printf(" *********【6】 查找学生*********
");
printf(" *********【7】 清空数据*********
");
printf("假如是第一次操作本系统,请先选择功能1添加学生
");
printf("选择操作:");
int n;
while(scanf("%d",&n)!=EOF)
{
switch(n){
case 0:
return 0;
case 1:
add(); //添加函数
break;
case 2:
display(); //显示函数
break;
case 3:
modify(); // 修改函数
break;
case 4:
Delete(); //删除函数
break;
case 5:
sort(); //排序函数
break;
case 6:
search(); //查找函数
break;
case 7:
empty(); //清空函数
break;
default:
break;
}
}
return 0;
}