数据结构课设——药店的药品销售统计系统(排序应用)

问题描述

设计一系统,实现医药公司定期对销售各药品的记录进行统计,可按药品的编号、单价、销售量或销售额做出排名。

需求分析

  • 按编号排序输出药品销售记录
  • 按单价排序输出药品销售记录
  • 按销售量排序输出药品销售记录
  • 按销售额排序输出药品销售记录

算法思想

首先从数据文件中读出各药品的信息记录,存储在顺序表中。按编号排序时采用直接插入排序法,将待插入的记录暂存到监视哨中,记录逐个后移,直到找到插入位置;按单价排序时采用冒泡排序法,就是不断地比较和交换,将比较大的交换到前面,直到不需要交换为止;按销售量排序采用快速排序,去第一个记录为枢轴,把比枢轴小的关键字交换到前面,大的交换到后面,结果把排序记录分成两个表,再对左右子表重复上述过程,直至每一子表只有一个记录时,排序完成;按销售额排序采用堆排序,建初堆,然后把堆调整成大根堆。

算法设计

1、菜单void menu()——输出所有功能选择。

2、输出全部信息void Allprint(SequenList S)——从前往后遍历结构体数组,输出药的具体信息。

3、读文件void Read(SequenList &S)——从数据文件中读出各药品的信息记录,存储在顺序表中。

4、按编号排序void Numsort(SequenList &S)——直接插入排序法,将待插入的记录暂存到监视哨中,记录逐个后移,直到找到插入位置

5、按单价排序void Pricesort(SequenList &S)——冒泡排序法,不断地比较和交换,将比较大的交换到前面,直到不需要交换为止。

6、对子表排序int Par(SequenList &S,int low,int high)——对顺序表的子表进行一趟排序,返回枢轴位置。

7、快速排序void Qsort(SequenList &S,int low,int high)——对子序列快速排序。

8、按销售量排序void Countsort(SequenList &S)——对顺序表L做快速排序。

9、调整堆void HeapAdjust(SequenList &S,int s,int m)——把堆调整成大根堆。

10、建堆void CreatHeap(SequenList &S)——建初堆,不断调用 HeapAdjust()。11、按销售额排序void Salesort(SequenList &S)——将无序序列建成初堆后,反复进行交换和堆调整。

12、主函数int main()——使用while循环和switch对功能进行调用,使用清屏函数,使界面更理想。

代码实现

#include<stdio.h>//实验五
#include<iostream>
#include<stdlib.h>
#include<string.h>
using namespace std;
#define MaxSize 11
typedef struct node
{
  char num[5];  //开四个出现两次name
  char name[10];
  float price;
  int count;
  float sale;
}DataType;
typedef struct
{
  DataType r[MaxSize];
  int length;
}SequenList;
void menu(){
    cout<<"****************药品销售统计系统****************"<<endl;
    cout<<"                1、按编号排序"<<endl;
    cout<<"                2、按单价排序"<<endl;
    cout<<"                3、按销售量排序"<<endl;
    cout<<"                4、按销售额排序"<<endl;
    cout<<"                5、退出"<<endl;
    cout<<"************************************************"<<endl;
    cout<<"请选择..."<<endl;
}
void Allprint(SequenList S){
    printf("药品编号    药名        单价  销出数量  销售额\n");
    for (int i = 1;i <=S.length;i++)
	{
		printf("%s\t %s\t%.2f\t%d\t%.2f\n",S.r[i].num,S.r[i].name,S.r[i].price,S.r[i].count,S.r[i].sale);
	}
}
void Read(SequenList &S){
	FILE *fp=fopen("C:\\Users\\chenxianning\\Documents\\c c++\\数据结构课设\\test.txt","rb");
	if(fp== NULL)
	{
		printf("can not open file!\n");
		exit(0);
	}
	for(int i = 1;i<15;i++)
	{
		int nRes = fscanf(fp,"%s%s%f%d%f",S.r[i].num,S.r[i].name,&S.r[i].price,&S.r[i].count,&S.r[i].sale);
		if (nRes == -1)
		{
			S.length=i-1;
			fclose(fp);
			break;
		}
	}
	fclose(fp);
}
void Numsort(SequenList &S)//直接插入排序
{
    int i,j;
    for(i=2;i<=S.length;++i){
        if(strcmp(S.r[i].num,S.r[i-1].num)<0)
        {
            S.r[0]=S.r[i];
            S.r[i]=S.r[i-1];
            for(j=i-2;strcmp(S.r[0].num,S.r[j].num)<0;--j)
                S.r[j+1]=S.r[j];
            S.r[j+1]=S.r[0];
        }
    }
    Allprint(S);
}
void Pricesort(SequenList &S){//冒泡排序
    int m=S.length;
    int flag=1;
    int j;
    DataType t;
    while((m>0)&&(flag==1)){
        flag=0;
        for(j=1;j<m;j++)
            if(S.r[j].price>S.r[j+1].price)
            {
                flag=1;
                t=S.r[j];
                S.r[j]=S.r[j+1];
                S.r[j+1]=t;
            }
        --m;
    }
    Allprint(S);
}
int Par(SequenList &S,int low,int high){//对顺序表的子表进行一趟排序,返回枢轴位置
    S.r[0]=S.r[low];
    int piv=S.r[low].count;
    while(low<high){
        while(low<high&&S.r[high].count>=piv)--high;
        S.r[low]=S.r[high];
        while(low<high&&S.r[low].count<=piv)++low;
        S.r[high]=S.r[low];
    }
    S.r[low]=S.r[0];
    return low;
}
void Qsort(SequenList &S,int low,int high)//快速排序
{
    if(low<high)
    {
        int piv=Par(S,low,high);
        Qsort(S,low,piv-1);
        Qsort(S,piv+1,high);
    }
}
void Countsort(SequenList &S){
    Qsort(S,1,S.length);
    Allprint(S);
}
void HeapAdjust(SequenList &S,int s,int m){//调整堆
    DataType rc=S.r[s];
    for(int j=2*s;j<=m;j*=2){
        if(j<m&&S.r[j].sale<S.r[j+1].sale)++j;
        if(rc.sale>=S.r[j].sale)break;
        S.r[s]=S.r[j];
        s=j;
    }
    S.r[s]=rc;
}
void CreatHeap(SequenList &S){
    int n=S.length;
    for(int i=n/2;i>0;--i)
        HeapAdjust(S,i,n);
}
void Salesort(SequenList &S){//堆排序
    CreatHeap(S);
    DataType t;
    for(int i=S.length;i>1;--i){
        t=S.r[1];
        S.r[1]=S.r[i];
        S.r[i]=t;
        HeapAdjust(S,1,i-1);
    }
    Allprint(S);
}
int main(){
    SequenList S;
    Read(S);
    int m;
    while(m!=5){
        menu();
        cin>>m;
        switch(m){
        case 1:
            Numsort(S);
            break;
        case 2:
            Pricesort(S);
            break;
        case 3:
            Countsort(S);
            break;
        case 4:
            Salesort(S);
            break;
        case 5:
            cout<<"感谢您的使用!"<<endl;
            return 0;
        default:
            cout<<"没有该选项!"<<endl;
        }
        system("pause");
        system("cls");
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/DEAR_CXN/article/details/86555956