人力资源管理师
报考指南考试报名准考证打印成绩查询考试题库

重置密码成功

请谨慎保管和记忆你的密码,以免泄露和丢失

注册成功

请谨慎保管和记忆你的密码,以免泄露和丢失

当前位置:首页人力资源管理师考试问答正文
用C语言编写学生成绩管理系统 用C语言编写学生成绩管理系统
题目nbsp学生成绩管理要求在一个x.txt文件里存放10个学生的信息内容自定姓名学号成绩长度为5的数组按学生的成绩排序,将4们课以上不及格的学生删除,重新存入x1.txt文件。
bianningzui1回答 · 5778人浏览
最佳答案
用户头像
bengjunshu 新兵答主 01-24 TA获得超过2211个赞
我有一个功能基本上和这差不多。我贴给你吧。
#include
#include
#include
#include
#include
struct student
{
char name[21];
char number[21];
char specialty[21];
char sex[3];
int c_score;
int english_score;
int math_score;
int total_score;
};
typedef struct student student;
student studentArray[100];
char putout[9][21]={"姓名","学号","专业","性别","c语言","英语","数学","总成绩","名次"}; //为格式化输出做准备
int count=0; //用来记录学生的记录个数
//函数声明部分
void initialStudent();
void addStudent();
int judge(int );
void delstudent();
void modifyStudent();
void searchStudent();
void studentProfile();
void getaveragescore(int [],int ,double *);
void setOrder(int );
void selectSort(char [][21],int [],int ,int ,int ,char [][21]);
void printUnpassed();
void printAll();
int menuselect();
void clear();
//创建e:\\student.txt文件,并初始化,同时计算出文件中含有的记录总数
void initial()
{
FILE *fp;
char choice=''y'';
int i=0;
fp=fopen("e:\\student.txt","r");
if(!fp)
{
printf("创建文件失败,即将返回");
return ;
}
for(;fread(&studentArray[i],sizeof(struct student),1,fp)!=0;i++);
count=i;
}
//添加学生信息
void addStudent()
{
FILE *fp;
char choice=''y'';
int i=0;
//int sum;
fp=fopen("e:\\student.txt","ab");
if(!fp)
{
printf("创建文件失败,即将返回");
return ;
}
while(choice==''y'')
{
printf("输入学生姓名,学号,专业,性别,中间以空格符隔开");
fflush(stdin);
scanf("%s%s%s%s",studentArray[i].name,studentArray[i].number,studentArray[i].specialty,studentArray[i].sex);
printf("输入学生c语言分数,英语分数,数学分数");
scanf("%d%d%d",&studentArray[i].c_score,&studentArray[i].english_score,&studentArray[i].math_score);
//sum=judge(studentArray[i].c_score)+judge(studentArray[i].english_score)+judge(studentArray[i].math_score);
while(!judge(studentArray[i].c_score)||!judge(studentArray[i].english_score)||!judge(studentArray[i].math_score))
{
printf("成绩输入错误,请重新输入");
scanf("%d%d%d",&studentArray[i].c_score,&studentArray[i].english_score,&studentArray[i].math_score);
//sum=judge(studentArray[i].c_score)+judge(studentArray[i].english_score)+judge(studentArray[i].math_score);
}
studentArray[i].total_score=studentArray[i].c_score+studentArray[i].english_score+studentArray[i].math_score;
fwrite(&studentArray[i],sizeof(struct student),1,fp); //将数据写入文件中
i++;
count++;
printf("是否继续输入,继续输入输入y或者Y,其它值退出");
fflush(stdin);
choice=getchar();
choice=tolower(choice);
fflush(stdin); //清空缓存输入流
}
fclose(fp);
printf("输入任意键继续");
getch();
//return ;
}
//判断输入成绩是否合法
int judge(int score)
{
if(score<0||score>100)
return 0;
else if(score<60) //只是为了计算后面的不及格学生相关信息
return 4;
else
return 1;
}
//删除学生信息
void delStudent()
{
FILE *fp,*fp2;
int i;
char choice;
int flag1=1;
int flag2=0;
int found=0; //用来标记文件中是否含有此人
char studentName[21];
char studentNumber[21];
fp=fopen("e:\\student.txt","rb");
if(count==0)
{
printf("文件中没有信息,即将退出");
//getch();
return ;
}

fp2=fopen("e:\ est.txt","wb");
if(!fp2)
{
printf("文件创建失败,即将退出");
return;
}
i=0;
found=0;
printf("按照姓名删除请输入0,按照学号删除请输入1");
scanf("%d",&flag2);
fflush(stdin);
if(flag2==0)
{
printf("输入要删除的学生的姓名,按回车键结束");
scanf("%s",studentName);
for(i=0;fread(&studentArray[i],sizeof(struct student),1,fp)!=0;i++) //将其他人信息转移到test.txt文件中
if(strcmp(studentArray[i].name,studentName))
fwrite(&studentArray[i],sizeof(struct student),1,fp2);
else
{
found=1;
}
fclose(fp);
fclose(fp2);
fflush(stdin);
if(found==1)
printf("是否确认删除姓名为%s该学生信息,确认删除,输入y或者Y:",studentName);
else
{
printf("没有此人信息,即将退出");
return ;
}
choice=getchar();
if(choice==''y''||choice==''Y'') //将文件结构指针重新定向
{
count--;
fp=fopen("e:\\student.txt","wb");
fp2=fopen("e:\ est.txt","rb");
for(i=0;fread(&studentArray[i],sizeof(struct student),1,fp2)!=0;i++)
fwrite(&studentArray[i],sizeof(struct student),1,fp);
fclose(fp);
fclose(fp2);
}
}
else if(flag2==1)
{
printf("输入要删除的学生的学号,按回车键结束");
scanf("%s",studentNumber);
for(i=0;fread(&studentArray[i],sizeof(struct student),1,fp)!=0;i++) //将其他人信息转移到test.txt文件中
if(strcmp(studentArray[i].number,studentNumber))
fwrite(&studentArray[i],sizeof(struct student),1,fp2);
else
found=1;
fclose(fp);
fclose(fp2);
fflush(stdin);
if(found==1)
printf("是否确认删除学号为%s的该学生信息,确认删除,输入y或者Y:",studentNumber);
else
{
printf("没有此人信息,即将退出");
return;
}
choice=getchar();
if(choice==''y''||choice==''Y'') //将文件结构指针重新定向
{
count--;
fp=fopen("e:\\student.txt","wb");
fp2=fopen("e:\ est.txt","rb");
for(i=0;fread(&studentArray[i],sizeof(struct student),1,fp2)!=0;i++)
fwrite(&studentArray[i],sizeof(struct student),1,fp);
fclose(fp);
fclose(fp2);
}
}
else
printf("输入错误");
}
//修改学生信息
void modifyStudent()
{
FILE *fp,*fp2;
int flag=0;
int i;
char studentName[21];
student newdata;
fp=fopen("e:\\student.txt","rb");
if(count==0)
{
printf("文件中还没有学生信息,即将退出");
return ;
}
fp2=fopen("e:\ est.txt","wb");
if(!fp2)
{
printf("文件创建失败,即将返回");
return ;
}
printf("输入要修改的学生的姓名");
fflush(stdin);
scanf("%s",studentName);
for(i=0;fread(&studentArray[i],sizeof(struct student),1,fp)!=0;i++)
{
if(!strcmp(studentArray[i].name,studentName))
{
flag=1;
printf("输入要修改后学生的姓名,学号,专业,性别");
fflush(stdin);
scanf("%s%s%s%s",newdata.name,newdata.number,newdata.specialty,newdata.sex);
printf("输入修改后的学生的c语言分数,英语分数,数学分数");
scanf("%d%d%d",&newdata.c_score,&newdata.english_score,&newdata.math_score);
while(!judge(studentArray[i].c_score)||!judge(studentArray[i].english_score)||!judge(studentArray[i].math_score))
{
printf("输入不合法,请重新输入");
scanf("%d%d%d",&newdata.c_score,&newdata.english_score,&newdata.math_score);
}
newdata.total_score=newdata.c_score+newdata.english_score+newdata.math_score;
fwrite(&newdata,sizeof(struct student),1,fp2);
}
else
fwrite(&studentArray[i],sizeof(struct student),1,fp2);
}
if(flag==0)
{
printf("没有此人,即将退出");
return ;
}
fclose(fp);
fclose(fp2);
fp=fopen("e:\\student.txt","wb");
fp2=fopen("e:\ est.txt","rb");
for(i=0;fread(&studentArray[i],sizeof(struct student),1,fp2)!=0;i++)
fwrite(&studentArray[i],sizeof(struct student),1,fp);
printf("");
fclose(fp);
fclose(fp2);
}
//查找某学生信息
void searchStudent()
{
FILE *fp;
char studentName[21];
int flag=0;
int i;
fp=fopen("e:\\student.txt","rb");
if(count==0)
{
printf("文件中还没有学生记录,即将退出");
return ;
}
printf("输入你要查找的学生姓名");
fflush(stdin);
scanf("%s",studentName);
for(i=0;fread(&studentArray[i],sizeof(struct student),1,fp)!=0;i++)
{
if(strcmp(studentName,studentArray[i].name)==0)
{
flag=1;
printf("要查找的学生相关信息如下:");
printf("%-10s%-15s%-20s%-5s",putout[0],putout[1],putout[2],putout[3]);
printf("%-6s%-6s%-6s%-6s", putout[4],putout[5],putout[6],putout[7]);
printf("%-10s%-15s%-20s",studentArray[i].name,studentArray[i].number,studentArray[i].specialty);
printf("%-5s",studentArray[i].sex);
printf("%-6d%-6d",studentArray[i].c_score,studentArray[i].english_score);
printf("%-6d%-6d",studentArray[i].math_score,studentArray[i].total_score);
printf("");
printf("");
}
}
if(flag==0)
{
printf("没有找到姓名为%s的学生",studentName);
fflush(stdout);
printf("");
return ;
}
}
//学生信息总体概况,如平均分,总人数
void studentProfile()
{
FILE *fp;
int i;
int cArray[100]; //用来存储c语言分数
double cAverage; //用来存储c语言平均成绩
int englishArray[100];
double englishAverage;
int mathArray[100];
double mathAverage;
int totalArray[100];
double totalAverage;
fp=fopen("e:\\student.txt","rb");
if(count==0)
{
printf("没有学生信息,即将退出");
return ;
}
for(i=0;fread(&studentArray[i],sizeof(struct student),1,fp)!=0;i++)
{
cArray[i]=studentArray[i].c_score;
englishArray[i]=studentArray[i].english_score;
mathArray[i]=studentArray[i].math_score;
totalArray[i]=studentArray[i].total_score;
}
printf("共有%d个学生信息",count);
getaveragescore(cArray,i,&cAverage);
getaveragescore(englishArray,i,&englishAverage);
getaveragescore(mathArray,i,&mathAverage);
getaveragescore(totalArray,i,&totalAverage);
printf("c语言平均成绩为%.2f分",cAverage);
printf("英语平均成绩为%.2f分",englishAverage);
printf("数学平均成绩为%.2f分",mathAverage);
printf("总分平均成绩为%.2f分",totalAverage);
printf("");
}
//统计平均分
void getaveragescore(int arrayscore[],int n,double *averagescore)
{
double sum=0;
int i=0;
for(;i sum+=arrayscore[i];
*averagescore=sum/n;
}
//有序输出学生的成绩
void setOrder(int flag) //flag表示选择成绩是从低到高还是从高到底排序,0从低到高,1则反之
{

FILE *fp;
int i,j;
student temp;
int choice;
//flag=0;
fp=fopen("e:\\student.txt","rb");
if(count==0)
{
printf("没有学生信息,即将退出");
return ;
}
for(i=0;fread(&studentArray[i],sizeof(struct student),1,fp)!=0;i++); //将数据读入结构数组中
for(choice =1;choice<5;choice++)
switch(choice)
{
case 1:printf("c语言成绩:");
for(i=0;i for(j=i+1;j if(studentArray[i].c_score>studentArray[j].c_score)
{
temp=studentArray[i];
studentArray[i]=studentArray[j];
studentArray[j]=temp;
}
if(!flag)
{
printf("%-10s%-10s%-15s%-10s",putout[8],putout[0],putout[1],putout[4]);
for(i=0;i printf("%-10d%-10s%-15s%-10d",count-i,studentArray[i].name,studentArray[i].number,studentArray[i].c_score);
}

else
{
printf("%-10s%-10s%-15s%-10s",putout[8],putout[0],putout[1],putout[4]);
for(i=count-1;i>=0;i--)
printf("%-10d%-10s%-15s%-10d",count-i,studentArray[i].name,studentArray[i].number,studentArray[i].c_score);
}
break;
case 2:printf("英语成绩:");
for(i=0;i for(j=i+1;j if(studentArray[i].english_score>studentArray[j].english_score)
{
temp=studentArray[i];
studentArray[i]=studentArray[j];
studentArray[j]=temp;
}
if(!flag)
{
printf("%-10s%-10s%-15s%-10s",putout[8],putout[0],putout[1],putout[5]);
for(i=0;i printf("%-10d%-10s%-15s%-10d",count-i,studentArray[i].name,studentArray[i].number,studentArray[i].english_score);
}
else
{
printf("%-10s%-10s%-15s%-10s",putout[8],putout[0],putout[1],putout[5]);
for(i=count-1;i>=0;i--)
printf("%-10d%-10s%-15s%-10d",count-i,studentArray[i].name,studentArray[i].number,studentArray[i].english_score);
}
break;
case 3:printf("数学成绩:");
for(i=0;i for(j=i+1;j if(studentArray[i].math_score>studentArray[j].math_score)
{
temp=studentArray[i];
studentArray[i]=studentArray[j];
studentArray[j]=temp;
}
if(!flag)
{
printf("%-10s%-10s%-15s%-10s",putout[8],putout[0],putout[1],putout[6]);
for(i=0;i printf("%-10d%-10s%-15s%-10d",count-i,studentArray[i].name,studentArray[i].number,studentArray[i].math_score);
}
else
{
printf("%-10s%-10s%-15s%-10s",putout[8],putout[0],putout[1],putout[6]);
for(i=count-1;i>=0;i--)
printf("%-10d%-10s%-15s%-10d",count-i,studentArray[i].name,studentArray[i].number,studentArray[i].math_score);
}
break;
case 4:printf("总成绩:");
for(i=0;i for(j=i+1;j if(studentArray[i].total_score>studentArray[j].total_score)
{
temp=studentArray[i];
studentArray[i]=studentArray[j];
studentArray[j]=temp;
}
if(!flag)
{
printf("%-10s%-10s%-15s%-10s",putout[8],putout[0],putout[1],putout[7]);
for(i=0;i printf("%-10d%-10s%-15s%-10d",count-i,studentArray[i].name,studentArray[i].number,studentArray[i].total_score);
}
else
{
printf("%-10s%-10s%-15s%-10s",putout[8],putout[0],putout[1],putout[7]);
for(i=count-1;i>=0;i--)
printf("%-10d%-10s%-15s%-10d",count-i,studentArray[i].name,studentArray[i].number,studentArray[i].total_score);
}
break;
return ;
}
}
//输出不及格学生的相关信息
void printUnpassed()
{
FILE *fp;
int i;
int total=0;
fp=fopen("e:\\student.txt","rb");
if(count==0)
{
printf("没有学生信息,即将退出");
return ;
}
printf("不及格学生相关信息如下所示");
printf("%-10s%-15s%-20s%-5s",putout[0],putout[1],putout[2],putout[3]);
printf("%-6s%-6s%-6s%-6s", putout[4],putout[5],putout[6],putout[7]);
for(i=0;fread(&studentArray[i],sizeof(struct student),1,fp)!=0;i++)
if(judge(studentArray[i].c_score)+judge(studentArray[i].english_score)+judge(studentArray[i].math_score)>3)
{
printf("%-10s%-15s%-20s",studentArray[i].name,studentArray[i].number,studentArray[i].specialty);
printf("%-5s",studentArray[i].sex);
printf("%-6d%-6d",studentArray[i].c_score,studentArray[i].english_score);
printf("%-6d%-6d",studentArray[i].math_score,studentArray[i].total_score);
printf("");
total++;
}
printf("不及格学生人数共有%d人",total);
return ;

}
//输出文件中所有学生信息记录
void printAll()
{
FILE *fp;
int i;
//student buffer;
fp=fopen("e:\\student.txt","rb");
if(count==0)
{
printf("现在还没有学生记录,即将退出");
printf("");
return ;
}
printf("该文件中一共有%d个学生记录,记录如下:",count);
printf("%-10s%-15s%-20s%-5s",putout[0],putout[1],putout[2],putout[3]);
printf("%-6s%-6s%-6s%-6s", putout[4],putout[5],putout[6],putout[7]);
for(i=0;fread(&studentArray[i],sizeof(struct student),1,fp)!=0;i++)
{
printf("%-10s%-15s%-20s",studentArray[i].name,studentArray[i].number,studentArray[i].specialty);
printf("%-5s",studentArray[i].sex);
printf("%-6d%-6d",studentArray[i].c_score,studentArray[i].english_score);
printf("%-6d%-6d",studentArray[i].math_score,studentArray[i].total_score);
printf("");
}
}
void clear()
{
FILE *fp;
char ch;
printf("你确认删除所有学生记录吗?继续输入y活着Y");
fflush(stdin);
ch=getchar();
if(ch==''y''||ch==''Y'')
{
fp=fopen("e:\\student.txt","wb");
count=0;
}
return ;

}
//主函数
int main()
{
int choice;
printf("**************************欢迎进入学生信息管理系统******************************");
initial();
while(1)
switch(choice=menuselect())
{
case 0:
printf("************************欢迎下次再次使用*********************");
exit(0);
case 1:
addStudent();
break;
case 2:
delStudent();
break;
case 3:
modifyStudent();
break;
case 4:
searchStudent();
break;
case 5:
studentProfile();
break;
case 6:
setOrder(0);
break;
case 7:
setOrder(1);
break;
case 8:
printUnpassed();
break;
case 9:
printAll();
break;
case 10:
system("CLS");
//clrscr();
break;
case 11:
clear();
break;
}
return 0;
}
//菜单函数
int menuselect()
{
int choice;
do
{
printf("0.退出管理系统");
printf("1.添加学生信息记录");
printf("2.删除某一指定学生记录");
printf("3.修改某一给定学生信息");
printf("4.查找某一给定学生信息");
printf("5.输出学生信息总体概况");
printf("6.按照分数从低到高输出学生相关信息");
printf("7.按照分数从高到低输出学生相关信息");
printf("8.输出不及格学生的相关信息");
printf("9.输出文件中所有学生的记录");
printf("10.清屏");
printf("11.删除所有学生记录");
printf("输入你的选择:");
scanf("%d",&choice);
}while(choice<0||choice>11);
return choice;
}

声明:本文内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:service@bkw.cn 进行举报,并提供相关证据,工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。

|
测一测是否符合报考条件
免费测试,不要错过机会
提交
互动交流

微信扫码关注公众号

获取更多考试热门资料

温馨提示

信息提交成功,稍后帮考专业顾问免费为您解答,请保持电话畅通!

我知道了~!
温馨提示

信息提交成功,稍后帮考专业顾问给您发送资料,请保持电话畅通!

我知道了~!

提示

信息提交成功,稍后班主任联系您发送资料,请保持电话畅通!