在C

时间:2015-11-29 03:37:11

标签: c string sorting structure

我正在尝试编写一个Function,它从结构中获取一组字符串,并按字母顺序组织结构。以下是我到目前为止的情况:

// Preconditions: array of structure "Shift"
// integer value indicating number of shifts
// Postconditions: none - this function does not return anything.
// Actions: Sort the shifts by the TA's first name.

void sort_data(struct Shift shift_data[], int *num_shifts)
{
int i,j;

    for(i=0; i<num_shifts; i++)
        for (j=0; j<num_shifts; j++)
        {
        if (strcmp(shift_data[i+1].name, shift_data[i].name) < 0)
              {
                  temp[i]=shift_data[i];
                  shift_data[i]=shift_data[i+1];
                  shift_data[i]=temp[i];
              }

        }


}

我不确定是否需要嵌套循环。我确信正在读取和指向num_shifts和shift数据。我还在我的代码顶部声明了我的结构和结构相关变量:

struct Shift
{
char name[100];
char day_of_week[100];
int start_hour;
int end_hour;
};

struct Shift shift_data[100];
struct Shift temp[100];

很抱歉没有说明这一点,但我需要使用这三个功能:

int read_data(struct Shift shift_data[], int *num_shifts);
void sort_data(struct Shift shift_data[], int *num_shifts);
void print_data(struct Shift shift[], int *num_shifts);

我可以添加其他内容,但不需要。

我需要弄清楚如何根据存储在shift_data [i] .name

中存储的函数中的字符串类型对结构进行排序

提前感谢您的帮助

这就是完成的排序功能:

// Preconditions: array of structure "Shift"
// integer value indicating number of shifts
// Postconditions: none - this function does not return anything.
// Actions: Sort the shifts by the TA's first name.

void sort_data(struct Shift shift_data[], int *num_shifts)
{
    int i, changed;
    do
    {
        changed = 0;
        for (i=0; i < (*num_shifts) - 1; i++)
        {
            if (strcmp(shift_data[i].name, shift_data[i+1].name) > 0)
            {
                memcpy(&temp, shift_data + i, sizeof (struct Shift));
                memcpy(shift_data + i, shift_data + i + 1, sizeof (struct Shift));
                memcpy(shift_data + i + 1, &temp, sizeof (struct Shift));
                changed = 1;
           }
        }
    } while (changed != 0);
}

谢谢大家的帮助

2 个答案:

答案 0 :(得分:1)

最简单的方法应该是调用qsort()

/* for strcmp() */
#include <string.h>
/* for qsort() */
#include <stdlib.h>

int cmpShift(const void* lhs, const void* rhs)
{
    return strcmp(((struct Shift*)lhs)->name, ((struct Shift*)rhs)->name);
}

void sort_data(struct Shift shift_data[], int *num_shifts)
{
    qsort(shift_data, *num_shifts, sizeof(struct Shift), cmpShift);
}

答案 1 :(得分:1)

这是一个非常基本的冒泡排序,与更高级的排序算法相比非常低效,并且仍然复制整个结构而不是指针,但它比你开始的更接近:

struct Shift shift_data[100];
struct Shift temp;

void sort_data(struct Shift shift_data[], int *num_shifts)
{
    int i, changed;
    do
    {
        changed = 0;
        for (i=0; i < (*num_shifts) - 1; i++)
        {
            if (strcmp(shift_data[i].name, shift_data[i+1].name) > 0)
            {
                memcpy(&temp, shift_data + i, sizeof (struct Shift));
                memcpy(shift_data + i, shift_data + i + 1, sizeof (struct Shift));
                memcpy(shift_data + i + 1, &temp, sizeof (struct Shift));
                changed = 1;
           }
        }
    } while (changed != 0);
}

基本思想是它扫描数组,将每个项目与下一个项目进行比较。每当它注意到一个故障时,它就会交换它们。在随后的通过中,应该在前面的物品漂浮到前面,如“泡沫”,对于应该在后面的物品,它们是相同的。该函数保留变量changed以确定是否对最后一次通过数组进行了任何更改 - 当它通过整个数组而不更改任何内容时,循环可以退出,因为数组已排序。

请注意,当您传入指针int *num_shifts时,需要使用*num_shifts取消引用它以访问它指向的int

您并不需要提及read_data()print_data()函数,它们与排序无关,这就是您所询问的内容。

你的函数的// Postconditions:之一肯定是数组已经排序了吗?