c++基于图书购买系统的二次开发
这是大一学习c++的一位同学的大作业,里面存在着诸多缺点,我对其中的一部分进行了改进,增加了一部分功能,但是还是有一些bug我至今修复不了。下面是原来的流程图
这是系统原本的功能。
在此基础上我添加了书籍排序,查找书籍和删除书籍的功能,并且对增加书籍进行了修改。
首先我先讲讲对增加书籍的修改
可以看到在输入要录入两本书籍时,只录入一本时就提示已经录入了两本。
在查看报错时发现
是price在报错,我仔细地查看了代码发现
点击查看代码
void System::Add_book()
{
cout << "请输入添加书籍数量:" << endl;
int addNum = 0;
cin >> addNum;
if (addNum > 0)
{
int newSize = m_bookNum + addNum;
book** newSpace = new book * [newSize];
if (this->m_bookArray != NULL)
{
for (int i = 0; i < this->m_bookNum; i++)
newSpace[i] = this->m_bookArray[i];
}
for (int i = 0; i < addNum; i++)
{
string ID;
string name;
string author;
string publish;
double price;
cout << "请输入第" << i + 1 << "本书的编号:" << endl;
cin >> ID;
cout << "请输入第" << i + 1 << "本书的名字:" << endl;
cin >> name;
cout << "请输入第" << i + 1 << "本书的作者:" << endl;
cin >> author;
cout << "请输入第" << i + 1 << "本书的出版社:" << endl;
cin >> publish;
cout << "请输入第" << i + 1 << "本书的定价:" << endl;
cin >> price;
book* b1 = NULL;
b1 = new book(ID, name, author, publish, price);
newSpace[this->m_bookNum + i] = b1;
delete[] this->m_bookArray;
this->m_bookArray = newSpace;
this->m_FileIsEmpty = false;
this->m_bookNum = newSize;
cout << "成功录入" << addNum << "本书籍" << endl;
this->book_save();
}
}
else
{
cout << "输入有误" << endl;
}
system("pause");
system("cls");
}
点击查看代码
void System::Add_book()
{
cout << "请输入添加书籍数量:" << endl;
int addNum = 0;
cin >> addNum;
if (addNum > 0)
{
int newSize = m_bookNum + addNum;
book** newSpace = new book * [newSize];
if (this->m_bookArray != NULL)
{
for (int i = 0; i < this->m_bookNum; i++)
newSpace[i] = this->m_bookArray[i];
}
for (int i = 0; i < addNum; i++)
{
string ID;
string name;
string author;
string publish;
double price;
cout << "请输入第" << i + 1 << "本书的编号:" << endl;
cin >> ID;
cout << "请输入第" << i + 1 << "本书的名字:" << endl;
cin >> name;
cout << "请输入第" << i + 1 << "本书的作者:" << endl;
cin >> author;
cout << "请输入第" << i + 1 << "本书的出版社:" << endl;
cin >> publish;
cout << "请输入第" << i + 1 << "本书的定价:" << endl;
cin >> price;
book* b1 = NULL;
b1 = new book(ID, name, author, publish, price);
newSpace[this->m_bookNum + i] = b1;
}
delete[] this->m_bookArray;
this->m_bookArray = newSpace;
this->m_FileIsEmpty = false;
this->m_bookNum = newSize;
cout << "成功录入" << addNum << "本书籍" << endl;
this->book_save();
}
else
{
cout << "输入有误" << endl;
}
system("pause");
system("cls");
}
此时也可以发现文本内也有输入进去的内容
其次我再来讲述一下我增加的书籍排序功能
点击查看代码
void System::Sort_book()
{
if (this->m_FileIsEmpty)
{
cout << "文件不存在或记录为空!" << endl;
system("pause");
system("cls");
}
else
{
cout << "请选择排序方式:" << endl;
cout << "1、按书定价进行升序" << endl;
cout << "2、按书定价进行降序" << endl;
int select = 0;
cin >> select;
int n = this->get_bookNum();
for (int i = 0; i < n; i++)
{
int minOrmax = i;
for (int j = 0; j < n; j++)
{
if (select == 1)//升序
{
if (this->m_bookArray[minOrmax]->getprice() > this->m_bookArray[j]->getprice()) {
minOrmax = j;
}
}
else//降序
{
if (this->m_bookArray[minOrmax]->getprice() < this->m_bookArray[j]->getprice()) {
minOrmax = j;
}
}
}
if (i != minOrmax)
{
book* temp = this->m_bookArray[i];
this->m_bookArray[i] = this->m_bookArray[minOrmax];
this->m_bookArray[minOrmax] = temp;
}
}
cout << "排序成功!排序后的结果为:" << endl;
for (int i = 0; i < n; i++)
{
this->m_bookArray[i]->display();
cout << endl;
}
//this->init_book();
system("pause");
system("cls");
}
}
再来看看升序排序完后
接下来是降序
然后是我增加的查找书籍功能
点击查看代码
void book::find_book()
{
cout << "请选择查找的方式:" << endl;
cout << "1、按书的编号查找" << endl;
cout << "2、按书名查找" << endl;
int select = 0;
cin >> select;
System users;
int n1 = users.get_bookNum();
users.m_bookNum = n1;
users.m_bookArray = new book * [users.m_bookNum];
users.init_book();
if (select == 1)
{
//按编号查找
string id;
cout << "请输入书的编号:" << endl;
cin >> id;
int ret = this->IsExist(id);
if (ret != -1)
{
cout << "查找成功!该书籍信息如下:" << endl;
users.m_bookArray[ret]->display();
}
else
{
cout << "查找失败,无此书" << endl;
}
}
else if (select == 2)
{
//按书名查找
string name;
cout << "请输入书名:" << endl;
cin >> name;
bool flag = true;
for (int j = 0; j < n1; j++) {
if (users.m_bookArray[j]->getbook_name() == name)
{
cout << "查找成功!该书籍信息如下:" << endl;
users.m_bookArray[j]->display();
flag = false;
}
}
if (flag)
{
cout << "查找失败,查无此书" << endl;
}
}
else
{
cout << "输入选项有误!" << endl;
}
system("pause");
system("cls");
}
点击查看代码
int book::IsExist(string i)
{
System u;
int n = u.get_bookNum();
u.m_bookNum = n;
u.m_bookArray = new book * [u.m_bookNum];
u.init_book();
for (int j = 0; j < n; j++)
{
if (u.m_bookArray[j]->getbook_ID() == i)
return j;
}
return -1;
}
最后一项是删除书籍功能
点击查看代码
void book::Del_book()
{
System User;
int n1 = User.get_bookNum();
User.m_bookNum = n1;
User.m_bookArray = new book * [User.m_bookNum];
User.init_book();
cout << "请输入想要删除书籍编号:" << endl;
string id = "";
cin >> id;
int index = this->IsExist(id);
if (index != -1)//说明书存在,并且要删除掉index位置上的书籍
{
for (int i = index; i < User.m_bookNum - 1; i++)
{
//数据前移
User.m_bookArray[i] = User.m_bookArray[i + 1];
}
User.m_bookNum--;//更新数组中记录总书籍数
//数据同步更新到文件中
User.book_save();
cout << "删除成功!" << endl;
}
else
{
cout << "删除失败,未找到该书籍" << endl;
}
system("pause");
system("cls");
}
至此我的第一次二次开发就结束了。下面是我的更改完后的流程图
虽然这些效果看起来很简单但是却费了我好多时间,一个是因为是二次开发,需要阅读别人的代码,在别人的基础之上来开发新功能必须得先理解他的代码,而这份代码因为是大一同学所做,技术不够成熟,可读性极差,需要反反复复的去阅读,可以说真的和屎山一样,有时候找一个东西找半天,而且源代码不够简洁,太影响阅读体验了,还有一个原因是我本来还想再增加一个书本的数量的功能的,但是我在实现的过程中却遇到了与上面的price一样的问题,但是我查了许多资料都无法解决,真的很苦恼,最最最主要的原因是这个系统的数据本来是由txt文件储存在本地的,我本来想链接数据库的但是不知道为什么,在一天晚上我安装好了,并且配置好了环境之后,第二天起来竟然不可以用了!!!我的visual studio显示mysql Access denied for user ‘root’@’localhost’ (using password: YES)(PS:忘记截图了当时)查询了好多资料都无法得到解决,真的是心态爆炸。
以上就是我的第一次二次开发的过程,充满了坎坷与崎岖
最后付上我的更改完后的全部源代码
点击查看代码
<book.h>
#pragma once
#include <iostream>
using namespace std;
class book {
string book_name;
string book_ID;
string author;
string publishing;
double price;
int bookNum;
public:
book();
book(string id, string name, string au, string pu, double pr);
void display();
string getbook_ID();
string getbook_name();
string getauthor();
string getpublishing();
double getprice();
//int getbookNum();
//void deletebooknum(int book_num);
void find_book();
int IsExist(string i);
void show_book();
void Del_book();
};
<Buyer.h>
#pragma once
#include <iostream>
#include <string>
using namespace std;
class buyer {
protected:
string name;
int buyerID;
string address;
double pay;
string identity;
double discount;
int leaguer_grade;
public:
buyer();
buyer(string n, int b, string a, double p, string i, double d = 0, int l = 0);
string getbuyname();
string getaddress();
double getpay();
int getid();
virtual void display() = 0;
virtual void setpay(double = 0) = 0;
virtual string getidentity() = 0;
virtual int getleaguer_grade() = 0;
virtual double getdiscount() = 0;
};
<honoured_guest.h>
#pragma once
#include <iostream>
#include <string>
using namespace std;
#include "Buyer.h"
class honoured_guest :public buyer {
public:
honoured_guest(string n, int b, string a, double p, string i = "贵宾", double d = 0, int l = 0);
virtual void display();
virtual void setpay(double p);
virtual string getidentity();
int getleaguer_grade();
double getdiscount();
};
<layfolk.h>
#pragma once
#include <iostream>
#include <string>
using namespace std;
#include "Buyer.h"
class layfolk :public buyer {
public:
layfolk(string n, int b, string a, double p, string i = "普通人", double d = 0, int l = 0);
virtual void display();
virtual void setpay(double p);
virtual string getidentity();
int getleaguer_grade();
double getdiscount();
};
<member.h>
#pragma once
#include <iostream>
#include <string>
using namespace std;
#include "Buyer.h"
#include <sstream>
class member :public buyer {
public:
member(string n, int b, string a, double p, string i = "会员", double d = 0, int l = 0);
virtual void display();
virtual void setpay(double p);
virtual string getidentity();
int getleaguer_grade();
double getdiscount();
double getpay();
};
<order.h>
#pragma once
#include <iostream>
#include "book.h"
#include "Buyer.h"
#include "system1.h"
using namespace std;
class order {
public:
void select_book(System& user1, int k, string id,int book_num);
order();
};
<system1.h>
#pragma once
#include <iostream>
using namespace std;
#include "Buyer.h"
#include "member.h"
#include "layfolk.h"
#include "honoured_guest.h"
#include "book.h"
#include <fstream>
#define FILENAME "buyerFile.txt"
#define FILENAME1 "book.txt"
class System {
public:
System();
~System();
void Show_menu();
//记录购书人数
int m_UseNum;
//买家数组指针
buyer** m_UseArray;
void Add_Use();
//判断文件是否为空标志
bool m_FileIsEmpty;
void save();
void book_save();
void init_Use();
void init_book();
int get_UseNum();
void ExitSystem();
int m_bookNum;
book** m_bookArray;
int get_bookNum();
void Sort_book();
void Add_book();
};
<book.cpp>
#include "book.h"
#include "system1.h"
book::book()
{
book_ID = "";
book_name = "";
author = "";
publishing = "";
price = 0;
bookNum = 0;
}
book::book(string id, string name, string au, string pu, double pr)
{
book_ID = id;
book_name = name;
author = au;
publishing = pu;
price = pr;
//bookNum = book_num;
}
void book::display()
{
cout << "书号:" << book_ID << "\t";
cout << "书名:" << book_name << "\t";
cout << "作者:" << author << "\t";
cout << "出版社:" << publishing << "\t";
cout << "定价:" << price << "\t";
//cout << "存余量" << bookNum << endl;
}
string book::getbook_ID()
{
return book_ID;
}
string book::getbook_name()
{
return book_name;
}
string book::getauthor()
{
return author;
}
string book::getpublishing()
{
return publishing;
}
double book::getprice()
{
return price;
}
//int book::getbookNum()
//{
//return bookNum;
//}
//void book::deletebooknum(int book_num)
//{
//bookNum -= book_num;
//}
void book::find_book()
{
cout << "请选择查找的方式:" << endl;
cout << "1、按书的编号查找" << endl;
cout << "2、按书名查找" << endl;
int select = 0;
cin >> select;
System users;
int n1 = users.get_bookNum();
users.m_bookNum = n1;
users.m_bookArray = new book * [users.m_bookNum];
users.init_book();
if (select == 1)
{
//按编号查找
string id;
cout << "请输入书的编号:" << endl;
cin >> id;
int ret = this->IsExist(id);
if (ret != -1)
{
cout << "查找成功!该书籍信息如下:" << endl;
users.m_bookArray[ret]->display();
}
else
{
cout << "查找失败,无此书" << endl;
}
}
else if (select == 2)
{
//按书名查找
string name;
cout << "请输入书名:" << endl;
cin >> name;
bool flag = true;
for (int j = 0; j < n1; j++) {
if (users.m_bookArray[j]->getbook_name() == name)
{
cout << "查找成功!该书籍信息如下:" << endl;
users.m_bookArray[j]->display();
flag = false;
}
}
if (flag)
{
cout << "查找失败,查无此书" << endl;
}
}
else
{
cout << "输入选项有误!" << endl;
}
system("pause");
system("cls");
}
int book::IsExist(string i)
{
System u;
int n = u.get_bookNum();
u.m_bookNum = n;
u.m_bookArray = new book * [u.m_bookNum];
u.init_book();
for (int j = 0; j < n; j++)
{
if (u.m_bookArray[j]->getbook_ID() == i)
return j;
}
return -1;
}
void book::show_book()
{
System users;
int n1 = users.get_bookNum();
users.m_bookNum = n1;
users.m_bookArray = new book * [users.m_bookNum];
users.init_book();
for (int i = 0; i < n1; i++)
{
users.m_bookArray[i]->display();
cout << endl;
}
system("pause");
system("cls");
}
void book::Del_book()
{
System User;
int n1 = User.get_bookNum();
User.m_bookNum = n1;
User.m_bookArray = new book * [User.m_bookNum];
User.init_book();
cout << "请输入想要删除书籍编号:" << endl;
string id = "";
cin >> id;
int index = this->IsExist(id);
if (index != -1)//说明书存在,并且要删除掉index位置上的书籍
{
for (int i = index; i < User.m_bookNum - 1; i++)
{
//数据前移
User.m_bookArray[i] = User.m_bookArray[i + 1];
}
User.m_bookNum--;//更新数组中记录总书籍数
//数据同步更新到文件中
User.book_save();
cout << "删除成功!" << endl;
}
else
{
cout << "删除失败,未找到该书籍" << endl;
}
system("pause");
system("cls");
}
<Buyer.cpp>
#include "Buyer.h"
buyer::buyer()
{
name = "";
buyerID = 0;
address = "";
pay = 0;
identity = "";
discount = 0;
leaguer_grade = 0;
}
buyer::buyer(string n, int b, string a, double p, string i, double d, int l)
{
identity = i;
name = n;
buyerID = b;
address = a;
pay = p;
discount = d;
leaguer_grade = l;
}
double buyer::getpay()
{
return pay;
}
string buyer::getaddress()
{
return address;
}
string buyer::getbuyname()
{
return name;
}
int buyer::getid()
{
return buyerID;
}
<honoured_guest.cpp>
#include "honoured_guest.h"
honoured_guest::honoured_guest(string n, int b, string a, double p, string i, double d, int l) :buyer(n, b, a, p, i, d, l)
{
}
void honoured_guest::display()
{
cout << "购书人姓名:" << name << "\t";
cout << "购书人编号:" << buyerID << "\t";
cout << "购书人为" << identity << "!折扣率为" << discount * 100 << "% \n";
cout << "地址:" << address << "\n\n";
}
void honoured_guest::setpay(double p)
{
pay = 0;
pay = pay + (1 - discount / 100) * p;
}
string honoured_guest::getidentity()
{
return identity;
}
int honoured_guest::getleaguer_grade()
{
return 0;
}
double honoured_guest::getdiscount()
{
return discount;
}
<layfolk.cpp>
#include "layfolk.h"
layfolk::layfolk(string n, int b, string a, double p, string i, double d, int l) :buyer(n, b, a, p, i, d, l)
{
}
void layfolk::display()
{
cout << "购书人姓名:" << name << "\t";
cout << "购书人编号:" << buyerID << "\t";
cout << "购书人为" << identity << "\n";
cout << "地址:" << address << "\n\n";
}
void layfolk::setpay(double p)
{
pay = 0;
pay = pay + p;
}
string layfolk::getidentity()
{
return identity;
}
int layfolk::getleaguer_grade()
{
return 0;
}
double layfolk::getdiscount()
{
return 0;
}
<member.cpp>
#include "member.h"
member::member(string n, int b, string a, double p, string i, double d, int l) :buyer(n, b, a, p, i, d, l)
{
}
void member::display()
{
cout << "购书人姓名:" << name << "\t";
cout << "购书人编号:" << buyerID << "\t";
cout << "购书人为" << identity << ",级别:" << leaguer_grade << "\n";
cout << "地址:" << address << "\n";
}
void member::setpay(double p)
{
if (this->leaguer_grade == 1)
pay = 0.95 * p;
else if (this->leaguer_grade == 2)
pay = 0.90 * p;
else if (this->leaguer_grade == 3)
pay = 0.85 * p;
else if (this->leaguer_grade == 4)
pay = 0.80 * p;
else if (this->leaguer_grade == 5)
pay = 0.70 * p;
else
cout << "级别错误!";
}
string member::getidentity()
{
return identity;
}
int member::getleaguer_grade()
{
return leaguer_grade;
}
double member::getdiscount()
{
return 0;
}
double member::getpay()
{
return pay;
}
<order.cpp>
#include "order.h"
#include "book.h"
void order::select_book(System& user1, int k, string a,int book_num)
{
int n = user1.get_UseNum();
int n3 = 0;
user1.m_UseNum = n;
user1.m_UseArray = new buyer * [user1.m_UseNum];
user1.init_Use();
for (int i = 0; i < n; i++)
{
if (user1.m_UseArray[i]->getid() == k)
{
n3 = i;
cout << "购书人为:" << user1.m_UseArray[i]->getbuyname() << " 购书人编号为:" << user1.m_UseArray[i]->getid() << endl;
}
}
int n1 = user1.get_bookNum();
int n2 = 0;
user1.m_bookNum = n1;
user1.m_bookArray = new book * [user1.m_bookNum];
user1.init_book();
for (int i = 0; i < n1; i++)
{
if (user1.m_bookArray[i]->getbook_ID() == a) {
n2 = i;
//user1.m_bookArray[i]->deletebooknum(book_num);
cout << "书籍编号为:" << user1.m_bookArray[i]->getbook_ID() << " 书名:" << user1.m_bookArray[i]->getbook_name() << " 作者:"
<< user1.m_bookArray[i]->getauthor() << " 出版社:" << user1.m_bookArray[i]->getpublishing() << " 定价:" << user1.m_bookArray[i]->getprice() << endl;
//<< "书籍存余量:" << user1.m_bookArray[i]->getbookNum() << endl;
}
}
double pr = user1.m_bookArray[n2]->getprice();
//user1.m_UseArray[n3]->setpay(pr);
if (user1.m_UseArray[n3]->getidentity() == "普通人")
{
}
else if (user1.m_UseArray[n3]->getidentity() == "会员")
{
int i = user1.m_UseArray[n3]->getleaguer_grade();
if (i == 1)
pr = .95 * pr;
else if (i == 2)
pr = 0.90 * pr;
else if (i == 3)
pr = .85 * pr;
else if (i == 4)
pr = 0.80 * pr;
else if (i == 5)
pr = 0.70 * pr;
}
else if (user1.m_UseArray[n3]->getidentity() == "贵宾")
{
double d = user1.m_UseArray[n3]->getdiscount();
pr = (1 - d / 100) * pr;
}
cout << "应付:" << pr << endl;
}
order::order()
{
}
<system1.cpp>
#include "system1.h"
System::System()
{
//1.文件不存在
ifstream ifs;
ifstream ifs1;
ifs1.open(FILENAME1, ios::in);
if (!ifs1.is_open())
{
this->m_bookNum = 0;
this->m_bookArray = NULL;
this->m_FileIsEmpty = true;
ifs1.close();
return;
}
ifs.open(FILENAME, ios::in);
if (!ifs.is_open())
{
//cout << "文件不存在" << endl;
this->m_UseNum = 0;
this->m_UseArray = NULL;
this->m_FileIsEmpty = true;
ifs.close();
return;
}
char ch1;
ifs1 >> ch1;
if (ifs1.eof())
{
this->m_bookNum = 0;
this->m_bookArray = NULL;
this->m_FileIsEmpty = true;
ifs1.close();
return;
}
//2.文件存在但数据为空
char ch;
ifs >> ch;
if (ifs.eof())
{
//cout << "文件为空!" << endl;
this->m_UseNum = 0;
this->m_UseArray = NULL;
this->m_FileIsEmpty = true;
ifs.close();
return;
}
int num1 = this->get_bookNum();
this->m_bookNum = num1;
this->m_bookArray = new book * [this->m_bookNum];
this->init_book();
//3.文件存在,并且记录数据
int num = this->get_UseNum();
//cout << "购物者人数为:" << num << endl;
this->m_UseNum = num;
this->m_UseArray = new buyer * [this->m_UseNum];
this->init_Use();
//for (int i = 0; i < this->m_UseNum; i++)
//{
//cout << "购书人姓名:" << this->m_UseArray[i]->getbuyname()
//<< " 购书人编号:" << this->m_UseArray[i]->getid();
//if (this->m_UseArray[i]->getidentity() == "会员")
//cout << " 购书人为会员,级别:" << this->m_UseArray[i]->getleaguer_grade();
//else if (this->m_UseArray[i]->getidentity() == "贵宾")
//cout << "购书人为贵宾!折扣率为" << this->m_UseArray[i]->getdiscount();
//cout << "地址:" << this->m_UseArray[i]->getaddress() << endl;
//}
}
void System::Add_Use()
{
cout << "请输入购书人数量:" << endl;
int addNum = 0;//保存用户的输入数量
cin >> addNum;
if (addNum > 0)
{
int newSize = m_UseNum + addNum;
buyer** newSpace = new buyer * [newSize];
if (this->m_UseArray != NULL)
{
for (int i = 0; i < this->m_UseNum; i++)
{
newSpace[i] = this->m_UseArray[i];
}
}
for (int i = 0; i < addNum; i++)
{
int layer;
double discount;
string name;
int buyerID;
string address;
double pay;
int Select;
string identity = "";
cout << "请输入第" << i + 1 << "个购书者名字:" << endl;
cin >> name;
cout << "请输入第" << i + 1 << "个购书者编号:" << endl;
cin >> buyerID;
cout << "请输入第" << i + 1 << "个购书者地址:" << endl;
cin >> address;
//cout << "请输入第" << i + 1 << "个购书者费用:" << endl;
pay=0;
cout << "请选择您的身份:" << endl;
cout << "1、普通人" << endl;
cout << "2、会员" << endl;
cout << "3、贵宾" << endl;
cin >> Select;
buyer* _buyer = NULL;
switch (Select)
{
case 1:
discount = 0;
layer = 0;
identity = "普通人";
_buyer = new layfolk(name, buyerID, address, pay, identity, discount, layer);
break;
case 2:
discount = 0;
identity = "会员";
cout << "请输入您的会员级别:" << endl;
cin >> layer;
_buyer = new member(name, buyerID, address, pay, identity, discount, layer);
break;
case 3:
layer = 0;
identity = "贵宾";
cout << "请输入折扣率:" << endl;
cin >> discount;
_buyer = new honoured_guest(name, buyerID, address, pay, identity, discount, layer);
break;
default:
break;
}
newSpace[this->m_UseNum + i] = _buyer;
}
//释放原有空间
delete[] this->m_UseArray;
//更改新空间的指向
this->m_UseArray = newSpace;
this->m_FileIsEmpty = false;
//更新新的购书者人数
this->m_UseNum = newSize;
cout << "成功录入" << addNum << "名购物者信息" << endl;
this->save();
}
else
{
cout << "输入有误" << endl;
}
system("pause");
system("cls");
}
int System::get_UseNum()
{
ifstream ifs;
int layer;
double discount;
ifs.open(FILENAME, ios::in);
string name1;
int ID;
string address1;
double pay1;
string type;
int num = 0;
while (ifs >> name1 && ifs >> ID && ifs >> address1 && ifs >> pay1 && ifs >> type && ifs >> discount && ifs >> layer)
{
num++;
}
return num;
}
System::~System()
{
if (this->m_UseArray != NULL)
{
delete[] this->m_UseArray;
this->m_UseArray = NULL;
}
}
void System::init_Use()
{
ifstream ifs;
ifs.open(FILENAME, ios::in);
string name1;
int ID;
string address1;
double pay1;
string type;
int layer;
double discount;
int index = 0;
while (ifs >> name1 && ifs >> ID && ifs >> address1 && ifs >> pay1 && ifs >> type && ifs >> discount && ifs >> layer)
{
buyer* buyer = NULL;
if (type == "会员")
buyer = new member(name1, ID, address1, pay1, type, discount, layer);
else if (type == "普通人")
buyer = new layfolk(name1, ID, address1, pay1, type, discount, layer);
else
buyer = new honoured_guest(name1, ID, address1, pay1, type, discount, layer);
this->m_UseArray[index] = buyer;
index++;
}
ifs.close();
}
void System::save()
{
ofstream ofs;
ofs.open(FILENAME, ios::out);
for (int i = 0; i < this->m_UseNum; i++)
{
ofs << this->m_UseArray[i]->getbuyname() << " "
<< this->m_UseArray[i]->getid() << " "
<< this->m_UseArray[i]->getaddress() << " "
<< this->m_UseArray[i]->getpay() << " "
<< this->m_UseArray[i]->getidentity() << " "
<< this->m_UseArray[i]->getdiscount() << " "
<< this->m_UseArray[i]->getleaguer_grade() << endl;
}
ofs.close();
}
void System::book_save()
{
ofstream ofs;
ofs.open(FILENAME1, ios::out);
for (int i = 0; i < this->m_bookNum; i++)
{
ofs << this->m_bookArray[i]->getbook_ID() << " "
<< this->m_bookArray[i]->getbook_name() << " "
<< this->m_bookArray[i]->getauthor() << " "
<< this->m_bookArray[i]->getpublishing() << " "
<< this->m_bookArray[i]->getprice() << endl;
}
ofs.close();
}
void System::Show_menu()
{
cout << "****************************************" << endl;
cout << "**********欢迎使用网上购书系统**********" << endl;
cout << "**********0.退出网上购书系统************" << endl;
cout << "**********1.填写使用者信息**************" << endl;
cout << "**********2.显示书籍********************" << endl;
cout << "**********3.选择要购买的书籍并结账******" << endl;
cout << "**********4.添加书籍********************" << endl;
cout << "**********5.对书籍进行排序**************" << endl;
cout << "**********6.查找书籍********************" << endl;
cout << "**********7.删除书籍********************" << endl;
cout << "****************************************" << endl;
cout << endl;
}
void System::ExitSystem()
{
cout << "欢迎下次使用" << endl;
system("pause");
exit(0);
}
void System::init_book()
{
ifstream ifs;
ifs.open(FILENAME1, ios::in);
string name;
string ID;
string publish;
string auth;
double price;
int index = 0;
while (ifs >> ID && ifs >> name && ifs >> auth && ifs >> publish && ifs >> price)
{
book* Book = NULL;
Book = new book(ID, name, auth, publish, price);
this->m_bookArray[index] = Book;
index++;
}
ifs.close();
}
int System::get_bookNum()
{
ifstream ifs;
ifs.open(FILENAME1, ios::in);
string name;
string ID;
string publish;
string auth;
double price;
int num = 0;
while (ifs >> ID && ifs >> name && ifs >> auth && ifs >> publish && ifs >> price)
{
num++;
}
return num;
}
void System::Sort_book()
{
if (this->m_FileIsEmpty)
{
cout << "文件不存在或记录为空!" << endl;
system("pause");
system("cls");
}
else
{
cout << "请选择排序方式:" << endl;
cout << "1、按书定价进行升序" << endl;
cout << "2、按书定价进行降序" << endl;
int select = 0;
cin >> select;
int n = this->get_bookNum();
for (int i = 0; i < n; i++)
{
int minOrmax = i;
for (int j = 0; j < n; j++)
{
if (select == 1)//升序
{
if (this->m_bookArray[minOrmax]->getprice() > this->m_bookArray[j]->getprice()) {
minOrmax = j;
}
}
else//降序
{
if (this->m_bookArray[minOrmax]->getprice() < this->m_bookArray[j]->getprice()) {
minOrmax = j;
}
}
}
if (i != minOrmax)
{
book* temp = this->m_bookArray[i];
this->m_bookArray[i] = this->m_bookArray[minOrmax];
this->m_bookArray[minOrmax] = temp;
}
}
cout << "排序成功!排序后的结果为:" << endl;
for (int i = 0; i < n; i++)
{
this->m_bookArray[i]->display();
cout << endl;
}
//this->init_book();
system("pause");
system("cls");
}
}
void System::Add_book()
{
cout << "请输入添加书籍数量:" << endl;
int addNum = 0;
cin >> addNum;
if (addNum > 0)
{
int newSize = this->m_bookNum + addNum;
book** newSpace = new book * [newSize];
if (this->m_bookArray != NULL)
{
for (int i = 0; i < this->m_bookNum; i++)
newSpace[i] = this->m_bookArray[i];
}
for (int i = 0; i < addNum; i++)
{
string ID;
string name;
string author;
string publish;
double price;
cout << "请输入第" << i + 1 << "本书的编号:" << endl;
cin >> ID;
cout << "请输入第" << i + 1 << "本书的名字:" << endl;
cin >> name;
cout << "请输入第" << i + 1 << "本书的作者:" << endl;
cin >> author;
cout << "请输入第" << i + 1 << "本书的出版社:" << endl;
cin >> publish;
cout << "请输入第" << i + 1 << "本书的定价:" << endl;
cin >> price;
book* b1 = NULL;
b1 = new book(ID, name, author, publish, price);
newSpace[this->m_bookNum + i] = b1;
}
delete[] this->m_bookArray;
this->m_bookArray = newSpace;
this->m_FileIsEmpty = false;
this->m_bookNum = newSize;
cout << "成功录入" << addNum << "本书籍" << endl;
this->book_save();
}
else
{
cout << "输入有误" << endl;
}
system("pause");
system("cls");
}
<网上购书结账系统.cpp>
#include <iostream>
using namespace std;
#include "system1.h"
#include "Buyer.h"
#include "member.h"
#include "layfolk.h"
#include "honoured_guest.h"
#include "book.h"
#include "order.h"
int main()
{
System users;
int choice = 0;
order order1;
book b1;
while (true)
{
users.Show_menu();
cout << "请输入您的选择:" << endl;
cin >> choice;
switch (choice)
{
case 0://退出
users.ExitSystem();
break;
case 1://填写信息
users.Add_Use();
break;
case 2://显示书籍
b1.show_book();
break;
case 3://选择书籍并结账
{
int k;
string I;
double p;
int booknum;
int op;
while (1)
{
cout << "选择要购买书籍的人的编号:" << endl;
cin >> k;
cout << "选择要购买书籍的ID:" << endl;
cin >> I;
cout << "选择要购买书籍的数量" << endl;
cin >> booknum;
order1.select_book(users, k, I,booknum);
cout << "请继续选择" << endl;
cout << "1、退出" << endl;
cout << "2、继续购买" << endl;
cin >> op;
if (op == 2)
continue;
else
system("pause");
system("cls");
break;
}
break;
}
case 4:
users.Add_book();//添加书籍
break;
case 5:
users.Sort_book();//排序
break;
case 6:
b1.find_book();//查找书籍
break;
case 7:
b1.Del_book();//删除书籍
break;
default:
system("cls");
break;
}
}
system("pause");
return 0;
}