设计一个程序,来管理超市中的商品信息。一件商品的信息包括:商品名称、商品单价、商品数量。
要求:
(1) 利用链表结构来存放超市中的所有商品信息。
(2) 程序运行后循环输出以下提示菜单来进行功能选择:
功能分解:
打印商品信息:定义一个函数,打印超市中所有商品的信息。
查询商品信息:定义一个函数,根据商品名称查询并打印该商品的信息。
增加商品信息:定义一个函数,增加一件新商品的信息。
删除商品信息:定义一个函数,根据商品名称查询并删除该商品的信息。
退出:结束程序的执行。
设计一个程序,来管理超市中的商品信息。一件商品的信息包括:商品名称、商品单价、商品数量。
要求:
(1) 利用链表结构来存放超市中的所有商品信息。
(2) 程序运行后循环输出以下提示菜单来进行功能选择:
功能分解:
打印商品信息:定义一个函数,打印超市中所有商品的信息。
查询商品信息:定义一个函数,根据商品名称查询并打印该商品的信息。
增加商品信息:定义一个函数,增加一件新商品的信息。
删除商品信息:定义一个函数,根据商品名称查询并删除该商品的信息。
退出:结束程序的执行。
【以下回答由 GPT 生成】
我可以为你提供一个基本的示例函数来实现以上功能。以下是一个使用C++语言的示例程序:
#include <iostream>
#include <string>
struct Product {
std::string name;
double price;
int quantity;
Product* next;
};
// 全局变量,用来表示商品链表的头指针
Product* productList = nullptr;
// 打印商品信息
void printProductInfo() {
Product* current = productList;
if (current == nullptr) {
std::cout << "超市暂无商品信息" << std::endl;
return;
}
std::cout << "商品信息如下:" << std::endl;
while (current != nullptr) {
std::cout << "名称:" << current->name
<< ",单价:" << current->price
<< ",数量:" << current->quantity
<< std::endl;
current = current->next;
}
}
// 根据商品名称查询商品信息
void queryProduct(const std::string& productName) {
Product* current = productList;
while (current != nullptr) {
if (current->name == productName) {
std::cout << "名称:" << current->name
<< ",单价:" << current->price
<< ",数量:" << current->quantity
<< std::endl;
return;
}
current = current->next;
}
std::cout << "没有找到商品信息" << std::endl;
}
// 增加商品信息
void addProduct(const std::string& productName, double price, int quantity) {
Product* newProduct = new Product;
newProduct->name = productName;
newProduct->price = price;
newProduct->quantity = quantity;
if (productList == nullptr) {
productList = newProduct;
} else {
Product* current = productList;
while (current->next != nullptr) {
current = current->next;
}
current->next = newProduct;
}
std::cout << "商品信息添加成功" << std::endl;
}
// 删除商品信息
void removeProduct(const std::string& productName) {
if (productList == nullptr) {
std::cout << "超市暂无商品信息" << std::endl;
return;
}
Product* previous = nullptr;
Product* current = productList;
while (current != nullptr) {
if (current->name == productName) {
if (previous == nullptr) {
productList = current->next;
} else {
previous->next = current->next;
}
delete current;
std::cout << "商品信息删除成功" << std::endl;
return;
}
previous = current;
current = current->next;
}
std::cout << "没有找到商品信息" << std::endl;
}
int main() {
int choice;
std::string productName;
double price;
int quantity;
while (true) {
std::cout << "菜单:" << std::endl;
std::cout << "1. 打印商品信息" << std::endl;
std::cout << "2. 查询商品信息" << std::endl;
std::cout << "3. 增加商品信息" << std::endl;
std::cout << "4. 删除商品信息" << std::endl;
std::cout << "5. 退出" << std::endl;
std::cout << "请选择功能:" << std::endl;
std::cin >> choice;
switch (choice) {
case 1:
printProductInfo();
break;
case 2:
std::cout << "请输入要查询的商品名称:" << std::endl;
std::cin >> productName;
queryProduct(productName);
break;
case 3:
std::cout << "请输入要增加的商品名称:" << std::endl;
std::cin >> productName;
std::cout << "请输入商品单价:" << std::endl;
std::cin >> price;
std::cout << "请输入商品数量:" << std::endl;
std::cin >> quantity;
addProduct(productName, price, quantity);
break;
case 4:
std::cout << "请输入要删除的商品名称:" << std::endl;
std::cin >> productName;
removeProduct(productName);
break;
case 5:
return 0;
default:
std::cout << "无效的选择" << std::endl;
break;
}
}
return 0;
}
以上示例程序在主函数中实现了一个无限循环的菜单选择功能,通过用户输入选择相应的功能。每个功能对应一个函数,根据用户的选择调用相应的函数来实现功能。菜单选项包括了打印商品信息、查询商品信息、增加商品信息、删除商品信息和退出。使用链表结构来存储商品信息,每个商品都包括名称、单价和数量。请确保在退出程序前,正确释放申请的内存。
希望以上代码能对你有所帮助,如果你有任何问题,请随时提问。
【相关推荐】