求能自动按照药品类别,生成每类药品的库存数量的函数(c++)
发布网友
发布时间:2023-05-30 21:21
我来回答
共2个回答
热心网友
时间:2024-11-26 19:27
你要的功能我已经做完了,调试通过,你可以把我的代码全部copy进去,就可以使用了。
#include<string>
#include<iostream>
using namespace std;
static int N;
struct Medicine //定义链表
{
int ID; //ID号
string name; //药品名称
string category; //药品类别
int num;//药品数量
/*
string ids; //药品批准文号
string company; //生产企业
int month; //有效期
int date_year; //年
int date_month; //月
int date_day; //日
*/
Medicine *pNext;
};
Medicine* create(int n)
{
int ID; //ID号
string name; //药品名称
string category; //药品类别
int num;//药品数量
/*
string ids; //药品批准文号
string company; //生产企业
int month; //有效期
int date_year; //年
int date_month; //月
int date_day;
*/
Medicine *h,*p,*s;
if ((h=new Medicine)==NULL)
{
cout<<"分配内存失败"<<endl;
exit(1);
}
h->name=" ";
h->ID = 0;
h->pNext=NULL;
p=h;
for (int i=0;i!=n;++i)
{
if((s=new Medicine)==NULL)
{
cout<<"分配内存失败"<<endl;
exit(1);
}
p->pNext=s;
cout<<"请输入第"<<i+1<<"个药品的ID:";
cin>>ID;
s->ID = ID;
cout<<"请输入第"<<i+1<<"个药品的名称:";
cin>>name;
s->name = name;
cout<<"请输入第"<<i+1<<"个药品的类别:";
cin>>category;
s->category = category;
cout<<"请输入第"<<i+1<<"个药品的数量:";
cin>>num;
s->num = num;
s->pNext=NULL;
p=s;
}
//p->pNext=NULL;
return h;
}
//查询结点
Medicine* search(const Medicine *h, string name, int N)
{
int temp = N;
Medicine *p=h->pNext;
for (int i=0;i!=temp;++i)
{
if (name.compare(p->name)==0)
{
return p;
}
p=p->pNext;
}
return NULL;
}
//后插入
//extern N;
void Insert(Medicine *p)
{
Medicine *s;
if ((s=new Medicine)==NULL)
{
cout<<"分配内存失败"<<endl;
exit(1);
}
cout<<"请输入你要插入的药品名称:";
cin>>s->name;
s->pNext=p->pNext;
p->pNext=s;
++N;
}
//查询前向结点
Medicine* searchPre(Medicine *head, string name)
{
Medicine *p=head;
Medicine *q=p->pNext;
for (int i=0;i!=N;++i)
{
if (q!=NULL)
{
if (name.compare(q->name)==0)
return p;
}
else
return NULL;
p = q;
q = q->pNext;
}
return NULL;
}
//删除结点
void Delete(Medicine *p, Medicine *q) //p为删除的结点指针,q为前一个结点指针
{
Medicine *temp;
temp=p->pNext;
delete p;
q->pNext=temp;
--N;
};
//释放内存空间
void display(Medicine *h)
{
/*
Medicine *p;
for (int i=0;i!=N;++i)
{
p = h;
while(p->pNext)
{
p=p->pNext;
}
if(p != h)
{
cout<<"[DEL:"<<p->name<<"]"<<endl;
delete p;
}
}
*/
Medicine *p=h->pNext;
Medicine *pTemp;
while(NULL!=p->pNext)
{
pTemp = p->pNext;
cout<<"[删除:"<<p->name<<"]"<<endl;
delete(p);
p->pNext=NULL;
p=pTemp;
}
cout<<"[删除:"<<pTemp->name<<"]"<<endl;
delete(pTemp);
/*
cout<<"[DEL:"<<h->name<<"]"<<endl;
delete(h);
h=NULL;
*/
}
void main()
{
Medicine *head,*p=0,*q=0;
//char str[20];
string str;
int flag=1;
int sum;
int k;
cout<<"总共药品数量:";
cin>>N;
head=create(N);
cout<<"\r\n";
//display(head);
cout<<"所要查询的药品名是: ";
cin>>str;
//查询当前结点
if(p=search(head,str,N))
{
cout<<"该药品的ID为"<<p->ID;
}
else
{
cout<<"该药品不存在";
}
cout<<"\r\n";
// Insert(p);
while(flag)
{
sum=0;
p=head->pNext;
cout<<"开始统计药品,请输入药品类别"<<"\r\n";
cin>>str;
for(k=0;k!=N;++k)
{
if (str.compare(p->category)==0)
{
sum=sum+p->num;
}
p=p->pNext;
}
cout<<"属于"<<str<<"的药品有"<<sum<<"个库存量"<<"\r\n";
cout<<"是否统计完毕?(1继续统计/0下一个项目)";
cin>>flag;
}
//查询前驱结点并删除当前结点.
cout<<"所要删除的药品名是: ";
cin>>str;
q = head->pNext;
if(str.compare(q->name)==0)
{
delete q;
}
else
{
if(q=searchPre(head,str))
{
p=search(head,str,N);
Delete(p,q);
}
else
{
cout<<"该药品不存在";
}
}
cout<<"\r\n"<<"该药品已经删除完毕"<<"\r\n";
cout<<"程序将要推出,现在演示删除全部链表"<<"\r\n";
display(head);
}
我只是做了药品类的一部分变量,其余的相似,你自己添加吧,程序调试通过。
热心网友
时间:2024-11-26 19:28
自动按照药品类别,生成每类药品的库存数量?不怎么懂,库存数量多少呀?没有使用?
还是按照药品类别,自动生成每类药品库存数量的函数呀???