c++利用函数重载求最大值,最小值,平均值和中值
c++利用函数重载求最大值,最小值,平均值和中值重点根本就是排序,什么重载都是细枝末节
本帖最后由 jhq999 于 2021-11-19 12:55 编辑double GetVal(double invals[],int count )
{
int i=0,j=0,flag=1;
double sum=0,tmp=0;
for(i=0;i<count;i++)
{
flag=1;
for(j=0;j<count-i;j++)
{
if(0==i)sum+=invals;
if(invals<invals)
{
flag=0;
tmp=invals;invals=invals;invals=tmp;
}
}
if(flag)break;
}
return sum/(double)count;
}
double GetVal(double invals[],int count,bool isoddnum)
{
if(isoddnum)return invals;
return ( invals+ invals)/2;
}
void GetVal(double invals[],int count,double outvals[] )
{
outvals=GetVal(invals,count);
outvals=invals;
outvals=invals;
outvals=GetVal(invals,count,count%2);
}
int main()
{
double a[]={6,2,4,3,4,7,1,5},b={0};
GetVal(a,sizeof(a)/sizeof(double),b);
printf("最小:%.2lf",b);
printf("最大:%.2lf",b);
printf("平均值:%.2lf",b);
printf("中值:%.2lf",b);
return 0;
}
页:
[1]