编写一个c++的代码(尽可能简单)可以实现以下要求,歌手比赛系统对一次歌手比赛的成绩进行管理,功能要求:1、依次输入每个选手的编号、姓名、十个评委的成绩,根据输入计算出总成绩和平均成绩(去掉最高分,去掉最低分)。2、显示主菜单如下:1)输入选手数据2)评委打分3)成绩排序(按平均分)4)数据查询5追加学生数据6退出系统)
时间: 2023-06-14 08:03:59 浏览: 109
以下是一个简单的 C 代码实现:
```
#include <stdio.h>
#include <string.h>
// 定义常量
#define MAX_STUDENTS 50
#define MAX_NAME_LEN 20
#define MAX_SCORES 10
// 定义结构体
struct student {
int id;
char name[MAX_NAME_LEN];
float scores[MAX_SCORES];
float total_score;
float avg_score;
};
// 定义全局变量
struct student students[MAX_STUDENTS];
int num_students = 0;
// 函数声明
void input_student_data();
void input_scores();
void sort_scores();
void query_student_data();
void add_student_data();
int main() {
int choice;
do {
printf("\n主菜单:\n");
printf("1) 输入选手数据\n");
printf("2) 评委打分\n");
printf("3) 成绩排序(按平均分)\n");
printf("4) 数据查询\n");
printf("5) 追加学生数据\n");
printf("6) 退出系统\n");
printf("请选择操作:");
scanf("%d", &choice);
switch (choice) {
case 1:
input_student_data();
break;
case 2:
input_scores();
break;
case 3:
sort_scores();
break;
case 4:
query_student_data();
break;
case 5:
add_student_data();
break;
case 6:
printf("谢谢使用!\n");
break;
default:
printf("无效的选择!\n");
}
} while (choice != 6);
return 0;
}
void input_student_data() {
if (num_students >= MAX_STUDENTS) {
printf("学生数量已达到上限!\n");
return;
}
struct student s;
printf("请输入学生编号:");
scanf("%d", &s.id);
printf("请输入学生姓名:");
scanf("%s", s.name);
students[num_students++] = s;
printf("学生信息已添加!\n");
}
void input_scores() {
if (num_students == 0) {
printf("还没有录入学生信息!\n");
return;
}
int id, index;
printf("请输入学生编号:");
scanf("%d", &id);
for (int i = 0; i < num_students; i++) {
if (students[i].id == id) {
index = i;
break;
}
if (i == num_students - 1) {
printf("找不到该学生,请重新输入编号!\n");
return;
}
}
float max_score = 0, min_score = 10;
float sum = 0;
for (int i = 0; i < MAX_SCORES; i++) {
printf("请输入第 %d 位评委的成绩:", i + 1);
scanf("%f", &students[index].scores[i]);
if (students[index].scores[i] > max_score) {
max_score = students[index].scores[i];
}
if (students[index].scores[i] < min_score) {
min_score = students[index].scores[i];
}
sum += students[index].scores[i];
}
students[index].total_score = sum - max_score - min_score;
students[index].avg_score = students[index].total_score / 8;
printf("评分完成!\n");
}
void sort_scores() {
if (num_students == 0) {
printf("还没有录入学生信息!\n");
return;
}
int i, j;
struct student temp;
for (i = 0; i < num_students - 1; i++) {
for (j = i + 1; j < num_students; j++) {
if (students[i].avg_score < students[j].avg_score) {
temp = students[i];
students[i] = students[j];
students[j] = temp;
}
}
}
printf("成绩排序完成!\n");
printf("序号\t姓名\t总分\t平均分\n");
for (i = 0; i < num_students; i++) {
printf("%d\t%s\t%.2f\t%.2f\n", students[i].id, students[i].name, students[i].total_score, students[i].avg_score);
}
}
void query_student_data() {
if (num_students == 0) {
printf("还没有录入学生信息!\n");
return;
}
int id, index;
printf("请输入学生编号:");
scanf("%d", &id);
for (int i = 0; i < num_students; i++) {
if (students[i].id == id) {
index = i;
break;
}
if (i == num_students - 1) {
printf("找不到该学生,请重新输入编号!\n");
return;
}
}
printf("学生信息如下:\n");
printf("编号:%d\n", students[index].id);
printf("姓名:%s\n", students[index].name);
printf("成绩:");
for (int i = 0; i < MAX_SCORES; i++) {
printf("%.1f ", students[index].scores[i]);
}
printf("\n总分:%.2f\n", students[index].total_score);
printf("平均分:%.2f\n", students[index].avg_score);
}
void add_student_data() {
if (num_students >= MAX_STUDENTS) {
printf("学生数量已达到上限!\n");
return;
}
struct student s;
printf("请输入学生编号:");
scanf("%d", &s.id);
printf("请输入学生姓名:");
scanf("%s", s.name);
students[num_students++] = s;
printf("学生信息已添加!\n");
// 让用户选择是否继续录入成绩
char choice;
printf("是否要录入成绩?(y/n)");
scanf(" %c", &choice);
if (choice == 'y') {
input_scores();
}
}
```
阅读全文