C语言设计一个汽车牌照管理系统,要求利用链式基数排序方法实现车牌号的排序,然后利用折半查找法实现对汽车牌照按关键字进行查找,设计一个菜单可以调用这两个功能,并输出车辆信息
时间: 2024-03-14 20:50:00 浏览: 136
好的,我可以帮您解答这个问题。在车牌信息结构体的基础上,我们还需要添加一些其他的信息,例如汽车品牌、颜色、车型等等。完整的结构体定义如下:
```c
typedef struct car_plate {
char plate_number[8]; //车牌号
char owner_name[20]; //车主姓名
char owner_tel[12]; //车主联系电话
char brand[20]; //汽车品牌
char color[10]; //汽车颜色
char model[20]; //汽车型号
struct car_plate *next; //指向下一个结点的指针
} CarPlate;
```
接下来,我们可以设计一个添加车牌信息的函数:
```c
void addCarPlate(CarPlate **head) {
CarPlate *p = (CarPlate *)malloc(sizeof(CarPlate));
printf("Enter plate number: ");
scanf("%s", p->plate_number);
printf("Enter owner name: ");
scanf("%s", p->owner_name);
printf("Enter owner telephone: ");
scanf("%s", p->owner_tel);
printf("Enter car brand: ");
scanf("%s", p->brand);
printf("Enter car color: ");
scanf("%s", p->color);
printf("Enter car model: ");
scanf("%s", p->model);
p->next = NULL;
if (*head == NULL) {
*head = p;
} else {
CarPlate *tail = *head;
while (tail->next != NULL) {
tail = tail->next;
}
tail->next = p;
}
}
```
然后,我们可以利用链式存储结构来存储车牌信息。在此基础上,我们可以利用链式基数排序方法对车牌号进行排序,代码如下:
```c
void radixSort(CarPlate **head) {
const int radix = 10;
int i, j;
CarPlate *p = *head, *tail[radix], *tmp = NULL;
for (i = 0; i < 7; i++) {
for (j = 0; j < radix; j++) {
tail[j] = NULL;
}
while (p != NULL) {
int k = p->plate_number[i] - '0';
if (tail[k] == NULL) {
tail[k] = p;
*head = p;
} else {
tail[k]->next = p;
tail[k] = p;
}
p = p->next;
}
for (j = radix - 1; j >= 0; j--) {
if (tail[j] != NULL) {
tail[j]->next = tmp;
tmp = tail[j];
}
}
p = tmp;
tmp = NULL;
}
}
```
接下来,我们可以利用折半查找法来实现对汽车牌照的查找。代码如下:
```c
CarPlate *binarySearch(CarPlate *head, char *key) {
CarPlate *low = head, *high = NULL;
while (low != high) {
CarPlate *mid = low;
int cmp = strcmp(mid->plate_number, key);
if (cmp < 0) {
low = mid->next;
} else if (cmp > 0) {
high = mid;
} else {
return mid;
}
}
return NULL;
}
```
最后,我们可以设计一个菜单来调用这两个功能,并输出车辆信息,代码如下:
```c
int main() {
CarPlate *head = NULL, *p = NULL;
int choice;
while (1) {
printf("1. Add a car plate\n");
printf("2. Sort car plates\n");
printf("3. Search a car plate\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
addCarPlate(&head);
break;
case 2:
radixSort(&head);
p = head;
while (p != NULL) {
printf("Plate number: %s\n", p->plate_number);
printf("Owner name: %s\n", p->owner_name);
printf("Owner telephone: %s\n", p->owner_tel);
printf("Car brand: %s\n", p->brand);
printf("Car color: %s\n", p->color);
printf("Car model: %s\n", p->model);
printf("\n");
p = p->next;
}
break;
case 3:
char key[8];
printf("Enter the plate number you want to search: ");
scanf("%s", key);
p = binarySearch(head, key);
if (p == NULL) {
printf("Not found!\n");
} else {
printf("Plate number: %s\n", p->plate_number);
printf("Owner name: %s\n", p->owner_name);
printf("Owner telephone: %s\n", p->owner_tel);
printf("Car brand: %s\n", p->brand);
printf("Car color: %s\n", p->color);
printf("Car model: %s\n", p->model);
printf("\n");
}
break;
case 4:
return 0;
default:
printf("Invalid choice!\n");
break;
}
}
return 0;
}
```
以上就是利用链式基数排序方法实现车牌号的排序,并利用折半查找法实现对汽车牌照按关键字进行查找的汽车牌照管理系统,并输出车辆信息。
阅读全文