临时写的,没有编译,应该差不离。
#include <stdio.h>
#define MAX_CORE_NUM 3
#define MAX_STU_NUM 100
struct stu_info_t
{
int no;
char name[50];
int is_male;
int core[MAX_CORE_NUM];
int core_total;
int rank;
};
int class_min_stu[MAX_CORE_NUM];
int class_max_stu[MAX_CORE_NUM];
struct stu_info_t stu_info[MAX_STU_NUM];
int stu_num = 0;
int input_stu_info()
{
char q = 0;
int i = 0;
int j = 0;
char c = 0;
printf("请输入所有学生信息:\n");
i = 0;
while (q != 'q' && q != 'Q' && i < MAX_STU_NUM)
{
printf("
学号:");
scanf("%d", &(stu_info[i].no));
printf("姓名:");
scanf("%s", stu_info[i].name);
printf("性别(M/m - 男生,F/f - 女生):");
scanf("%c", &c);
if (c == 'M' || c == 'c')
stu_info[i].is_male = 1;
else
stu_info[i].is_male = 0;
stu_info[i].core_total = 0;
for (j = 0; j < MAX_CORE_NUM; j++)
{
printf("课程%d得分:", j+1);
scanf("%d", &(stu_info[i].core[j]));
stu_info[i].core_total += stu_info[i].core[j];
}
i++;
if (i >= MAX_STU_NUM)
{
printf("最多输入%d个学生信息\n", MAX_STU_NUM);
break;
}
printf("退出(Q/q):");
scanf("%c", &q);
}
stu_num = i;
return 0;
}
int save_stu_info()
{
int i = 0;
int j = 0;
FILE * fp = NULL;
fp = fopen("students.txt", "rw");
if (fp == NULL)
return -1;
fprintf(fp, "\t学号\t姓名\t性别");
for (j = 0; j < MAX_CORE_NUM; j++)
{
fprintf(fp, "\t课程%d", j+1);
}
fprintf(fp, "\r\n");
for (i = 0; i < stu_num; i++)
{
fprintf(fp, "\t%d\t%s\t%s", stu_info[i].no, stu_info[i].name, (stu_info[i].is_male ? "男" : "女"));
for (j = 0; j < MAX_CORE_NUM; j++)
{
fprintf(fp, "\t%d", stu_info[i].core[j]);
}
fprintf(fp, "\r\n");
}
fclose(fp);
return 0;
}
int cal_rank()
{
int i = 0;
int j = 0;
int class_min;
int class_max;
struct stu_info_t t;
for (i = 0; i < stu_num - 1; i++)
{
for (j = i + 1; j < stu_num; j++)
{
if (stu_info[i].core_total < stu_info[j].core_total)
{
memcpy(&t, &stu_info[j], sizeof(struct stu_info_t));
memcpy(&stu_info[j], &stu_info[i], sizeof(struct stu_info_t));
memcpy(&stu_info[i], &t, sizeof(struct stu_info_t));
}
}
}
for (i = 0; i < stu_num; i++)
{
stu_info[i].rank = i + 1;
}
for (j = 0; j < MAX_CORE_NUM; j++)
{
class_min = 0;
class_max = 10000;
for (i = 0; i < stu_num; i++)
{
if (stu_info[i].core[j] < class_min)
{
class_min = stu_info[i].core[j];
class_min_stu[j] = i;
}
if (stu_info[i].core[j] > class_max)
{
class_max = stu_info[i].core[j];
class_max_stu[j] = i;
}
}
}
return 0;
}
int output_total()
{
int i = 0;
printf("\t学号\t姓名\t总分\t排名\n");
for (i = 0; i < stu_num; i++)
{
printf("\t%d\t%s\t%d\t%d\n", stu_info[i].no, stu_info[i].name, stu_info[i].core_total, stu_info[i].rank);
}
return 0;
}
int output_class()
{
int i = 0;
int j = 0;
int core = 0;
for (j = 0; j < MAX_CORE_NUM; j++)
{
core = stu_info[class_max_stu[j]].core[j];
printf("课程%d最高分:%d\n", j+1, core);
printf("\t学号\t姓名\n");
for (i = 0; i < stu_num; i++)
{
if (stu_info[i].core[j] == core)
{
printf("\t%d\t%s\n", stu_info[i].no, stu_info[i].name);
}
}
}
for (j = 0; j < MAX_CORE_NUM; j++)
{
core = stu_info[class_min_stu[j]].core[j];
printf("课程%d最低分:%d\n", j+1, core);
printf("\t学号\t姓名\n");
for (i = 0; i < stu_num; i++)
{
if (stu_info[i].core[j] == core)
{
printf("\t%d\t%s\n", stu_info[i].no, stu_info[i].name);
}
}
}
return 0;
}
void main()
{
input_stu_info();
save_stu_info();
cal_rank();
output_total();
output_class();
return;
}
追问要求主页上有信息输入、成绩排名,最高最低分学生信息 、退出
谢了
追答里面有啊
input_stu_info管信息输入
output_total输出整体排名
output_class输出最高最低分学生名单
除非你要求更复杂的界面,那我就不擅长了
你把input还有ouput那几个子函数修改一下试试
追问用户界面菜单最少包括 信息输入、成绩排名,最高最低分学生信息 、退出
麻烦你增加一下
追答不好意思,界面我不擅长,看看别人是否有解吧
追问i].name,stud[i].gender ,stud[i].english,stud[i].chinese,stud[i].math,stud[i].english+stud[i].chinese+stud[i].math);
else
printf("%d%d%s%s%d%d%d%d\n",j++,stud[i].num,stud[i].name,stud[i].gender ,stud[i].english,stud[i].chinese,stud[i].math,stud[i].english+stud[i].chinese+stud[i].math);
}
fclose(fp);
}
麻烦你给我修改一下这个程序
太给力了,你的回答完美解决了我的问题!
本回答被提问者采纳