药店的药品销售统计系统(排序应用)
[问题描述]
设计一系统,实现医药公司定期对销售各药品的记录进行统计,可按药品的编号、单价、销售量或销售额做出排名。
[实现提示]
在本设计中,首先从数据文件中读出各药品的信息记录,存储在顺序表中。各药品的信息包括:药品编号、药名、药品单价、销出数量、销售额。药品编号共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;
源码:
#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<ostream>
#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);
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 Radix_sort()
{//编号基数排序
cout<<"按编号从小到大(基数排序):"<<endl<<endl;
queue<Datatype>A,B,C,D,E,F,G,H,I,J,K,L,M,NN,O,P,Q,R,S,T,U,V,W,X,Y,Z;
//创建A~Z一共26个队列实现基数排序
int k,i,j;
for(i=3;i>=0;i--)
{//从低位往高位依次排序
if(i)
{//后三位数字的排序
for(j=1;j<=N;j++)
{//进队里
if(l->r[j].num[i]=='0')
A.push(l->r[j]);
else if(l->r[j].num[i]=='1')
B.push(l->r[j]);
else if(l->r[j].num[i]=='2')
C.push(l->r[j]);
else if(l->r[j].num[i]=='3')
D.push(l->r[j]);
else if(l->r[j].num[i]=='4')
E.push(l->r[j]);
else if(l->r[j].num[i]=='5')
F.push(l->r[j]);
else if(l->r[j].num[i]=='6')
G.push(l->r[j]);
else if(l->r[j].num[i]=='7')
H.push(l->r[j]);
else if(l->r[j].num[i]=='8')
I.push(l->r[j]);
else if(l->r[j].num[i]=='9')
J.push(l->r[j]);
}
k=1;
//出队列
while(!A.empty())
{
l->r[k++]=A.front();
A.pop();
}
while(!B.empty())
{
l->r[k++]=B.front();
B.pop();
}
while(!C.empty())
{
l->r[k++]=C.front();
C.pop();
}
while(!D.empty())
{
l->r[k++]=D.front();
D.pop();
}
while(!E.empty())
{
l->r[k++]=E.front();
E.pop();
}
while(!F.empty())
{
l->r[k++]=F.front();
F.pop();
}
while(!G.empty())
{
l->r[k++]=G.front();
G.pop();
}
while(!H.empty())
{
l->r[k++]=H.front();
H.pop();
}
while(!I.empty())
{
l->r[k++]=I.front();
I.pop();
}
while(!J.empty())
{
l->r[k++]=J.front();
J.pop();
}
}
else
{//第一位字母的排序
for(int j=1;j<=N;j++)
{//进队列
if(l->r[j].num[i]=='A')
A.push(l->r[j]);
else if(l->r[j].num[i]=='B')
B.push(l->r[j]);
else if(l->r[j].num[i]=='C')
C.push(l->r[j]);
else if(l->r[j].num[i]=='D')
D.push(l->r[j]);
else if(l->r[j].num[i]=='E')
E.push(l->r[j]);
else if(l->r[j].num[i]=='F')
F.push(l->r[j]);
else if(l->r[j].num[i]=='G')
G.push(l->r[j]);
else if(l->r[j].num[i]=='H')
H.push(l->r[j]);
else if(l->r[j].num[i]=='I')
I.push(l->r[j]);
else if(l->r[j].num[i]=='J')
J.push(l->r[j]);
else if(l->r[j].num[i]=='K')
K.push(l->r[j]);
else if(l->r[j].num[i]=='L')
L.push(l->r[j]);
else if(l->r[j].num[i]=='M')
M.push(l->r[j]);
else if(l->r[j].num[i]=='N')
NN.push(l->r[j]);
else if(l->r[j].num[i]=='O')
O.push(l->r[j]);
else if(l->r[j].num[i]=='P')
P.push(l->r[j]);
else if(l->r[j].num[i]=='Q')
Q.push(l->r[j]);
else if(l->r[j].num[i]=='R')
R.push(l->r[j]);
else if(l->r[j].num[i]=='S')
S.push(l->r[j]);
else if(l->r[j].num[i]=='T')
T.push(l->r[j]);
else if(l->r[j].num[i]=='U')
U.push(l->r[j]);
else if(l->r[j].num[i]=='V')
V.push(l->r[j]);
else if(l->r[j].num[i]=='W')
W.push(l->r[j]);
else if(l->r[j].num[i]=='X')
X.push(l->r[j]);
else if(l->r[j].num[i]=='Y')
Y.push(l->r[j]);
else if(l->r[j].num[i]=='Z')
Z.push(l->r[j]);
}
int k=1;
//出队列
while(!A.empty())
{
l->r[k++]=A.front();
A.pop();
}
while(!B.empty())
{
l->r[k++]=B.front();
B.pop();
}
while(!C.empty())
{
l->r[k++]=C.front();
C.pop();
}
while(!D.empty())
{
l->r[k++]=D.front();
D.pop();
}
while(!E.empty())
{
l->r[k++]=E.front();
E.pop();
}
while(!F.empty())
{
l->r[k++]=F.front();
F.pop();
}
while(!G.empty())
{
l->r[k++]=G.front();
G.pop();
}
while(!H.empty())
{
l->r[k++]=H.front();
H.pop();
}
while(!I.empty())
{
l->r[k++]=I.front();
I.pop();
}
while(!J.empty())
{
l->r[k++]=J.front();
J.pop();
}
while(!K.empty())
{
l->r[k++]=K.front();
K.pop();
}
while(!L.empty())
{
l->r[k++]=L.front();
L.pop();
}
while(!M.empty())
{
l->r[k++]=M.front();
M.pop();
}
while(!NN.empty())
{
l->r[k++]=NN.front();
NN.pop();
}
while(!O.empty())
{
l->r[k++]=O.front();
O.pop();
}
while(!P.empty())
{
l->r[k++]=P.front();
P.pop();
}
while(!Q.empty())
{
l->r[k++]=Q.front();
Q.pop();
}
while(!R.empty())
{
l->r[k++]=R.front();
R.pop();
}
while(!S.empty())
{
l->r[k++]=S.front();
S.pop();
}
while(!T.empty())
{
l->r[k++]=T.front();
T.pop();
}
while(!U.empty())
{
l->r[k++]=U.front();
U.pop();
}
while(!V.empty())
{
l->r[k++]=V.front();
V.pop();
}
while(!W.empty())
{
l->r[k++]=W.front();
W.pop();
}
while(!X.empty())
{
l->r[k++]=X.front();
X.pop();
}
while(!Y.empty())
{
l->r[k++]=Y.front();
Y.pop();
}
while(!Z.empty())
{
l->r[k++]=Z.front();
Z.pop();
}
}
}
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<< " 7、退出管理系统 " << endl;
cout<< " ◇ ◇" << endl;
cout<< " MADE BY CWB " << 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");
Radix_sort();
system("pause");
break;
}
case 4:
{
system("cls");
bubbling_sort();
system("pause");
break;
}
case 5:
{
system("cls");
Heap_sort();
system("pause");
break;
}
case 6:
{
system("cls");
quick_sort();
system("pause");
break;
}
case 7:
{
system("cls");
exit();
return 0;
}
default:
{
cin.clear();
cin.sync();
cout<<"输入有误,请重新输入!"<<endl;
system("pause");
break;
}
}
}
return 0;
}
/*
W123 忘情水 9.9 50 495
X256 相思豆 12 40 480
M198 六神丸 12.5 55 687.5
B852 回魂丹 8 20 160
D584 大力丸 10 33 330
M665 孟婆汤 8.4 20 168
Y532 隐身草 6.6 23 151.8
M661 软筋散 13 42 546
D591 风油精 4.5 35 157.5
*/