联系方式

  • QQ:99515681
  • 邮箱:99515681@qq.com
  • 工作时间:8:00-21:00
  • 微信:codinghelp

您当前位置:首页 >> C/C++编程C/C++编程

日期:2018-07-06 04:09


#include<stdio.h>

#include<stdlib.h>

#include<string.h>

#include<time.h>

#include<math.h>



struct singer

{

int number;

char name[20];

float scores[10];

}

singers[100];

int singersCount;



void _inputSingerData()

{

printf("输入选手个数:");

scanf("%d", &singersCount);

printf("输入%d个选手的姓名:\n", singersCount);

for (int i = 0; i < singersCount; i++)

{

singer s;

scanf("%s", s.name);

s.number = i + 1;

for (int j = 0; j < 10; j++)

{//分数初始化为0

s.scores[j] = 0;

}

singers[i] = s;

}

}


void _inputScores()

{

for (int i = 0; i < singersCount; i++)

{

printf("输入选手%d分数(10个):", i + 1);

for (int j = 0; j < 10; j++)

{

scanf("%f", &singers[i].scores[j]);

}

}

}


//算平均分

float __getAverage(float scores[])

{

//找出最大最小值位置

int max = 0;

for (int j = 0; j < 10; j++)

{

if (scores[j] > scores[max])

{

max = j;

}

}

int min = 0;

for (int j = 0; j < 10; j++)

{

if (scores[j] < scores[min])

{

min = j;

}

}


float sum = 0;

for (int j = 0; j < 10; j++)

{

if (j != max && j != min) //去掉最大最小值

{

sum += scores[j];

}

}

return sum / 8;

}


void _sortByAverage()

{

float averages[100];

singer sortArr[100];

for (int i = 0; i < singersCount; i++)

{

sortArr[i] = singers[i];

averages[i] = __getAverage(sortArr[i].scores);

}

//排序

for (int i = 0; i < singersCount; i++)

{

for (int j = i + 1; j < singersCount; j++)

{

if (averages[j] > averages[i])

{

float t = averages[j];

averages[j] = averages[i];

averages[i] = t;

singer s = sortArr[j];

sortArr[j] = sortArr[i];

sortArr[i] = s;

}

}

}

printf("按平均分排序结果:\n");

for (int i = 0; i < singersCount; i++)

{

printf("编号:%d  姓名:%s  平均分:%.2f\n", sortArr[i].number, sortArr[i].name, averages[i]);

}

}


void _searchByNumber()

{

int number;

printf("输入选手编号:");

scanf("%d", &number);

for (int i = 0; i < singersCount; i++)

{

if (number == i + 1)

{

float sum = 0;

for (int j = 0; j < 10; j++)

{

sum += singers[i].scores[j];

}

printf("编号:%d  姓名:%s  平均分:%.2f  总分:%.2f\n", singers[i].number, singers[i].name, __getAverage(singers[i].scores), sum);

printf("十个评委的打分:");

for (int j = 0; j < 10; j++)

{

printf("%.2f ", singers[i].scores[j]);

}

printf("\n");

return;

}

}

printf("此编号选手不存在!\n");

}

//追加数据

void _addSinger()

{

printf("输入选手%d的姓名:\n", singersCount + 1);

singer s;

scanf("%s", s.name);

s.number = singersCount + 1;

printf("输入十个评委的打分:");

for (int j = 0; j < 10; j++)

{

scanf("%f", &s.scores[j]);

}

singers[singersCount] = s;

singersCount++;

}


void _saveData()

{

FILE *fp;     //变量类型声明。声明 fp 是 FILE型指针

if ((fp = fopen("singersData.txt", "w")) == NULL)     //singersData.txt为文件名 w只写

{

printf("File open error!\n");    //打开文件错误

exit(0);

}

for (int i = 0; i < singersCount; i++)

{

float sum = 0;

for (int j = 0; j < 10; j++)

{

sum += singers[i].scores[j];

}

fprintf(fp, "编号:%d  姓名:%s  平均分:%.2f  总分:%.2f  ", singers[i].number, singers[i].name, __getAverage(singers[i].scores), sum);

fprintf(fp, "十个评委的打分:");   //fprintf是用于文件操作的

for (int j = 0; j < 10; j++)

{

fprintf(fp, "%.2f ", singers[i].scores[j]);

if (j == 9) fprintf(fp, "\n");

}

}

fclose(fp);     //关闭这个文件

printf("已存入文件“singersData.txt”\n");

}


int main()

{

printf("***************************歌手比赛管理系统************************************\n");

printf("  \n");

printf("***********************************G*******************************************\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("你想进行的操作:");

   

while (1)

{

char input;

scanf("%c", &input);

switch (input)

{

case '1':

_inputSingerData();

printf("你想进行的操作:");

break;

case '2':

_inputScores();

printf("你想进行的操作:");

break;

case '3':

_sortByAverage();

printf("你想进行的操作:");

break;

case '4':

_searchByNumber();

printf("你想进行的操作:");

break;

case '5':

_addSinger();

printf("你想进行的操作:");

break;

case '6':

_saveData();

printf("你想进行的操作:");

break;

case '7':

system("cls");

return 0;

}

}

}


版权所有:编程辅导网 2021 All Rights Reserved 联系方式:QQ:99515681 微信:codinghelp 电子信箱:99515681@qq.com
免责声明:本站部分内容从网络整理而来,只供参考!如有版权问题可联系本站删除。 站长地图

python代写
微信客服:codinghelp