使用通用函数查找最大值

时间:2015-09-24 09:52:41

标签: c

我正在为自己编写一些东西,我想写一个简单的泛型函数,它在数组中找到最大值。

ioctl(FP,CDROMEJECT)

函数适用于int和chars,但是当我传递浮点数时,它返回4.4表示浮点数,44.44表示双精度数。我错过了什么?谢谢。

2 个答案:

答案 0 :(得分:2)

你的代码没有意义; memcmp()应该如何计算大于一个字节的最大值?请注意,its return value仅告诉您两个第一个不同的字节之间的关系。这不适用于浮点数据类型,除非你假设是big-endian,否则它不会对整数有效。所以,简而言之,它不起作用。

您必须使用正确的类型和实际的比较运算符来获取正确的代码。它不会很漂亮,但C很难制作出非常通用的代码。

此外,您对maxAdr的处理完全没有意义,并且会泄漏内存。

答案 1 :(得分:1)

问题在于,在泛型函数中,您无法对两个二进制数据块如何相互关联做出任何假设。呼叫者必须告诉您的功能如何操作。

这是如何将程序重新编写为传统的通用C编程:

#include <stdio.h>
#include <stdbool.h>
#include <stdint.h>

// declare a desired format for the generic comparison function:
typedef bool comp_func (const void* p1, const void* p2);

// write comparison functions ("functors") for all valid types:

bool more_int (const void* p1, const void* p2)
{
  return *(int*)p1 > *(int*)p2;
}

bool more_char (const void* p1, const void* p2)
{
  return *(char*)p1 > *(char*)p2;
}

bool more_float (const void* p1, const void* p2)
{
  return *(float*)p1 > *(float*)p2;
}

bool more_double (const void* p1, const void* p2)
{
  return *(double*)p1 > *(double*)p2;
}

// The actual function. 
const void* get_max_value (const void* arr,  // pointer to 1st element (base)
                           size_t arr_size,  // size of array 
                           size_t obj_size,  // size of one object
                           comp_func* more)  // comparison function
{
  const uint8_t* byte = arr;          // can't take the contents of void* so convert to bytes
  const void* max;                    // pointer to the largest value found

  if(more(&byte[0], &byte[obj_size])) // initialize max by comparing the first two objects
  {
    max = &byte[0];
  }
  else
  {
    max = &byte[obj_size];
  }

  for(size_t i=2; i<arr_size; i++) // start at index 2, index 0 and 1 already checked
  {
    if(more(&byte[i*obj_size], max))
    {
      max = &byte[i*obj_size];
    }
  }

  return max;
}


int main (void)
{
  int    iarray[]  = {3, 4, 1, 5, 2};
  char   charray[] = {'A', 'C', 'E', 'B', 'D'};
  float  farray[]  = {2.2f, 5.5f, 3.3f, 4.4f, 1.1f};
  double darray[]  = {11.11, 22.22, 55.55, 33.33, 44.44};
  const void *pMax;

  pMax = get_max_value(iarray,
                       sizeof(iarray)/sizeof(*iarray),
                       sizeof(*iarray),
                       more_int);
  printf("\n%d\n", *(const int*)pMax);

  pMax = get_max_value(charray,
                       sizeof(charray)/sizeof(*charray),
                       sizeof(*charray),
                       more_char);
  printf("\n%c\n", *(const char*)pMax);

  pMax = get_max_value(farray,
                       sizeof(farray)/sizeof(*farray),
                       sizeof(*farray),
                       more_float);
  printf("\n%1.1f\n", *(const float*) pMax);

  pMax = get_max_value(darray,
                       sizeof(darray)/sizeof(*darray),
                       sizeof(*darray),
                       more_double);
  printf("\n%2.2f\n", *(const double*) pMax);

  return 0;
}

输出:

5

E

5.5

55.55
相关问题