#include <stdio.h>
#include <string.h>
#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
}
#include<stdio.h>#include<math.h>
int
main(void)
{
float score[10] /*定义一个包含10个数的数组*/
int i /*定义变量I*/
int max/*定义变量MAX,用于存数组中最大的那个数*/
int min/*定义变量MIN,用于存数组中最小的那个数*/
float average/*定义10个数的平均数average*/
float sum=0,left_sum/*定义10个数的和sum,定义除去两个指定数值余下的和left_sum*/
float best_score,worst_score/*best_score是离平均分最接近的那个数,worst_score是离平均分最远的那个数*/
/*定义cha来存数组中的数值减去平均分后的值*******************
**smallest_cha来存数组中的数减去平均分的差数值最小的那个数**
**bigest_cha来存数组中的数减去平均分的差数值最大的那个数***/
float cha,smallest_cha,biggest_cha
printf("Please input ten scores(0--100) :\n")/*提示输入数组的10个数的大小*/
for(i=0i<10i++)
scanf("%f",&score[i])/*依次输入数组中每个数的数值*/
for(i=0i<10i++)
sum+=score[i]/*把10个数的数值相加存到SUM里面*/
max=score[0]/*假定score[0]为数组中最大的那个数*/
min=score[0]/*假定score[0]为数组中最小的那个数*/
/*10个数依次比较大小*/
for(i=1i<10i++)
{
if(score[i]>=max)
max=score[i]
else if(score[i]<=min)
min=score[i]
}
left_sum=sum-max-min/*变量left_score等于总和减去两个指定的数*/
average=left_sum/8/*average是余下8个数数值的平均*/
/*打印最大的数和最小的数*/
printf("The max score is %d.\n",max)
printf("The min score is %d.\n",min)
printf("The average score is:")
printf("%f\n",average)/*打印平均分数*/
best_score=score[0]/*假定score[0]为最接近平均分的那个数*/
worst_score=score[0]/*假定score[0]为最偏离平均分的那个数*/
smallest_cha=score[0]-average/*假定smallest_cha是(score[0]-average)的值*/
biggest_cha=score[0]-average/*假定biggest_cha是(score[0]-average)的值*/
/*如果smallest_cha,biggest_cha小于或等于0**
**则取它们的相反数*************************/
if(smallest_cha<=0)
{
smallest_cha=-smallest_cha
biggest_cha=-biggest_cha
}
/*依次比较,筛选出best_score,worst_score*/
for(i=1i<10i++)
{
cha=score[i]-average
if(cha<=0)
cha=-cha
if(cha<=smallest_cha)
{
best_score=score[i]
smallest_cha=cha
}
else if(cha>=biggest_cha)
{
worst_score=score[i]
biggest_cha=cha
}
}
/*打印best_score,worst_score的值*/
printf("The best_score is %f.\n",best_score)
printf("The worst_score is %f.\n",worst_score)
return 0/*函数返回0,表示结束*/
}