用java编写一个程序,输入药品编号、药名、药品单价、销售量、销售额存储在顺序表中,药品编号共4位,采用字母和数字混合编号,如:A125,前一位为大写字母,后三位为数字,在本系统中,对药品单价的排序采用冒泡排序法,对销售量的排序采用快速排序法,对销售额的排序采用堆排序法,对药品编号进行排序时,可采用多种排序法。
时间: 2024-01-06 12:04:57 浏览: 77
Java 冒泡法,选择法,插入法,快速排序法,实现代码。
5星 · 资源好评率100%
好的,根据您的要求,我可以给您提供以下Java程序:
```java
import java.util.Scanner;
public class MedicineSales {
private String[] medicineID; //药品编号数组
private String[] medicineName; //药品名称数组
private double[] unitPrice; //药品单价数组
private int[] salesVolume; //销售量数组
private double[] salesAmount; //销售额数组
private int size; //顺序表大小
public MedicineSales(int capacity) {
medicineID = new String[capacity];
medicineName = new String[capacity];
unitPrice = new double[capacity];
salesVolume = new int[capacity];
salesAmount = new double[capacity];
size = 0;
}
//添加药品信息
public void addMedicine(String id, String name, double price, int volume) {
medicineID[size] = id;
medicineName[size] = name;
unitPrice[size] = price;
salesVolume[size] = volume;
salesAmount[size] = price * volume;
size++;
}
//冒泡排序:按照药品单价从小到大排序
public void bubbleSortByUnitPrice() {
for (int i = 0; i < size - 1; i++) {
for (int j = 0; j < size - i - 1; j++) {
if (unitPrice[j] > unitPrice[j+1]) {
swap(j, j+1);
}
}
}
}
//快速排序:按照销售量从大到小排序
public void quickSortBySalesVolume(int left, int right) {
if (left < right) {
int pivotIndex = partition(left, right);
quickSortBySalesVolume(left, pivotIndex - 1);
quickSortBySalesVolume(pivotIndex + 1, right);
}
}
private int partition(int left, int right) {
int pivotIndex = (left + right) / 2;
int pivotValue = salesVolume[pivotIndex];
swap(pivotIndex, right);
int storeIndex = left;
for (int i = left; i < right; i++) {
if (salesVolume[i] > pivotValue) {
swap(i, storeIndex);
storeIndex++;
}
}
swap(storeIndex, right);
return storeIndex;
}
//堆排序:按照销售额从大到小排序
public void heapSortBySalesAmount() {
buildMaxHeap();
for (int i = size - 1; i >= 1; i--) {
swap(0, i);
maxHeapify(0, i);
}
}
private void buildMaxHeap() {
for (int i = size / 2; i >= 0; i--) {
maxHeapify(i, size);
}
}
private void maxHeapify(int index, int heapSize) {
int left = index * 2 + 1;
int right = index * 2 + 2;
int largest = index;
if (left < heapSize && salesAmount[left] > salesAmount[largest]) {
largest = left;
}
if (right < heapSize && salesAmount[right] > salesAmount[largest]) {
largest = right;
}
if (largest != index) {
swap(index, largest);
maxHeapify(largest, heapSize);
}
}
//选择排序:按照药品编号从小到大排序
public void selectionSortByMedicineID() {
for (int i = 0; i < size - 1; i++) {
int minIndex = i;
for (int j = i + 1; j < size; j++) {
if (medicineID[j].compareTo(medicineID[minIndex]) < 0) {
minIndex = j;
}
}
if (minIndex != i) {
swap(i, minIndex);
}
}
}
private void swap(int index1, int index2) {
String tempID = medicineID[index1];
medicineID[index1] = medicineID[index2];
medicineID[index2] = tempID;
String tempName = medicineName[index1];
medicineName[index1] = medicineName[index2];
medicineName[index2] = tempName;
double tempPrice = unitPrice[index1];
unitPrice[index1] = unitPrice[index2];
unitPrice[index2] = tempPrice;
int tempVolume = salesVolume[index1];
salesVolume[index1] = salesVolume[index2];
salesVolume[index2] = tempVolume;
double tempAmount = salesAmount[index1];
salesAmount[index1] = salesAmount[index2];
salesAmount[index2] = tempAmount;
}
//输出顺序表中的药品信息
public void print() {
System.out.println("药品编号\t药品名称\t药品单价\t销售量\t销售额");
for (int i = 0; i < size; i++) {
System.out.printf("%-10s\t%-10s\t%-8.2f\t%-6d\t%-8.2f\n", medicineID[i], medicineName[i], unitPrice[i], salesVolume[i], salesAmount[i]);
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入顺序表大小:");
int capacity = scanner.nextInt();
MedicineSales medicineSales = new MedicineSales(capacity);
for (int i = 0; i < capacity; i++) {
System.out.print("请输入药品编号:");
String id = scanner.next();
System.out.print("请输入药品名称:");
String name = scanner.next();
System.out.print("请输入药品单价:");
double price = scanner.nextDouble();
System.out.print("请输入销售量:");
int volume = scanner.nextInt();
medicineSales.addMedicine(id, name, price, volume);
}
System.out.println("按照药品单价从小到大排序:");
medicineSales.bubbleSortByUnitPrice();
medicineSales.print();
System.out.println("按照销售量从大到小排序:");
medicineSales.quickSortBySalesVolume(0, capacity - 1);
medicineSales.print();
System.out.println("按照销售额从大到小排序:");
medicineSales.heapSortBySalesAmount();
medicineSales.print();
System.out.println("按照药品编号从小到大排序:");
medicineSales.selectionSortByMedicineID();
medicineSales.print();
}
}
```
这个程序使用了顺序表来存储药品信息,并实现了四种排序算法来对药品信息进行排序。您可以根据需要修改程序来适应您的实际情况。
阅读全文