一、基础类
读者最大借书量、最长借书时间(按天计算)。这两个都是类成员
class Base
{
private:
static int borrownumberMax;
static int borrowtimeMax;
public:
Base(){};
~Base(){};
static int getBorrowNumberMax(){return borrownumberMax;}
static int getBorrowTimeMax(){return borrowtimeMax;}
static void setBorrowNumberMax(int bnm);
static void setBorrowTimeMax(int btm);
static void display();
};
void Base:: setBorrowNumberMax(int bnm)
{
borrownumberMax=bnm;
}
void Base::setBorrowTimeMax(int btm)
{
borrowtimeMax=btm;
}
void Base::display()
{
cout<<borrownumberMax<<" "<<borrowtimeMax<<endl;
}
int Base::borrownumberMax=5;
int Base::borrowtimeMax=60;
分析:
类成员是static类型,静态数据成员的声明方式:
注意:
1.get函数定义的时候,也要加上static
2.要写display函数
3.静态成员内容补充:
二、日期类
包括年月日,重载输入运算符时,要进行数据的合法性检验;
重载输出运算符时,按照“年/月/日”形式输出;重载+运算符。
class Date
{
private:
int year;
int month;
int day;
public:
Date();
Date(int year,int month,int day);
~Date(){}
int RunNian(int y);
int getYear(){return year;}
int getMonth(){return month;}
int getDay(){return day;}
friend istream & operator >>(istream &in,Date &d);
friend ostream & operator <<(ostream &out,Date &d);
Date operator +(int n);
};
Date::Date()
{
year=2020;
month=1;
day=1;
}
Date::Date(int year,int month,int day)
{
this->year=year;
this->month=month;
this->day=day;
}
int Date::RunNian(int y)
{
int r=0;
if(y%4==0&&y%100!=0||y%400==0)
{
r=1;
}
return r;
}
ostream &operator <<(ostream &out,Date &d)
{
out<<d.getYear()<<"/"<<d.getMonth()<<"/"<<d.getDay();
}
istream &operator >>(istream &in,Date &d)
{
int mm[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
while(1)
{
in>>d.year>>d.month>>d.day;
if(d.getYear()>=2020&&d.getMonth()<=12&&d.getDay()<=mm[d.getMonth()])
break;
}
return in;
}
Date Date :: operator +(int n)
{
int mm[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
if(RunNian(year)==1)
{
mm[2]=29;
}
int daysum=day+n;
int mon=0;
if(daysum>mm[month])
{
int i=month;
while(daysum>mm[i])
{
daysum=daysum-mm[i];
mon++;class Date
{
private:
int year;
int month;
int day;
public:
Date();
Date(int year,int month,int day);
~Date(){}
int RunNian(int y);
int getYear(){return year;}
int getMonth(){return month;}
int getDay(){return day;}
friend istream & operator >>(istream &in,Date &d);
friend ostream & operator <<(ostream &out,Date &d);
Date operator +(int n);
};
Date::Date()
{
year=2020;
month=1;
day=1;
}
Date::Date(int year,int month,int day)
{
this->year=year;
this->month=month;
this->day=day;
}
int Date::RunNian(int y)
{
int r=0;
if(y%4==0&&y%100!=0||y%400==0)
{
r=1;
}
return r;
}
ostream &operator <<(ostream &out,Date &d)
{
out<<d.getYear()<<"/"<<d.getMonth()<<"/"<<d.getDay();
}
istream &operator >>(istream &in,Date &d)
{
int mm[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
while(1)
{
in>>d.year>>d.month>>d.day;
if(d.getYear()>=2020&&d.getMonth()<=12&&d.getDay()<=mm[d.getMonth()])
break;
}
return in;
}
Date Date :: operator +(int n)
{
int mm[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
if(RunNian(year)==1)
{
mm[2]=29;
}
int daysum=day+n;
int mon=0;
if(daysum>mm[month])
{
int i=month;
while(daysum>mm[i])
{
daysum=daysum-mm[i];
mon++;
i=i%12;
i++;
}
}
month=month+mon;
if(month>12)
{
year++;
month=month%12;
}
day=daysum;
return *this;
}
i=i%12;
i++;
}
}
month=month+mon;
if(month>12)
{
year++;
month=month%12;
}
day=daysum;
return *this;
}
分析:
1.重载输入运算符,进行合法性检验
合法性检验:设置判断闰年函数,定义月份天数的数组
int Date::RunNian(int y)
{
int r=0;
if(y%4==0&&y%100!=0||y%400==0)
{
r=1;
}
return r;
}
istream &operator >>(istream &in,Date &d)
{
int mm[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
while(1)
{
in>>d.year>>d.month>>d.day;
if(d.getYear()>=2020&&d.getMonth()<=12&&d.getDay()<=mm[d.getMonth()])
break;
}
return in;
}
2.重载输出运算符,输出形式为年/月/日
ostream &operator <<(ostream &out,Date &d)
{
out<<d.getYear()<<"/"<<d.getMonth()<<"/"<<d.getDay();
}
3.重载加法运算符,进行日期的计算
注意计算日期方法
Date Date :: operator +(int n)
{
int mm[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
if(RunNian(year)==1)
{
mm[2]=29;
}
int daysum=day+n;
int mon=0;
if(daysum>mm[month])
{
int i=month;
while(daysum>mm[i])
{
daysum=daysum-mm[i];
mon++;
i=i%12;
i++;
}
}
month=month+mon;
if(month>12)
{
year++;
month=month%12;
}
day=daysum;
return *this;
}
i=i%12;
i++;
}
}
month=month+mon;
if(month>12)
{
year++;
month=month%12;
}
day=daysum;
return *this;
}
技巧:
1.重载输入运算符
2.重载输出运算符
注:
3.重载加法运算符
三、图书、读者类
图书类:
书号(常成员)、书名、作者、出版社、出版日期、图书类型(基础/文学休闲/专业)、馆藏总量、在馆数量、借阅记录向量;
class Book
{
private:
const string booknumber;
string title;
string author;
string pulisher;
Date pulishdate;
string booktype;
int booksum;
int bookin;
vector<Record>br;
public:
Book(){}
Book(string bn):booknumber(bn){}
Book(string bn,string t,string a,string p,Date pd,string bt,int bs,int bi);
Book(string bn,string t,string a,string p,Date pd,string bt,int bs,int bi,vector<Record>br);
~Book(){}
string getBookNumber(){return booknumber;}
string getTitle(){return title;}
string getAuthor(){return author;}
Date getPulishDate(){return pulishdate;}
string getBookType(){return booktype;}
int getBookSum(){return booksum;}
int getBookIn(){return bookin;}
vector <Record> getBR(){return br;}
void setTitle(string title);
void setAuthor(string author);
void setPulisher(string pulisher);
void setPulishDate(Date pulishdate);
void setBookType(string booktype);
void setBookSum(int booksum);
void setBookIn(int bookin);
void setBR(vector<Record>br);
void display();
Book &operator=(const Book &b);
friend istream & operator >>(istream &in,Book &b);
friend ostream & operator <<(ostream &out,Book &b);
};
Book::Book(string bn,string t,string a,string p,Date pd,string bt,int bs,int bi):booknumber(bn)
{
title=t;
author=a;
pulisher=p;
pulishdate=pd;
booktype=bt;
booksum=bs;
bookin=bi;
}
Book::Book(string bn,string t,string a,string p,Date pd,string bt,int bs,int bi,vector<Record>brr):booknumber(bn)
{
title=t;
author=a;
pulisher=p;
pulishdate=pd;
booktype=bt;
booksum=bs;
bookin=bi;
br=brr;
}
void Book::setTitle(string title)
{
this->title=title;
}
void Book::setPulisher(string pulisher)
{
this->pulisher=pulisher;
}
void Book::setPulishDate(Date pulishdate)
{
this->pulishdate=pulishdate;
}
void Book::setAuthor(string author)
{
this->author=author;
}
void Book::setBookType(string booktype)
{
this->booktype=booktype;
}
void Book::setBookSum(int booksum)
{
this->booksum=booksum;
}
void Book::setBookIn(int bookin)
{
this->bookin=bookin;
}
void Book::setBR(vector<Record>br)
{
this->br=br;
}
void Book::display()
{
cout<<booknumber<<" "<<title<<" "<<author<<" "<<pulisher<<" "<<pulishdate<<" "<<booktype<<" "<<booksum<<" "<<bookin<<endl;
}
Book &Book::operator=(const Book &b)
{
string *temp=const_cast<string*>(&booknumber);
*temp=b.booknumber;
title=b.title;
author=b.author;
pulisher=b.pulisher;
pulishdate=b.pulishdate;
booktype=b.booktype;
booksum=b.booksum;
bookin=b.bookin;
br=b.br;
}
istream & operator >>(istream &in,Book &b)
{
string *temp=const_cast<string*>(&b.booknumber);
string bn;
in>>bn;
*temp=bn;
in>>b.title>>b.author>>b.pulisher>>b.booktype>>b.booksum>>b.bookin;
return in;
}
ostream & operator <<(ostream &out,Book &b)
{
out<<b.booknumber<<" "<<b.title<<" "<<b.author<<" "<<b.pulisher<<" "<<b.pulishdate<<" "<<b.booktype<<" "<<b.booksum<<" "<<b.bookin<<endl;
return out;
}
读者类:
学号(常成员)、姓名、专业、班级、已借图书数量、借阅记录向量;
class Reader{
private:
const string Sno;
string name;
string major;
string banji;
int borrownumber;
vector<Record>br;
public:
Reader(){borrownumber=0;}
Reader(string s):Sno(s){};
Reader(string s,string n,string m,string ban,int bn);
Reader(string s,string n,string m,string ban,int bn,vector<Record>br);
~Reader(){};
string getSno(){return Sno;}
string getName(){return name;}
string getMajor(){return major;}
string getBanJi(){return banji;}
int getBorrowNumber(){return borrownumber;}
vector<Record>getBR(){return br;}
void setName(string name);
void setMajor(string major);
void setBanJi(string banji);
void setBorrowNumber(int borrownumber);
void setBR(vector<Record>br);
void display();
Reader &operator=(const Reader &r);
friend istream & operator >>(istream &in,Reader &r);
friend ostream & operator <<(ostream &out,Reader&r);
};
Reader::Reader(string s,string n,string m,string ban,int bn):Sno(s)
{
name=n;
major=m;
banji=ban;
borrownumber=bn;
}
Reader::Reader(string s,string n,string m,string ban,int bn,vector<Record>brr):Sno(s)
{
name=n;
major=m;
banji=ban;
borrownumber=bn;
br=brr;
}
void Reader::setName(string name)
{
this->name=name;
}
void Reader::setMajor(string major)
{
this->major=major;
}
void Reader::setBanJi(string banji)
{
this->banji=banji;
}
void Reader::setBorrowNumber(int borrownumber)
{
this->borrownumber=borrownumber;
}
void Reader::setBR(vector<Record>br)
{
this->br=br;
}
void Reader::display()
{
cout<<Sno<<" "<<name<<" "<<major<<" "<<banji<<" "<<borrownumber<<endl;
}
Reader &Reader::operator=(const Reader &r)
{
string *temp=const_cast<string*>(&Sno);
*temp=r.Sno;
name=r.name;
major=r.major;
borrownumber=r.borrownumber;
banji=r.banji;
br=r.br;
return *this;
}
istream &operator >>(istream &in,Reader &r)
{
string *temp=const_cast<string*>(&r.Sno);
string xh;
in>>xh;
*temp=xh;
in>>r.name>>r.major>>r.banji>>r.borrownumber;
return in;
}
ostream &operator <<(ostream &out,Reader &r)
{
out<<r.Sno<<" "<<r.name<<" "<<r.major<<" "<<r.banji<<" "<<r.borrownumber<<endl;
return out;
}
注:
1.常成员的声明
2.重载赋值运算符
四、操作类
数据成员:
图书/学生/借阅记录向量
成员函数:
对图书进行文件读写、在图书向量内完成对图书基本信息的增删查改;
对学生进行文件读写、在学生向量内完成对学生基本信息的增删查改;
借阅记录的管理和统计功能后续添加;
class operate
{
private:
vector<Book>b1;
vector<Reader>r1;
vector<Date>d1;
multimap<string,int>bookname;
multimap<string,int>booknumber;
multimap<string,int>readername;
multimap<string,int>readernumber;
public:
operate();
~operate();
operate(vector<Book>b1,vector<Reader>r1,vector<Date>d1);
vector<Book>getB(){return b1;}
vector<Reader>getR(){return r1;}
vector<Date>getD(){return d1;}
void BookRead();
void BookWrite();
void BookAdd(Book &b);
void BookSelect1(string bn);
void BookSelect2(string t);
void BookModify(string bn);
void BookDelete(string booknumber);
void ReaderRead();
void ReaderWrite();
void ReaderAdd(Reader &r);
void ReaderDelete(string Sno);
void ReaderModify(string Sno);
void ReaderSelect1(string Sno);
void ReaderSelect2(string name);
};
operate::operate()
{
BookRead();
ReaderRead();
}
operate::~operate()
{
BookWrite();
ReaderWrite();
}
operate::operate(vector<Book>b1,vector<Reader>r1,vector<Date>d1)
{
this->b1=b1;
this->r1=r1;
this->d1=d1;
}
void operate::BookRead()
{ Book t;
int a,b;
ifstream ins("d:\\book.text");
if(!ins)
{
return ;
}
while(ins.eof())
{
ins>>t;
ins>>a>>b;
t.setBookSum(a);
t.setBookIn(b);
b1.push_back(t);
bookname.insert(make_pair(t.getTitle(),b1.size()-1));
booknumber.insert(make_pair(t.getBookNumber(),b1.size()-1));
}
ins.close();
}
void operate::BookWrite()
{
fstream outs("d:\\book.txt");
vector<Book>::iterator i;
for(i=b1.begin();i!=b1.end();i++)
{
outs<<*i;
}
outs.close();
}
void operate::BookAdd(Book &b)
{
multimap<string,int>::iterator i;
i=booknumber.find(b.getTitle());
if(i==booknumber.end())
{
b.setBookSum(b.getBookSum()+1);
b.setBookIn(b.getBookIn()+1);
b1.push_back(b);
bookname.insert(make_pair(b.getTitle(),b1.size()-1));
booknumber.insert(make_pair(b.getBookNumber(),b1.size()-1));
}
else{
b.setBookSum(b1[i->second].getBookSum()+1);
b.setBookIn(b1[i->second].getBookIn()+1);
b1[i->second]=b;
}
}
void operate::BookDelete(string booknumber)
{
vector<Book>::iterator pos;
for(pos=b1.begin();pos<b1.end();pos++)
{
if(pos->getBookNumber()==booknumber)
{
b1.erase(pos);
}
}
}
void operate::BookSelect1(string bn)
{
multimap<string,int>::iterator i;
i=booknumber.find(bn);
if(i==booknumber.end())
{
return;
}
else
{
cout<<b1[i->second];
}
}
void operate::BookSelect2(string t)
{
multimap<string,int>::iterator i,j;
i=booknumber.find(t);
if(i==bookname.end())
{
return;
}
else
{
for(j=bookname.lower_bound(t);j!=bookname.upper_bound(t);j++)
{
cout<<b1[j->second]<<endl;
}
}
}
void operate::BookModify(string bn)
{
multimap<string,int>::iterator i;
i=booknumber.find(bn);
if(i==booknumber.end())
return;
else{
cin>>b1[i->second];
cout<<b1[i->second];
}
}
void operate::ReaderRead()
{ Reader p;
ifstream ins("d:\\reader.text");
if(!ins)
{
return ;
}
while(ins.eof())
{
ins>>p;
r1.push_back(p);
bookname.insert(make_pair(p.getName(),r1.size()-1));
booknumber.insert(make_pair(p.getSno(),r1.size()-1));
}
ins.close();
}
void operate::ReaderWrite()
{
fstream outs("d:\\reader.txt");
vector<Reader>::iterator i;
for(i=r1.begin();i!=r1.end();i++)
{
outs<<*i;
}
outs.close();
}
void operate::ReaderAdd(Reader &r)
{
r1.push_back(r);
readername.insert(make_pair(r.getName(),r1.size()-1));
readernumber.insert(make_pair(r.getSno(),r1.size()-1));
}
void operate::ReaderDelete(string Sno)
{
vector<Reader>::iterator pos;
for(pos=r1.begin();pos<r1.end();pos++)
{
if(pos->getSno()==Sno)
{
r1.erase(pos);
}
}
}
void operate::ReaderSelect1(string Sno)
{
multimap<string,int>::iterator i;
i=readernumber.find(Sno);
if(i==readernumber.end())
{
return;
}
else
{
cout<<r1[i->second];
}
}
void operate::ReaderSelect2(string name)
{
multimap<string,int>::iterator i,j;
i=readername.find(name);
if(i==readername.end())
{
return;
}
else
{
for(j=readername.lower_bound(name);j!=readername.upper_bound(name);j++)
{
cout<<r1[j->second]<<endl;
}
}
}
void operate::ReaderModify(string Sno)
{
multimap<string,int>::iterator i;
i=readernumber.find(Sno);
if(i==readernumber.end())
return;
else{
cin>>r1[i->second];
cout<<r1[i->second];
}
}
分析:
1.数据成员(借阅向量——vector)注:(vector的一些基础操作)
2.增删查改操作(利用map/multimap)
(1)map插入数据
1.使用数组 :用数组方式插入数据
2.insert函数插入pair数据
3.insert插入value_type数据
注:
使用后两者插入数据,利用的是集合的唯一性,也就是说当map容器中已经有了所要插入的数据,则insert操作是插入不了数据的。而使用数组操作时,会将已有的数据覆盖
(2)map中删除数据
earse():
1.iterator erase(iterator it);//通过一个条目对象删除
2.iterator erase(iterator first,iterator last)//删除一个范围
3.size_type erase(const Key&key);//通过关键字删除
4 .clear()就相当于enumMap.erase(enumMap.begin(),enumMap.end());
(3)查找操作
注意分为按学号和姓名查找还有模糊查找、区间查找等等
3.文件的读写操作
(1)读操作
void operate::BookRead()
{ Book t;
int a,b;
ifstream ins("d:\\book.text");
if(!ins)
{
return ;
}
while(ins.eof())
{
ins>>t;
ins>>a>>b;
t.setBookSum(a);
t.setBookIn(b);
b1.push_back(t);
bookname.insert(make_pair(t.getTitle(),b1.size()-1));
booknumber.insert(make_pair(t.getBookNumber(),b1.size()-1));
}
ins.close();
}
(2)写操作
void operate::BookWrite()
{
fstream outs("d:\\book.txt");
vector<Book>::iterator i;
for(i=b1.begin();i!=b1.end();i++)
{
outs<<*i;
}
outs.close();
}
五、心得体会
最近代码的难度越来越大了,经常为代码熬夜、头秃,也经常拿过题目来不知道该怎么下手。
不过最近学了很多很有用的知识,比如stl,比如文件的读写操作等等。
自从尝试了编系统了之后,对程序设计这门课算是又有了一个全新的认识。自己也开始审视自己之前学过的知识,觉得又有了不一样的体悟。虽然编程的过程比较痛苦,但是能编出来系统的感觉还是很好,每次完成作业的时候都觉得特别爽。而且因为有deadline,所以做事情的效率也比之前高了很多很多。
最后就是觉得,自己还差得远呢,还是得继续加油。我会继续努力的!!!!