#include
#include
#define Total 10
struct People {
int ID;
char name[20];
double score[10];
double final_score;
int rank;
} people[Total];
void inputInfo()
{
printf("Please input data:\n");
for (int i = 0; i < Total; i ++) {
scanf("%d", &people[i].ID);
getchar();
scanf("%s", people[i].name);
scanf("%lf", &people[i].score[0]);
scanf("%lf", &people[i].score[1]);
scanf("%lf", &people[i].score[2]);
scanf("%lf", &people[i].score[3]);
scanf("%lf", &people[i].score[4]);
scanf("%lf", &people[i].score[5]);
scanf("%lf", &people[i].score[6]);
scanf("%lf", &people[i].score[7]);
scanf("%lf", &people[i].score[8]);
scanf("%lf", &people[i].score[9]);
people[i].final_score = 0.0;
people[i].rank = i + 1;
}
}
void calculateScore()
{
for (int i = 0; i < Total; i ++) {
double max = people[i].score[0];
double min = people[i].score[0];
for (int j = 0; j < 10; j ++) {
people[i].final_score += people[i].score[j];
if (people[i].score[j] > max)
max = people[i].score[j];
if (people[i].score[j] < min)
min = people[i].score[j];
}
people[i].final_score = people[i].final_score - max - min;
people[i].final_score /= 8;
}
printf("Calculate complete!\n");
}
void sort()
{
calculateScore();
for (int i = 0; i < Total - 1; i ++) {
for (int j = 0; j < Total - 1 - i; j ++) {
if (people[j].final_score < people[j + 1].final_score && people[j].rank < people[j + 1].rank) {
struct People tmp = people[j];
people[j] = people[j + 1];
people[j + 1] = tmp;
people[j].rank = j + 1;
people[j + 1].rank = j + 2;
}
if (people[j].final_score > people[j + 1].final_score && people[j].rank > people[j + 1].rank) {
struct People tmp = people[j];
people[j] = people[j + 1];
people[j + 1] = tmp;
people[j].rank = j + 1;
people[j + 1].rank = j + 2;
}
}
}
printf("Sort complete!\n");
}
void searchByID()
{
int ID;
printf("Please input the ID of the person:");
scanf("%d", &ID);
for (int i = 0; i < Total; i ++) {
if (people[i].ID == ID) {
printf("-----------------------------------------\n");
printf("-- You are viewing the person No.%d \n", ID);
printf("-- The final score is: %.2lf \n", people[i].final_score);
printf("-- The rank is: %d \n", people[i].rank);
printf("-----------------------------------------\n");
return;
}
}
}
void searchByName()
{
char name[20];
printf("Please input the name of the person:");
scanf("%s", name);
for (int i = 0; i < Total; i ++) {
if (strcmp(people[i].name, name) == 0) {
printf("-----------------------------------------------\n");
printf("-- You are viewing the person: %s \n", name);
printf("-- The final score is: %.2lf \n", people[i].final_score);
printf("-- The rank is: %d \n", people[i].rank);
printf("-----------------------------------------------\n");
return;
}
}
}
void quertPeople()
{
int choice;
printf("Do you want to search by ID or by name? 0-ID,1-name:");
scanf("%d", &choice);
if (choice == 0)
searchByID();
if (choice == 1)
searchByName();
}
void menu()
{
printf("-------------------------------------------------\n");
printf("-- 1 - input data --\n");
printf("-- 2 - calculate the score and sort the data --\n");
printf("-- 3 - search for person --\n");
printf("-- 0 - exit --\n");
printf("-------------------------------------------------\n");
}
int main() {
int choice;
menu();
scanf("%d", &choice);
while (choice != 0){
switch (choice) {
case 1:
inputInfo();
break;
case 2:
sort();
break;
case 3:
quertPeople();
break;
default:
break;
}
menu();
scanf("%d", &choice);
}
return 0;
}
c语言比赛评分帮解决