写一个完整的公交查询系统,要求(菜单中有线路查询,站点查询,换乘查询,添加线路信息,退出程序和需要手动导入本地站点信息,)程序能自动判断线路之间是否有相同站点,起点和终点直达判断,
时间: 2024-03-11 22:47:02 浏览: 104
公交线路查询程序设计
4星 · 用户满意度95%
好的,我可以为你提供一个基本框架,你可以根据自己的需求进行修改和完善。
首先,你需要设计一个数据结构来存储线路和站点信息。可以考虑使用图的数据结构,其中每个站点是一个节点,每条公交线路是一条边。可以使用邻接表或邻接矩阵来表示这个图。
```python
class BusStop:
def __init__(self, name):
self.name = name
self.lines = []
class BusLine:
def __init__(self, name, stops):
self.name = name
self.stops = stops
class BusMap:
def __init__(self):
self.stops = {}
self.lines = {}
def add_stop(self, name):
if name not in self.stops:
self.stops[name] = BusStop(name)
def add_line(self, name, stops):
if name not in self.lines:
self.lines[name] = BusLine(name, stops)
for stop in stops:
self.add_stop(stop)
self.stops[stop].lines.append(name)
def get_stop_by_name(self, name):
if name in self.stops:
return self.stops[name]
return None
def get_line_by_name(self, name):
if name in self.lines:
return self.lines[name]
return None
def get_lines_by_stop(self, stop_name):
stop = self.get_stop_by_name(stop_name)
if stop:
return stop.lines
return None
def get_stops_by_line(self, line_name):
line = self.get_line_by_name(line_name)
if line:
return line.stops
return None
```
对于线路查询和站点查询,可以考虑使用深度优先搜索或广度优先搜索来实现。对于换乘查询,可以考虑使用 Dijkstra 算法或 A* 算法来找到最短路径。
```python
from queue import PriorityQueue
class BusPath:
def __init__(self, stops, total_distance):
self.stops = stops
self.total_distance = total_distance
def __lt__(self, other):
return self.total_distance < other.total_distance
class BusFinder:
def __init__(self, bus_map):
self.bus_map = bus_map
def find_path(self, start, end):
pq = PriorityQueue()
pq.put(BusPath([start], 0))
visited = set()
while not pq.empty():
path = pq.get()
last_stop = path.stops[-1]
if last_stop == end:
return path
if last_stop in visited:
continue
visited.add(last_stop)
for line in self.bus_map.get_lines_by_stop(last_stop):
for stop in self.bus_map.get_stops_by_line(line):
if stop not in visited:
distance = 1 # assume all lines have the same distance
new_path = BusPath(path.stops + [stop], path.total_distance + distance)
pq.put(new_path)
return None
```
添加线路信息可以通过命令行或者图形界面来实现。在添加线路信息时,需要判断线路之间是否有相同站点,可以使用哈希表来加快判断速度。
```python
def add_stop_command(bus_map):
name = input("Enter stop name: ")
bus_map.add_stop(name)
print("Stop added successfully.")
def add_line_command(bus_map):
name = input("Enter line name: ")
stops = input("Enter stop names separated by comma: ").split(",")
bus_map.add_line(name, stops)
print("Line added successfully.")
def main_loop(bus_map):
while True:
print("Please select an option:")
print("1. Line search")
print("2. Stop search")
print("3. Transfer search")
print("4. Add line")
print("5. Quit")
choice = input("Enter option number: ")
if choice == "1":
line_name = input("Enter line name: ")
line = bus_map.get_line_by_name(line_name)
if line:
print("Stops:", ",".join(line.stops))
else:
print("Line not found.")
elif choice == "2":
stop_name = input("Enter stop name: ")
stop = bus_map.get_stop_by_name(stop_name)
if stop:
print("Lines:", ",".join(stop.lines))
else:
print("Stop not found.")
elif choice == "3":
start = input("Enter start stop name: ")
end = input("Enter end stop name: ")
finder = BusFinder(bus_map)
path = finder.find_path(start, end)
if path:
print("Path:", "->".join(path.stops), "Distance:", path.total_distance)
else:
print("Path not found.")
elif choice == "4":
add_line_command(bus_map)
elif choice == "5":
break
else:
print("Invalid option.")
```
起点和终点直达判断可以使用最短路径算法来实现。如果两个站点之间有直达的公交线路,则直接返回这个线路信息;否则,可以使用 Dijkstra 算法或 A* 算法来找到最短路径。
需要手动导入本地站点信息可以考虑使用文件读写来实现。可以将站点信息保存在一个文本文件中,然后程序启动时读取这个文件并将信息载入内存。
退出程序可以在命令行或者图形界面中添加一个退出按钮或者命令来实现。
当然,上述只是一个大体的框架,具体实现还需要考虑很多细节和边界情况。希望这些思路能对你有所帮助,祝你实现成功!
阅读全文