![]() |
#2
hoho5682011-09-10 10:34
|

#include <iostream>
#include <string>
#include<fstream>
using namespace std;
const char*file="save.bat";
struct SaveDate
{
int Sclass;
int Snum;
float Sprice;
};
class Date
{
private:
int num;
float price;
public:
Date(int a,float b):num(a),price(b){};
virtual ~Date(){};
virtual void print()=0;
int getnum()const{return num;}
float getpirce()const{return price;}
};
class book:public Date
{
public:
book(int a,float b):Date(a,b){};
virtual void print();
};
void book::print()
{
cout<<"图书的编号为"<<getnum()<<"号"<<endl;
cout<<"图书的价格为"<<getpirce()<<"元"<<endl;
}
class drug:public Date
{
public:
drug(int a,float b):Date(a,b){};
virtual void print();
};
void drug::print()
{
cout<<"药品的编号为"<<getnum()<<"号"<<endl;
cout<<"药品的价格为"<<getpirce()<<"元"<<endl;
}
class Node
{
private:
Node *next;
Date *D;
public:
Node( Date* a):D(a),next(0){};
~Node(){delete D;D=0;}
Node* getn()const{return next;}
Date* getD()const{return D;}
void setn(Node *a){next=a;}
};
class list
{
private:
Node* head;
int total;
public:
void Inser( Date * a);
void all()const;
list(){head=0;total=0;}
~list();
Date* find(const int x);
Date* operator[](const int x);
void clear(const int x );
int gett(){return total;}
Node* gethead(){return head;}
int gettotal()const{return total;}
};
void list::Inser(Date* a)
{
total++;
Node*q=head;
Node*f=new Node(a);
if(!head)
{
head=f;
return;
}
if(head->getD()->getnum()>f->getD()->getnum())
{
head=f;
f->setn(q);
return;
}
if(!head->getn())
{
head->setn(f);
return;
}
while(q)
{
Node*e=q->getn();
int c=e->getD()->getnum();
if(c>f->getD()->getnum())
{
q->setn(f);
f->setn(e);
return;
}
if(!e->getn())
{
e->setn(f);
return;
}
q=q->getn();
}
}
void list::all()const
{
if(!head)
{
cout<<"没有数据"<<endl;
return;
}
Node *k=head;
while(k)
{
k->getD()->print();
k=k->getn();
}
}
list::~list()
{
Node *a;
int i=1;
while(head)
{
a=head;
head=head->getn();
delete a;
cout<<"删除第"<<i<<"个节点"<<endl;
i++;
}
}
Date* list::operator [](const int x)
{
Node *a=head;
if(x>=total)
{
cout<<"找不到数据!"<<endl;
return NULL;
}
for(int i=0;i<x;i++)
a=a->getn();
return a->getD();
}
Date* list::find(const int x)
{
Node *a=head;
if(!head)
{
cout<<"无数据!"<<endl;
return NULL;
}
while(a)
{
if(a->getD()->getnum()==x)
return a->getD();
a=a->getn();
}
cout<<"找不到数据!"<<endl;
return NULL;
}
void list::clear(const int x)
{
if(!head)
{
cout<<"无数据删除!"<<endl;
return;
}
Node *a=head;
Node *b;
int c;
if(head->getD()->getnum()==x)
{
if(!head->getn())
{
delete head;
head=0;
cout<<"数据清空!"<<endl;
return;
}
else
{
head=head->getn();
// cout<<head->getD()<<endl;;
delete a;
total--;
a=0;
cout<<"删除成功!现在还有"<<total<<"个节点"<<endl;
//cout<<head->getD()<<endl;
return;
}
}
while(a)
{
if(a->getn()==0)
{
cout<<"无数据"<<endl;
return;
}
b=a->getn();
c=b->getD()->getnum();
if(c==x)
{
a->setn(b->getn());
delete b;
total--;
b=0;
cout<<"删除成功!现在还有"<<total<<"个节点"<<endl;
return;
}
a=a->getn();
//cout<<"没有该编号"<<endl;
}
cout<<"没有该编号"<<endl;
}
class measure:private list
{
private:
//list l;
public:
void relase(Date*);
void run();
};
void measure::relase(Date*a)
{
Node*head1;
head1=gethead();
while(head1)
{
if(head1->getD()->getnum()==a->getnum())
{
cout<<"你输入的商品与编号"<<head1->getD()->getnum()<<"相等!"<<endl;
return;
}
head1=head1->getn();
}
Inser(a);
}
void measure::run()
{
bool g=false;
Date* p=0;
//measure l;
//list l;
int w=0;
int q=0;
while(1)
{
cout<<"1.增加商品 2.列出所有商品 3.查找商品 4.删除商品 5.商品数目 6.保存 7.读取 8.退出 "<<endl;
cin>>q;
switch(q)
{
case 1:
{
cout<<"1.图书类 2.药品类"<<endl;
cin>>w;
if(w==1||w==2)
{
int a;float b;
cout<<"请输入编号"<<endl;
cin>>a;
cout<<"请输入价格"<<endl;
cin>>b;
if(w==1)
{
p=new book(a,b);
relase(p);
break;
}
if(w==2)
{
p=new drug(a,b);
relase(p);
break;
}
}
else
cout<<"请输入1~2之间的数字"<<endl;
}
break;
case 2:all();
break;
case 3:
{
cout<<"1.按编号查找,2按序列号查找"<<endl;
cin>>w;
if(w==1||w==2)
{ int s;
if(w==1)
{
cout<<"请输入编号"<<endl;
cin>>s;
if(!find(s))
{
cin.get();
cin.get();
cout<<"按回车返回"<<endl;
break;
}
find(s)->print();
break;
}
if(w==2)
{
cout<<"请输入序列号"<<endl;
cin>>s;
if(!(*this)[s-1])
{
cin.get();
cin.get();
cout<<"按回车返回"<<endl;
break;
}
(*this)[s-1]->print();
break;
}
}
else
cout<<"请输入1~2之间的数字"<<endl;
}
break;
case 4:
{
cout<<"请输入要删除商品的编号"<<endl;
cin>>w;
clear(w);
}
break;
case 5:cout<<"商品的数量为"<<gett()<<endl;
break;
case 6:
{
long num=gettotal();
ofstream fcout;
fcout.open(file,ios::out|ios::binary);
SaveDate *date2=new SaveDate;
for(int i=0;i<num;i++)
{
Date *a=(*this)[i];
if(typeid(*a).name()==typeid(book).name())
{
date2->Sclass=1;
}
if(typeid(*a).name()==typeid(drug).name())
{
date2->Sclass=2;
}
date2->Snum=(*this)[i]->getnum();
date2->Sprice=(*this)[i]->getpirce();
cout<<"*::"<<date2->Sclass<<date2->Snum<<date2->Sprice<<endl;//没有问题
fcout.write((char*)&date2,sizeof date2); // 保存1个没有问题,2个就出问题了,比如第一个数据为111 第2个为222 但读取的结果2个都是222
fcout.seekp(sizeof date2,ios::beg);
// fcout.close();
}
fcout.clear();
fcout.close();
}
break;
case 7:
{
long num=gettotal();
SaveDate *ax=new SaveDate;
ifstream fcin(file,ios::binary);
if(!fcin.is_open())
{
cout<<"读取失败!"<<endl;
}
else
{
while(fcin.read((char*)&ax,sizeof ax))
{
cout<<ax->Sclass<<ax->Snum<<ax->Sprice<<endl;
}
if(fcin.eof())
{
fcin.clear();
fcin.close();
}
}
}
break;
case 8:g=true;
break;
}
if(g)
break;//switch
}//while
}
void main()
{
measure r;
r.run();
}
该程序不是完整的程序,所以有点乱 #include <string>
#include<fstream>
using namespace std;
const char*file="save.bat";
struct SaveDate
{
int Sclass;
int Snum;
float Sprice;
};
class Date
{
private:
int num;
float price;
public:
Date(int a,float b):num(a),price(b){};
virtual ~Date(){};
virtual void print()=0;
int getnum()const{return num;}
float getpirce()const{return price;}
};
class book:public Date
{
public:
book(int a,float b):Date(a,b){};
virtual void print();
};
void book::print()
{
cout<<"图书的编号为"<<getnum()<<"号"<<endl;
cout<<"图书的价格为"<<getpirce()<<"元"<<endl;
}
class drug:public Date
{
public:
drug(int a,float b):Date(a,b){};
virtual void print();
};
void drug::print()
{
cout<<"药品的编号为"<<getnum()<<"号"<<endl;
cout<<"药品的价格为"<<getpirce()<<"元"<<endl;
}
class Node
{
private:
Node *next;
Date *D;
public:
Node( Date* a):D(a),next(0){};
~Node(){delete D;D=0;}
Node* getn()const{return next;}
Date* getD()const{return D;}
void setn(Node *a){next=a;}
};
class list
{
private:
Node* head;
int total;
public:
void Inser( Date * a);
void all()const;
list(){head=0;total=0;}
~list();
Date* find(const int x);
Date* operator[](const int x);
void clear(const int x );
int gett(){return total;}
Node* gethead(){return head;}
int gettotal()const{return total;}
};
void list::Inser(Date* a)
{
total++;
Node*q=head;
Node*f=new Node(a);
if(!head)
{
head=f;
return;
}
if(head->getD()->getnum()>f->getD()->getnum())
{
head=f;
f->setn(q);
return;
}
if(!head->getn())
{
head->setn(f);
return;
}
while(q)
{
Node*e=q->getn();
int c=e->getD()->getnum();
if(c>f->getD()->getnum())
{
q->setn(f);
f->setn(e);
return;
}
if(!e->getn())
{
e->setn(f);
return;
}
q=q->getn();
}
}
void list::all()const
{
if(!head)
{
cout<<"没有数据"<<endl;
return;
}
Node *k=head;
while(k)
{
k->getD()->print();
k=k->getn();
}
}
list::~list()
{
Node *a;
int i=1;
while(head)
{
a=head;
head=head->getn();
delete a;
cout<<"删除第"<<i<<"个节点"<<endl;
i++;
}
}
Date* list::operator [](const int x)
{
Node *a=head;
if(x>=total)
{
cout<<"找不到数据!"<<endl;
return NULL;
}
for(int i=0;i<x;i++)
a=a->getn();
return a->getD();
}
Date* list::find(const int x)
{
Node *a=head;
if(!head)
{
cout<<"无数据!"<<endl;
return NULL;
}
while(a)
{
if(a->getD()->getnum()==x)
return a->getD();
a=a->getn();
}
cout<<"找不到数据!"<<endl;
return NULL;
}
void list::clear(const int x)
{
if(!head)
{
cout<<"无数据删除!"<<endl;
return;
}
Node *a=head;
Node *b;
int c;
if(head->getD()->getnum()==x)
{
if(!head->getn())
{
delete head;
head=0;
cout<<"数据清空!"<<endl;
return;
}
else
{
head=head->getn();
// cout<<head->getD()<<endl;;
delete a;
total--;
a=0;
cout<<"删除成功!现在还有"<<total<<"个节点"<<endl;
//cout<<head->getD()<<endl;
return;
}
}
while(a)
{
if(a->getn()==0)
{
cout<<"无数据"<<endl;
return;
}
b=a->getn();
c=b->getD()->getnum();
if(c==x)
{
a->setn(b->getn());
delete b;
total--;
b=0;
cout<<"删除成功!现在还有"<<total<<"个节点"<<endl;
return;
}
a=a->getn();
//cout<<"没有该编号"<<endl;
}
cout<<"没有该编号"<<endl;
}
class measure:private list
{
private:
//list l;
public:
void relase(Date*);
void run();
};
void measure::relase(Date*a)
{
Node*head1;
head1=gethead();
while(head1)
{
if(head1->getD()->getnum()==a->getnum())
{
cout<<"你输入的商品与编号"<<head1->getD()->getnum()<<"相等!"<<endl;
return;
}
head1=head1->getn();
}
Inser(a);
}
void measure::run()
{
bool g=false;
Date* p=0;
//measure l;
//list l;
int w=0;
int q=0;
while(1)
{
cout<<"1.增加商品 2.列出所有商品 3.查找商品 4.删除商品 5.商品数目 6.保存 7.读取 8.退出 "<<endl;
cin>>q;
switch(q)
{
case 1:
{
cout<<"1.图书类 2.药品类"<<endl;
cin>>w;
if(w==1||w==2)
{
int a;float b;
cout<<"请输入编号"<<endl;
cin>>a;
cout<<"请输入价格"<<endl;
cin>>b;
if(w==1)
{
p=new book(a,b);
relase(p);
break;
}
if(w==2)
{
p=new drug(a,b);
relase(p);
break;
}
}
else
cout<<"请输入1~2之间的数字"<<endl;
}
break;
case 2:all();
break;
case 3:
{
cout<<"1.按编号查找,2按序列号查找"<<endl;
cin>>w;
if(w==1||w==2)
{ int s;
if(w==1)
{
cout<<"请输入编号"<<endl;
cin>>s;
if(!find(s))
{
cin.get();
cin.get();
cout<<"按回车返回"<<endl;
break;
}
find(s)->print();
break;
}
if(w==2)
{
cout<<"请输入序列号"<<endl;
cin>>s;
if(!(*this)[s-1])
{
cin.get();
cin.get();
cout<<"按回车返回"<<endl;
break;
}
(*this)[s-1]->print();
break;
}
}
else
cout<<"请输入1~2之间的数字"<<endl;
}
break;
case 4:
{
cout<<"请输入要删除商品的编号"<<endl;
cin>>w;
clear(w);
}
break;
case 5:cout<<"商品的数量为"<<gett()<<endl;
break;
case 6:
{
long num=gettotal();
ofstream fcout;
fcout.open(file,ios::out|ios::binary);
SaveDate *date2=new SaveDate;
for(int i=0;i<num;i++)
{
Date *a=(*this)[i];
if(typeid(*a).name()==typeid(book).name())
{
date2->Sclass=1;
}
if(typeid(*a).name()==typeid(drug).name())
{
date2->Sclass=2;
}
date2->Snum=(*this)[i]->getnum();
date2->Sprice=(*this)[i]->getpirce();
cout<<"*::"<<date2->Sclass<<date2->Snum<<date2->Sprice<<endl;//没有问题
fcout.write((char*)&date2,sizeof date2); // 保存1个没有问题,2个就出问题了,比如第一个数据为111 第2个为222 但读取的结果2个都是222
fcout.seekp(sizeof date2,ios::beg);
// fcout.close();
}
fcout.clear();
fcout.close();
}
break;
case 7:
{
long num=gettotal();
SaveDate *ax=new SaveDate;
ifstream fcin(file,ios::binary);
if(!fcin.is_open())
{
cout<<"读取失败!"<<endl;
}
else
{
while(fcin.read((char*)&ax,sizeof ax))
{
cout<<ax->Sclass<<ax->Snum<<ax->Sprice<<endl;
}
if(fcin.eof())
{
fcin.clear();
fcin.close();
}
}
}
break;
case 8:g=true;
break;
}
if(g)
break;//switch
}//while
}
void main()
{
measure r;
r.run();
}