任何类型的一般分类,与结构斗争

时间:2015-05-17 19:56:51

标签: c sorting struct

我在为任何类型实现常规排序算法时遇到一些麻烦。我已经完成了常规排序,但我无法弄清楚如何为我所制作的结构编写比较函数。结构是汽车,具有名称,型号年份和价格。我正在比较这些年份,并按升序排序。

到目前为止,我已经编写了用于排序字符串的函数,这些函数与通用算法一起使用。我相信一般排序算法是正确编写的,它是在sort.c

中设计的
#include "sort.h"
#include <string.h>


/* Swap two pointers.                                                                                                                                                                                          */
static
void swap(void** left, void** right) {
  void* temp = *left;
  *left = *right;
  *right = temp;
}

/* Sort Array                                                                                                                           
 * This function sorts the data stored in the array.
 * The actual sorting routine is                                                                    
 * Bubble-Sort.                                                                                                                         
 */
void sort_array(void* Array[], unsigned size, int (*compare)(void*,void*))
{
  int i;
  int have_swapped = 1;

  while (have_swapped) {
    have_swapped = 0;
    for (i = 0; i < size - 1; ++i ){
      if (compare(Array[i],Array[i+1])) {
        swap(&Array[i+1], &Array[i]);
        have_swapped = 1;
      }
    }
  }
}

我认为我已经正确编写了compare_structs的代码,但是当我尝试运行程序时,它会在终端中进入无限循环。我不知道为什么会这样做。

我正在尝试学习C并将指针/函数作为参数传递。我想编写这个compare_structs程序,使其符合sort.c中的常规排序算法,因此我认为必须返回-1才能进行交换。我找不到导致无限循环的错误。任何帮助表示赞赏!

以下是sort_structs.c

#include <stdio.h>
#include <string.h>
#include "sort.h"
#include <stdlib.h>

/* Automobile */
struct automobile {
  const char* name;
  unsigned year;
  unsigned price;
};

struct automobile one = { "AMC Pacer", 1975, 12900 };
struct automobile two = { "Cadillac Fleetwood", 1981, 4995 };
struct automobile three = { "Ford Pinto", 1971, 4200 };
struct automobile four = { "Suzuki X90", 1996, 1625 };
struct automobile five = { "Chrysler TC", 1991, 2495 };
struct automobile six = { "Cadillac Cimarron", 1986, 4990 };
struct automobile seven = { "Plymouth Prowler", 1997, 60000 };
struct automobile eight =  { "Ford Edsel", 1958, 17000 };
struct automobile nine =  { "Yugo", 1985, 3990 };
struct automobile ten =  { "Pontiac Aztek", 2001, 603 };

/* Test Data
 * Here I'm creating an array that points to the structures defined
 */
unsigned data_size = 10;
struct automobile* data[10] = {
  &one,
  &two,
  &three,
  &four,
  &five,
  &six,
  &seven,
  &eight,
  &nine,
  &ten
};

static
int compare_structs(void* left, void* right) {
  struct automobile *x = left;
  struct automobile *y = right;
  int xYear = x->year;
  int yYear = y->year;
  if (xYear > yYear) return -1;
}

/* Test program
 *
 * This program tests sort_array with an array of automobile objects.  Or
 * rather, an array of pointers to automobile objects.
 */
int main() {
  int i;
  int status = EXIT_SUCCESS;

  sort_array((void**)data, data_size, &compare_structs);
for(i = 0; i < data_size - 1; ++i) {
    if (data[i]->year > data[i+1]->year)  {
      fprintf(stderr, "\"%s\" and \"%s\" are out of order\n",data[i]->name, data[i+1]->name);
      status = EXIT_FAILURE;
    }
  }

  return status;
}

1 个答案:

答案 0 :(得分:1)

为了让代码正常运行,我做了很多更改。我会尽我所能回忆并解释每一个。

<强> 1。调用sort_array

你最初这样叫sort_array:

sort_array((void**)data, data_size, &compare_structs);

