请编写一个用python编写的车站查询系统课程设计
时间: 2024-06-08 11:07:59 浏览: 68
好的,以下是一个简单的车站查询系统的Python代码示例:
```python
stations = {
'北京': ['北京南', '北京西', '北京北'],
'上海': ['上海虹桥', '上海南', '上海火车站'],
'广州': ['广州南', '广州东'],
'深圳': ['深圳北', '深圳站'],
'成都': ['成都东', '成都南']
}
def search():
start = input('请输入起始站:')
end = input('请输入终点站:')
for city, station_list in stations.items():
if start in station_list:
start_city = city
if end in station_list:
end_city = city
if start_city == end_city:
print('起始站和终点站在同一城市,无需换乘。')
print('从{}到{}的直达车次有:'.format(start, end))
for train in direct_trains(start, end):
print(train)
else:
print('需要换乘。')
print('从{}到{}的车次有:'.format(start, end))
for train1, station1 in trains(start_city, end_city):
for train2, station2 in trains(end_city, start_city):
if station1 == station2:
print('乘坐{}从{}到{},换乘{}从{}到{}'.format(train1, start, station1, train2, station1, end))
def direct_trains(start, end):
direct_trains_list = []
for train, stations in all_trains.items():
if start in stations and end in stations:
direct_trains_list.append(train)
return direct_trains_list
def trains(start_city, end_city):
trains_list = []
for train, stations in all_trains.items():
if start_city in train and end_city in train:
start_index = stations.index(start_city)
end_index = stations.index(end_city)
if start_index < end_index:
trains_list.append((train, stations[start_index:end_index+1]))
else:
trains_list.append((train, stations[end_index:start_index+1][::-1]))
return trains_list
all_trains = {
'G1': ['北京南', '上海虹桥', '南京南', '杭州东', '福州南', '广州南', '深圳北'],
'G2': ['深圳北', '广州南', '福州南', '杭州东', '南京南', '上海虹桥', '北京南'],
'G3': ['北京南', '上海虹桥', '南京南', '杭州东', '温州南', '福州南', '广州南', '深圳北'],
'G4': ['深圳北', '广州南', '福州南', '温州南', '杭州东', '南京南', '上海虹桥', '北京南']
}
search()
```
该程序使用字典储存各个城市的火车站信息和所有高速列车的车站信息,然后根据用户输入的起始站和终点站查询直达车次或需要换乘的车次。如果是直达车次,则输出所有直达车次的名称;如果需要换乘,则输出两个车次和换乘站点的信息。
阅读全文