文章目录
1. 课题预计目标
1.1【购买新的图书】
从键盘输入图书信息(注意购买金额是自动计算的,不需要输入),把准备购买的一本新的图书加入购物车。
如果购物车中已有相同图书号的图书,则不能重复购买,并提示购买失败的信息。
若可以购买,则自动计算购买金额(购买金额=购买数量*价格),并显示购买到购物车的这本图书的信息。
1.2【查询购物车图书】
从键盘输入待查询的图书号,并按照指定图书号查询显示购物车中该图书号的图书信息。
若查询失败,则显示查询失败信息;否则显示查询成功信息,并显示该图书号的图书信息。
1.3【按图书号显示所有图书】
把购物车中所有图书按照图书号的递增次序显示每一本图书的图书信息。
最后列出购物车中所购买图书的购买数量总额、购买金额总额。
1.4【修改购买数量】
从键盘输入待修改购买数量的图书号和待修改的购买数量,并按照待修改购买数量的图书号修改购物车中该图书号的图书购买数量,自动计算该图书号的图书购买金额(购买金额=购买数量*价格)。
若修改失败,则显示失败信息;否则显示修改成功信息,并显示该图书号的图书信息。
1.5【移出购物车】
从键盘输入待移出的图书号,并把该指定图书号的图书移出购物车。
若找不到待移出的图书,则显示移出图书操作失败的提示信息;否则显示该图书号的图书信息,然后移出该图书,并显示成功移出的提示信息,最后显示输出成功移出后购物车中的所有图书信息。
2. 系统E-R图
3. 编码实现
3.1 图书结构体定义
struct Shop {
string bookNumber;//图书编号
string title;//图书名字
string author;//图书作者
string publisher;//图书出版社
string publicationDate;//图书出版日期
double price;//图书价格
int num;//图书购买数量
double money;//图书购买金额
};
vector<Shop> shops;
3.2 实现新增购物车信息
void addBook() {
Shop newbook;
cout << "请输入新图书的信息:" << endl;
cout << "图书号: ";
while (true) {
cin >>newbook.bookNumber;
for ( Shop& shop : shops) {
if (shop.bookNumber == newbook.bookNumber) {
cout << "图书编号已经存在,加入失败!";
return;
}
}
if (newbook.bookNumber.size()!= 4) {
cout << "编号不规范,请你重新输入" << endl;
}
else {
break;
}
}
cout << "书名: ";
cin >> newbook.title;
cout << "作者: ";
cin >> newbook.author;
cout << "出版社: ";
cin >> newbook.publisher;
cout << "出版日期 (YYYY-MM-DD): ";
cin >> newbook.publicationDate;
cout << "价格: ";
while (true)
{
cin >> newbook.price;
if (newbook.price <= 0) {
cout << "输入的价格不规范,请重新输入" << endl;
}
else {
break;
}
}
cout << "购买数量:";
cin >> newbook.num;
cout << "购买金额:" << newbook.price * newbook.num<<endl;
//vector迭代器装的是对象
shops.push_back(newbook);
cout << endl;
}
3.3 实现查找购物车信息
void searchBook() {
string bookNumber;
cout << "请输入图书编号进行查找: ";
cin >> bookNumber;
auto it = find_if(shops.begin(), shops.end(), [bookNumber](const Shop& c) {
return c.bookNumber == bookNumber;
});
if (it != shops.end()) {
cout << "该图书的信息如下:" << endl;
cout << "编号: " << it->bookNumber<< endl;
cout << "名字: " << it->title << endl;
cout << "作者: " << it->author << endl;
cout << "出版社: " << it->publisher << endl;
cout << "出版日期:" << it->publicationDate << endl;
cout << "价格:" << it->price << endl;
cout << "购买数量:" << it->num << endl;
}
else {
cout << "系统中无该图书信息!" << endl;
}
}
这段代码实现了一个查找图书信息的函数searchBook()
,首先从用户输入中获取要查找的图书编号,然后通过在shops
这个容器中查找,使用了STL中的find_if
算法和lambda表达式来实现查找,判断条件是容器中的元素的bookNumber
属性是否等于用户输入的图书编号。一旦查找到对应的图书信息,就会输出该图书的详细信息,包括编号、名字、作者、出版社、出版日期、价格和购买数量。如果没有找到对应的图书信息,就会输出提示信息。
3.4 实现按图书号升序排序
void displayAllBookbyNum() {
sort(shops.begin(), shops.end(), [](const Shop& s1, const Shop& s2) {
return s1.bookNumber < s2
.bookNumber;
});
for (auto& shop : shops) {
cout << "该图书的信息如下:" << endl;
cout << "编号: " << shop.bookNumber << endl;
cout << "名字: " << shop.title << endl;
cout << "作者: " << shop.author << endl;
cout << "出版社: " << shop.publisher << endl;
cout << "出版日期:" << shop.publicationDate << endl;
cout << "价格:" << shop.price << endl;
cout << "购买数量:" << shop.num << endl;
}
}
这段代码实现了按照图书编号从小到大的顺序显示所有书店的图书信息。具体实现方式是使用sort函数对shops向量中的所有元素按照图书编号升序排序,然后遍历输出每一个图书的编号、名字、作者、出版社、出版日期、价格和购买数量。lambda表达式作为sort函数的第二个参数,用于定义比较函数,比较函数的返回值决定了排序顺序。
3.5 实现修改购物车里图书的信息
void modifyBook() {
string bookNumber;
cout << "请输入要修改的图书的编号:";
cin >> bookNumber;
for (Shop& shop : shops) {
if (shop.bookNumber == bookNumber) {
while (1) {
cout << "请输入新的的图书的编号:";
cin >> shop.bookNumber;
if (shop.bookNumber.size() != 4) {
cout << "编号不规范,请你重新输入" << endl;
}
else {
break;
}
}
int num;
cout << "请输入该图书新的购买数量:";
cin >> num;
cout << "图书的购买金额为:" << num * shop.price << endl;
return;
shops.push_back(shop);
}
}
cout << "系统中无该图书信息!";
}
用户需要输入新的图书编号和购买数量。如果输入的图书编号不规范(长度不为4),则会提示用户重新输入。否则,程序会更新当前图书的编号和数量,并计算出新的购买金额。最后,修改后的图书信息会被加入到存储图书信息的vector中。
3.6 实现删除购物车图书信息
void deleteBook() {
string bookNumber;
cout << "请输入你要移除的图书编号: ";
cin >> bookNumber;
//[]是捕获列表,局部变量放在这里
auto it = find_if(shops.begin(), shops.end(), [bookNumber](const Shop& c) {
return c.bookNumber == bookNumber;
});
if (it != shops.end()) {
shops.erase(it);
cout << "成功移出该图书!" << endl;
}
else {
cout << "系统中无该图书信息!" << endl;
}
}
这是一个函数,作用是从图书店库存中删除一本书。具体过程如下:
-
首先要求用户输入要删除的图书的编号,存储在
bookNumber
变量中。 -
使用
find_if
算法在shops
容器中查找符合条件的元素,即bookNumber
等于某个Shop
对象的bookNumber
成员变量值。 -
如果找到了符合条件的元素,则使用
erase
函数将其从shops
容器中删除,并输出成功移除该图书的信息。 -
如果没有找到符合条件的元素,则输出系统中无该图书信息的提示信息。
4.系统全部源代码
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
struct Shop {
string bookNumber;//图书编号
string title;//图书名字
string author;//图书作者
string publisher;//图书出版社
string publicationDate;//图书出版日期
double price;//图书价格
int num;//图书购买数量
double money;//图书购买金额
};
vector<Shop> shops;
void addBook() {
Shop newbook;
cout << "请输入新图书的信息:" << endl;
cout << "图书号: ";
while (true) {
cin >>newbook.bookNumber;
for ( Shop& shop : shops) {
if (shop.bookNumber == newbook.bookNumber) {
cout << "图书编号已经存在,加入失败!";
return;
}
}
if (newbook.bookNumber.size()!= 4) {
cout << "编号不规范,请你重新输入" << endl;
}
else {
break;
}
}
cout << "书名: ";
cin >> newbook.title;
cout << "作者: ";
cin >> newbook.author;
cout << "出版社: ";
cin >> newbook.publisher;
cout << "出版日期 (YYYY-MM-DD): ";
cin >> newbook.publicationDate;
cout << "价格: ";
while (true)
{
cin >> newbook.price;
if (newbook.price <= 0) {
cout << "输入的价格不规范,请重新输入" << endl;
}
else {
break;
}
}
cout << "购买数量:";
cin >> newbook.num;
cout << "购买金额:" << newbook.price * newbook.num<<endl;
//vector迭代器装的是对象
shops.push_back(newbook);
cout << endl;
}
void deleteBook() {
string bookNumber;
cout << "请输入你要移除的图书编号: ";
cin >> bookNumber;
//[]是捕获列表,局部变量放在这里
auto it = find_if(shops.begin(), shops.end(), [bookNumber](const Shop& c) {
return c.bookNumber == bookNumber;
});
if (it != shops.end()) {
shops.erase(it);
cout << "成功移出该图书!" << endl;
}
else {
cout << "系统中无该图书信息!" << endl;
}
}
void modifyBook() {
string bookNumber;
cout << "请输入要修改的图书的编号:";
cin >> bookNumber;
for (Shop& shop : shops) {
if (shop.bookNumber == bookNumber) {
while (1) {
cout << "请输入新的的图书的编号:";
cin >> shop.bookNumber;
if (shop.bookNumber.size() != 4) {
cout << "编号不规范,请你重新输入" << endl;
}
else {
break;
}
}
int num;
cout << "请输入该图书新的购买数量:";
cin >> num;
cout << "图书的购买金额为:" << num * shop.price << endl;
return;
shops.push_back(shop);
}
}
cout << "系统中无该图书信息!";
}
void searchBook() {
string bookNumber;
cout << "请输入图书编号进行查找: ";
cin >> bookNumber;
auto it = find_if(shops.begin(), shops.end(), [bookNumber](const Shop& c) {
return c.bookNumber == bookNumber;
});
if (it != shops.end()) {
cout << "该图书的信息如下:" << endl;
cout << "编号: " << it->bookNumber<< endl;
cout << "名字: " << it->title << endl;
cout << "作者: " << it->author << endl;
cout << "出版社: " << it->publisher << endl;
cout << "出版日期:" << it->publicationDate << endl;
cout << "价格:" << it->price << endl;
cout << "购买数量:" << it->num << endl;
}
else {
cout << "系统中无该图书信息!" << endl;
}
}
void displayAllBookbyNum() {
sort(shops.begin(), shops.end(), [](const Shop& s1, const Shop& s2) {
return s1.bookNumber < s2
.bookNumber;
});
for (auto& shop : shops) {
cout << "该图书的信息如下:" << endl;
cout << "编号: " << shop.bookNumber << endl;
cout << "名字: " << shop.title << endl;
cout << "作者: " << shop.author << endl;
cout << "出版社: " << shop.publisher << endl;
cout << "出版日期:" << shop.publicationDate << endl;
cout << "价格:" << shop.price << endl;
cout << "购买数量:" << shop.num << endl;
}
}
void displayAllBook() {
if (shops.empty()) {
cout << "系统无图书信息" << endl;
}
for ( auto& shop : shops) {
cout << "该图书的信息如下:" << endl;
cout << "编号: " << shop.bookNumber << endl;
cout << "名字: " << shop.title << endl;
cout << "作者: " << shop.author << endl;
cout << "出版社: " << shop.publisher << endl;
cout << "出版日期:" << shop.publicationDate << endl;
cout << "价格:" << shop.price << endl;
cout << "购买数量:" << shop.num << endl;
}
}
int main() {
int choice;
while (true) {
cout << "<***** 公司信息管理系统 *****>" << endl;
cout << "1. 新增购物车图书信息" << endl;
cout << "2. 查找购物车图书信息" << endl;
cout << "3. 按图书编号显示购物车图书信息" << endl;
cout << "4. 修改购物车图书信息" << endl;
cout << "5. 删除购物车图书信息" << endl;
cout << "6. 显示所有公司公司信息" << endl;
cout << "0. 退出系统" << endl;
cout << "请输入你的选择: ";
cin >> choice;
switch (choice) {
case 1:
addBook();
break;
case 2:
searchBook();
break;
case 3:
displayAllBookbyNum();
break;
case 4:
modifyBook();
break;
case 5:
deleteBook();
break;
case 6:
displayAllBook();
break;
case 0:
return 0;
default:
cout << "你的输入有误,请重新输入!" << endl;
break;
}
cout << endl;
}
return 0;
}