虽然它需要(a)将数据变量转换为void *并且(b)在compare-function之前不需要address-of运算符。 (如果您引用一个函数但不调用它,则该语句将作为函数的地址进行求值)

结果是:

sort_array((void*)data, data_size, compare_structs);

<强> 2。从compare_structs返回值 如果left的年份值大于right的年份值,则只返回compare_structs的值。您应该返回3个值中的1个。 -1,0和1以便于升序/降序排序,0表示不需要交换。

if (xYear > yYear) return -1;

变为

return (xYear - yYear);

第3。检查比较的返回值 您最初只检查返回是否是某种东西。您可以检查大于0或小于0以允许升序/降序排序。 因此,

if (compare(Array[i],Array[i+1]))

变为(按升序排序)

if (compare(Array[i],Array[i+1]) > 0)

整理这些修改并使用小mod运行结果以打印输出,导致以下内容被打印到控制台。

0. - 1958
1. - 1971
2. - 1975
3. - 1981
4. - 1985
5. - 1986
6. - 1991
7. - 1996
8. - 1997
9. - 2001

最后,这是完整的代码:

#include <stdio.h>
#include <string.h>
//#include "sort.h"
#include <stdlib.h>
//#include <string.h>


/* Swap two pointers.                                                                                                                                                                                          */
static
void swap(void** left, void** right)
{
    void* temp = *left;
    *left = *right;
    *right = temp;
}

/* Sort Array
 * This function sorts the data stored in the array.
 * The actual sorting routine is
 * Bubble-Sort.
 */
void sort_array(void* Array[], unsigned size, int (*compare)(void*,void*))
{
    int i;
    int have_swapped = 1;

    while (have_swapped)
    {
        have_swapped = 0;
        for (i = 0; i < size - 1; ++i )
        {
            if (compare(Array[i],Array[i+1]) > 0)
            {
                swap(&Array[i+1], &Array[i]);
                have_swapped = 1;
            }
        }
    }
    i = 100;
}





/* Automobile

 */
struct automobile
{
    const char* name;
    unsigned year;
    unsigned price;
};

struct automobile one =
{
    "AMC Pacer",
    1975,
    12900
};

struct automobile two =
{
    "Cadillac Fleetwood",
    1981,
    4995
};

struct automobile three =
{
    "Ford Pinto",
    1971,
    4200
};

struct automobile four =
{
    "Suzuki X90",
    1996,
    1625
};

struct automobile five =
{
    "Chrysler TC",
    1991,
    2495
};

struct automobile six =
{
    "Cadillac Cimarron",
    1986,
    4990
};

struct automobile seven =
{
    "Plymouth Prowler",
    1997,
    60000
};

struct automobile eight =
{
    "Ford Edsel",
    1958,
    17000
};

struct automobile nine =
{
    "Yugo",
    1985,
    3990
};

struct automobile ten =
{
    "Pontiac Aztek",
    2001,
    603
};

/* Test Data
 * Here I'm creating an array that points to the structures defined
 */
unsigned data_size = 10;
struct automobile* data[10] =
{
    &one,
    &two,
    &three,
    &four,
    &five,
    &six,
    &seven,
    &eight,
    &nine,
    &ten
};

static
int compare_structs(void* left, void* right)
{
    struct automobile *x = left;
    struct automobile *y = right;
    int xYear = x->year;
    int yYear = y->year;
    //if (xYear > yYear) return -1;
    return (xYear - yYear);
}
/* Test program
 *
 * This program tests sort_array with an array of automobile objects.  Or
 * rather, an array of pointers to automobile objects.
 */
int main()
{
    int i;
    int status = EXIT_SUCCESS;

    sort_array((void*)data, data_size, compare_structs);

    for(i = 0; i < data_size - 1; ++i)
    {
        if (data[i]->year > data[i+1]->year)
        {
            fprintf(stderr, "\"%s\" and \"%s\" are out of order\n",data[i]->name, data[i+1]->name);
            status = EXIT_FAILURE;
        }
    }

    for (i=0; i<data_size; i++)
        printf("%d. - %d\n", i, data[i]->year);

    return status;
}