数据文件中读出各药品的信息记录,存储在顺序表中。各药品的信息包括:药品编号、药名、药品单价、销出数量、销售额。药品编号共4位,采用字母和数字混合编号,如:A125,前一位为大写字母,后三位为数字,按药品编号进行排序时,可采用多种排序法。对各药品的单价、销售量或销售额进行排序时,可采用多种排序方法,如直接插入排序、冒泡排序、快速排序,直接选择排序等方法。在本系统中,对单价的排序采用冒泡排序法,对销售量的排序采用快速排序法,对销售额的排序采用堆排序法。
时间: 2024-04-04 14:34:15 浏览: 156
好的,这是一个非常具体的问题,以下是可能的Java代码实现:
药品类:
```Java
public class Medicine {
private String medNo; //药品编号
private String medName; //药名
private double price; //药品单价
private int quantity; //销出数量
private double sales; //销售额
public Medicine(String medNo, String medName, double price, int quantity, double sales) {
this.medNo = medNo;
this.medName = medName;
this.price = price;
this.quantity = quantity;
this.sales = sales;
}
public String getMedNo() {
return medNo;
}
public String getMedName() {
return medName;
}
public double getPrice() {
return price;
}
public int getQuantity() {
return quantity;
}
public double getSales() {
return sales;
}
}
```
顺序表类:
```Java
public class MedicineList {
private Medicine[] data; //存储药品信息的数组
private int size; //顺序表实际长度
public MedicineList(int maxSize) {
data = new Medicine[maxSize];
size = 0;
}
//按药品编号排序
public void sortByMedNo() {
for (int i = 0; i < size - 1; i++) {
for (int j = 0; j < size - 1 - i; j++) {
if (data[j].getMedNo().compareTo(data[j + 1].getMedNo()) > 0) {
Medicine temp = data[j];
data[j] = data[j + 1];
data[j + 1] = temp;
}
}
}
}
//按单价排序
public void sortByPrice() {
for (int i = 0; i < size - 1; i++) {
for (int j = 0; j < size - 1 - i; j++) {
if (data[j].getPrice() > data[j + 1].getPrice()) {
Medicine temp = data[j];
data[j] = data[j + 1];
data[j + 1] = temp;
}
}
}
}
//按销售量排序
public void sortByQuantity() {
quickSortQuantity(data, 0, size - 1);
}
//快速排序
private void quickSortQuantity(Medicine[] array, int low, int high) {
if (low < high) {
int pivot = partitionQuantity(array, low, high);
quickSortQuantity(array, low, pivot - 1);
quickSortQuantity(array, pivot + 1, high);
}
}
private int partitionQuantity(Medicine[] array, int low, int high) {
int pivot = array[low].getQuantity();
while (low < high) {
while (low < high && array[high].getQuantity() >= pivot) {
high--;
}
array[low] = array[high];
while (low < high && array[low].getQuantity() <= pivot) {
low++;
}
array[high] = array[low];
}
array[low] = new Medicine("", "", pivot, 0, 0);
return low;
}
//按销售额排序
public void sortBySales() {
heapSortSales(data);
}
//堆排序
private void heapSortSales(Medicine[] array) {
int n = array.length;
for (int i = n / 2 - 1; i >= 0; i--) {
heapifySales(array, n, i);
}
for (int i = n - 1; i >= 0; i--) {
Medicine temp = array[0];
array[0] = array[i];
array[i] = temp;
heapifySales(array, i, 0);
}
}
private void heapifySales(Medicine[] array, int n, int i) {
int largest = i;
int l = 2 * i + 1;
int r = 2 * i + 2;
if (l < n && array[l].getSales() > array[largest].getSales()) {
largest = l;
}
if (r < n && array[r].getSales() > array[largest].getSales()) {
largest = r;
}
if (largest != i) {
Medicine temp = array[i];
array[i] = array[largest];
array[largest] = temp;
heapifySales(array, n, largest);
}
}
//添加药品信息
public void addMedicine(Medicine medicine) {
data[size++] = medicine;
}
//输出药品信息
public void print() {
for (int i = 0; i < size; i++) {
System.out.println(data[i].getMedNo() + "\t" + data[i].getMedName() + "\t" + data[i].getPrice() + "\t" + data[i].getQuantity() + "\t" + data[i].getSales());
}
}
}
```
主程序:
```Java
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
MedicineList medicineList = new MedicineList(100);
try {
Scanner scanner = new Scanner(new File("data.txt"));
while (scanner.hasNext()) {
String line = scanner.nextLine();
String[] fields = line.split(",");
String medNo = fields[0];
String medName = fields[1];
double price = Double.parseDouble(fields[2]);
int quantity = Integer.parseInt(fields[3]);
double sales = Double.parseDouble(fields[4]);
Medicine medicine = new Medicine(medNo, medName, price, quantity, sales);
medicineList.addMedicine(medicine);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
//按药品编号排序
medicineList.sortByMedNo();
System.out.println("按药品编号排序:");
medicineList.print();
//按单价排序
medicineList.sortByPrice();
System.out.println("按单价排序:");
medicineList.print();
//按销售量排序
medicineList.sortByQuantity();
System.out.println("按销售量排序:");
medicineList.print();
//按销售额排序
medicineList.sortBySales();
System.out.println("按销售额排序:");
medicineList.print();
}
}
```
注:假设数据文件名为"data.txt",每行格式为"药品编号,药名,药品单价,销出数量,销售额"。
阅读全文