五、药店的药品销售统计系统(排序应用)

凌云 关注

收藏于 : 2019-03-18 10:03   被转藏 : 1   

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/AJayB/article/details/69358021

五、药店的药品销售统计系统(排序应用)
[问题描述]
设计一系统,实现医药公司定期对销售各药品的记录进行统计,可按药品的编号、单价、销售量或销售额做出排名。
[实现提示]
在本设计中,首先从数据文件中读出各药品的信息记录,存储在顺序表中。各药品的信息包括:药品编号、药名、药品单价、销出数量、销售额。药品编号共4位,采用字母和数字混合编号,如:A125,前一位为大写字母,后三位为数字,按药品编号进行排序时,可采用基数排序法。对各药品的单价、销售量或销售额进行排序时,可采用多种排序方法,如直接插入排序、冒泡排序、快速排序,直接选择排序等方法。在本设计中,对单价的排序采用冒泡排序法,对销售量的排序采用快速排序法,对销售额的排序采用堆排序法。
药品信息的元素类型定义:
typedef struct node
{ char num[4]; / 药品编号 /
char name[10]; / 药品名称 /
float price; / 药品单价 /
int count; / 销售数量 /
float sale; / 本药品销售额 /
}DataType;
存储药品信息的顺序表的定义:
typedef struct
{ DataType r[MaxSize];
int length;
}SequenList;

算法设计
先把文件读取出来,显示其中的信息。冒泡排序:找前两个数的进行比较,若第一个大于第二个,则互换两个位置。两个for循环进行他的排序。快速排序:找到第一个作为枢纽,在最后找到一个比 枢纽小的数放到前面,在前面找到一个比枢纽大的数放到后面,最后high=low的时候循环结束。
堆排序,建立堆,将R[l..n]看成是一棵完全二叉树的顺序存储结构,利用完全二叉树中双亲结点和孩子结点之间的内在关系,在当前无序区中选择关键字最大(或最小)的记录

算法思想

流程图
int line()
void read()
void write()
void show()//读取文件中的内容,并把内容显示出来
void bubbling_sort()//冒泡排序
void QSort(int low, int high)
void quick_sort()//快速排列
void HeapAdjust(int s, int m)
void Heap_sort()//堆排序
void Swap(int i, int j)//交换的内容
main()主函数

                   创建顺序表int InitList(SqList &L)
            顺序表的基本操作之插入int ListInsert





