源程序:
# include
# include
#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 {
1
Goods * temp=new Goods ;//构建新结点信息
length++; //每加一个结点 链表长度增1 temp->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<<\ } }while(g!='Y'&&g!='N'); }while(g=='Y');//判断是否继续插入新结点 }
void Open ()//打开一个数据文件,并建立链表关联 和文件中的记录对应 { char fname[20];//文件名称 cout<<\ cin>>fname; //输入要打开的文件名 ifstream infile (fname);//创建输入文件流 infile>>length; cout<<\ //if(length==0)cout<<\数据为空\\n\ for(int i=0;i Goods * t=new Goods ; t->next=NULL; 2 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();//关闭文件流 }//open void Save ()//保存链表信息到文件 { if(length==0) { cout<<\列表为空 不需存盘 \\n\ return ; } char fname[20];//文件名称 cout<<\ cin>>fname; ofstream outfile(fname);//创建输出文件流 Goods * temp=head->next; outfile< void printinfor( Goods * current)//输出一个结点的信息到字符界面 { if(current==NULL) { cout<<\元素为空!!! \\n \ return; } cout.fill(' '); cout< 3 cout.width(8); cout.width(8); cout< cout< cout<< current->indate.year<<\ cout.width(10); cout< void 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< 4 printinfor( current ); current=current->next; } }//show Goods *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\ return NULL; } return temp; } void Queryindate()//按入库日期查询 { Date t; cout<<\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) { 5