Files
2026-06-17 22:02:41 +08:00

287 lines
11 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
from house_service import *
def print_menu():
print("\n===== 房屋出租系统 V5.0 =====")
print("1. 新增房源")
print("2. 删除房源")
print("3. 修改房源")
print("4. 查询房源")
print("5. 查看/管理操作记录")
print("6. 查看/管理浏览历史")
print("7. 地址字符串解析测试")
print("8. 按区域关键词模糊查询")
print("9. 初始化小区邻接矩阵")
print("10. 更新小区间距离")
print("11. 房源二叉树范围查询(租金/面积)")
print("12. 房源冒泡排序(租金/面积)")
print("13. 房源快速排序(租金/面积)")
#V5.0 新增菜单
print("14. 可视化小区距离图(邻接矩阵)")
print("15. 计算小区间最短路径(Dijkstra")
print("16. 基于最短路径推荐房源")
print("17. 退出系统")
print("============================")
# V2原有操作记录子菜单
def op_record_menu():
while True:
print("\n----- 操作记录管理 -----")
print("1. 查看最近10条")
print("2. 取出最新一条")
print("3. 清空记录")
print("4. 返回")
c = input("请选择:")
if c == "1":
lst = get_recent_records("operation", 10)
print(lst if lst else "暂无记录")
elif c == "2":
print(pop_operation_stack() or "暂无记录")
elif c == "3":
clear_stack("operation")
elif c == "4":
break
else:
print("输入有误")
# V2浏览历史子菜单
def browse_history_menu():
while True:
print("\n----- 浏览历史管理 -----")
print("1. 查看最近10条")
print("2. 取出最新一条")
print("3. 清空历史")
print("4. 返回")
c = input("请选择:")
if c == "1":
id_list = get_recent_records("browse", 10)
if not id_list:
print("暂无浏览历史")
continue
for idx, hid in enumerate(id_list, 1):
info = query_house("id", hid)
if info:
h = info[0]
print(f"{idx}. ID:{hid} 地址:{h['address']}")
else:
print(f"{idx}. ID:{hid} 房源已不存在")
elif c == "2":
hid = pop_browse_stack()
if not hid:
print("暂无浏览历史")
continue
info = query_house("id", hid)
print(f"最近浏览ID:{hid}")
elif c == "3":
clear_stack("browse")
elif c == "4":
break
else:
print("输入有误")
def main():
while True:
print_menu()
choice = input("请输入功能编号:")
# ========== V1 原有功能==========
if choice == "1":
addr = input("输入房源地址:")
try:
price = float(input("输入月租金:"))
area = float(input("输入面积:"))
except:
print("租金面积必须是数字!")
continue
htype = input("输入户型:")
if add_house(addr, price, area, htype):
new_id = get_next_house_id() - 1
push_operation_stack(f"新增房源ID:{new_id} 地址:{addr}")
elif choice == "2":
try:
hid = int(input("输入要删除房源ID"))
res = query_house("id", hid)
addr = res[0]["address"] if res else "未知地址"
if delete_house(hid):
push_operation_stack(f"删除房源ID:{hid} 地址:{addr}")
except:
print("ID必须是整数")
elif choice == "3":
try:
hid = int(input("输入要修改房源ID"))
edit = {}
a = input("新地址(回车不修改)")
if a: edit["address"] = a
p = input("新租金(回车不修改)")
if p: edit["price"] = float(p)
ar = input("新面积(回车不修改)")
if ar: edit["area"] = float(ar)
t = input("新户型(回车不修改)")
if t: edit["house_type"] = t
if edit:
if update_house(hid, edit):
push_operation_stack(f"修改房源ID:{hid} 变更:{edit}")
else:
print("未填写任何修改内容")
except:
print("输入格式错误")
elif choice == "4":
print("1-ID 2-地址 3-租金 4-户型")
sel = input("选择查询类型:")
map_dic = {"1": "id", "2": "address", "3": "price", "4": "house_type"}
if sel not in map_dic:
print("选择无效")
continue
val = input("输入查询值:")
res_list = query_house(map_dic[sel], val)
if not res_list:
print("未找到房源")
continue
for h in res_list:
print(f"ID:{h['id']} 地址:{h['address']} 租金:{h['price']}")
if map_dic[sel] == "id":
push_browse_stack(h["id"])
# ========== V2 原有功能 ==========
elif choice == "5":
op_record_menu()
elif choice == "6":
browse_history_menu()
# ========== V3 原有功能==========
elif choice == "7":
full_addr = input("输入完整地址(如:四川省德阳市什邡市XX小区):")
res = parse_address_string(full_addr)
print("解析结果:", res)
elif choice == "8":
keyword = input("输入区域关键词(如:什邡、德阳):")
res_list = fuzzy_query_by_district(keyword)
if not res_list:
print("未找到该区域房源")
else:
for h in res_list:
print(f"ID:{h['id']} 地址:{h['address']}")
elif choice == "9":
community_input = input("输入所有小区名称,用英文逗号分隔:")
community_list = community_input.split(",")
init_community_matrix(community_list)
elif choice == "10":
c1 = input("输入小区A名称:")
c2 = input("输入小区B名称:")
try:
dis = float(input("输入两小区距离:"))
update_matrix_distance(c1, c2, dis)
except:
print("距离必须是数字")
# ========== V4 原有功能==========
elif choice == "11":
print("请选择查询维度:1-租金 2-面积")
dim_choice = input("输入维度编号:")
key_field = "price" if dim_choice == "1" else "area"
field_name = "租金" if dim_choice == "1" else "面积"
try:
min_val = float(input(f"输入{field_name}最小值:"))
max_val = float(input(f"输入{field_name}最大值:"))
except:
print("数值必须是数字!")
continue
all_houses = read_houses()
if not all_houses:
print("暂无房源数据!")
continue
bst_root = build_house_bst(all_houses, key_field)
result = search_bst_range(bst_root, min_val, max_val, key_field)
if not result:
print(f"未找到{field_name}{min_val}-{max_val}之间的房源")
else:
print(f"\n{field_name}{min_val}-{max_val}之间的房源:")
for h in result:
print(f"ID:{h['id']} 地址:{h['address']} {field_name}:{h[key_field]}")
elif choice == "12":
print("请选择排序维度:1-租金 2-面积")
dim_choice = input("输入维度编号:")
sort_field = "price" if dim_choice == "1" else "area"
field_name = "租金" if dim_choice == "1" else "面积"
print("请选择排序方式:1-升序 2-降序")
sort_way = input("输入方式编号:")
reverse = (sort_way == "2")
all_houses = read_houses()
if not all_houses:
print("暂无房源数据!")
continue
sorted_houses = bubble_sort_houses(all_houses, sort_field, reverse)
print(f"\n{field_name}{'降序' if reverse else '升序'}排序结果(冒泡排序):")
for h in sorted_houses:
print(f"ID:{h['id']} 地址:{h['address']} {field_name}:{h[sort_field]}")
elif choice == "13":
print("请选择排序维度:1-租金 2-面积")
dim_choice = input("输入维度编号:")
sort_field = "price" if dim_choice == "1" else "area"
field_name = "租金" if dim_choice == "1" else "面积"
print("请选择排序方式:1-升序 2-降序")
sort_way = input("输入方式编号:")
reverse = (sort_way == "2")
all_houses = read_houses()
if not all_houses:
print("暂无房源数据!")
continue
sorted_houses = quick_sort_houses(all_houses, sort_field, reverse)
print(f"\n{field_name}{'降序' if reverse else '升序'}排序结果(快速排序):")
for h in sorted_houses:
print(f"ID:{h['id']} 地址:{h['address']} {field_name}:{h[sort_field]}")
# ========== V5 新增功能分支 ==========
elif choice == "14":
# 可视化小区图
print_community_graph()
elif choice == "15":
# 计算最短路径
start_comm = input("输入起始小区名称:")
end_comm = input("输入目标小区名称:")
shortest_dist, shortest_path = dijkstra_shortest_path(start_comm, end_comm)
if shortest_dist is not None:
print(f"\n{start_comm}{end_comm} 的最短距离:{shortest_dist:.1f}")
print(f"最短路径:{' -> '.join(shortest_path)}")
elif choice == "16":
# 基于路径推荐房源
target_comm = input("输入目标小区名称:")
try:
max_dist = float(input("输入最大推荐距离:"))
except:
print("距离必须是数字!")
continue
recommend_list = recommend_houses_by_path(target_comm, max_dist)
if not recommend_list:
print(f"未找到{target_comm}周边{max_dist}范围内的房源")
else:
print(f"\n{target_comm}周边{max_dist}范围内的推荐房源:")
for h in recommend_list:
print(f"ID:{h['id']} 地址:{h['address']} 租金:{h['price']} 面积:{h['area']}")
elif choice == "17":
print("系统退出成功!")
break
else:
print("请输入有效编号")
if __name__ == "__main__":
main()