药品单价的排序       药品销售量的排序      药品销售排序
      (冒泡排序)          (快速排序 )        (堆排序)  
                   这里写代码片  #include "stdafx.h"   #include<stdio.h>   #include<cstdio>   #include<string>   #include<cstring>   #include<string.h>   #include<vector>   #include<algorithm>   #include<queue>   #include<stack>   #include<math.h>   #include<cmath>   #include<iomanip>   #include<stdlib.h>   #include<list>   #include<map>   #include<fstream>   #include<iostream>   using   namespace   std  ;  #define MAXSIZE 100   int  N =  0  ;  typedef   struct  node  /*药品信息的元素定义*/  {  string  num;  /*药品编号*/   string  name;  /*药品名称*/   float  price;  /*药品单价*/   int  count;  /*销售数量*/   float  sale;  /*本药品销售额*/  }Datatype;  typedef   struct   /*存储药品信息的顺序表的定义*/  {
    Datatype r[MAXSIZE +  1  ];  int  length;
}Sequnenlist;
Sequnenlist *l =  new  Sequnenlist;  int  line()
{  //获取文件中的药品种数   int  len =  0  ;  string  str;
    ifstream file1(  "drug.txt"  );  while  (file1)
    {
        getline(file1, str);  //每次读取一行   if  (str.length()>  4  )  //长度4是随便写的,代表本行有数据  len++;  //记录药品种类数  }  return  len;
}  void  show()
{  //输出   cout  <<  "====序号=====编号=====名称=====单价=====销售量=====销售额===="  << endl;  for  (  int  i =  1  ; i <= N; i++)  cout  <<  "    ["  << i <<  "]   "  << setw(  7  ) << l->r[i].num
        << setw(  10  ) << l->r[i].name
        << setw(  8  ) << l->r[i].price
        << setw(  9  ) << l->r[i].count
        << setw(  12  ) << l->r[i].sale
        << endl;
}  void  read()
{  //读文件  ifstream ofile;  string  nu;  string  na;  float  pr;  int  co;  float  sa;
    ofile.open(  "drug.txt"  , ios::in);  if  (ofile.is_open())
    {  for  (  int  i =  1  ; i <= N; i++)
        {
            ofile >> nu >> na >> pr >> co >> sa;
            l->r[i].num = nu;
            l->r[i].name = na;
            l->r[i].price = pr;
            l->r[i].count = co;
            l->r[i].sale = sa;  if  (i == N)
            {  cout  <<  "文件导入成功。=^_^="  << endl;  cout  <<  "药品种类数:"  << i << endl;
                show();
            }
        }
    }  else   cout  <<  "文件打开失败"  << endl;
}  void  write()
{  //写文件  ofstream ifile(  "drug_1.txt"  );  for  (  int  i =  1  ; i <= N; i++)
    {
        ifile << l->r[i].num <<  "   "  << l->r[i].name <<  "   "  << l->r[i].price <<  "   "  << l->r[i].count <<  "   "  << l->r[i].sale << endl;
    }
    show();  cout  <<  "药品全部导出到文件 “drug_1.txt”"  << endl;
}  int  Partition(  int  low,  int  high)  //快排  {  //一趟比较,返回枢纽所在位置  l->r[  0  ] = l->r[low];  //将枢纽key暂存在r[0]   int  pivotkey = l->r[  0  ].count;  while  (low<high)
    {  while  (low<high&&l->r[high].count >= pivotkey)
            --high;
        l->r[low] = l->r[high];  //将比枢纽小的移到低端   while  (low<high&&l->r[low].count <= pivotkey)
            ++low;
        l->r[high] = l->r[low];  //将比枢纽大的移到高端  }
    l->r[low] = l->r[  0  ];  return  low;  //返回枢纽位置  }  void  QSort(  int  low,  int  high)
{  if  (low<high)
    {  int  pivotloc = Partition(low, high);  //将序列一分为二  QSort(low, pivotloc -  1  );  //对低子表进行递归排序  QSort(pivotloc +  1  , high);  //对高子表进行递归排序  }
}  void  quick_sort()
{  //销售量快排   cout  <<  "按销售量从小到大(快排):"  << endl << endl;
    QSort(  1  , l->length);  //书上276 对顺序表整体排序  show();
}  void  Swap(  int  i,  int  j)
{  //交换第i和第j个药品  l->r[MAXSIZE].count = l->r[i].count;
    l->r[MAXSIZE].name = l->r[i].name;
    l->r[MAXSIZE].num = l->r[i].num;
    l->r[MAXSIZE].price = l->r[i].price;
    l->r[MAXSIZE].sale = l->r[i].sale;

    l->r[i].count = l->r[j].count;
    l->r[i].name = l->r[j].name;
    l->r[i].num = l->r[j].num;
    l->r[i].price = l->r[j].price;
    l->r[i].sale = l->r[j].sale;

    l->r[j].count = l->r[MAXSIZE].count;
    l->r[j].name = l->r[MAXSIZE].name;
    l->r[j].num = l->r[MAXSIZE].num;
    l->r[j].price = l->r[MAXSIZE].price;
    l->r[j].sale = l->r[MAXSIZE].sale;
}  void  bubbling_sort()
{  //单价排序冒泡排序法   cout  <<  "按单价从小到大(冒泡排序):"  << endl << endl;  for  (  int  i =  1  ; i <= N -  1  ; i++)  //从第一个开始相邻的两个比较  价格从小到大排序   for  (  int  j = i +  1  ; j <= N; j++)  if  (l->r[i].price>l->r[j].price)
        Swap(i, j);
    show();
}  void  HeapAdjust(  int  s,  int  m)
{  //堆排序筛选算法  Datatype rc;
    rc.count = l->r[s].count;
    rc.name = l->r[s].name;
    rc.num = l->r[s].num;
    rc.price = l->r[s].price;
    rc.sale = l->r[s].sale;  for  (  int  j =  2  * s; j <= m; j *=  2  )
    {  //沿key较大的孩子节点向下筛选   if  ((j<m) && l->r[j].sale<l->r[j +  1  ].sale) ++j;  //j为key较大的记录的下标   if  (rc.sale >= l->r[j].sale)  break  ;  //rc应插入在位置s上  l->r[s].count = l->r[j].count;
        l->r[s].name = l->r[j].name;
        l->r[s].num = l->r[j].num;
        l->r[s].price = l->r[j].price;
        l->r[s].sale = l->r[j].sale;
        s = j;
    }
    l->r[s].count = rc.count;
    l->r[s].name = rc.name;
    l->r[s].num = rc.num;
    l->r[s].price = rc.price;
    l->r[s].sale = rc.sale;
}  void  Heap_sort()
{  //销售额的排序堆排序法   cout  <<  "按销售额从小到大(堆排序):"  << endl << endl;  for  (  int  i = l->length /  2  ; i >=  1  ; i--)
        HeapAdjust(i, l->length);  for  (  int  i = l->length; i>  1  ; i--)
    {
        Swap(  1  , i);  //将堆顶记录和当前未经排序子序列中   //的最后一个记录交换  HeapAdjust(  1  , i -  1  );  //将l->r[1~i-1]重新调整为大顶堆  }
    show();
}  void  menu()
{  //菜单   cout  << endl;  cout  <<  "           ◆-------◆---------◆---------◆-------◆"  << endl;  cout  <<  "                       药品信息管理系统               "  << endl;  cout  <<  "           ◇                                      ◇"  << endl;  cout  <<  "                       1、导入药品信息               "  << endl;  cout  <<  "           ◇          2、导出药品信息             ◇"  << endl;  cout  <<  "                       3、单价排序                   "  << endl;  cout  <<  "           ◇          4、销售额排序               ◇"  << endl;  cout  <<  "                       5、销售量排序                 "  << endl;  cout  <<  "           ◇          6、退出管理系统             ◇"  << endl;  cout  <<  "                                                     "  << endl;  cout  <<  "           ◇                                      ◇"  << endl;  cout  <<  "           ◆-------◆---------◆---------◆-------◆"  << endl;  cout  << endl << endl <<  "请选择功能:"  ;
}  void   exit  ()
{  //退出   cout  << endl;  cout  <<  "           ◆-------◆---------◆---------◆-------◆"  << endl;  cout  << endl;  cout  <<  "           ◇           感谢您的使用!=^_^=        ◇"  << endl;  cout  << endl;  cout  <<  "           ◆-------◆---------◆---------◆-------◆"  << endl;  cout  << endl;
}  int  main()
{  int  n;
    system(  "color 37"  );
    l->length = N = line() -  1  ;  while  (  1  )
    {
        system(  "cls"  );
        menu();  cin  >> n;  switch  (n)
        {  case   1  :
        {
                  system(  "cls"  );
                  read();
                  system(  "pause"  );  break  ;
        }  case   2  :
        {
                  system(  "cls"  );
                  write();
                  system(  "pause"  );  break  ;
        }  case   3  :
        {
                  system(  "cls"  );
                  bubbling_sort();
                  system(  "pause"  );  break  ;
        }  case   4  :
        {
                  system(  "cls"  );
                  Heap_sort();
                  system(  "pause"  );  break  ;
        }  case   5  :
        {
                  system(  "cls"  );
                  quick_sort();
                  system(  "pause"  );  break  ;
        }  case   6  :
        {
                  system(  "cls"  );  exit  ();  return   0  ;
        }  default  :
        {  cin  .clear();  cin  .sync();  cout  <<  "输入有误,请重新输入!"  << endl;
                   system(  "pause"  );  break  ;
        }
        }
    }  return   0  ;
}  
 阅读文章全部内容  
点击查看
文章点评
相关文章
凌云 关注

文章收藏:9255

TA的最新收藏