简体   繁体   English

用通用大小对结构数组进行排序

[英]Sort an array of struct with generic size

I am new to C and I guess my code is far away of best practice.. 我是C语言的新手,我想我的代码与最佳实践相去甚远。

I have an Array of struct in my C programm. 我的C程序中有一个结构数组。 The size of the array is 255 (allocated) but not all of it is used. 数组的大小为255(已分配),但并未全部使用。

the Array in my eample is filled with following vaues: 示例中的Array充满了以下信息:

2;a;121212;121212;0
9;c;121212;121212;1
6;d;121212;121212;1
4;e;121212;121212;1
1;v;121212;121212;1
8;x;121212;121212;1

the rest is filled with null values.. (I guess) 其余的都填充为空值。(我猜)

Now my problem is not with the actual sorting. 现在我的问题不在于实际排序。 It's when I start sorting, it will come to the point of taking an empty Index of my Array and faulting. 从我开始排序的时候,我就得出了一个空的Array索引并出错的地步。

the array of books: 书的排列:

struct book{
    int ID;
    char name[MAX_STR_LEN];
    char dateIn[DATE_LEN];
    char dateOut[DATE_LEN];
    int isIn;
};


/* array of my books */
struct book books[MAX_BOOKS];

The sorting function: 排序功能:

void sort()
{
    /* first find out how many indexes there are */
    int h;
    for (h = 0; h< MAX_BOOKS; h++)
    {
        if (books[h].ID == 0)
        {
            break;
        }
    }

    int j =  0;

    int swaped = 1;
    struct book temp;
    while (swaped == 1)       //bubble sort on the book name
    {
        for(j=0;j< h ;j++)
        {
            swaped = 0;
            if(strcmp(books[j].name,books[j + 1].name)>0)
            {

                //copy to temp val
                temp.ID = books[j].ID;
                strcpy(temp.name,books[j].name);
                strcpy(temp.dateIn,books[j].dateIn);
                strcpy(temp.dateOut,books[j].dateOut);
                temp.isIn = books[j].isIn;


                //copy next val
                books[j].ID = books[j + 1].ID;
                strcpy(books[j].name,books[j + 1].name);
                strcpy(books[j].dateIn,books[j + 1].dateIn);
                strcpy(books[j].dateOut,books[j + 1].dateOut);
                books[j].isIn = books[j + 1].isIn;


                //copy back temp val
                books[j + 1].ID = temp.ID;
                strcpy(books[j+ 1].name,temp.name);
                strcpy(books[j + 1].dateIn,temp.dateIn);
                strcpy(books[j + 1].dateOut,temp.dateOut);
                books[j + 1].isIn = temp.isIn;


                swaped = 1;

            }
        }
    }

}

So my question would be if its possible to sort only values with where the ID is not 0? 所以我的问题是,是否可以仅对ID不为0的值排序?

If you need more information, I will try to deliver.. 如果您需要更多信息,我将尽力提供。

Thanks in advance! 提前致谢!

for(j=0;j< h ;j++)
{
    if(strcmp(books[j].name,books[j + 1].name)>0)
    {

Here you are taking j from 0 to h-1 , inclusive. 在这里,您将j0h-1 (含)。 So you are trying to compare for j == h-1 the last element books[j] with the next one books[j+1] , which does not exist. 因此,您尝试将j == h-1的最后一个元素books[j]与不存在的下一books[j+1] You probably only want to check up to the pair h-2 / h-1 , that is for(j=0; j<h-1; j++) . 您可能只想检查h-2 / h-1 ,即for(j=0; j<h-1; j++)

Why you are doing such big calculation for swaping? 为什么您要进行如此大的交换计算? why dont you just do 你为什么不做

if(strcmp(books[j].name,books[j + 1].name)>0){

   temp = books[j];
   books[j] = books[j+1];
   books[j+1] = temp;
} 

And it is possible to sort your structure array. 并且可以对结构数组进行排序。 If you are able to do swaping on struct then you can do any kind of sorting algorithm. 如果您能够在struct上进行交换,则可以执行任何种类的排序算法。

change this condition if(strcmp(books[j].name,books[j + 1].name)>0) to if(books[j].ID>books[j + 1].id) then you can sort the struct with respect to ID . 将此条件if(strcmp(books[j].name,books[j + 1].name)>0)更改为if(books[j].ID>books[j + 1].id)则可以对关于ID struct。

But follow the @Nabla's answer too. 但是也要遵循@Nabla的回答。

Nabla's answer is right. 纳布拉的答案是正确的。 Also, you set swapped = 0; 此外,您设置swapped = 0; at the wrong place. 在错误的地方。 You have: 你有:

while (swapped == 1)       //bubble sort on the book name
{
    for (j = 0; j < h; j++)
    {
        swapped = 0;

but you want: 但您想要:

while (swapped == 1)       //bubble sort on the book name
{
    swapped = 0;
    for (j = 0; j < h; j++)
    {

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM