哈工大mooc编程题目——学生信息管理系统3.0(被格式逼疯的我,代码200行不到)
思路:
根据题意模拟就行了,一定要复制粘贴题上的输出,我差点被逼疯。还要利用qsort进行结构体排序
代码:
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
#define MAX_LEN 10 /* 字符串最大长度 */
#define STU_NUM 30
typedef long L; /* 最多的学生人数 */
int n;
float sum ,aver;
char menu[19][100]={{"1.Input record"},//输入信息
{"2.Caculate total and average score of course"},//计算总分和平均分
{"3.Sort in descending order by score"},//按成绩由高到低排出名次表;
{"4.Sort in ascending order by score"},//按成绩由低到高排出名次表;
{"5.Sort in ascending order by number"},//按学号由小到大排出成绩表;
{"6.Sort in dictionary order by name"},//按姓名的字典顺序排出成绩表;
{"7.Search by number"},//按学号查询学生排名及其考试成绩;
{"8.Search by name"},//按姓名查询学生排名及其考试成绩;
{"9.Statistic analysis"},//按优秀(90~100)、良好(80~89)、中等(70~79)、
//及格(60~69)、不及格(0~59)5个类别,统计每个类别的人数以及所占的百分比;
{"10.List record"},//输出每个学生的学号、姓名、考试成绩。
{"0.Exit"}//退出菜单
};
typedef struct Studen
{
L id;
char name[MAX_LEN+2] ;
float score;
}STU;
STU stu[STU_NUM+2];
int comp1(const void *p1, const void *p2)
{
STU *c = (STU*)p1;
STU *d = (STU*)p2;
return d->score - c->score;//从高到低
}
int comp2(const void *p1,const void *p2)
{
STU *a = (STU*)p1;
STU *b = (STU*)p2;
return a->score - b->score;//从低到高
}
int comp3(const void *p1,const void *p2)
{
STU *a = (STU*)p1;
STU *b = (STU*)p2;
return a->id - b->id;
}
int comp4(const void *p1,const void *p2 )
{
STU *a = (STU*)p1;
STU *b = (STU*)p2;
return strcmp(a->name,b->name);
}
void Print(STU stu0[])//输出函数
{
for(int i=0;i<n;i++)
{
printf("%ld\t%s\t%.0f\n",stu0[i].id,stu0[i].name,stu0[i].score);
}
}
void Menuprint()//菜单输出
{
printf("Management for Students' scores\n");
for(int i=0;i<11;i++) printf("%s\n",menu[i]);
printf("Please Input your choice:\n");
}
void Input()//输入函数
{
printf("Input student's ID, name and score:\n");
for(int i=0;i<n;i++)
{
scanf("%ld%s%f",&stu[i].id,stu[i].name,&stu[i].score);
sum+=stu[i].score;
}
}
void Caculate()//计算总分和平均分
{
printf("sum=%.0f,aver=%.2f\n",sum,sum/n);
}
void Sort1( STU *stu1)//按成绩从高到低
{
printf("Sort in descending order by score:\n");
qsort(stu1,n,sizeof(STU),comp1);
Print(stu1);
}
void Sort2(STU *stu2)//c从低到高
{
printf("Sort in ascending order by score:\n");
qsort(stu2,n,sizeof(STU),comp2);
Print(stu2);
}
void Sort3(STU *stu3)
{
printf("Sort in ascending order by number:\n");
qsort(stu3,n,sizeof(STU),comp3);
Print(stu3);
}
void Sort4(STU *stu4)
{
printf("Sort in dictionary order by name:\n");
qsort(stu4,n,sizeof(STU),comp4);
Print(stu4);
}
void Search1()
{
printf("Input the number you want to search:\n");
L ID;
scanf("%ld",&ID);
for(int i=0;i<n;i++)
{
if(stu[i].id==ID)
{
printf("%ld\t%s\t%.0f\n",stu[i].id,stu[i].name,stu[i].score);
return ;
}
}
printf("Not found!\n");
}
void Search2()
{
printf("Input the name you want to search:\n");
char name[MAX_LEN+1];
scanf("%s",name);
for(int i=0;i<n;i++)
{
if(!strcmp(stu[i].name,name))
{
printf("%ld\t%s\t%.0f\n",stu[i].id,stu[i].name,stu[i].score);
return ;
}
}
printf("Not found!\n");
}
void Analysis()
{
int rate[10]={0};
for(int i=0;i<n;i++)
{
float g = stu[i].score;
if(g<60) rate[0]++;
if(g>=60&&g<70) rate[1]++;
if(g>=70&&g<80) rate[2]++;
if(g>=80&&g<90) rate[3]++;
if(g>=90&&g<100) rate[4]++;
if(g==100) rate[5]++;
}
printf("<60\t%d\t%.2f%%\n",rate[0],100.0*rate[0]/n);
printf("%d-%d\t%d\t%.2f%%\n",60,69,rate[1],100.0*rate[1]/n);
printf("%d-%d\t%d\t%.2f%%\n",70,79,rate[2],100.0*rate[2]/n);
printf("%d-%d\t%d\t%.2f%%\n",80,89,rate[3],100.0*rate[3]/n);
printf("%d-%d\t%d\t%.2f%%\n",90,99,rate[4],100.0*rate[4]/n);
printf("%d\t%d\t%.2f%%\n",100,rate[5],100.0*rate[5]/n);
}
int main()
{
int num;
printf("Input student number(n<30):\n");
scanf("%d",&n);
while(1)
{
Menuprint();
scanf("%d",&num);
switch(num)
{
case 0: break;
case 1: Input();break;
case 2: Caculate();break;
case 3: Sort1(stu); break;
case 4: Sort2(stu);break;
case 5: Sort3(stu);break;
case 6: Sort4(stu);break;
case 7: Search1();break;
case 8: Search2();break;
case 9: Analysis();break;
case 10: Print(stu);break;
default : printf("Input error!\n"); break;
}
if(!num) break;
}
printf("End of program!");
return 0;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 25岁的心里话