You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
pbfianv8o/第十一题啊啊啊.cpp

147 lines
4.3 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#include<stdio.h>
#include<stdlib.h> //定义杂项函数及内存分配函数 eg:exit()
#include<string.h> //字符串处理
#define N 3
#define M 10
int add = N - 1;
int sum[N] = { 0 }, Max[N], Min[N];
float aver[N] = { 0 };
struct player //结构体的运用
{
int num; //编号
char name[8]; //名字
int score[M]; //得分
};
void Inistplayer(struct player a[], int n)
{
int i;
for (i = 0; i < n; i++)
scanf("%d %s", &a[i].num, a[i].name); //录入选手数据
}
void Inputscore(int score[], int n) //评委打分
{
int i;
for (i = 0; i < n; i++)
scanf("%d",&score[i]);
}
void sort(struct player a[], float b[], int n) //sort==排序
{
int i, k, j,t;
float temp; //temp:临时文件夹
//struct player temp1;
char str[8];
for (i = 0; i < n-1; i++)
{
k = i;
for (j = i + 1; j < n; j++)
if (b[k] > b[j])
k = j;
if (k != i)
{
temp = b[i];
b[i] = b[k];
b[k] = temp;
strcpy(str, a[i].name); //strcpy是复制字符串的的库函数
strcpy(a[i].name, a[k].name);
strcpy(a[k].name, str);
t = a[i].num;
a[i].num = a[k].num;
a[k].num = t;
}
}
}
int main()
{
int flag = 1, n, i, m, j; //flag:判断标志
FILE *fp;
struct player a[20];
printf("||===============================================================================||\n");
printf("||===============================主菜单==========================================||\n");
printf("||***************************歌手比赛管理系统************************************||\n");
printf("||===============================================================================||\n");
printf("|| 1.输入选手数据 ||\n");
printf("|| 2.评委打分 ||\n");
printf("|| 3.成绩排序 ||\n");
printf("|| 4.数据查询 ||\n");
printf("|| 5.追加学生数据 ||\n");
printf("|| 6.写入数据文件 ||\n");
printf("|| 7.退出系统 ||\n");
printf("||===============================================================================||\n");
printf("||*******************************************************************************||\n");
printf("||*******************************************************************************||\n");
while(flag)
{
printf("输入序号以进行的操作:");
scanf("%d",&n);
switch(n)
{
case 1: //输入选手数据
Inistplayer(a,N);
break;
case 2: //评委打分
for(i=0;i<N;i++)
{
printf("%d位选手的成绩",i+1);
Inputscore(a[i].score,M); //输入评委打分
for(j=0;j<M;j++)
printf("%d\t",a[i].score[j]); //所有选手的各自得分
printf("\n");
}
break;
case 3: //成绩排序
for(i=0;i<N;i++)
{
Max[i]=a[i].score[0];
Min[i]=a[i].score[0];
for(j=0;j<M;j++)
{
sum[i]+=a[i].score[j];
if(Max[i]<a[i].score[j])
Max[i]=a[i].score[j];
if(Min[i]>a[i].score[j])
Min[i]=a[i].score[j];
}
aver[i]=(float)(sum[i]-Max[i]-Min[i])/(M-2); //计算平均值
}
sort(a,aver,N);
for(i=0;i<N;i++)
printf("%s\t%5.2f\n",a[i].name,aver[i]);
break;
case 4: //查询数据
printf("你想查询的选手的数据:");
scanf("%d",&m); //m为代选手编号的变量
for(i=0;i<N;i++)
if(a[i].num==m)
{
printf("%d 号选手%s 总分:%d,最高分%d,最低分%d,平均分%5.2f",a[i].num,a[i].name,sum[i],Max[i],Min[i],aver[i]);
printf("\n");
}
break;
case 5: //追加选手数据
printf("请输入要追加的选手数据:\n");
add=add+1;
scanf("%d %s",&a[add].num,a[add].name);
break;
case 6: //写入数据文件
printf("将数据写入文件:\n");
fp=fopen("date.txt","w");
for(int i=0;i<add+2;i++)
{
fprintf(fp,"%d %s\n",a[i].num,a[i].name);
}
printf("写入成功!\n");
fclose(fp);
break;
case 7: //退出系统
exit(0);
break;
default:flag=0; //当输入无效数字
}
}
return 0;
}