1. 录入全部歌手的基本信息:歌手编号及姓名。 2. 10 个评委的打分随机生成,分数在 0-100 之间,歌手最后得分去掉一个最高分和最低分,求平均值。 3. 显示全部歌手的信息。 4. 对全部歌手按成绩排序。 5. 根据歌手编号,查询该歌手全部信息。 6. 添加一个歌手。 7. 将全部歌手信息保存到一个文件之中。 8. 建立如图 28.1 所示菜单实现上述功能。编程
时间: 2024-03-13 21:46:16 浏览: 113
tessdata各语言集合包+tesseract-ocr-w64-setup-v5.3.0.20221214.exe
好的,以下是 C 语言的代码实现:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define MAX_SINGERS 100
#define MAX_NAME_LENGTH 50
// 歌手结构体
typedef struct Singer {
int id;
char name[MAX_NAME_LENGTH];
int scores[10];
float avg_score;
} Singer;
Singer singers[MAX_SINGERS];
int singer_count = 0;
// 随机生成分数
int random_score() {
return rand() % 101;
}
// 计算平均分
void calculate_avg_score(Singer *singer) {
int max_score = -1, min_score = 101, total_score = 0;
for (int i = 0; i < 10; i++) {
int score = singer->scores[i];
if (score > max_score) {
max_score = score;
}
if (score < min_score) {
min_score = score;
}
total_score += score;
}
total_score -= max_score + min_score;
singer->avg_score = total_score / 8.0;
}
// 录入歌手信息
void input_singer() {
if (singer_count >= MAX_SINGERS) {
printf("歌手数量已达到上限,无法添加新歌手!\n");
return;
}
Singer *singer = &singers[singer_count];
printf("请输入歌手编号:");
scanf("%d", &singer->id);
printf("请输入歌手姓名:");
scanf("%s", singer->name);
for (int i = 0; i < 10; i++) {
singer->scores[i] = random_score();
}
calculate_avg_score(singer);
singer_count++;
}
// 显示全部歌手信息
void show_all_singers() {
printf("编号\t姓名\t平均分\n");
for (int i = 0; i < singer_count; i++) {
Singer *singer = &singers[i];
printf("%d\t%s\t%.2f\n", singer->id, singer->name, singer->avg_score);
}
}
// 按成绩排序
int compare_singers(const void *a, const void *b) {
Singer *singer_a = (Singer *)a;
Singer *singer_b = (Singer *)b;
if (singer_a->avg_score < singer_b->avg_score) {
return 1;
} else if (singer_a->avg_score > singer_b->avg_score) {
return -1;
}
return 0;
}
void sort_singers() {
qsort(singers, singer_count, sizeof(Singer), compare_singers);
}
// 根据编号查询
void search_singer_by_id() {
int id;
printf("请输入要查询的歌手编号:");
scanf("%d", &id);
for (int i = 0; i < singer_count; i++) {
Singer *singer = &singers[i];
if (singer->id == id) {
printf("编号:%d\n", singer->id);
printf("姓名:%s\n", singer->name);
printf("成绩:");
for (int j = 0; j < 10; j++) {
printf("%d ", singer->scores[j]);
}
printf("\n");
printf("平均分:%.2f\n", singer->avg_score);
return;
}
}
printf("没有找到编号为 %d 的歌手!\n", id);
}
// 保存到文件
void save_to_file() {
FILE *fp = fopen("singers.txt", "w");
if (fp == NULL) {
printf("保存文件失败!\n");
return;
}
for (int i = 0; i < singer_count; i++) {
Singer *singer = &singers[i];
fprintf(fp, "%d %s ", singer->id, singer->name);
for (int j = 0; j < 10; j++) {
fprintf(fp, "%d ", singer->scores[j]);
}
fprintf(fp, "\n");
}
fclose(fp);
printf("保存文件成功!\n");
}
// 从文件读取
void load_from_file() {
FILE *fp = fopen("singers.txt", "r");
if (fp == NULL) {
printf("读取文件失败!\n");
return;
}
singer_count = 0;
while (!feof(fp)) {
Singer *singer = &singers[singer_count];
fscanf(fp, "%d %s", &singer->id, singer->name);
for (int j = 0; j < 10; j++) {
fscanf(fp, "%d", &singer->scores[j]);
}
calculate_avg_score(singer);
singer_count++;
}
fclose(fp);
printf("读取文件成功!\n");
}
// 菜单
void show_menu() {
printf("***************\n");
printf("1. 录入歌手信息\n");
printf("2. 显示全部歌手信息\n");
printf("3. 按成绩排序\n");
printf("4. 根据编号查询\n");
printf("5. 添加一个歌手\n");
printf("6. 保存到文件\n");
printf("7. 从文件读取\n");
printf("0. 退出程序\n");
printf("***************\n");
}
int main() {
srand((unsigned int)time(NULL));
while (1) {
show_menu();
int choice;
printf("请输入要执行的操作:");
scanf("%d", &choice);
switch (choice) {
case 0:
printf("程序结束,谢谢使用!\n");
return 0;
case 1:
input_singer();
break;
case 2:
show_all_singers();
break;
case 3:
sort_singers();
show_all_singers();
break;
case 4:
search_singer_by_id();
break;
case 5:
input_singer();
break;
case 6:
save_to_file();
break;
case 7:
load_from_file();
break;
default:
printf("无效的操作!\n");
break;
}
}
return 0;
}
```
注:为了方便测试,随机生成分数的代码使用了 `rand()` 函数,需要在代码开头加上 `#include <time.h>` 和 `srand((unsigned int)time(NULL));`。
阅读全文