对某图书购买系统的优化
前言
高尔基曾经说过:书籍是人类进步的阶梯,而修改代码是人类进步的电梯。
在一个朋友那找到了一个图书购买系统,在经过一番阅读理解后发现了一些不足之处,现对其进行改良优化。
对原代码的理解
主要功能有:
- 购书
- 查询图书信息
- 创建账号
- 身份信息的核验
在经过调试后发现基础功能相对比较完善,但是作为一个管理员只能在文件里修改库存的图书信息而无法通过程序来修改库存信息,这显然不符合 一个完整的购书系统的要求。
所以我准备在程序中额外加入可以修改图书库存信息的功能,这样管理员就不用再找到后台的数据文件去修改图书库存信息了。
点击查看原代码
#include<bits/stdc++.h>
#include<windows.h>
using namespace std;
class buyer {
protected:
string name;
int buyerID;
string address;
double pay;
public:
buyer();
buyer(string n, int b, string a, double p);
string getbuyname();
string getaddress();
double getpay();
int getid();
virtual void display() = 0;
virtual void setpay(double = 0) = 0;
};
class layfolk :public buyer { //普通人
public:
layfolk(string n, int b, string a, double p) :buyer(n, b, a, p) {
}
void display();
void setpay(double p);
};
class member :public buyer { //会员
private:
int leaguer_grade;
public:
member(string n, int b, int l, string a, double p) :buyer(n, b, a, p) {
leaguer_grade = l;
}
void display();
void setpay(double p);
};
class honoured_guest :public buyer { //贵宾
double discount_rate;
public:
honoured_guest(string n, int b, double r, string a, double p) :buyer(n, b, a, p) {
discount_rate = r;
}
void display();
void setpay(double p);
};
buyer::buyer() {
name = "";
buyerID = 0;
address = "";
pay = 0;
}
buyer::buyer(string n, int b, string a, double p) {
name = n;
buyerID = b;
address = a;
pay = p;
}
double buyer::getpay() {
return pay;
}
string buyer::getaddress() {
return address;
}
string buyer::getbuyname() {
return name;
}
int buyer::getid() {
return buyerID;
}
void layfolk::display() {
cout << "收货人姓名:" << name << "\t";
cout << "购书人编号:" << setw(6) << setfill('0') << buyerID << "\t";
cout << "当前购书人为普通客人" << "\n";
cout << "地址是:" << address << endl << endl;
}
void layfolk::setpay(double p) {
pay = pay + p;
}
void member::display() {
cout << "收货人姓名:" << name << "\t";
cout << "购书人编号:" << setw(6) << setfill('0') << buyerID << "\t";
cout << "购书人是" << leaguer_grade << "星级会员" << endl;
cout << "地址是:" << address << endl << endl;
}
void member::setpay(double p) {
if (leaguer_grade == 1) pay += 0.95 * p;
if (leaguer_grade == 2) pay += 0.9 * p;
if (leaguer_grade == 3) pay += 0.85 * p;
if (leaguer_grade == 4) pay += 0.8 * p;
if (leaguer_grade == 5) pay += 0.7 * p;
else cout << "请输入正确的会员等级!";
}
void honoured_guest::display() {
cout << "收货人姓名:" << name << "\t";
cout << "购书人编号:" << setw(6) << setfill('0') << buyerID << "\t";
cout << "购书人是贵宾!折扣率为:" << discount_rate * 100 << "%" << endl;
cout << "地址是:" << address << endl << endl;
}
void honoured_guest::setpay(double p) {
pay += (1 - discount_rate) * p;
}
static int x = 0;
class book {
protected:
string book_ID;
string book_name;
string author;
string publishing;
double price;
int num;
public:
book();
book(string i, string n, string a, string p, double pr, int number);
void display();
void display1();
string getbook_ID();
string getbook_name();
string getauthor();
string getpublishing();
double getprice();
int getnum();
void numdec(int n);
void addnum(int n);
void setID(string ID) {
book_ID = ID;
}
void setname(string name) {
book_name = name;
}
void setauthor(string au) {
author = au;
}
void setpublishing(string pu) {
publishing = pu;
}
void setprice(double pr) {
price = pr;
}
void setnum(int nu) {
num = nu;
}
} books[999999];
book::book() {
book_ID = "";
book_name = "";
author = "";
publishing = "";
price = 0;
}
book::book(string i, string n, string a, string p, double pr, int number) {
book_ID = i;
book_name = n;
author = a;
publishing = p;
price = pr;
num = number;
}
void book::display() {
cout << "书号:" << book_ID << "\t";
cout << "书名:" << book_name << "\t";
cout << "作者:" << author << endl;
cout << "出版社:" << publishing << "\t";
cout << "定价:" << price << endl;
cout << "当前数量:" << num << "本" << endl << 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::getnum() {
return num;
}
void book::addnum(int n) {
num += n;
}
void book::numdec(int n) {
num -= n;
}
void loading() {
printf("\n\n\t\tLoading......\n\n\n");
printf(" ");
for (int i = 4; i <= 40; ++i) {
cout << ">";
Sleep(30); //延迟输出
}
printf(" ");
}
void print_begin() {
cout << endl << "\t " << "欢迎进入图书购物系统!" << endl;
cout << "********************************************\n\n";
cout << "1.购买图书" << endl << endl << "2.查询图书库存" << endl << endl << "3.退出系统" << endl << endl;
cout << "请选择:";
}
void cls() {
system("cls");
system("color 0B");
}
void refresh() {
cls();
print_begin();
}
void opt_error() {
cout << "\n 请输入正确的选项!\n\n";
}
void get_txt() {
fstream f;
f.open("books.txt", ios::in);
string ID;
string name;
string au;
string pu;
double pr;
int nu;
int i;
for (i = 0; !f.eof(); i++) {
f >> ID >> name >> au >> pu >> pr >> nu;
books[i].setID(ID);
books[i].setname(name);
books[i].setauthor(au);
books[i].setpublishing(pu);
books[i].setprice(pr);
books[i].setnum(nu);
}
f.close();
x = i;
}
void print_menu() {
FILE* fp;
if ((fp = fopen("books.txt", "rb+")) == NULL) {
printf("\n\t当前没有库存!\n\n");
return;
}
fclose(fp);
get_txt();
cout << "\t\t\t本店书品" << endl << endl;
for (int i = 0; i < x - 1; i++) {
books[i].display();
}
}
void opt_layfolk(int ID) {
double money = 0;
// memset(books,0,sizeof(books));
get_txt();
cout << endl << "\t\t您好,亲爱的客人!" << endl;
cout << "1.按书名购买" << endl << "2.按书号购买" << endl << "请选择:";
string s;
cin >> s;
if (s == "1") {
while (1) {
cls();
print_menu();
cout << "请输入您要买的书的书名:";
string sn;
cin >> sn;
string m;
for (int i = 0; i < x - 1; i++) {
if (books[i].getbook_name() == sn) {
cout << endl << "当前《" << sn << "》还剩" << books[i].getnum() << "本" << endl << "单价为:" << books[i].getprice() << "元" << endl;
cout << "请输入您要买的数量:";
int shuliang;
cin >> shuliang;
if (shuliang <= books[i].getnum()) {
cout << "添加成功!" << endl;
money += shuliang * books[i].getprice();
books[i].numdec(shuliang);
break;
}
if (shuliang > books[i].getnum()) {
cout << "抱歉,库存不够哦亲~";
break;
}
}
if (i == x - 2) {
cout << "抱歉,暂时没有这本书哦亲~" << endl;
break;
}
}
cout << endl << "是否继续购买?" << endl << "1.继续" << endl << "2.结算" << endl << "请选择:";
string charge;
cin >> charge;
if (charge == "1") {
continue;
}
if (charge == "2") {
cout << "请输入收货人姓名和收货地址:" << endl;
string n, a;
cin >> n >> a;
layfolk lp(n, ID, a, 0);
cout << endl << "请确认您的订单信息:" << endl;
lp.setpay(money);
lp.display();
cout << "您需要支付的金额为:" << lp.getpay() << "元" << endl;
while (1) {
cout << "按1完成订单!" << endl << "按2取消订单" << endl;
cin >> m;
if (m == "1") {
cout << "订单已完成!" << endl << "感谢您的惠顾,欢迎下次光临!" << endl;
fstream fc("books.txt", ios::trunc); //重新录入改动后的库存信息
fc.close();
fstream f;
f.open("books.txt", ios::out);
for (int l = 0; l < x - 1; l++) {
f << books[l].getbook_ID() << " " << books[l].getbook_name() << " " << books[l].getauthor() << " " << books[l].getpublishing() << " " << books[l].getprice() << " " << books[l].getnum() << " ";
}
break;
}
if (m == "2") {
cout << "订单已取消!" << endl << "欢迎下次光临!" << endl;
break;
}
else {
cout << "请输入正确的选项哦~" << endl;
continue;
}
}
if (m == "1" || m == "2") break;
}
else {
cout << "请输入正确的选项哦" << endl;
}
}
}
if (s == "2") {
while (1) {
cls();
print_menu();
cout << "请输入您要买的书的书号:";
string sn;
cin >> sn;
string m;
for (int i = 0; i < x - 1; i++) {
if (books[i].getbook_ID() == sn) {
cout << endl << "当前" << sn << "号图书《" << books[i].getbook_name() << "》还剩" << books[i].getnum() << "本" << endl << "单价为:" << books[i].getprice() << "元" << endl;
cout << "请输入您要买的数量:";
int shuliang;
cin >> shuliang;
if (shuliang <= books[i].getnum()) {
cout << "添加成功!" << endl;
money += shuliang * books[i].getprice();
books[i].numdec(shuliang);
break;
}
if (shuliang > books[i].getnum()) {
cout << "抱歉,库存不够哦亲~";
break;
}
}
if (i == x - 2) {
cout << "抱歉,暂时没有这本书哦亲~" << endl;
break;
}
}
cout << endl << "是否继续购买?" << endl << "1.继续" << endl << "2.结算" << endl << "请选择:";
string charge;
cin >> charge;
if (charge == "1") {
continue;
}
if (charge == "2") {
cout << "请输入收货人姓名和收货地址:" << endl;
string n, a;
cin >> n >> a;
layfolk lp(n, ID, a, 0);
cout << endl << "请确认您的订单信息:" << endl;
lp.setpay(money);
lp.display();
cout << "您需要支付的金额为:" << lp.getpay() << "元" << endl;
while (1) {
cout << "按1完成订单!" << endl << "按2取消订单" << endl;
cin >> m;
if (m == "1") {
cout << "订单已完成!" << endl << "感谢您的惠顾,欢迎下次光临!" << endl;
fstream fc("books.txt", ios::trunc); //重新录入改动后的库存信息
fc.close();
fstream f;
f.open("books.txt", ios::out);
for (int l = 0; l < x - 1; l++) {
f << books[l].getbook_ID() << " " << books[l].getbook_name() << " " << books[l].getauthor() << " " << books[l].getpublishing() << " " << books[l].getprice() << " " << books[l].getnum() << " ";
}
break;
}
if (m == "2") {
cout << "订单已取消!" << endl << "欢迎下次光临!" << endl;
break;
}
else {
cout << "请输入正确的选项哦~" << endl;
continue;
}
}
}
else {
cout << "请输入正确的选项哦!" << endl;
}
if (m == "1" || m == "2") break;
}
}
}
void opt_member(int ID) {
double money = 0;
// memset(books,0,sizeof(books));
int star = ID % 10;
int l;
if (star == 1 || star == 2) l = 1; //计算会员星级
if (star == 3 || star == 4) l = 2;
if (star == 5 || star == 6) l = 3;
if (star == 7 || star == 8) l = 4;
if (star == 9 || star == 0) l = 5;
cout << endl << "\t\t您好,亲爱的" << l << "星会员!" << endl;
cout << "1.按书名购买" << endl << "2.按书号购买" << endl << "请选择:";
string s;
cin >> s;
if (s == "1") {
while (1) {
cls();
print_menu();
cout << "请输入您要买的书的书名:";
string sn;
cin >> sn;
string m;
for (int i = 0; i < x - 1; i++) {
if (books[i].getbook_name() == sn) {
cout << endl << "当前《" << sn << "》还剩" << books[i].getnum() << "本" << endl << "单价为:" << books[i].getprice() << "元" << endl;
cout << "请输入您要买的数量:";
int shuliang;
cin >> shuliang;
if (shuliang <= books[i].getnum()) {
cout << "添加成功!" << endl;
money += shuliang * books[i].getprice();
books[i].numdec(shuliang);
break;
}
if (shuliang > books[i].getnum()) {
cout << "抱歉,库存不够哦亲~";
break;
}
}
if (i == x - 2) {
cout << "抱歉,暂时没有这本书哦亲~" << endl;
break;
}
}
cout << endl << "是否继续购买?" << endl << "1.继续" << endl << "2.结算" << endl << "请选择:";
string charge;
cin >> charge;
if (charge == "1") {
continue;
}
if (charge == "2") {
cout << "请输入收货人姓名和收货地址:" << endl;
string n, a;
cin >> n >> a;
member lp(n, ID, l, a, 0);
cout << endl << "请确认您的订单信息:" << endl;
lp.setpay(money);
lp.display();
cout << "您需要支付的金额为:" << lp.getpay() << "元" << endl;
while (1) {
cout << "按1完成订单!" << endl << "按2取消订单" << endl;
cin >> m;
if (m == "1") {
cout << "订单已完成!" << endl << "感谢您的惠顾,欢迎下次光临!" << endl;
fstream fc("books.txt", ios::trunc); //重新录入改动后的库存信息
fc.close();
fstream f;
f.open("books.txt", ios::out);
for (int l = 0; l < x - 1; l++) {
f << books[l].getbook_ID() << " " << books[l].getbook_name() << " " << books[l].getauthor() << " " << books[l].getpublishing() << " " << books[l].getprice() << " " << books[l].getnum() << " ";
}
break;
}
if (m == "2") {
cout << "订单已取消!" << endl << "欢迎下次光临!" << endl;
break;
}
else {
cout << "请输入正确的选项哦~" << endl;
continue;
}
}
if (m == "1" || m == "2") break;
}
else {
cout << "请输入正确的选项哦" << endl;
}
}
}
if (s == "2") {
while (1) {
cls();
print_menu();
cout << "请输入您要买的书的书号:";
string sn;
cin >> sn;
string m;
for (int i = 0; i < x - 1; i++) {
if (books[i].getbook_ID() == sn) {
cout << endl << "当前" << sn << "号图书《" << books[i].getbook_name() << "》还剩" << books[i].getnum() << "本" << endl << "单价为:" << books[i].getprice() << "元" << endl;
cout << "请输入您要买的数量:";
int shuliang;
cin >> shuliang;
if (shuliang <= books[i].getnum()) {
cout << "添加成功!" << endl;
money += shuliang * books[i].getprice();
books[i].numdec(shuliang);
break;
}
if (shuliang > books[i].getnum()) {
cout << "抱歉,库存不够哦亲~";
break;
}
}
if (i == x - 2) {
cout << "抱歉,暂时没有这本书哦亲~" << endl;
break;
}
}
cout << endl << "是否继续购买?" << endl << "1.继续" << endl << "2.结算" << endl << "请选择:";
string charge;
cin >> charge;
if (charge == "1") {
continue;
}
if (charge == "2") {
cout << "请输入收货人姓名和收货地址:" << endl;
string n, a;
cin >> n >> a;
int star = ID % 10;
int l;
if (star == 1 || star == 2) l = 1; //计算会员星级
if (star == 3 || star == 4) l = 2;
if (star == 5 || star == 6) l = 3;
if (star == 7 || star == 8) l = 4;
if (star == 9 || star == 0) l = 5;
member lp(n, ID, l, a, 0);
cout << endl << "请确认您的订单信息:" << endl;
lp.setpay(money);
lp.display();
cout << "您需要支付的金额为:" << lp.getpay() << "元" << endl;
while (1) {
cout << "按1完成订单!" << endl << "按2取消订单" << endl;
cin >> m;
if (m == "1") {
cout << "订单已完成!" << endl << "感谢您的惠顾,欢迎下次光临!" << endl;
fstream fc("books.txt", ios::trunc); //重新录入改动后的库存信息
fc.close();
fstream f;
f.open("books.txt", ios::out);
for (int l = 0; l < x - 1; l++) {
f << books[l].getbook_ID() << " " << books[l].getbook_name() << " " << books[l].getauthor() << " " << books[l].getpublishing() << " " << books[l].getprice() << " " << books[l].getnum() << " ";
}
break;
}
if (m == "2") {
cout << "订单已取消!" << endl << "欢迎下次光临!" << endl;
break;
}
else {
cout << "请输入正确的选项哦~" << endl;
continue;
}
}
if (m == "1" || m == "2") break;
}
else {
cout << "请输入正确的选项哦!" << endl;
}
}
}
}
void opt_honoured_guest(int ID) {
double money = 0;
// memset(books,0,sizeof(books));
get_txt();
cout << endl << "\t\t您好,亲爱的贵宾!" << endl;
cout << "1.按书名购买" << endl << "2.按书号购买" << endl << "请选择:";
string s;
cin >> s;
if (s == "1") {
while (1) {
cls();
print_menu();
cout << "请输入您要买的书的书名:";
string sn;
cin >> sn;
string m;
for (int i = 0; i < x - 1; i++) {
if (books[i].getbook_name() == sn) {
cout << endl << "当前《" << sn << "》还剩" << books[i].getnum() << "本" << endl << "单价为:" << books[i].getprice() << "元" << endl;
cout << "请输入您要买的数量:";
int shuliang;
cin >> shuliang;
if (shuliang <= books[i].getnum()) {
cout << "添加成功!" << endl;
money += shuliang * books[i].getprice();
books[i].numdec(shuliang);
break;
}
if (shuliang > books[i].getnum()) {
cout << "抱歉,库存不够哦亲~";
break;
}
}
if (i == x - 2) {
cout << "抱歉,暂时没有这本书哦亲~" << endl;
break;
}
}
cout << endl << "是否继续购买?" << endl << "1.继续" << endl << "2.结算" << endl << "请选择:";
string charge;
cin >> charge;
if (charge == "1") {
continue;
}
if (charge == "2") {
cout << "请输入收货人姓名、收货地址以及您的专属折扣率:" << endl;
string n, a;
double r;
cin >> n >> a >> r;
honoured_guest lp(n, ID, r, a, 0);
cout << endl << "请确认您的订单信息:" << endl;
lp.setpay(money);
lp.display();
cout << "您需要支付的金额为:" << lp.getpay() << "元" << endl;
while (1) {
cout << "按1完成订单!" << endl << "按2取消订单" << endl;
cin >> m;
if (m == "1") {
cout << "订单已完成!" << endl << "感谢您的惠顾,欢迎下次光临!" << endl;
fstream fc("books.txt", ios::trunc); //重新录入改动后的库存信息
fc.close();
fstream f;
f.open("books.txt", ios::out);
for (int l = 0; l < x - 1; l++) {
f << books[l].getbook_ID() << " " << books[l].getbook_name() << " " << books[l].getauthor() << " " << books[l].getpublishing() << " " << books[l].getprice() << " " << books[l].getnum() << " ";
}
break;
}
if (m == "2") {
cout << "订单已取消!" << endl << "欢迎下次光临!" << endl;
break;
}
else {
cout << "请输入正确的选项哦~" << endl;
continue;
}
}
if (m == "1" || m == "2") break;
}
else {
cout << "请输入正确的选项哦" << endl;
}
}
}
if (s == "2") {
while (1) {
cls();
print_menu();
cout << "请输入您要买的书的书号:";
string sn;
cin >> sn;
string m;
for (int i = 0; i < x - 1; i++) {
if (books[i].getbook_ID() == sn) {
cout << endl << "当前" << sn << "号图书《" << books[i].getbook_name() << "》还剩" << books[i].getnum() << "本" << endl << "单价为:" << books[i].getprice() << "元" << endl;
cout << "请输入您要买的数量:";
int shuliang;
cin >> shuliang;
if (shuliang <= books[i].getnum()) {
cout << "添加成功!" << endl;
money += shuliang * books[i].getprice();
books[i].numdec(shuliang);
break;
}
if (shuliang > books[i].getnum()) {
cout << "抱歉,库存不够哦亲~";
break;
}
}
if (i == x - 2) {
cout << "抱歉,暂时没有这本书哦亲~" << endl;
break;
}
}
cout << endl << "是否继续购买?" << endl << "1.继续" << endl << "2.结算" << endl << "请选择:";
string charge;
cin >> charge;
if (charge == "1") {
continue;
}
if (charge == "2") {
cout << "请输入收货人姓名、收货地址以及您的专属折扣率:" << endl;
string n, a;
double r;
cin >> n >> a >> r;
honoured_guest lp(n, ID, r, a, 0);
cout << endl << "请确认您的订单信息:" << endl;
lp.setpay(money);
lp.display();
cout << "您需要支付的金额为:" << lp.getpay() << "元" << endl;
while (1) {
cout << "按1完成订单!" << endl << "按2取消订单" << endl;
cin >> m;
if (m == "1") {
cout << "订单已完成!" << endl << "感谢您的惠顾,欢迎下次光临!" << endl;
fstream fc("books.txt", ios::trunc); //重新录入改动后的库存信息
fc.close();
fstream f;
f.open("books.txt", ios::out);
for (int l = 0; l < x - 1; l++) {
f << books[l].getbook_ID() << " " << books[l].getbook_name() << " " << books[l].getauthor() << " " << books[l].getpublishing() << " " << books[l].getprice() << " " << books[l].getnum() << " ";
}
break;
}
if (m == "2") {
cout << "订单已取消!" << endl << "欢迎下次光临!" << endl;
break;
}
else {
cout << "请输入正确的选项哦~" << endl;
continue;
}
}
}
else {
cout << "请输入正确的选项哦!" << endl;
}
if (m == "1" || m == "2") break;
}
}
}
void opt_newperson() {
cout << "请输入您要申请的账号等级:" << endl;
cout << endl << "1.普通账号" << endl << endl << "2.会员账号" << endl << endl << "3.贵宾账号" << endl << endl << "4.退出" << endl << endl;
cout << "请选择:";
string s;
while (1) {
cin >> s;
if (s == "1") {
cls();
srand((unsigned)time(NULL));
int a = rand() % 900000 + 100000;
cout << "已成功创建普通账号!" << endl << endl;
cout << "您的账号ID为:" << " ";
printf("%06d\n", a);
break;
}
if (s == "2") {
cls();
srand((unsigned)time(NULL));
int a = rand() % 90000 + 10000;
cout << "请输入您要创建的会员账号星级(1~5)" << endl << endl; //根据星级给ID,1星最后一位为1或2; 2星为3或4; 。。5星为9或0
int s;
cin >> s;
if (s == 1) {
a = a / 10 * 10 + rand() % 1 + 1;
}
if (s == 2) {
a = a / 10 * 10 + rand() % 1 + 3;
}
if (s == 3) {
a = a / 10 * 10 + rand() % 1 + 5;
}
if (s == 4) {
a = a / 10 * 10 + rand() % 1 + 7;
}
if (s == 5) {
a = a / 10 * 10 + rand() % 1 + 9;
}
cout << "已成功创建" << s << "级会员账号!" << endl << endl;
cout << "您的账号ID为:" << " ";
printf("%06d\n", a);
break;
}
if (s == "3") {
cls();
srand((unsigned)time(NULL));
int a = rand() % 10000;
cout << "已成功创建贵宾账号!" << endl << endl;
cout << "您的账号ID为:" << " ";
printf("%06d\n", a);
break;
}
if (s == "4") {
cls();
break;
}
else {
cout << "请输入正确的选项!" << endl;
}
}
}
void opt1() {
cls();
cout << endl << "1.如果您已有账号,请输入“1”进行下一步操作!" << endl << endl;
cout << "2.如果您是本店的新用户,请输入“2”来为您注册一个账号!" << endl << endl;
cout << "3.退出" << endl << endl;
cout << "请选择:";
string s;
cin >> s;
if (s == "1") {
cls();
cout << "请输入您的ID:" << endl;
int ID;
cin >> ID; //根据ID号码来判断是哪种级别的客人
if (ID < 10000) {
cls();
print_menu();
opt_honoured_guest(ID);
}
if (ID >= 10000 && ID < 100000) {
cls();
print_menu();
opt_member(ID);
}
if (ID >= 100000) {
cls();
print_menu();
opt_layfolk(ID);
}
}
if (s == "2") {
cls();
opt_newperson();
}
if (s == "3") {
cls();
}
}
void opt2() {
cls();
FILE* fp;
if ((fp = fopen("books.txt", "rb+")) == NULL) {
printf("\n\t当前没有库存!\n\n");
return;
}
fclose(fp);
get_txt();
for (int i = 0; i < x - 1; i++) {
books[i].display();
}
}
void opt3() {
cls();
cout << endl << "\n\n\t感谢您的使用!" << endl << endl;
}
int main() {
system("color 0B");
loading();
cls();
print_begin();
int opt;
while (1) {
cin >> opt;
switch (opt) {
case 1:
opt1();
system("pause");
refresh();
break;
case 2:
opt2();
system("pause");
refresh();
break;
case 3:
opt3();
Sleep(500);
return 0;
default:
cls();
opt_error();
system("pause");
refresh();
break;
}
}
}
代码修改:
- 添加一个函数用来对图书信息文件进行读写,判断添加的图书是否为库存中已有的书,并依此来修改文件
- 添加了一个趣味功能:查询幸运指数
- 修改目录的内容
- 修改主函数的调用代码
点击查看修改后的代码
#include<bits/stdc++.h>
#include<windows.h>
using namespace std;
class buyer {
protected:
string name;
int buyerID;
string address;
double pay;
public:
buyer();
buyer(string n, int b, string a, double p);
string getbuyname();
string getaddress();
double getpay();
int getid();
virtual void display() = 0;
virtual void setpay(double = 0) = 0;
};
class layfolk :public buyer { //普通人
public:
layfolk(string n, int b, string a, double p) :buyer(n, b, a, p) {
}
void display();
void setpay(double p);
};
class member :public buyer { //会员
private:
int leaguer_grade;
public:
member(string n, int b, int l, string a, double p) :buyer(n, b, a, p) {
leaguer_grade = l;
}
void display();
void setpay(double p);
};
class honoured_guest :public buyer { //贵宾
double discount_rate;
public:
honoured_guest(string n, int b, double r, string a, double p) :buyer(n, b, a, p) {
discount_rate = r;
}
void display();
void setpay(double p);
};
buyer::buyer() {
name = "";
buyerID = 0;
address = "";
pay = 0;
}
buyer::buyer(string n, int b, string a, double p) {
name = n;
buyerID = b;
address = a;
pay = p;
}
double buyer::getpay() {
return pay;
}
string buyer::getaddress() {
return address;
}
string buyer::getbuyname() {
return name;
}
int buyer::getid() {
return buyerID;
}
void layfolk::display() {
cout << "收货人姓名:" << name << "\t";
cout << "购书人编号:" << setw(6) << setfill('0') << buyerID << "\t";
cout << "当前购书人为普通客人" << "\n";
cout << "地址是:" << address << endl << endl;
}
void layfolk::setpay(double p) {
pay = pay + p;
}
void member::display() {
cout << "收货人姓名:" << name << "\t";
cout << "购书人编号:" << setw(6) << setfill('0') << buyerID << "\t";
cout << "购书人是" << leaguer_grade << "星级会员" << endl;
cout << "地址是:" << address << endl << endl;
}
void member::setpay(double p) {
if (leaguer_grade == 1) pay += 0.95 * p;
if (leaguer_grade == 2) pay += 0.9 * p;
if (leaguer_grade == 3) pay += 0.85 * p;
if (leaguer_grade == 4) pay += 0.8 * p;
if (leaguer_grade == 5) pay += 0.7 * p;
else cout << "请输入正确的会员等级!";
}
void honoured_guest::display() {
cout << "收货人姓名:" << name << "\t";
cout << "购书人编号:" << setw(6) << setfill('0') << buyerID << "\t";
cout << "购书人是贵宾!折扣率为:" << discount_rate * 100 << "%" << endl;
cout << "地址是:" << address << endl << endl;
}
void honoured_guest::setpay(double p) {
pay += (1 - discount_rate) * p;
}
static int x = 0;
class book {
protected:
string book_ID;
string book_name;
string author;
string publishing;
double price;
int num;
public:
book();
book(string i, string n, string a, string p, double pr, int number);
void display();
void display1();
string getbook_ID();
string getbook_name();
string getauthor();
string getpublishing();
double getprice();
int getnum();
void numdec(int n);
void addnum(int n);
void setID(string ID) {
book_ID = ID;
}
void setname(string name) {
book_name = name;
}
void setauthor(string au) {
author = au;
}
void setpublishing(string pu) {
publishing = pu;
}
void setprice(double pr) {
price = pr;
}
void setnum(int nu) {
num = nu;
}
} books[999999];
book::book() {
book_ID = "";
book_name = "";
author = "";
publishing = "";
price = 0;
}
book::book(string i, string n, string a, string p, double pr, int number) {
book_ID = i;
book_name = n;
author = a;
publishing = p;
price = pr;
num = number;
}
void book::display() {
cout << "书号:" << book_ID << "\t";
cout << "书名:" << book_name << "\t";
cout << "作者:" << author << endl;
cout << "出版社:" << publishing << "\t";
cout << "定价:" << price << endl;
cout << "当前数量:" << num << "本" << endl << 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::getnum() {
return num;
}
void book::addnum(int n) {
num += n;
}
void book::numdec(int n) {
num -= n;
}
void loading() {
printf("\n\n\t\tLoading......\n\n\n");
printf(" ");
for (int i = 4; i <= 40; ++i) {
cout << ">";
Sleep(30); //延迟输出
}
printf(" ");
}
void print_begin() {
cout << endl << "\t " << "欢迎进入图书购物系统!" << endl;
cout << "********************************************\n\n";
cout << "1.购买图书" << endl << endl << "2.添加图书库存" << endl << endl << "3.查询图书库存" << endl << endl << "4.查询今日幸运指数" << endl << endl << "5.退出系统" << endl << endl;
cout << "请选择:";
}
void cls() {
system("cls");
system("color 0B");
}
void refresh() {
cls();
print_begin();
}
void opt_error() {
cout << "\n 请输入正确的选项!\n\n";
}
void get_txt() {
fstream f;
f.open("books.txt", ios::in);
string ID;
string name;
string au;
string pu;
double pr;
int nu;
int i;
for (i = 0; !f.eof(); i++) {
f >> ID >> name >> au >> pu >> pr >> nu;
books[i].setID(ID);
books[i].setname(name);
books[i].setauthor(au);
books[i].setpublishing(pu);
books[i].setprice(pr);
books[i].setnum(nu);
}
f.close();
x = i;
}
void print_menu() {
FILE* fp;
if ((fp = fopen("books.txt", "rb+")) == NULL) {
printf("\n\t当前没有库存!\n\n");
return;
}
fclose(fp);
get_txt();
cout << "\t\t\t本店书品" << endl << endl;
for (int i = 0; i < x - 1; i++) {
books[i].display();
}
}
void opt_layfolk(int ID) {
double money = 0;
// memset(books,0,sizeof(books));
get_txt();
cout << endl << "\t\t您好,亲爱的客人!" << endl;
cout << "1.按书名购买" << endl << "2.按书号购买" << endl << "请选择:";
string s;
cin >> s;
if (s == "1") {
while (1) {
cls();
print_menu();
cout << "请输入您要买的书的书名:";
string sn;
cin >> sn;
string m;
for (int i = 0; i < x - 1; i++) {
if (books[i].getbook_name() == sn) {
cout << endl << "当前《" << sn << "》还剩" << books[i].getnum() << "本" << endl << "单价为:" << books[i].getprice() << "元" << endl;
cout << "请输入您要买的数量:";
int shuliang;
cin >> shuliang;
if (shuliang <= books[i].getnum()) {
cout << "添加成功!" << endl;
money += shuliang * books[i].getprice();
books[i].numdec(shuliang);
break;
}
if (shuliang > books[i].getnum()) {
cout << "抱歉,库存不够哦亲~";
break;
}
}
if (i == x - 2) {
cout << "抱歉,暂时没有这本书哦亲~" << endl;
break;
}
}
cout << endl << "是否继续购买?" << endl << "1.继续" << endl << "2.结算" << endl << "请选择:";
string charge;
cin >> charge;
if (charge == "1") {
continue;
}
if (charge == "2") {
cout << "请输入收货人姓名和收货地址:" << endl;
string n, a;
cin >> n >> a;
layfolk lp(n, ID, a, 0);
cout << endl << "请确认您的订单信息:" << endl;
lp.setpay(money);
lp.display();
cout << "您需要支付的金额为:" << lp.getpay() << "元" << endl;
while (1) {
cout << "按1完成订单!" << endl << "按2取消订单" << endl;
cin >> m;
if (m == "1") {
cout << "订单已完成!" << endl << "感谢您的惠顾,欢迎下次光临!" << endl;
fstream fc("books.txt", ios::trunc); //重新录入改动后的库存信息
fc.close();
fstream f;
f.open("books.txt", ios::out);
for (int l = 0; l < x - 1; l++) {
f << books[l].getbook_ID() << " " << books[l].getbook_name() << " " << books[l].getauthor() << " " << books[l].getpublishing() << " " << books[l].getprice() << " " << books[l].getnum() << " ";
}
break;
}
if (m == "2") {
cout << "订单已取消!" << endl << "欢迎下次光临!" << endl;
break;
}
else {
cout << "请输入正确的选项哦~" << endl;
continue;
}
}
if (m == "1" || m == "2") break;
}
else {
cout << "请输入正确的选项哦" << endl;
}
}
}
if (s == "2") {
while (1) {
cls();
print_menu();
cout << "请输入您要买的书的书号:";
string sn;
cin >> sn;
string m;
for (int i = 0; i < x - 1; i++) {
if (books[i].getbook_ID() == sn) {
cout << endl << "当前" << sn << "号图书《" << books[i].getbook_name() << "》还剩" << books[i].getnum() << "本" << endl << "单价为:" << books[i].getprice() << "元" << endl;
cout << "请输入您要买的数量:";
int shuliang;
cin >> shuliang;
if (shuliang <= books[i].getnum()) {
cout << "添加成功!" << endl;
money += shuliang * books[i].getprice();
books[i].numdec(shuliang);
break;
}
if (shuliang > books[i].getnum()) {
cout << "抱歉,库存不够哦亲~";
break;
}
}
if (i == x - 2) {
cout << "抱歉,暂时没有这本书哦亲~" << endl;
break;
}
}
cout << endl << "是否继续购买?" << endl << "1.继续" << endl << "2.结算" << endl << "请选择:";
string charge;
cin >> charge;
if (charge == "1") {
continue;
}
if (charge == "2") {
cout << "请输入收货人姓名和收货地址:" << endl;
string n, a;
cin >> n >> a;
layfolk lp(n, ID, a, 0);
cout << endl << "请确认您的订单信息:" << endl;
lp.setpay(money);
lp.display();
cout << "您需要支付的金额为:" << lp.getpay() << "元" << endl;
while (1) {
cout << "按1完成订单!" << endl << "按2取消订单" << endl;
cin >> m;
if (m == "1") {
cout << "订单已完成!" << endl << "感谢您的惠顾,欢迎下次光临!" << endl;
fstream fc("books.txt", ios::trunc); //重新录入改动后的库存信息
fc.close();
fstream f;
f.open("books.txt", ios::out);
for (int l = 0; l < x - 1; l++) {
f << books[l].getbook_ID() << " " << books[l].getbook_name() << " " << books[l].getauthor() << " " << books[l].getpublishing() << " " << books[l].getprice() << " " << books[l].getnum() << " ";
}
break;
}
if (m == "2") {
cout << "订单已取消!" << endl << "欢迎下次光临!" << endl;
break;
}
else {
cout << "请输入正确的选项哦~" << endl;
continue;
}
}
}
else {
cout << "请输入正确的选项哦!" << endl;
}
if (m == "1" || m == "2") break;
}
}
}
void opt_member(int ID) {
double money = 0;
// memset(books,0,sizeof(books));
int star = ID % 10;
int l;
if (star == 1 || star == 2) l = 1; //计算会员星级
if (star == 3 || star == 4) l = 2;
if (star == 5 || star == 6) l = 3;
if (star == 7 || star == 8) l = 4;
if (star == 9 || star == 0) l = 5;
cout << endl << "\t\t您好,亲爱的" << l << "星会员!" << endl;
cout << "1.按书名购买" << endl << "2.按书号购买" << endl << "请选择:";
string s;
cin >> s;
if (s == "1") {
while (1) {
cls();
print_menu();
cout << "请输入您要买的书的书名:";
string sn;
cin >> sn;
string m;
for (int i = 0; i < x - 1; i++) {
if (books[i].getbook_name() == sn) {
cout << endl << "当前《" << sn << "》还剩" << books[i].getnum() << "本" << endl << "单价为:" << books[i].getprice() << "元" << endl;
cout << "请输入您要买的数量:";
int shuliang;
cin >> shuliang;
if (shuliang <= books[i].getnum()) {
cout << "添加成功!" << endl;
money += shuliang * books[i].getprice();
books[i].numdec(shuliang);
break;
}
if (shuliang > books[i].getnum()) {
cout << "抱歉,库存不够哦亲~";
break;
}
}
if (i == x - 2) {
cout << "抱歉,暂时没有这本书哦亲~" << endl;
break;
}
}
cout << endl << "是否继续购买?" << endl << "1.继续" << endl << "2.结算" << endl << "请选择:";
string charge;
cin >> charge;
if (charge == "1") {
continue;
}
if (charge == "2") {
cout << "请输入收货人姓名和收货地址:" << endl;
string n, a;
cin >> n >> a;
member lp(n, ID, l, a, 0);
cout << endl << "请确认您的订单信息:" << endl;
lp.setpay(money);
lp.display();
cout << "您需要支付的金额为:" << lp.getpay() << "元" << endl;
while (1) {
cout << "按1完成订单!" << endl << "按2取消订单" << endl;
cin >> m;
if (m == "1") {
cout << "订单已完成!" << endl << "感谢您的惠顾,欢迎下次光临!" << endl;
fstream fc("books.txt", ios::trunc); //重新录入改动后的库存信息
fc.close();
fstream f;
f.open("books.txt", ios::out);
for (int l = 0; l < x - 1; l++) {
f << books[l].getbook_ID() << " " << books[l].getbook_name() << " " << books[l].getauthor() << " " << books[l].getpublishing() << " " << books[l].getprice() << " " << books[l].getnum() << " ";
}
break;
}
if (m == "2") {
cout << "订单已取消!" << endl << "欢迎下次光临!" << endl;
break;
}
else {
cout << "请输入正确的选项哦~" << endl;
continue;
}
}
if (m == "1" || m == "2") break;
}
else {
cout << "请输入正确的选项哦" << endl;
}
}
}
if (s == "2") {
while (1) {
cls();
print_menu();
cout << "请输入您要买的书的书号:";
string sn;
cin >> sn;
string m;
for (int i = 0; i < x - 1; i++) {
if (books[i].getbook_ID() == sn) {
cout << endl << "当前" << sn << "号图书《" << books[i].getbook_name() << "》还剩" << books[i].getnum() << "本" << endl << "单价为:" << books[i].getprice() << "元" << endl;
cout << "请输入您要买的数量:";
int shuliang;
cin >> shuliang;
if (shuliang <= books[i].getnum()) {
cout << "添加成功!" << endl;
money += shuliang * books[i].getprice();
books[i].numdec(shuliang);
break;
}
if (shuliang > books[i].getnum()) {
cout << "抱歉,库存不够哦亲~";
break;
}
}
if (i == x - 2) {
cout << "抱歉,暂时没有这本书哦亲~" << endl;
break;
}
}
cout << endl << "是否继续购买?" << endl << "1.继续" << endl << "2.结算" << endl << "请选择:";
string charge;
cin >> charge;
if (charge == "1") {
continue;
}
if (charge == "2") {
cout << "请输入收货人姓名和收货地址:" << endl;
string n, a;
cin >> n >> a;
int star = ID % 10;
int l;
if (star == 1 || star == 2) l = 1; //计算会员星级
if (star == 3 || star == 4) l = 2;
if (star == 5 || star == 6) l = 3;
if (star == 7 || star == 8) l = 4;
if (star == 9 || star == 0) l = 5;
member lp(n, ID, l, a, 0);
cout << endl << "请确认您的订单信息:" << endl;
lp.setpay(money);
lp.display();
cout << "您需要支付的金额为:" << lp.getpay() << "元" << endl;
while (1) {
cout << "按1完成订单!" << endl << "按2取消订单" << endl;
cin >> m;
if (m == "1") {
cout << "订单已完成!" << endl << "感谢您的惠顾,欢迎下次光临!" << endl;
fstream fc("books.txt", ios::trunc); //重新录入改动后的库存信息
fc.close();
fstream f;
f.open("books.txt", ios::out);
for (int l = 0; l < x - 1; l++) {
f << books[l].getbook_ID() << " " << books[l].getbook_name() << " " << books[l].getauthor() << " " << books[l].getpublishing() << " " << books[l].getprice() << " " << books[l].getnum() << " ";
}
break;
}
if (m == "2") {
cout << "订单已取消!" << endl << "欢迎下次光临!" << endl;
break;
}
else {
cout << "请输入正确的选项哦~" << endl;
continue;
}
}
if (m == "1" || m == "2") break;
}
else {
cout << "请输入正确的选项哦!" << endl;
}
}
}
}
void opt_honoured_guest(int ID) {
double money = 0;
// memset(books,0,sizeof(books));
get_txt();
cout << endl << "\t\t您好,亲爱的贵宾!" << endl;
cout << "1.按书名购买" << endl << "2.按书号购买" << endl << "请选择:";
string s;
cin >> s;
if (s == "1") {
while (1) {
cls();
print_menu();
cout << "请输入您要买的书的书名:";
string sn;
cin >> sn;
string m;
for (int i = 0; i < x - 1; i++) {
if (books[i].getbook_name() == sn) {
cout << endl << "当前《" << sn << "》还剩" << books[i].getnum() << "本" << endl << "单价为:" << books[i].getprice() << "元" << endl;
cout << "请输入您要买的数量:";
int shuliang;
cin >> shuliang;
if (shuliang <= books[i].getnum()) {
cout << "添加成功!" << endl;
money += shuliang * books[i].getprice();
books[i].numdec(shuliang);
break;
}
if (shuliang > books[i].getnum()) {
cout << "抱歉,库存不够哦亲~";
break;
}
}
if (i == x - 2) {
cout << "抱歉,暂时没有这本书哦亲~" << endl;
break;
}
}
cout << endl << "是否继续购买?" << endl << "1.继续" << endl << "2.结算" << endl << "请选择:";
string charge;
cin >> charge;
if (charge == "1") {
continue;
}
if (charge == "2") {
cout << "请输入收货人姓名、收货地址以及您的专属折扣率:" << endl;
string n, a;
double r;
cin >> n >> a >> r;
honoured_guest lp(n, ID, r, a, 0);
cout << endl << "请确认您的订单信息:" << endl;
lp.setpay(money);
lp.display();
cout << "您需要支付的金额为:" << lp.getpay() << "元" << endl;
while (1) {
cout << "按1完成订单!" << endl << "按2取消订单" << endl;
cin >> m;
if (m == "1") {
cout << "订单已完成!" << endl << "感谢您的惠顾,欢迎下次光临!" << endl;
fstream fc("books.txt", ios::trunc); //重新录入改动后的库存信息
fc.close();
fstream f;
f.open("books.txt", ios::out);
for (int l = 0; l < x - 1; l++) {
f << books[l].getbook_ID() << " " << books[l].getbook_name() << " " << books[l].getauthor() << " " << books[l].getpublishing() << " " << books[l].getprice() << " " << books[l].getnum() << " ";
}
break;
}
if (m == "2") {
cout << "订单已取消!" << endl << "欢迎下次光临!" << endl;
break;
}
else {
cout << "请输入正确的选项哦~" << endl;
continue;
}
}
if (m == "1" || m == "2") break;
}
else {
cout << "请输入正确的选项哦" << endl;
}
}
}
if (s == "2") {
while (1) {
cls();
print_menu();
cout << "请输入您要买的书的书号:";
string sn;
cin >> sn;
string m;
for (int i = 0; i < x - 1; i++) {
if (books[i].getbook_ID() == sn) {
cout << endl << "当前" << sn << "号图书《" << books[i].getbook_name() << "》还剩" << books[i].getnum() << "本" << endl << "单价为:" << books[i].getprice() << "元" << endl;
cout << "请输入您要买的数量:";
int shuliang;
cin >> shuliang;
if (shuliang <= books[i].getnum()) {
cout << "添加成功!" << endl;
money += shuliang * books[i].getprice();
books[i].numdec(shuliang);
break;
}
if (shuliang > books[i].getnum()) {
cout << "抱歉,库存不够哦亲~";
break;
}
}
if (i == x - 2) {
cout << "抱歉,暂时没有这本书哦亲~" << endl;
break;
}
}
cout << endl << "是否继续购买?" << endl << "1.继续" << endl << "2.结算" << endl << "请选择:";
string charge;
cin >> charge;
if (charge == "1") {
continue;
}
if (charge == "2") {
cout << "请输入收货人姓名、收货地址以及您的专属折扣率:" << endl;
string n, a;
double r;
cin >> n >> a >> r;
honoured_guest lp(n, ID, r, a, 0);
cout << endl << "请确认您的订单信息:" << endl;
lp.setpay(money);
lp.display();
cout << "您需要支付的金额为:" << lp.getpay() << "元" << endl;
while (1) {
cout << "按1完成订单!" << endl << "按2取消订单" << endl;
cin >> m;
if (m == "1") {
cout << "订单已完成!" << endl << "感谢您的惠顾,欢迎下次光临!" << endl;
fstream fc("books.txt", ios::trunc); //重新录入改动后的库存信息
fc.close();
fstream f;
f.open("books.txt", ios::out);
for (int l = 0; l < x - 1; l++) {
f << books[l].getbook_ID() << " " << books[l].getbook_name() << " " << books[l].getauthor() << " " << books[l].getpublishing() << " " << books[l].getprice() << " " << books[l].getnum() << " ";
}
break;
}
if (m == "2") {
cout << "订单已取消!" << endl << "欢迎下次光临!" << endl;
break;
}
else {
cout << "请输入正确的选项哦~" << endl;
continue;
}
}
}
else {
cout << "请输入正确的选项哦!" << endl;
}
if (m == "1" || m == "2") break;
}
}
}
void opt_newperson() {
cout << "请输入您要申请的账号等级:" << endl;
cout << endl << "1.普通账号" << endl << endl << "2.会员账号" << endl << endl << "3.贵宾账号" << endl << endl << "4.退出" << endl << endl;
cout << "请选择:";
string s;
while (1) {
cin >> s;
if (s == "1") {
cls();
srand((unsigned)time(NULL));
int a = rand() % 900000 + 100000;
cout << "已成功创建普通账号!" << endl << endl;
cout << "您的账号ID为:" << " ";
printf("%06d\n", a);
break;
}
if (s == "2") {
cls();
srand((unsigned)time(NULL));
int a = rand() % 90000 + 10000;
cout << "请输入您要创建的会员账号星级(1~5)" << endl << endl; //根据星级给ID,1星最后一位为1或2; 2星为3或4; 。。5星为9或0
int s;
cin >> s;
if (s == 1) {
a = a / 10 * 10 + rand() % 1 + 1;
}
if (s == 2) {
a = a / 10 * 10 + rand() % 1 + 3;
}
if (s == 3) {
a = a / 10 * 10 + rand() % 1 + 5;
}
if (s == 4) {
a = a / 10 * 10 + rand() % 1 + 7;
}
if (s == 5) {
a = a / 10 * 10 + rand() % 1 + 9;
}
cout << "已成功创建" << s << "级会员账号!" << endl << endl;
cout << "您的账号ID为:" << " ";
printf("%06d\n", a);
break;
}
if (s == "3") {
cls();
srand((unsigned)time(NULL));
int a = rand() % 10000;
cout << "已成功创建贵宾账号!" << endl << endl;
cout << "您的账号ID为:" << " ";
printf("%06d\n", a);
break;
}
if (s == "4") {
cls();
break;
}
else {
cout << "请输入正确的选项!" << endl;
}
}
}
void opt1() {
cls();
cout << endl << "1.如果您已有账号,请输入“1”进行下一步操作!" << endl << endl;
cout << "2.如果您是本店的新用户,请输入“2”来为您注册一个账号!" << endl << endl;
cout << "3.退出" << endl << endl;
cout << "请选择:";
string s;
cin >> s;
if (s == "1") {
cls();
cout << "请输入您的ID:" << endl;
int ID;
cin >> ID; //根据ID号码来判断是哪种级别的客人
if (ID < 10000) {
cls();
print_menu();
opt_honoured_guest(ID);
}
if (ID >= 10000 && ID < 100000) {
cls();
print_menu();
opt_member(ID);
}
if (ID >= 100000) {
cls();
print_menu();
opt_layfolk(ID);
}
}
if (s == "2") {
cls();
opt_newperson();
}
if (s == "3") {
cls();
}
}
void opt2() {
cls();
cout << "请输入您要添加的图书的编号:" << endl;
string ID;
cin >> ID;
get_txt();
for (int i = 0; i < x - 1; i++) {
if (books[i].getbook_ID() == ID) { //如果是库存中已经记录的图书
cls();
cout << "检测到是已有的图书!" << endl;
cout << "请输入添加的数量: ";
int shuliang;
cin >> shuliang;
books[i].addnum(shuliang);
fstream f("books.txt", ios::out);
for (int l = 0; l < x - 1; l++) {
f << books[l].getbook_ID() << " " << books[l].getbook_name() << " " << books[l].getauthor() << " " << books[l].getpublishing() << " " << books[l].getprice() << " " << books[l].getnum() << " ";
}
cout << "添加成功!" << endl << "当前这本书还有" << books[i].getnum() << "本" << endl;
f.close();
break;
}
if (i == x - 2) { //如果是库存中没有的书
cls();
cout << "检测到是新的图书!" << endl;
cout << "请输入书本的书名 作者 出版社 定价 数量" << endl;
string n;
string a;
string p;
double pr;
int number;
cin >> n >> a >> p >> pr >> number;
fstream f("books.txt", ios::out | ios::app);
book bo(ID, n, a, p, pr, number);
f << bo.getbook_ID() << " " << bo.getbook_name() << " " << bo.getauthor() << " " << bo.getpublishing() << " " << bo.getprice() << " " << bo.getnum() << " ";
f.close();
cout << "已将《" << n << "》加入库存!";
}
}
}
void opt3() {
cls();
FILE* fp;
if ((fp = fopen("books.txt", "rb+")) == NULL) {
printf("\n\t当前没有库存!\n\n");
return;
}
fclose(fp);
get_txt();
for (int i = 0; i < x - 1; i++) {
books[i].display();
}
}
void opt4() {
cls();
int a;
srand((unsigned)time(NULL));
a = rand() % 101;
printf("\n您今日的幸运指数是: %d \n", a);
if (a < 30) printf("运势稍稍不佳,最好慎言慎行");
if (a > 30 && a < 70) printf("运势中和,努力加油吧!");
if (a > 70) printf("运势较好,记得把握机会哦");
printf("\n\nTips:相信美好的事情终会发生!\n\n");
}
void opt5() {
cls();
cout << endl << "\n\n\t感谢您的使用!" << endl << endl;
}
int main() {
system("color 0B");
loading();
cls();
print_begin();
int opt;
while (1) {
cin >> opt;
switch (opt) {
case 1:
opt1();
system("pause");
refresh();
break;
case 2:
opt2();
system("pause");
refresh();
break;
case 3:
opt3();
system("pause");
refresh();
break;
case 4:
opt4();
system("pause");
refresh();
break;
case 5:
opt5();
Sleep(500);
return 0;
default:
cls();
opt_error();
system("pause");
refresh();
break;
}
}
}
一些修改之后的程序截图
后谈
这次实践帮助我初步了解了逆向软件工程 的意义,我们不仅要会写程序也需要会读程序。在修改的过程中我也学习到了,我们在写程序时需要保证程序的可读性和可扩展性,这样在理解代码和修改代码的时候都会更加方便。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理