From 6cc6c1a0a768d23a3db2388d50d01d06965acde5 Mon Sep 17 00:00:00 2001 From: WJY-1123 <1229483859@qq.com> Date: Thu, 21 May 2026 21:28:04 +0800 Subject: [PATCH] house_service3.0.py --- house_service3.0.py | 84 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 house_service3.0.py diff --git a/house_service3.0.py b/house_service3.0.py new file mode 100644 index 0000000..4ef5482 --- /dev/null +++ b/house_service3.0.py @@ -0,0 +1,84 @@ +from house.house_operation import read_houses, write_houses +from house.house_operation import read_stack, write_stack +# V3.0 导入地址与邻接矩阵操作 +from house.address_operation import parse_address, save_address_info, get_address_info +from house.address_operation import add_community, set_distance, get_adj_matrix +from datetime import datetime + + +# V1.0 + V2.0 原有代码完全保留(此处省略重复代码,直接使用V2.0内容) +def get_next_house_id(): + houses = read_houses() + if not houses: + return 1 + max_id = max(house["id"] for house in houses) + return max_id + 1 + + +def add_house(address, price, area, house_type): + if not address or not house_type: + print("错误:地址和户型不能为空!") + return False + if not isinstance(price, (int, float)) or price <= 0: + print("错误:租金必须是正数!") + return False + if not isinstance(area, (int, float)) or area <= 0: + print("错误:面积必须是正数!") + return False + + new_house = { + "id": get_next_house_id(), + "address": address, + "price": price, + "area": area, + "house_type": house_type + } + houses = read_houses() + houses.append(new_house) + write_houses(houses) + + # V3.0 新增:地址解析并保存 + addr_info = parse_address(address) + save_address_info(new_house["id"], addr_info) + # V3.0 新增:自动添加小区到邻接矩阵 + add_community(addr_info["community"]) + + print(f"房源新增成功!房源ID:{new_house['id']}") + return True + + +# 原有 delete_house / update_house / query_house 完全不变 +# 原有栈操作 push_operation_stack / push_browse_stack 等完全不变 + +# ==================== V3.0 新增:地址串模糊查询 ==================== +def query_by_area(keyword): + """ + 串模糊查询:按区域关键词搜索房源 + 时间复杂度:O(n*L) + """ + houses = read_houses() + result = [] + for house in houses: + addr_info = get_address_info(house["id"]) + full_addr = f"{addr_info['province']}{addr_info['city']}{addr_info['district']}{addr_info['community']}" + if keyword in full_addr: + result.append(house) + return result + + +# ==================== V3.0 新增:邻接矩阵管理 ==================== +def add_community_distance(community1, community2, distance): + """添加小区距离(供后续路径规划使用)""" + return set_distance(community1, community2, distance) + + +def show_adj_matrix(): + """打印邻接矩阵(数组展示)""" + data = get_adj_matrix() + communities = data["communities"] + matrix = data["adj_matrix"] + print("\n===== 小区邻接矩阵 =====") + print("节点列表:", communities) + print("邻接矩阵(9999=不可达):") + for row in matrix: + print(row) \ No newline at end of file