新手问一道很弱的C++

来源:百度知道 编辑:UC知道 时间:2024/06/15 05:39:19
#include <iostream.h>
#include <stdlib.h>

float max(const void* a, const void* d);
float list[10]={32.1,456.87,332.67,442.0,98.12,451.79,340.12,54.55,99.87,72.5};

void main()
{
qsort(list, 10, sizeof(float), max);

for(int i=0; i< 10; i++)
cout<< list[i]<<" ";
cout<< endl;
}

float max(const void* a, const void* b)
{
return *(float*)a-*(float*)b;
}

谁帮我看看这道题怎么改
错误如下
C:\Documents and Settings\832v1.cpp(5) : warning C4305: 'initializing' : truncation from 'const double' to 'float'
C:\Documents and Settings\832v1.cpp(5) : warning C4305: 'initializing' : truncation from 'const double' to 'float'
C:\Documents and Settings\832v1.cpp(5) : warning C4305: 'initializing' : truncation from 'const double' to 'float'
C:\Documen

error C2664: 'qsort' : cannot convert parameter 4 from 'float (const void *,const void *)' to 'int (__cdecl *)(const void *,const void *)'

max()函数只能返回int型(qsort要求的),
int max(const void* a, const void* b)
{
if (*a > *b)
return 1;
else if(*a < *b)
return -1;

return 0;//为了很少出现的 double 类型相等的情况
}

warning C4305:由于float类型比double所表示的数据值范围小,所以double强转为float时可能发生数据截断。

error C2664: 把MAX函数的返回值改为int。

PS:没有编译过你自己试一下。