#include <stdio.h>
//#include <string.h>
#include <malloc.h>
#define N 5
double max(); //求最高分及其评委
double min(); //求最低分及其评委
double ping(); //求平均分
void pai(); //得分排序
void main()
{
int i;
double a[N] , *p_a;
char *p_b[N];
p_a = a;
for(i = 0; i < N; ++i)
{
p_b[i] = (char*)malloc(10*sizeof(char));
printf("请输入评委编号\n");
scanf("%s",p_b[i]);
printf("请输入得分\n");
scanf("%lf",&a[i]);
}
for(i = 0; i < N; ++i)
{
printf("评委%s打分%.2lf\n", p_b[i], a[i]);
}
pai(a, p_b);
printf(" 排序后得分为:\n");
for(i = 0; i < N; ++i)
{
printf("%10s", p_b[i]);
}
printf("\n");
for(i = 0; i < N; ++i)
{
printf("%10.2lf", a[i]);
}
printf("\n");
printf("最高分%.2lf\n", max(p_a));
printf("最低分%.2lf\n", min(p_a));
printf("去掉一个最高分%.2lf, 去掉一个最低分%.2lf, 平均得分%.2lf\n", max(p_a), min(p_a), ping(p_a));
}
double max(double * x) //利用指针将数组进行对比,取最大值
{
double n;
int i;
n = *x; //将分数第一个地址赋给指针X
for(i = 1; i < N; ++i)
{
if(n < *(x + i)) //取+1地址位的值与前位地址位的值对比,如果大于前位地址的值,就将值赋给前位地址
{
n = *(x + i);
}
}
return (n); //返回最大的值给主调函数
}
double min(double * x) //将分数第一位地址赋给指针x
{
double n;
int i;
n = *x;
for(i = 1; i < N; ++i)
{
if(n > *(x + i))
{
n = *(x + i);
}
}
return (n);
}
double ping(double * x)
{
double n = 0;
int i;
for(i = 0; i < N; ++i)
{
n += *(x + i);
}
n = (n-max(x)-min(x))/(double)(N - 2);
return (n);
}
void pai(double * x, char **y) //使用直接对比进行排序
{
double temp;
char *t;
int i, j;
for(i = 0; i < N-1; ++i)
{
for(j = i+1; j < N ; ++j)
{
if(x[i]< x[j])
{
temp = x[i];
x[i] = x[j];
x[j] = temp;
t = y[i];
y[i] = y[j];
y[j] = t;
}
}
}
}
|