c语言输出1.设计登录评分系统主界面 2.记录每一个选手信息及10位裁判打分及现场观众和网络观众的投票数 3.实现修改参赛选手信息功能 4.实现删除参赛选手信息功能 5.计算出冠亚季军 结果保存至文件中
时间: 2024-03-24 21:39:01 浏览: 62
以下是一个简单的C语言程序,实现了你提出的要求:
```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// 定义选手结构体
typedef struct {
char name[20];
float score[10];
int audience_vote;
int online_vote;
float total_score;
} Player;
// 声明全局变量
Player players[20];
int num_players = 0;
// 主菜单
void main_menu() {
printf("\n**********欢迎进入评分系统**********\n");
printf("1. 添加参赛选手\n");
printf("2. 查看参赛选手\n");
printf("3. 修改参赛选手信息\n");
printf("4. 删除参赛选手\n");
printf("5. 计算冠亚季军并保存结果\n");
printf("0. 退出评分系统\n");
printf("请输入数字选择操作:");
}
// 添加参赛选手
void add_player() {
Player p;
printf("请输入选手姓名:");
scanf("%s", p.name);
printf("请输入10位裁判的打分:");
for (int i = 0; i < 10; i++) {
scanf("%f", &p.score[i]);
}
printf("请输入现场观众投票数:");
scanf("%d", &p.audience_vote);
printf("请输入网络观众投票数:");
scanf("%d", &p.online_vote);
p.total_score = 0;
for (int i = 0; i < 10; i++) {
p.total_score += p.score[i];
}
p.total_score /= 10;
p.total_score += p.audience_vote + p.online_vote;
players[num_players++] = p;
printf("添加成功!\n");
}
// 查看参赛选手
void view_players() {
printf("目前有%d位参赛选手:\n", num_players);
for (int i = 0; i < num_players; i++) {
printf("姓名:%s\t总分:%.2f\n", players[i].name, players[i].total_score);
}
}
// 修改参赛选手信息
void modify_player() {
char name[20];
printf("请输入要修改的选手姓名:");
scanf("%s", name);
for (int i = 0; i < num_players; i++) {
if (strcmp(players[i].name, name) == 0) {
printf("请输入10位裁判的新打分:");
for (int j = 0; j < 10; j++) {
scanf("%f", &players[i].score[j]);
}
printf("请输入新的现场观众投票数:");
scanf("%d", &players[i].audience_vote);
printf("请输入新的网络观众投票数:");
scanf("%d", &players[i].online_vote);
players[i].total_score = 0;
for (int j = 0; j < 10; j++) {
players[i].total_score += players[i].score[j];
}
players[i].total_score /= 10;
players[i].total_score += players[i].audience_vote + players[i].online_vote;
printf("修改成功!\n");
return;
}
}
printf("未找到该选手!\n");
}
// 删除参赛选手
void delete_player() {
char name[20];
printf("请输入要删除的选手姓名:");
scanf("%s", name);
for (int i = 0; i < num_players; i++) {
if (strcmp(players[i].name, name) == 0) {
for (int j = i; j < num_players - 1; j++) {
players[j] = players[j + 1];
}
num_players--;
printf("删除成功!\n");
return;
}
}
printf("未找到该选手!\n");
}
// 比较函数,用于排序
int cmp(const void *a, const void *b) {
Player *pa = (Player *)a;
Player *pb = (Player *)b;
if (pa->total_score < pb->total_score) {
return 1;
} else if (pa->total_score > pb->total_score) {
return -1;
} else {
return 0;
}
}
// 计算冠亚季军并保存结果
void calculate_result() {
if (num_players < 3) {
printf("参赛选手不足3人,无法计算结果!\n");
return;
}
qsort(players, num_players, sizeof(Player), cmp);
FILE *fp = fopen("result.txt", "w");
fprintf(fp, "冠军:%s\t总分:%.2f\n", players[0].name, players[0].total_score);
fprintf(fp, "亚军:%s\t总分:%.2f\n", players[1].name, players[1].total_score);
fprintf(fp, "季军:%s\t总分:%.2f\n", players[2].name, players[2].total_score);
fclose(fp);
printf("计算完成!结果已保存至result.txt文件中。\n");
}
int main() {
int choice;
while (1) {
main_menu();
scanf("%d", &choice);
switch (choice) {
case 0:
printf("谢谢使用评分系统!\n");
return 0;
case 1:
add_player();
break;
case 2:
view_players();
break;
case 3:
modify_player();
break;
case 4:
delete_player();
break;
case 5:
calculate_result();
break;
default:
printf("输入有误,请重新输入!\n");
break;
}
}
}
```
这个程序使用了结构体存储选手信息,可以实现添加、查看、修改、删除选手信息的功能,并且可以计算出冠亚季军并保存结果至文件中。需要注意的是,该程序仅供参考,实际应用中可能需要根据具体需求进行修改。
阅读全文