数据结构与算法结课设计项目失物招领系统详细设计
1、设计抽象数据类型对应的类定义。
2、设计每个成员函数;
3、设计主函数
大项目,不是一下子搞得定的
该回答引用ChatGPT
请参考下面的解决方案,如果可行还请点击 采纳,感谢!
1、设计抽象数据类型对应的类定义,C++代码:
class LostAndFound {
public:
LostAndFound();
~LostAndFound();
void addLostItem(string itemName, string itemDesc);
void addFoundItem(string itemName, string itemDesc);
void claimLostItem(string itemName);
void listLostItems();
void listFoundItems();
private:
struct Item {
string name;
string desc;
bool claimed;
};
vector<Item> lostItems;
vector<Item> foundItems;
};
2、设计每个成员函数:
LostAndFound::LostAndFound() {}
LostAndFound::~LostAndFound() {}
void LostAndFound::addLostItem(string itemName, string itemDesc) {
Item item;
item.name = itemName;
item.desc = itemDesc;
item.claimed = false;
lostItems.push_back(item);
}
void LostAndFound::addFoundItem(string itemName, string itemDesc) {
Item item;
item.name = itemName;
item.desc = itemDesc;
item.claimed = false;
foundItems.push_back(item);
}
void LostAndFound::claimLostItem(string itemName) {
for (int i = 0; i < lostItems.size(); i++) {
if (lostItems[i].name == itemName && !lostItems[i].claimed) {
lostItems[i].claimed = true;
break;
}
}
}
void LostAndFound::listLostItems() {
cout << "Lost Items: " << endl;
for (int i = 0; i < lostItems.size(); i++) {
if (!lostItems[i].claimed) {
cout << lostItems[i].name << " - " << lostItems[i].desc << endl;
}
}
}
void LostAndFound::listFoundItems() {
cout << "Found Items: " << endl;
for (int i = 0; i < foundItems.size(); i++) {
if (!foundItems[i].claimed) {
cout << foundItems[i].name << " - " << foundItems[i].desc << endl;
}
}
}
2、设计主函数:
int main() {
LostAndFound laf;
laf.addLostItem
用了差不多一个多钟头,终于搞出来了!!
以下是Python代码写出来的,如果你想转成其他代码可以私信我。
#先定义一个失物的类
class LostAndFound:
def __init__(self):
self.items = []
#定义一个失物函数 里面包含失物名称、描述、失主姓名、失主联系方式
def add_item(self, item_name, item_description, owner_name, owner_contact):
item = {
'item_name': item_name,
'item_description': item_description,
'owner_name': owner_name,
'owner_contact': owner_contact
}
self.items.append(item)
#定义一个查找函数 用于查询失物的名称
def search_item(self, item_name):
for item in self.items:
if item['item_name'] == item_name:
return item
return None
#定义一个删除函数 用于删除失物的名称
def remove_item(self, item_name):
item = self.search_item(item_name)
if item:
self.items.remove(item)
return True
return False
#定义主函数界面
def main():
lost_and_found = LostAndFound()
while True:
print("1. 添加物品")
print("2. 查找物品")
print("3. 删除物品")
print("0. 退出系统")
choice = int(input("请输入您的选择:"))
if choice == 1:
item_name = input("请输入物品名称:")
item_description = input("请输入物品描述:")
owner_name = input("请输入失主姓名:")
owner_contact = input("请输入失主联系方式:")
lost_and_found.add_item(item_name, item_description, owner_name, owner_contact)
print("物品添加成功!")
elif choice == 2:
item_name = input("请输入您要查找的物品名称:")
item = lost_and_found.search_item(item_name)
if item:
print("物品名称:", item['item_name'])
print("物品描述:", item['item_description'])
print("失主姓名:", item['owner_name'])
print("失主联系方式:", item['owner_contact'])
else:
print("未找到该物品!")
elif choice == 3:
item_name = input("请输入您要删除的物品名称:")
result = lost_and_found.remove_item(item_name)
result = lost_and_found.remove_item(item_name)
if result:
print("物品删除成功!")
else:
print("未找到该物品!")
if choice == 0:
print("退出系统!")
break
else:
print("输入有误,请重新输入!")
if __name__ == '__main__':
main()
总结:这个类中包含:
items
用于存储所有的物品add_item
:用于添加物品 函数 `search_item` :用于查询物品
函数 `remove_item` :用于删除物品
最后的 main
函数实现了失物招领系统的用户界面,使用户可以通过命令行输入完成物品的添加、查询、删除操作。你是想用什么语言做呢?