借书系统,剩余书本的输出有点问题
#include <iostream>
#include <string>
using namespace std;
class CBook{
private:
string name;
string writer;
string publicName;
double price;
int number;
string ISBN;
public:
CBook() // 初始化
{
name = "Unknown";
writer = "Unknown";
publicName = "Unknown";
price = 0.0;
number = 0;
ISBN = "0000000000000";
}
void setData(string n, string w, string pN, double p, int num, string i) // 写入数据
{
name = n;
writer = w;
publicName = pN;
price = p;
number = num;
ISBN = i;
}
void borrow(int i = 1) // 借书,函数执行后 :number - 1
{
number = number - i;
cout << "Name : " << name << endl;
cout << "Writer : " << writer << endl;
cout << "ISBN : " << ISBN << endl;
cout << "Borrow Successfully!" << endl;
cout << "Remaining Number : " << number << endl;
cout << endl;
}
void print() // 输出书本信息
{
cout << "Name : " << name << endl;
cout << "Writer : " << writer << endl;
cout << "Public Name : " << publicName << endl;
cout << "Price : " << price << endl;
cout << "Remaining Number : " << number << endl;
cout << "ISBN : " << ISBN << endl;
cout << endl;
}
int getNumber() // 返回书本的剩余数量
{
return number;
}
};
class CReader{
private:
string Name;
string No;
string Major;
int Max;
int Number;
CBook *Books;
public:
CReader(){ //初始化
Name = "Unknown";
No = "000000";
Major = "Unknown";
Max = 10;
Number = 0;
Books = new CBook[Max];
}
CReader(const CReader &r){ // 拷贝构造函数
Name = r.Name;
No = r.No;
Major = r.Major;
Max = r.Max;
Number = r.Number;
Books = new CBook[Max];
Books = r.Books;
}
~CReader() // 析构函数
{
delete []Books;
}
void setData(string n, string n0, string m, int M, int bn) // 设置数据
{
Name = n;
No = n0;
Major = m;
Max = M;
Number = bn;
}
void printInfo() // 输出个人信息,如果借过书,则同时输出借出的书本的信息
{
cout << "Name : " << Name << endl;
cout << "No : " << No << endl;
cout << "Major : " << Major << endl;
cout << "Max borrow-Number : " << Max << endl;
cout << "Borrowed number : " << Number << endl;
cout << endl;
if(Number > 0){
cout << "Borrowed books information : " << endl;
for(int i = 0; i < Number; i++)
{
Books[i].print();
}
}
}
void borrow(CBook &b) // 借书函数
{
if(Number < Max && b.getNumber() > 0){
Books[Number++] = b;
b.borrow();
}
}
};
int main()
{
CBook arrB[1];
CReader arrR[1];
arrB[0].setData("Java编程思想", "埃克尔", "机械工业出版社", 78, 3, "9787111213826"); // 设置Java编程思想这本书的基本信息
arrR[0].setData("陈小艺", "150302", "计算机", 10, 0); // 设置陈小艺同学的个人信息,最大借书量10本,已借书量0本
arrR[0].printInfo(); // 输出陈小艺的个人信息
arrR[0].borrow(arrB[0]); // 让陈小艺借Java编程思想这本书
arrR[0].printInfo(); // 再次输出陈小艺的个人信息
}
#include <string>
using namespace std;
class CBook{
private:
string name;
string writer;
string publicName;
double price;
int number;
string ISBN;
public:
CBook() // 初始化
{
name = "Unknown";
writer = "Unknown";
publicName = "Unknown";
price = 0.0;
number = 0;
ISBN = "0000000000000";
}
void setData(string n, string w, string pN, double p, int num, string i) // 写入数据
{
name = n;
writer = w;
publicName = pN;
price = p;
number = num;
ISBN = i;
}
void borrow(int i = 1) // 借书,函数执行后 :number - 1
{
number = number - i;
cout << "Name : " << name << endl;
cout << "Writer : " << writer << endl;
cout << "ISBN : " << ISBN << endl;
cout << "Borrow Successfully!" << endl;
cout << "Remaining Number : " << number << endl;
cout << endl;
}
void print() // 输出书本信息
{
cout << "Name : " << name << endl;
cout << "Writer : " << writer << endl;
cout << "Public Name : " << publicName << endl;
cout << "Price : " << price << endl;
cout << "Remaining Number : " << number << endl;
cout << "ISBN : " << ISBN << endl;
cout << endl;
}
int getNumber() // 返回书本的剩余数量
{
return number;
}
};
class CReader{
private:
string Name;
string No;
string Major;
int Max;
int Number;
CBook *Books;
public:
CReader(){ //初始化
Name = "Unknown";
No = "000000";
Major = "Unknown";
Max = 10;
Number = 0;
Books = new CBook[Max];
}
CReader(const CReader &r){ // 拷贝构造函数
Name = r.Name;
No = r.No;
Major = r.Major;
Max = r.Max;
Number = r.Number;
Books = new CBook[Max];
Books = r.Books;
}
~CReader() // 析构函数
{
delete []Books;
}
void setData(string n, string n0, string m, int M, int bn) // 设置数据
{
Name = n;
No = n0;
Major = m;
Max = M;
Number = bn;
}
void printInfo() // 输出个人信息,如果借过书,则同时输出借出的书本的信息
{
cout << "Name : " << Name << endl;
cout << "No : " << No << endl;
cout << "Major : " << Major << endl;
cout << "Max borrow-Number : " << Max << endl;
cout << "Borrowed number : " << Number << endl;
cout << endl;
if(Number > 0){
cout << "Borrowed books information : " << endl;
for(int i = 0; i < Number; i++)
{
Books[i].print();
}
}
}
void borrow(CBook &b) // 借书函数
{
if(Number < Max && b.getNumber() > 0){
Books[Number++] = b;
b.borrow();
}
}
};
int main()
{
CBook arrB[1];
CReader arrR[1];
arrB[0].setData("Java编程思想", "埃克尔", "机械工业出版社", 78, 3, "9787111213826"); // 设置Java编程思想这本书的基本信息
arrR[0].setData("陈小艺", "150302", "计算机", 10, 0); // 设置陈小艺同学的个人信息,最大借书量10本,已借书量0本
arrR[0].printInfo(); // 输出陈小艺的个人信息
arrR[0].borrow(arrB[0]); // 让陈小艺借Java编程思想这本书
arrR[0].printInfo(); // 再次输出陈小艺的个人信息
}
代码太长了,大概是这样
首先设置好一本书本和一个user的信息之后,输出个人信息,没有问题
然后输出这本书本的信息
然后让这个user去借这本书,然后输出借书后的信息
借书的时候会输出这本书借出后的信息,也就是结束成功,并且number - 1,设置的时候,这本书的可借书量是3本,也就是借出之后还剩下两本
然后再次输出这本书本的信息
到上面为止,输出都没有问题,原来书本是3本,借出之后剩下两本
然后下面输出user的信息之后,就出现了问题
也就是这本书剩余原本应该是2本,但是这里输出了3本
找不到原因,想问一下这个事怎么回事,如何修改去解决