我需要编写一个代码,在结构中我记录汽车的车牌,型号,品牌,年份和每日价值。注册这些数据后,我需要根据汽车对整个结构进行排序。车牌,然后在屏幕上显示给用户。我在冒泡排序以及qsort()时遇到同样的问题,它只对盘子的第一个字母进行排序,所以如果我放置" ADB-1234 "对于汽车1," ABC-1234 "对于汽车2,它不会互换,而 ADB-1234 将是第一个牌照,而不是第二个。
应该是这样的,例如:
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
...排序
输出
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
这里是使用冒泡排序的代码:
#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);
}
}
这里是使用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);
}
}
答案 0 :(得分:1)
你的问题在这里:
return ( carroB->placa - carroA->placa );
placa
字段是指针 - C没有真正的字符串。从另一个指针中减去一个指针并不比较字符串,它会减去两个指针的内存地址。
您应该使用strcmp
函数比较字符串。我会留给你阅读那篇文章。