c语言

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct student{
int id;
char* name;
double score;
};
void inof_print();//首次提示
void ReadInfo(student*);//输入记录
void ScoreAscSort(student*,const int);//按成绩由低到高排序
void ScoreDescSort(student*,const int);//按成绩由高到低排序
void NumAscSort(student*,const int);//按学号由小到大排序
void NameAscSort(student*,const int);//按姓名字典顺序排序
void NumSearch(student*,const int,int id);//按学号查询
void NameSearch(student*,const int,char* name);//按姓名查询
double TotalScore(student*,const int);//计算总分
void StatNumAndPer(student*,const int);//统计各等级人数及比例
void printInfo(student*,const int);//输出记录模块
int cal_how_many_student_in_txt();
int main()
{
student* person=(student*)malloc(20sizeof(student));
int select;
int count;
inof_print();
while(scanf("%d",&select)){
switch(select){
case 1:ReadInfo(person);printf(“共录入%d位学生\n”,cal_how_many_student_in_txt());count=cal_how_many_student_in_txt();
break;
case 2:printf(“总分:%.2lf,平均分:%.2lf\n”,TotalScore(person,count),TotalScore(person,count)/count);
break;
case 3:ScoreDescSort(person,count);printf(“按成绩由高到低排出名次表排序成功!\n”);
break;
case 4:ScoreAscSort(person,count);
break;
case 5:NumAscSort(person,count);
break;
case 6:NameAscSort(person,count);
break;
case 7:int id;printf(“请输入学号:”);scanf("%d",&id);NumSearch(person,count,id);
break;
case 8:char
name=(char*)malloc(10sizeof(char));printf(“请输入姓名:”);scanf("%s",name);NameSearch(person,count,name);
break;
}
if(select9)StatNumAndPer(person,count);
if(select
10)printInfo(person,count);
if(select==0){
printf(“退出学生管理系统成功!”);
break;
}
};
free(person);
return 0;
}
int cal_how_many_student_in_txt(){
FILE fp = NULL;
fp = fopen(“test.txt”, “r”);
int i=0;
char
name=(char
)malloc(10sizeof(char));
while(fgets(name,100,(FILE )fp )!=NULL)
i++;
fclose(fp);
return i;
}
void inof_print(){
printf("\t\t
********************************************************\n");
printf("\t\t欢迎使用********\n");
printf("\t\t**********************************************************");
printf("\n\t\t1.Input record *****");
printf("\n\t\t
2.Caculate total and average score of course ");
printf("\n\t\t3.Sort in descending order by score *****");
printf("\n\t\t
4.Sort in ascending order by score ");
printf("\n\t\t5.Sort in ascending order by number *****");
printf("\n\t\t
6.Sort in dictionary order by name ");
printf("\n\t\t7.Search by number *****");
printf("\n\t\t
8.Search by name ");
printf("\n\t\t9.Statistic analysis *****");
printf("\n\t\t
10.List record ");
printf("\n\t\t0.Exit \n");
printf("\t\t
************************\n");
}
void ScoreAscSort(student* s,const int count){
int k;
for(int i=0;i<count-1;i++){
k=i;
for(int j=i+1;j<count;j++)
if(s[k].score>s[j].score)
k=j;
if(k!=i){
student temp=s[i];
s[i]=s[k];
s[k]=temp;
}
}
printf(“按成绩由低到高排出名次表排序成功!\n”);
}
void ScoreDescSort(student* s,const int count){
int k;
for(int i=0;i<count-1;i++){
k=i;
for(int j=i+1;j<count;j++)
if(s[k].score<s[j].score)
k=j;
if(k!=i){
student temp=s[i];
s[i]=s[k];
s[k]=temp;
}
}
}
void NumAscSort(student* s,const int count){
int k;
for(int i=0;i<count-1;i++){
k=i;
for(int j=i+1;j<count;j++)
if(s[k].id>s[j].id)
k=j;
if(k!=i){
student temp=s[i];
s[i]=s[k];
s[k]=temp;
}
}
printf(“按学号由小到大排出成绩表排序成功\n”);
}
void NameAscSort(student* s,const int count){
int k;
for(int i=0;i<count-1;i++){
k=i;
for(int j=i+1;j<count;j++)
if(strcmp(s[k].name,s[j].name)>0)
k=j;
if(k!=i){
student temp=s[i];
s[i]=s[k];
s[k]=temp;
}
}
printf(“按姓名的字典顺序排出成绩表排序成功\n”);
}
void printInfo(student* person,const int count){
for(int i=0;i<count;i++)
printf("%d %s %.2lf\n",person[i].id,person[i].name,person[i].score);
printf(“总分:%.2lf,平均分:%.2lf\n”,TotalScore(person,count),TotalScore(person,count)/count);
}
void ReadInfo(student* person){
FILE fp = NULL;
fp = fopen(“test.txt”, “r”);
int i=0;
for(;i<cal_how_many_student_in_txt();i++){
person[i].name=(char
)malloc(10sizeof(char));
fscanf(fp, “%d %s %lf\n”, &person[i].id,person[i].name,&person[i].score);
}
fclose(fp);
printf(“成功读取已存数据!\n是否增加记录(Y/N):”);
getchar();
char flag;
scanf("%c",&flag);
if(flag==‘Y’){
fp = fopen(“test.txt”, “a”);
while(flag==‘Y’){
person[i].name=(char
)malloc(10sizeof(char));
printf(“请依次输入学号 姓名 成绩:”);
scanf("%d %s %lf",&person[i].id,person[i].name,&person[i].score);
fprintf(fp,"\n%d %s %lf",person[i].id,person[i].name,person[i].score);
i++;
printf(“是否增加记录(Y/N):”);
getchar();
scanf("%c",&flag);
if(flag!=‘Y’){
fclose(fp);
printf(“结束输入记录\n”);
break;
}
}
}
else
printf(“结束输入记录\n”);
fclose(fp);
}
void NumSearch(student
s,const int count,int id){
ScoreDescSort(s,count);
int i=0,flag=1;
for(;i<count;i++)
if(id==s[i].id){
flag=0;
break;
}
if(!flag)
printf(“排名%d名,考试成绩为%.2lf\n”,i+1,s[i].score);
else
printf(“该学生不存在!\n”);
}
void NameSearch(student* s,const int count,char* name){
ScoreDescSort(s,count);
int i=0,flag=1;
for(;i<count;i++)
if(!strcmp(name,s[i].name)){
flag=0;
break;
}
if(!flag)
printf(“排名%d名,考试成绩为%.2lf\n”,i+1,s[i].score);
else
printf(“该学生不存在!\n”);
}
double TotalScore(student* s,const int count){
double sum=0;
for(int i=0;i<count;i++)
sum+=s[i].score;
return sum;
}
void StatNumAndPer(student* s,const int count){
int step[5]={0};
for(int i=0;i<count;i++){
if(s[i].score>=90)
step[4]++;
else if(s[i].score>=80)
step[3]++;
else if(s[i].score>=70)
step[2]++;
else if(s[i].score>=60)
step[1]++;
else
step[0]++;
}
printf(“优秀\t良好\t中等\t及格\t不及格\n”);
printf("%d\t%d\t%d\t%d\t%d\n",step[4],step[3]+step[4],step[3]+step[4]+step[2],step[3]+step[4]+step[2]+step[1],step[0]);
printf("%.2lf\t%.2lf\t%.2lf\t%.2lf\t%.2lf\n",step[4]*1.0/count,(step[3]+step[4])*1.0/count,(step[3]+step[4]+step[2])*1.0/count,(step[3]+step[4]+step[2]+step[1])*1.0/count,step[0]*1.0/count);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值