继续做贡献
程序代码:
#include <vector>
#include <iostream>
#include <cstdlib>
#include <cstring>
using namespace std;
enum emLen{emLen_Name=10,emLen_Remarks=100,emLen_Type=20};
class CPet
{
static unsigned int m_iTotal;
unsigned int m_iIndex;//寵物編號
unsigned int m_iOwnerIndex;//寵物所屬主人編號
char m_szPetName[emLen_Name];//寵物名字,阿2/阿3.。。
unsigned int m_iPrice;//寵物價格
char m_szRemarks[emLen_Remarks];//備註信息
char m_szPetType[emLen_Type];//寵物類型,貓?狗?。。。
public:
CPet(unsigned int & index/*out*/,unsigned int iOwner,char * szType,unsigned int iPrice,char * szName=NULL,char * szRemarks=NULL):
m_iIndex(m_iTotal++),
m_iOwnerIndex(iOwner),
m_iPrice(iPrice)
{
try
{
if(m_iTotal>=12)
throw "寵物店太小了,再也容不下多餘寵物了";
if(!szType)
throw "本店不收未知物種的寵物";
}
catch(char*e)
{
cout<<e<<endl;
exit(-1);
}
memset(m_szPetType,0,sizeof(m_szPetType));
memset(m_szRemarks,0,sizeof(m_szRemarks));
memset(m_szPetName,0,sizeof(m_szPetName));
strcpy(m_szPetType,szType);
if(szName)
strcpy(m_szPetName,szName);
if(szRemarks)
strcpy(m_szRemarks,szRemarks);
index = m_iIndex;
}
~CPet(){}
unsigned int GetIndex(){return m_iIndex;}
void SetPetName(char * szName=""){strcpy(m_szPetName,szName);}//设置宠物名字
char * GetPetName(){return m_szPetName;}
char * GetPetType(){return m_szPetType;}
void SetPetPrice(unsigned int iPrice){m_iPrice=iPrice;}//设置宠物价格
unsigned int GetPetPrice(){return m_iPrice;}
unsigned int GetPetIndex(){return m_iIndex;}
unsigned int GetPetOwner(){return m_iOwnerIndex;}//获取宠物所属主人编号
void SetPetOwner(unsigned int index){m_iOwnerIndex = index;}//设置所属主人
void SetPetRemarks(char * szRemarks){strcpy(m_szRemarks,szRemarks);}//设置备注
char * GetPetRemarks(){return m_szRemarks;}
};
unsigned int CPet::m_iTotal=0;
class CPetOwner
{
static unsigned int m_iTotal;
unsigned int m_iIndex; //宠物主人編號
char m_szName[emLen_Name]; //名字
char m_szRemarks[emLen_Remarks];//备注
vector<unsigned int>m_vPets; //目前拥有的宠物的编号
vector<unsigned int>m_vBought; //购买宠物历史
vector<unsigned int>m_vSold; //出售宠物历史
public:
CPetOwner(unsigned int&index/*out*/,char * szName,char * szRemarks=""):m_iIndex(m_iTotal++)
{
try
{
if(!szName)
throw "本店不收无名氏的宠物";
}
catch(char * e)
{
cout<<e<<endl;
exit(-1);
}
strcpy(m_szName,szName);
strcpy(m_szRemarks,szRemarks);
index = m_iIndex;
}
unsigned int GetIndex(){return m_iIndex;}//获取编号
char * GetName(){return m_szName;}
char * GetRemarks(){return m_szRemarks;}
void SetPetRemarks(char * szRemarks){strcpy(m_szRemarks,szRemarks);}//设置备注
void BuyPet(unsigned index){m_vPets.push_back(index);m_vBought.push_back(index);}//购买宠物
bool SellPet(unsigned index,CPetOwner& otherOwner);//出卖宠物
bool HasPet(unsigned index);//是否具有该编号的宠物
void AddPet(unsigned index){m_vPets.push_back(index);}
};
unsigned int CPetOwner::m_iTotal=0;
bool CPetOwner::SellPet(unsigned index,CPetOwner& otherOwner)//出卖宠物
{
for(vector<unsigned int>::iterator i=m_vPets.begin();i != m_vPets.end();i++)
{
if(*i == index)
{
otherOwner.BuyPet(index);
m_vSold.push_back(index);
m_vPets.erase(i);
return true;
}
}
return false;
}
bool CPetOwner::HasPet(unsigned index)//是否具有该编号的宠物
{
bool has=false;
for(unsigned int i=0;i<m_vPets.size();i++)
{
if(m_vPets[i] == index)
{
has = true;
break;
}
}
return has;
}
void show_usage()
{
cout<<"\n\n\n\n"
<<"===========员工操作规程==================\n"
<<"\t 1 新加一个宠物\n"
<<"\t 2 宠物买卖\n"
<<"\t 3 当前已收容宠物信息\n"
<<"\t 4 宠物主人信息\n"
<<"\t 0 退出\n"
<<"=========================================\n"
<<"请输入选择:";
}
int get_user_sel()
{
show_usage();
int sel;
cin>>sel;
while(sel<0||sel>4)
{
cout<<"输入有误,请重新输入:\n";
show_usage();
cin>>sel;
}
return sel;
}
vector<CPetOwner> owner;
vector<CPet> pet;
void print_owner_info()
{
cout<<"\t编号\t姓名\t备注\n";
for(unsigned int i=0;i<owner.size();i++)
{
cout<<"\t"<<owner[i].GetIndex()+1<<"\t"<<owner[i].GetName()<<"\t"<<owner[i].GetRemarks()<<"\n";
}
}
char * get_owner_name(unsigned int index)
{
char * p=NULL;
for(unsigned int i=0;i<owner.size();i++)
{
if(owner[i].GetIndex() == index)
{
p = owner[i].GetName();
break;
}
}
return p;
}
void print_pet_info()
{
cout<<"\t编号\t类型\t名字\t价格\t备注\t主人\n";
for(unsigned int i=0;i<pet.size();i++)
{
cout<<"\t"<<pet[i].GetIndex()+1
<<"\t"<<pet[i].GetPetType()
<<"\t"<<pet[i].GetPetName()
<<"\t"<<pet[i].GetPetPrice()
<<"\t"<<pet[i].GetPetRemarks()
<<"\t"<<get_owner_name(pet[i].GetPetOwner())
<<"\n";
}
}
void print_pet_info(unsigned int index)
{
for(unsigned int i=0;i<pet.size();i++)
{
if(pet[i].GetPetOwner() == index)
cout<<"\t"<<pet[i].GetIndex()+1
<<"\t"<<pet[i].GetPetType()
<<"\t"<<pet[i].GetPetName()
<<"\t"<<pet[i].GetPetPrice()
<<"\t"<<pet[i].GetPetRemarks()
<<"\n";
}
}
bool AddNewPet()
{
cout<<"输入宠物类型:";
char szType[emLen_Type]={0};
cin>>szType;
cout<<"输入宠物名字:";
char szPetName[emLen_Name]={0};
cin>>szPetName;
cout<<"输入价格:";
unsigned int price=0;
cin>>price;
cout<<"输入宠物备注信息:";
char szRemarks[emLen_Remarks]={0};
cin>>szRemarks;
cout<<"=====================================================\n"
<<"\t 1 查看已有宠物主人信息,并选择宠物主人编号\n"
<<"\t 2 新加一个宠物主人信息\n"
<<"\t 3 取消\n"
<<"=====================================================\n"
<<"请输入选择:";
int sel = 0;
cin>>sel;
while(sel<1||sel>3)
{
cout<<"输入有误,请请重新输入\n"
<<"=====================================================\n"
<<"\t 1 查看已有宠物主人信息,并选择宠物主人编号\n"
<<"\t 2 新加一个宠物主人信息,并设置为宠物主人\n"
<<"\t 3 取消\n"
<<"=====================================================\n"
<<"请输入选择:";
cin>>sel;
}
unsigned int index;
switch(sel)
{
case 1:
{
print_owner_info();
cout<<"=====================================================\n"
<<"请输入选择:";
cin>>index;
while(index<1 || index >owner.size())
{
cout<<"选择有误请重新选择:\n";
print_owner_info();
cout<<"=====================================================\n"
<<"请输入选择:";
cin>>index;
}
index--;
}
break;
case 2:
{
char name[emLen_Name]={0};
char remarks[emLen_Remarks]={0};
cout << "输入姓名:";
cin >> name;
cout << "输入备注信息:";
cin >> remarks;
owner.push_back(CPetOwner(index,name,remarks));
}
break;
case 3:
default:
return false;
}
unsigned int last_pet_index;
pet.push_back(CPet(last_pet_index,index,szType,price,szPetName,szRemarks));
owner[index].AddPet(last_pet_index);
return true;
}
//宠物买卖
void pet_business()
{
cout<<"宠物主人信息:\n";
print_owner_info();
unsigned int sell;
cout<<"请选择卖家编号:";
cin>>sell;
while(sell<1 || sell> owner.size())
{
cout<<"输入有误,请重新输入:";
cin>>sell;
}
unsigned int buy;
cout<<"请选择买家编号:";
cin>>buy;
while(buy<1 || buy> owner.size() || sell == buy)
{
cout<<"输入有误,请重新输入:";
cin>>buy;
}
sell--;
buy--;
cout<<"宠物信息\n";
cout<<"\t编号\t类型\t名字\t价格\t备注\n";
print_pet_info(sell);
cout<<"请输入要交易的宠物编号\n";
unsigned index;
cin>>index;
while(!owner[sell].HasPet(index-1))
{
cout<<"输入有误,请重新输入:";
cin>>index;
}
index--;
owner[sell].SellPet(index,owner[buy]);
pet[index].SetPetOwner(buy);
}
int main()
{
int sel = 0;
while((sel=get_user_sel())!=0)
{
switch(sel)
{
case 1:
AddNewPet();
break;
case 2:
pet_business();
break;
case 3:
print_pet_info();
break;
case 4:
print_owner_info();
break;
default:
break;
}
}
owner.clear();
pet.clear();
return 0;
}