简体   繁体   English

如何在结构中使用Bubble Sort / qsort()按字母顺序排序

[英]How to use Bubble Sort / qsort() in a struct to sort alphabetically

I need to write a code where in a struct I register the license plate,model, brand, year and daily value of a car. 我需要编写代码以在结构中注册汽车的车牌,型号,品牌,年份和每日价值。 After registering this data, I need to sort the entire struct based on the cars' license plate, and then display on the screen for the user. 注册此数据后,我需要根据汽车的车牌对整个结构进行排序,然后在屏幕上为用户显示。 I am having the same trouble with the Bubble Sort as well as qsort(), it sorts only the first letter of the plate, so if i put " ADB-1234 " for car 1, and " ABC-1234 " for car 2, it won't swap and ADB-1234 will be the first license plate, instead of second. 我在冒泡排序和qsort()上都遇到了同样的麻烦,它仅对车牌的第一个字母进行排序,因此,如果我将“ ADB-1234 ”用于汽车1,将“ ABC-1234 ”用于汽车2,它不会交换, ADB-1234将是第一个牌照,而不是第二个。

It should be like this, for example: 应该是这样的,例如:

INPUT INPUT

Car 1

car plate: AGH-1234
car model: GTR
car brand: Nissan
car year: 2016
daily value of car: 100

Car 2

car plate: ABC-1234
car model: Corolla
car brand: Toyota
car year: 2014
daily value of car:50

Ordering... 订购...

OUTPUT OUTPUT

Car 2
car plate: ABC-1234
car model: Corolla
car brand: Toyota
car year: 2014
daily value of car:50

Car 1

car plate: AGH-1234
car model: GTR
car brand: Nissan
car year: 2016
daily value of car: 100

Here's the code with Bubble Sort: 这是Bubble Sort的代码:

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

int main()
{

struct carro // STRUCT
{
    char placa[50];
    char marca[50];
    char modelo[50];
    char ano[50];
    char valordiaria[50];
};

struct carro car[3];


int x=0; //COUNTER



for(x=0; x<3; x++) // CAR REGISTER
{

    printf("\nCarro: %d", (x+1));

    printf("\nPlaca: ");
    gets(car[x].placa);
    fflush(stdin);

    printf("Marca: ");
    gets(car[x].marca);
    fflush(stdin);

    printf("Modelo: ");
    gets(car[x].modelo);
    fflush(stdin);

}

              //BUBBLESORT******

struct carro hold;
int o, pass;
int i=0;

for ( pass = 0; pass < 3 ; pass++ )//number of passes
    for ( o = 0; o < 3 - pass; o++ )//single pass
        if ( strcmp( car[o].placa, car[o+1].placa ) > 0)  
        {
            hold = car[o];
            car[o] = car[o+1];//swap array elements if statement is true
            car[o+1] = hold;
        }

printf("\n\nNomes em ordem alfabetica: \n");
for(x=0; x<3; x++) // MOSTRA NA TELA A STRUCT ORDENADA
{
    printf("\n\n\nCarro: %d", (x+1));
    printf("\nPlaca: %s", car[x].placa);
    printf("\nMarca: %s", car[x].marca);
    printf("\nModelo: %s", car[x].modelo);
    printf("\nAno: %s", car[x].ano);

}

}  

Here's the code using qsort() : 这是使用qsort()的代码:

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


typedef struct   // STRUCT
{
    char placa[50];
    char marca[50];
    char modelo[50];
    char ano[50];
    char valordiaria[50];
} carro;
carro car[3];

int compare (const void * a, const void * b)
{

    carro *carroA = (carro *)a;
    carro *carroB = (carro *)b;



    return ( carroB->placa - carroA->placa );
}


int main()
{
    //struct carro car[3];

    int x=0; //COUNTER

    for(x=0; x<3; x++) // CAR REGISTER
    {

        printf("\nCarro: %d", (x+1));

        printf("\nPlaca: ");
        scanf("%s",car[x].placa);
        //fflush(stdin);

        printf("Marca: ");
        scanf("%s",car[x].marca);
        //fflush(stdin);

        //printf("Modelo: ");
        //scanf("%s",car[x].modelo);
        //fflush(stdin);
        /*
                printf("Ano: ");
                gets(car[x].ano);
                fflush(stdin);

                printf("Valor da diaria: ");
                fflush(stdin);
                gets(car[x].valordiaria);
                */
    }


    qsort (car, 3, sizeof(carro), compare);

    printf("\n\nSTRUCT ORDENADA: \n");
    for(x=0; x<3; x++) // MOSTRA NA TELA A STRUCT ORDENADA
    {
        printf("\n\n\nCarro: %d", (x+1));
        printf("\nPlaca: %s", car[x].placa);
        printf("\nMarca: %s", car[x].marca);
        //printf("\nModelo: %s", car[x].modelo);
        //printf("\nAno: %s", car[x].ano);
        //printf("\Valor da diaria: %s", car[x].valordiaria);
    }
}

Your problem is here: 您的问题在这里:

return ( carroB->placa - carroA->placa );

The placa field is a pointer - C doesn't have real strings. placa字段是一个指针-C没有真正的字符串。 And subtracting one pointer from another doesn't compare the strings, it subtracts the memory addresses of the two pointers. 从另一个指针减去一个指针不会比较字符串,而是减去两个指针的内存地址。

You should instead compare the strings using the strcmp function. 您应该使用strcmp函数来比较字符串。 I'll leave it to you to read the docs on that one. 我将留给您阅读有关该文档的文档。

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

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