简体   繁体   中英

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.

It should be like this, for example:

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

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:

#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() :

#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. 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. I'll leave it to you to read the docs on that one.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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