酷代码 AI
菜单
全部AI  /  热门  /  AI写代码神器
服务商

使用c语言写出一个药品排序模块:(1)根据药品条形码排序 (2)根据药品库存数量排序(3) 根据药品生产日期排序,屏幕上输出排序后的药品信息。

以下是一个使用C语言编写的药品排序模块,包括根据药品条形码、库存数量和生产日期进行排序的功能。 ```c #include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct { char barcode[20]; int quantity; char productionDate[10]; } Medicine; void sortMedicinesByBarcode(Medicine *medicines, int n) { for (int i = 0; i < n - 1; i++) { for (int j = 0; j < n - i - 1; j++) { if (strcmp(medicines[j].barcode, medicines[j + 1].barcode) > 0) { Medicine temp = medicines[j]; medicines[j] = medicines[j + 1]; medicines[j + 1] = temp; } } } } void sortMedicinesByQuantity(Medicine *medicines, int n) { for (int i = 0; i < n - 1; i++) { for (int j = 0; j < n - i - 1; j++) { if (medicines[j].quantity > medicines[j + 1].quantity) { Medicine temp = medicines[j]; medicines[j] = medicines[j + 1]; medicines[j + 1] = temp; } } } } void sortMedicinesByProductionDate(Medicine *medicines, int n) { for (int i = 0; i < n - 1; i++) { for (int j = 0; j < n - i - 1; j++) { if (strcmp(medicines[j].productionDate, medicines[j + 1].productionDate) > 0) { Medicine temp = medicines[j]; medicines[j] = medicines[j + 1]; medicines[j + 1] = temp; } } } } void printMedicines(Medicine *medicines, int n) { printf("Barcode\tQuantity\tProduction Date\n"); for (int i = 0; i < n; i++) { printf("%s\t%d\t%s\n", medicines[i].barcode, medicines[i].quantity, medicines[i].productionDate); } } int main() { int n; printf("Enter the number of medicines: "); scanf("%d", &n); Medicine *medicines = (Medicine *)malloc(n * sizeof(Medicine)); printf("Enter the details of each medicine:\n"); for (int i = 0; i < n; i++) { printf("Medicine %d:\n", i + 1); printf("Barcode: "); scanf("%s", medicines[i].barcode); printf("Quantity: "); scanf("%d", &medicines[i].quantity); printf("Production Date: "); scanf("%s", medicines[i].productionDate); } printf("\nSorting by Barcode:\n"); sortMedicinesByBarcode(medicines, n); printMedicines(medicines, n); printf("\nSorting by Quantity:\n"); sortMedicinesByQuantity(medicines, n); printMedicines(medicines, n); printf("\nSorting by Production Date:\n"); sortMedicinesByProductionDate(medicines, n); printMedicines(medicines, n); free(medicines); return 0; } ``` 这个程序首先定义了一个`Medicine`结构体,包含药品的条形码、库存数量和生产日期信息。然后实现了三个排序函数,分别按照条形码、库存数量和生产日期对药品数组进行排序。最后在`main`函数中获取用户输入的药品信息,并调用相应的排序函数进行排序,然后输出排序后的药品信息。 [2024-06-01 19:17:20 | AI写代码神器 | 1025点数解答]

相关提问