admin 管理员组

文章数量: 887019

C语言,歌手比赛成绩管理系统

以下为实验内容,要求对歌手比赛的成绩进行管理

//编写一个c程序,对一次歌手比赛的成绩进行管理,其中,每个选手的数据包括编号,姓名,十个评委的成绩,总成绩和平均成绩(去掉最高分,去掉最低分)
#pragma warning(disable:4996)//忽略4996错误
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define LARGEST_NUMBER 50//最多可容纳的歌手数量
#define NAME_LEN 64//选手姓名
/*歌手的结构体*/
struct student
{int id;//选手编号char name[NAME_LEN];//选手姓名float grade[10];//10个评委给的成绩float toatl;//总成绩float avarage;//平均成绩
};
struct student std[LARGEST_NUMBER] = { {0} };//定义歌手并对其进行初始化
int select;//选择系统
int firstnumber = 5;//初始的歌手数量
int addnumber = 0;//增加的歌手数量(便于实现学生数据的追加)
/*返回总成绩的函数*/float total_grade(float a[], int n)
{float sum=0;for (int i = 0; i < n; i++){sum = sum + a[i];}return sum;
}
/*返回平均成绩的函数*/float avarage_grade( float a[],int n)
{float max = a[0];float min = a[0];for (int i = 1; i < n; i++){if (a[i]>max){max = a[i];}if (a[i]<min){min = a[i];}}float avarage = (total_grade(a, 10) - max - min) / 8;return avarage;
}
/*将总成绩和平均成绩赋给结构体的函数*/void total_avarage()
{for (int i = 0; i < firstnumber+addnumber; i++){std[i].toatl = total_grade(std[i].grade, 10);std[i].avarage = avarage_grade(std[i].grade, 10);}
}
/*菜单选择*/ void menu()
{printf("*****主菜单******\n");printf("(1):输入选手数据(初始为5个歌手)。\n");printf("(2):评委打分。\n");printf("(3)成绩排序(按平均分)。\n");printf("(4:数据查询。\n");printf("(5):追加学生数据。\n");printf("(6):写入数据文件。\n");printf("(7):退出系统。\n");printf("请输入1~7的数字:");scanf_s("%d", &select);do{if (select > 7 || select < 1){printf("请重新输入1~7的数字:");scanf_s("%d", &select);}} while (select > 7 || select < 1);
}
/*输入选手数据的函数*/void menu_1()
{for (int i = 0; i < firstnumber; i++){printf("***第%d个选手***\n", i + 1);printf("编号:"); scanf_s("%d", &std[i].id);printf("姓名:"); scanf_s("%s", std[i].name, NAME_LEN);}
}
/*评委打分的函数*/void menu_2()
{for (int i = 0; i < firstnumber; i++){printf("***请依次输入成绩(每个评委给的成绩用空格隔开)***\n");printf("---姓名:%-8s编号:%-8d的成绩---\n", std[i].name,std[i].id);for (int j = 0; j < 10; j++){scanf_s("%f", &std[i].grade[j]);}}printf("           ↓↓↓成绩单↓↓↓\n");for (int i = 0; i < firstnumber+addnumber; i++){printf("***姓名:%-6s 编号:%-4d的成绩***\n", std[i].name, std[i].id);for (int j = 0; j < 5; j++){printf("%6.1f", std[i].grade[j]);}putchar('\n');for (int j = 5; j < 10; j++){printf("%6.1f", std[i].grade[j]);}putchar('\n');total_avarage();printf("总成绩:%-8.1f", std[i].toatl);printf("平均成绩:%-8.1f\n", std[i].avarage);}}
/*成绩排序(按平均成绩从大到小排序)的函数*/void menu_3()
{total_avarage();for (int i = 0; i < firstnumber+addnumber-1; i++)//冒泡排序法{for (int j = 0; j < firstnumber+addnumber-1; j++){if (std[j].avarage<std[j+1].avarage){struct student temp = std[j];std[j] = std[j + 1];std[j + 1] = temp;}}}printf("  编号  |  姓名  |  平均成绩\n");for (int i = 0; i < firstnumber+addnumber; i++){printf("   %-5d|%-8s|    %-8.1f\n", std[i].id,std[i].name, std[i].avarage);}
}
/*数据查询*/void menu_4()
{int choice;//选择char tendname[NAME_LEN] = {"\0"};//姓名查询int tendid=0;//编号查询int a = 0;//学生结构体的下标printf("***数据查询系统***\n");printf("(1):按照姓名查询\n");printf("(2):按照编号查询\n");printf("(3):返回主菜单\n");printf("请输入一个1~3的数字:"); scanf_s("%d", &choice);do{if (choice>3||choice<1){printf("请重新输入一个1~3的数"); scanf_s("%d", &choice);}} while (choice>3||choice<0);if (choice == 1)//按照性名查询{a = 0;printf("请输入要查询的学生姓名:");	scanf_s("%s", tendname,NAME_LEN);for (int i = 0; i < firstnumber+addnumber; i++){if (strcmp(tendname,std[i].name)!=0){a++;}if (strcmp(tendname,std[i].name)==0){break;//跳出循环必须有!!!}}if (a >= firstnumber+addnumber){printf("!!!查询失败!!!,\n请输入正确的姓名\a\n");menu_4();}else{printf("***查询成功***\n");printf("  编号  |  姓名  |  平均成绩  |  总成绩\n");printf("   %-5d|%-8s|    %-8.1f|   %-6.1f\n", std[a].id, std[a].name, std[a].avarage,std[a].toatl);system("pause");menu_4();}}if (choice==2)//按照编号查询{a = 0;printf("请输入要查询的学生编号:"); scanf_s("%d", &tendid);for (int i = 0; i < firstnumber+addnumber; i++){if (tendid!=std[i].id){a++;}else{break;//这个跳出循环非常重要,找bug找了俩小时!!!}}if (a >= firstnumber+addnumber){printf("!!!查询失败!!!\n请输入正确的编号\a\n");menu_4();}if (a <= firstnumber+addnumber){printf("***查询成功***\n");printf("  编号  |  姓名  |  平均成绩  |  总成绩\n");printf("   %-5d|%-8s|    %-8.1f|   %-6.1f\n", std[a].id, std[a].name, std[a].avarage,std[a].toatl);system("pause");menu_4();}}if (choice==3)//返回主菜单{}
}
/*追加学生数据*/void menu_5()
{printf("***追加学生数据***\n");addnumber++;printf("请输入追加学生的编号:"), scanf_s("%d", &std[firstnumber + addnumber -1].id);printf("请输入追加学生的姓名:"), scanf_s("%s", std[firstnumber + addnumber - 1].name,NAME_LEN);printf("请依次输入追加学生的成绩(以空格或回车隔开)\n");for (int i = 0; i < 10; i++){scanf_s("%f", &std[firstnumber + addnumber - 1].grade[i]);}printf("**************追加成功**************\n");printf("↓↓↓按任意键显示追加数据后的成绩单↓↓↓\n");system("pause");for (int i = 0; i < firstnumber + addnumber; i++){printf("***姓名:%-6s 编号:%-4d的成绩***\n", std[i].name, std[i].id);for (int j = 0; j < 5; j++){printf("%6.1f", std[i].grade[j]);}putchar('\n');//为了美观,一行显示5个成绩for (int j = 5; j < 10; j++){printf("%6.1f", std[i].grade[j]);}putchar('\n');total_avarage();printf("总成绩:%-8.1f", std[i].toatl);printf("平均成绩:%-8.1f\n", std[i].avarage);}}
/*写入数据文件*/void menu_6()
{FILE* fp;fp = fopen("数据文件.txt", "w");if (fp==NULL){printf("写入文件失败\n");}else{for (int i = 0; i < firstnumber+addnumber; i++){fprintf(fp, "*****姓名:%-6s 编号:%-4d的成绩*****\n", std[i].name, std[i].id);for (int j = 0; j < 5; j++){fprintf(fp, "%6.1f", std[i].grade[j]);}fprintf(fp, "\n");for (int j = 5; j < 10; j++){fprintf(fp, "%6.1f", std[i].grade[j]);}fprintf(fp, "\n");fprintf(fp, "   总成绩:%-8.1f", std[i].toatl);fprintf(fp, "平均成绩:%-8.1f\n", std[i].avarage);fprintf(fp, "-------------------------------------\n");}printf("***写入文件成功***\n");fclose(fp);}system("pause");
}
int main()
{menu();while (1){if (select==1){menu_1();menu();}if (select==2){menu_2();menu();}if (select==3){menu_3();menu();}if (select==4){menu_4();menu();}if (select==5){menu_5();menu();}if (select==6){menu_6();menu();}if (select==7){printf("***退出成功!***");break;}}return 0;
}

代码使用vs2019写的,用其他编译器,把scanf_s改成scanf就OK了,如果还不行,就把循环里面的int i=0,int j=0拿出来,放到循环外面,就可以编译运行了。


自学以来第一次接触成绩管理,大佬轻点喷

本文标签: C语言 歌手比赛成绩管理系统