📄 class.cpp
字号:
#include "class.h"
#include <string.h>
#include <iostream.h>
#include <stdlib.h>
Book::Book()
{
strcpy(Title,"");
Code = 0;
Next = NULL;
}
Book::Book(char *title,long code)
{
strcpy(Title,title);
Code = code;
Next = NULL;
}
void Book::Show()
{
cout<<"书籍:\t"<<Title<<'\t'<<Code<<endl;
}
ostream& operator<<(ostream& os, Book& bk) // 重载插入运算符
{
os<<bk.Title<<' '<<bk.Type<<' '<<bk.Code<<' ';
cout<<bk.Title<<' '<<bk.Type<<' '<<bk.Code<<' ';
return os;
}
Item::Item():Book()
{
strcpy(Author,"");
strcpy(IndexCode,"");
Type = 0;
}
Item::Item(char *title,char *author,char *index,int code):Book(title,code)
{
strcpy(Author,author);
strcpy(IndexCode,index);
Type = 0;
}
Item::Item(Item & it)
{
strcpy(Author,it.Author);
strcpy(Title,it.Title);
strcpy(IndexCode,it.IndexCode);
Code = it.Code;
Type = it.Type;
}
void Item::SetAuthor(char *t1)
{
strcpy(Author, t1);
}
void Item::SetIndexCode(char *t1)
{
strcpy(IndexCode, t1);
}
void Item::Show()
{
cout<<"书:\t"<<Title<<'\t'<<Author<<'\t'<<IndexCode<<'\t'<<Code<<'\n';
}
ostream& operator<<(ostream& os, Item& it) // 重载插入运算符
{
os<<it.Title<<' '<<it.Author<<' '<<it.IndexCode<<' '<<it.Code<<'\n';
cout<<it.Title<<' '<<it.Author<<' '<<it.IndexCode<<' '<<it.Code<<'\n';
return os;
}
istream& operator>>(istream& is, Item& it) // 重载提取运算符
{
is>>it.Title;
is>>it.Author>>it.IndexCode>>it.Code;
return is;
}
Magazine::Magazine(char *title,int vol,LANG lang,int code):Book(title,code)
{
Volume = vol;
Lang = lang;
Type = 1;
}
Magazine::Magazine(Magazine & mz){
strcpy(Title,mz.Title);
Volume = mz.Volume;
Lang = mz.Lang;
Code = mz.Code;
Type = 1;
}
ostream& operator<<(ostream& os, Magazine& mg) // 重载插入运算符
{
os<<mg.Title<<' '<<mg.Volume<<' '<<Magazine::LANG(mg.Lang)<<' '<<mg.Code<<'\n';
cout<<mg.Title<<' '<<mg.Volume<<' '<<mg.Lang<<' '<<mg.Code<<'\n';
return os;
}
istream& operator>>(istream& is, Magazine& mg) // 重载提取运算符
{
int j;
is>>mg.Title>>mg.Volume>>j;
mg.Lang = Magazine::LANG(j);
is>>mg.Code;
return is;
}
void Magazine::Show()
{
cout<<"杂志:\t"<<Title<<'\t'<<"卷号"<<Volume<<'\t'<<((Lang==1)?"Chinese":"English")<<'\t'<<Code<<'\n';
}
Reader::Reader()
{
strcpy(Name,"");
strcpy(Position,"");
Counter = 0;
Age = 0;
Code = 0;
items = NULL;
}
Reader::Reader(char *name,char *posi,int age,int code)
{
strcpy(Name,name);
strcpy(Position,posi);
Counter = 0;
Age = age;
Code = code;
items = NULL;
}
/*
Reader::Reader(Reader & rd)
{
/* strcpy(Name,rd.Name);
strcpy(Position,rd.Position);
Counter = 0;
Age = rd.Age;
Code = rd.Code;
Item *p = rd.items,*q;
while(p)
{
q = new Item;
*q = *p;
if(Counter==0)items = q;
p = p->Next;
Counter++;
}
}*/
Reader::~Reader()
{
}
void Reader::SetName(char *t)
{
strcpy(Name,t);
}
void Reader::SetPosition(char *t)
{
strcpy(Position,t);
}
void Reader::SetAge(int t)
{
Age = t;
}
void Reader::SetCode(long t)
{
Code = t;
}
void Reader::ShowBooks()
{
Book* p = items;
if(p==NULL) { cout<<"您未借书!";return;}
while(p){
p->Show();
p = p->Next;
}
}
void Reader::Show(){
cout<<"读者:\t"<<Name<<'\t'<<Position<<'\t'<<Age<<'\t'<<Code<<'\n';
}
void Reader::AddBook(Magazine it)
{
Magazine *p;
p = new Magazine; // 创建一个新结点
if(p){
*p = it;
p->Next = items;//插入表头
items = p;
Counter++;
}
}
void Reader::AddBook(Item it)
{
Item *p;
p = new Item; // 创建一个新结点
if(p){
*p = it;
p->Next = items;//插入表头
items = p;
Counter++;
}
}
void Reader::DelBook(Book it)
{
Book * p = items,*q;
while(p->GetCode()!=it.GetCode()&&p!=NULL){
q = p;
p = p->Next;
}
if(p->GetCode()==it.GetCode())
{
if(p == items) items = p->Next;
else q->Next = p->Next;
delete p;
}
return;
}
ostream& operator<<(ostream& os, Reader& rd) // 重载插入运算符
{
os<<rd.Name<<' '<<rd.Position<<' '<<rd.Age<<' '<<rd.Code<<' '<<rd.Counter<<'\n';
Book* p = rd.items;
while(p){
os<<p->GetType()<<' ';
if(p->GetType())
os<<*(Magazine*)p;
else os<<*(Item*)p;
p = p->Next;
}
cout<<rd.Name<<' '<<rd.Position<<' '<<rd.Age<<' '<<rd.Code<<'\n';
return os;
}
istream& operator>>(istream& is, Reader& rd) // 重载提取运算符
{
Item it;
Magazine mg;
int type,count;
is>>rd.Name>>rd.Position>>rd.Age>>rd.Code>>count;
for( int i=0; i<count; i++){
is>>type;
if(type){
is>>mg;
rd.AddBook(mg);
}
else{
is>>it;
rd.AddBook(it);
}
}
return is;
}
Loan::Loan(Loan & l){
Code = l.Code;
if(Code) mag = l.mag;
else item = l.item;
reader = l.reader;
manager = l.manager;
}
void Loan::Show(){
if(Type)mag.Show();
else item.Show();
reader.Show();
manager.Show();
}
ostream& operator<<(ostream& os, Loan& ln) // 重载插入运算符
{
os<<ln.Type<<' '<<ln.Code<<' '<<ln.reader<<' ';
cout<<ln.Type<<' '<<ln.Code<<' '<<ln.reader<<' ';
if(ln.Type){
os<<ln.mag<<' ';
cout<<ln.mag<<' ';}
else{
os<<ln.item<<' ';
cout<<ln.item<<' ';
}
os<<ln.manager<<'\n';
cout<<ln.manager<<'\n';
return os;
}
istream& operator>>(istream& is, Loan& ln) // 重载提取运算符
{
is>>ln.Type>>ln.Code>>ln.reader;
if(ln.Type){
is>>ln.mag;
}
else{
is>>ln.item;
}
is>>ln.manager;
return is;
}
Manager::Manager(char* name,int age,int code)
{
strcpy(Name,name);
Age = age;
Code = code;
}
void Manager::Show()
{
cout<<"管理员:\t"<<Name<<'\t'<<Age<<'\t'<<Code<<endl;
}
ostream& operator<<(ostream& os, Manager& mgr) // 重载运算符
{
os<<mgr.Name<<' '<<mgr.Age<<' '<<mgr.Code<<'\n';
cout<<mgr.Name<<' '<<mgr.Age<<' '<<mgr.Code<<'\n';
return os;
}
istream& operator>>(istream& is, Manager& mgr) // 重载提取运算符
{
is>>mgr.Name>>mgr.Age>>mgr.Code;
return is;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -