源程序:# include <fstream.h> //包含文件读写# include <iostream.h># include <string.h> //包含strcmp()字符是否相等#include <stdlib.h> //包含system("cls")清屏#define MAX 60 //下列字符数组的大小struct Date{//日期char year[MAX];//年char month[MAX];//月char day[MAX];//日};struct Goods{//药品信息char name[MAX];//药品名称char price[MAX];//药品价格char number[MAX];//药品数量char cost[MAX];//药品总价char kind[MAX];//药品的种类Date indate;//入库日期Date xiaoqi;//到期时间Goods * next;//下一个结点};class Cangkuguanli { //类定义与实现private:int length;//客户数量Goods * head;//列表的头结点Goods * current;//当前结点public:Cangkuguanli()//构造函数{head=new Goods;//创建头结点current=head;current->next=NULL;length=0;//长度为0}void Creatlist()//创建新的列表{char g='Y';int s=0;length=0;//初始长度为0;current=head;do {Goods * temp=new Goods ;//构建新结点信息length++; //每加一个结点链表长度增1temp->next=NULL;cout<<" 请输入药品名称: ";cin>>temp->name;cout<<" 请输入单价 : ";cin>>temp->price;cout<<" 请输入药品数量: ";cin>>temp->number;cout<<" 请输入总费用 : ";cin>>temp->cost;cout<<" 请输入日期 (**** ** **) : ";cin>>temp->indate.year>>temp->indate.month>>temp->indate.day;cout<<" 请输入药品有效期 (**** ** **):";cin>>temp->xiaoqi.year>>temp->xiaoqi.month>>temp->xiaoqi.day;cout<<" 请输入药品种类: ";cin>>temp->kind;if(head==NULL){head=temp;current=temp;} //head头指针,current尾指针else {current->next=temp,current=temp;}do{cout<<" next ? (Y N) "; //是否继续存入新产品cin>>g;if(g!='Y'&&g!='N'){cout<<"\n error \n ";}}while(g!='Y'&&g!='N');}while(g=='Y');//判断是否继续插入新结点}void Open ()//打开一个数据文件,并建立链表关联和文件中的记录对应{char fname[20];//文件名称cout<<" input the name of the file \n";cin>>fname; //输入要打开的文件名ifstream infile (fname);//创建输入文件流infile>>length;cout<<"\n length is: "<<length<<endl;//if(length==0)cout<<" 数据为空\n";for(int i=0;i<length;i++)//创建新的结点保存文件的数据{Goods * t=new Goods ;t->next=NULL;infile>>t->name>>t->price>>t->number>>t->cost>>t->kind>>t->indate.year>>t->indate.month>>t->indate.day;if(head==NULL){head=t;current=t;}//跟上面的链表创建相似else {current->next=t,current=t;}}infile.close();//关闭文件流}//openvoid Save ()//保存链表信息到文件{if(length==0){cout<<" 列表为空不需存盘 \n";return ;}char fname[20];//文件名称cout<<" input the name of the file you want to put data in \n";cin>>fname;ofstream outfile(fname);//创建输出文件流Goods * temp=head->next;outfile<<length<<endl;//先写入文件的长度while (temp!=NULL)//把所有结点写入到文件fname{outfile<<temp->name<<" "<<temp->price<<" "<<temp->number<<" "<<temp->cost<<" "<<temp->kind<< " "<<temp->indate.year<<" "<<temp->indate.month<<" "<<temp->indate.day<<" "<<temp->xiaoqi.year<<" "<<temp->xiaoqi.month<<" "<<temp->xiaoqi.day<<endl; //写入每个结点信息temp=temp->next;}outfile.close();//关闭文件流}void printinfor( Goods * current)//输出一个结点的信息到字符界面{if(current==NULL){cout<<"\n元素为空 \n ";return;}cout.fill(' ');cout<<current->name;cout.width(8);cout.width(8);cout<<current->price;cout.width(8);cout<<current->number;cout.width(8);cout<<current->cost; //就是给current->cost10个字符输出,不够往前面加‘ ’空格cout.width(10);cout<<current->kind;cout.width(10);cout<<current->indate.year<<"."<<current->indate.month<<"."<<current->indate.day;cout.width(10);cout<<current->xiaoqi.year<<"."<<current->xiaoqi.month<<"."<<current->xiaoqi.da y;cout<<endl;}// printinforvoid Show()//输出所有结点信息到字符界面{current=head->next;if(current==NULL){cout<<"列表为空 \n";return ;}cout.fill(' ');cout.width(2);cout<<"名称";cout.width(8);cout<<"价格";cout.width(8);cout<<"数量";cout.width(8);cout<<"总费用";cout.width(10);cout<<"药品的种类";cout.width(15);cout<<"购进时间";cout.width(15);cout<<"有效期";cout<<endl; //输出属性列while(current!=NULL){ //循环输出所有结点printinfor( current );current=current->next;}}//showGoods *Searchindate(Date t){ //按购入日期搜索返回结点指针temp //结构体t含有t.year t.month t.day三个信息块Date d;bool f=false;Goods * temp;current=head->next;while(current!=NULL){d=current->indate;if(!strcmp(d.year,t.year)&&!strcmp(d.month,t.month)&&!strcmp(d.day,t.day)) {temp=current;f=true;break;}current=current->next;}if(f==false){cout<<"\n 没有满足要求的信息 \n";return NULL;}return temp;}void Queryindate()//按入库日期查询{Date t;cout<<" input the date (**** ** **): ";cin>>t.year>>t.month>>t.day;printinfor(Searchindate (t));}//Queryindate()Goods * Searchname (char r[])//搜索药品名返回结点指针temp{ Goods * temp;current=head->next;bool f=false;while(current!=NULL){if(strcmp(current->name,r)==0){temp=current;f=true;break;}current=current->next;}if(f==false){cout<<"\n 商品名为 : "<<r<<" 的商品不存在 \n ";return NULL;}return temp;}void Queryname() //按药品名查询{char r[MAX];cout<<" input the name: ";cin>>r;printinfor(Searchname (r));}void Query ()//查询函数一个次级菜单{char m;do{cout<<" ********************************************************** "<<endl; cout<<"*******<a> 按入库的时间查询 ***********"<<endl; cout<<"*******<b> 按药品的名称查询 ***********"<<endl; cout<<"*******<c> 退出 ***********"<<endl; cout<<" ********************************************************** "<<endl; cin>>m;switch (m){ //分别调用按不同查询方式下的函数case 'a': Queryindate();break;case 'b': Queryname();break;case 'c': return ;default: cout<<"error!! ";}}while(m!='h');}//Queryvoid Removebynumber () //按总量删除某个结点{char t[MAX]; //需要删除的总量值bool f=false; //表示是否找到满足要求的结点Goods * temp; //指向找到的结点,并删除cout<<" 请输入药品的数量: ";cin>>t;current=head;while(current->next!=NULL){if(strcmp(current->next->number,t)==0) //判断是否满足条件 {temp=current->next;current->next=temp->next;length--; //没删除一个结点,链表长度减1f=true;delete temp;//释放结点cout<<"已删除此药品\n";//break; 此处屏蔽break,也就是说找到第一个满足条件的结点,并不退出循环,而是继续删除}else{current=current->next;}}if(f==false)//false没有改动,表示没有满足条件的结点{cout<<"此药品不存在 ,无须删除 \n";}}//Removebynumbervoid Removebyname () //按名称删除某个结点{char t[20];bool f=false;;Goods * temp;cout<<" 请输入药品的名称:";cin>>t;current=head;while(current->next!=NULL){if(strcmp(current->next->name,t)==0){temp=current->next;current->next=temp->next;length--;f=true;delete temp;cout<<"已删除此药品\n";//break;此处屏蔽break,也就是说找到第一个满足条件的结点,并不退出循环,而是继续删除}else{current=current->next;}}if(f==false){cout<<"此药品不存在 ,无须删除 \n";}}//Removebynamevoid Removebyindate () //按购进时间删除某个结点{Date t,d;bool f=false;Goods * temp;cout<<" 请输入查找药品的日期: ";cin>>t.year>>t.month>>t.day;current=head;while(current->next!=NULL){d=current->next->indate;if(d.year==t.year&&d.month==t.month&&d.day==t.day){temp=current->next;current->next=temp->next;length--;f=true;delete temp;cout<<"已删除此药品\n";}else{current=current->next;}}if(f==false){cout<<"此元素不存在 ,无须删除 \n";}}//Removebyindatevoid Remove ()//删除函数{char g='Y';do{cout<<" ********************************************************** "<<endl; cout<<"*******<a> 按药品总数量删除 ***********"<<endl;cout<<"*******<b> 按药品名称删除 ***********"<<endl; cout<<"*******<c> 按入库时间删除 ***********"<<endl;cout<<"*******<d> 退出 ***********"<<endl;cout<<" ********************************************************** "<<endl; cin>>g;switch (g){case 'a': Removebynumber ();break;case 'b': Removebyname();break;case 'c': Removebyindate();break;case 'd': return ;default: cout<<"error!! ";}} while(g!='d');}void Add(){Goods * temp;current=head;while(current->next!=NULL)//找到尾指针{current=current->next;}temp=new Goods;temp->next=NULL;length++;//链表长加1cout<<" 请输入药品名称: ";cin>>temp->name;cout<<" 请输入单价 : ";cin>>temp->price;cout<<" 请输入药品数量 : ";cin>>temp->number;cout<<" 请输入总费用 : ";cin>>temp->cost;cout<<" 请输入入购日期 (**** ** **) ";cin>>temp->indate.year>>temp->indate.month>>temp->indate.day;cout<<"请输入有效期 (**** ** **)";cin>>temp->xiaoqi.year>>temp->xiaoqi.month>>temp->xiaoqi.day;cout<<" 请输入药品的种类:"<<endl;cin>>temp->kind;current->next=temp;//这时current指向尾指针,将要插入的结点接到current后面 current=temp;}//addvoid Modify (){//通过药品名找到要修改的数据项char n[MAX];cout<<"请输入您要修改的药品名:"<<endl;cin>>n;Goods * temp=Searchname (n);//按商品名查找cout<<" 请输入单价:";cin>>temp->price;cout<<" 请输入药品的总费用:\n";cin>>temp->cost;cout<<" 请输入日期 (**** ** **):\n";cin>>temp->indate.year>>temp->indate.month>>temp->indate.day;cout<<" 请输入药品的数量:\n";cin>>temp->number;cout<<" 请输入药品的种类:\n";cin>>temp->kind;}void operation (){char g='Y';do{cout<<"\t";cout.fill('*');cout.width(21);cout<<'*';cout<<"医院药房管理系统";cout.fill('*');cout.width(19);cout<<'*';cout<<"\n";cout<<"\t********************************************************\n"; cout<<"\t MWDICINE STORE MANAGEMENT SYSTERM\n";cout<<"\t********************************************************\n"; cout<<"\t && && <C> 药品入库 &&&&&&&&&&&&&&& \n"; cout<<"\t && && <R> 药品出库 &&&&&&&&&&&&&&& \n"; cout<<"\t && && <M> 药品调价 &&& \n";cout<<"\t && && <A>新药品入库 &&& \n"; cout<<"\t && && <O> 打开文件 &&&&&&&&&&&&&&& \n"; cout<<"\t && && <S> 保存文件 &&&&&&&&&&&&&&& \n"; cout<<"\t &&& <D> 显示库存 &&& \n"; cout<<"\t &&& <Q> 药品查询 &&& \n"; cout<<"\t &&& <L> 上下限制 &&& \n"; cout<<"\t &&& <E> 退出 &&& \n"; cout<<"\t********************************************************\n"; cout<<"\t********************************************************\n"; cout<<"\t";cout.fill('*');cout.width(18);cout<<'*';cout<<"医院药房管理系统";cout.fill('*');cout.width(22);cout<<'*';cout<<endl;cin>>g;switch (g){case 'C': Creatlist();break;case 'A': Add();break;case 'M': Modify ();break;case 'D': Show ();break;case 'Q': Query ();break;case 'R': Remove();break;case 'O': Open ();break;case 'S': Save();break;case 'N': system ("cls");break;case 'E': return ;default: cout<<" error!! input the right character \n";}//switch}while(g!='E');}//operation};//Cangkuguanliint main (){Cangkuguanli A;A.operation(); return 0;}。