2024.4.11(周四)进度

做python大作业,但是没实现图形化界面
import os
M = 15 # 校园景点数量
INF = 0x3f3f3f3f


class Campus:
    att = ["","正门","科技楼","第一教学楼","基础教学楼","图书馆","北小门","宿舍区","西操场","学二食堂","澡堂","体育馆","学一食堂"]   #景点
    inf = [['']] * M
    edges = [[INF] * M] * M  #
    Nodes_Num = 0
    edges_Num = 0   #总结点数,总边数

def putMap():
    print("                                                                                                                 ")
    print("                                                石家庄铁道大学校园导游图                                                    ")
    print("                                                                                                                 ")
    print("                                                                                                                 ")
    print("        ===================================================================================================      ")
    print("                                           ----1:正门                                                          ")
    print("                     /    \\                /    |    \\                                                         ")
    print("                    /      \\              /     |     \\                                                        ")
    print("                   /        -----         |      |      ----- 2:科技楼------                                     ")
    print("                  /               \\       |     |              |   |        |                                   ")
    print("                 9:学二食堂                 --- ----3:第一教学楼      ---------   |        |                                     ")
    print("         ===================================================================================================     ")
    print("                |                  |    |       |    /            |             5:图书馆      \                                ")
    print("                |                  |    |       |   /             |             \\             \8:西操场                  ")
    print("                |                  /     \\      |  /              |             -- 11:体育馆                    ")
    print("                12:学一食堂           /        -4:基础教学楼              |                       |                        ")
    print("                 \\              /              |                 /                        |                      ")
    print("                  \\            /               |                /        /------------                   ")
    print("        ===================================================================================================       ")
    print("                       7:宿舍区-----------------                 |      /                     |                     ")
    print("                           |                     \               |     /                      |                    ")
    print("                           |                      \          10: 澡堂-------                 |                     ")
    print("                           |                       \                         |                |                    ")
    print("                            -----------------------6:北小门----------------------------------                     ")
    print("                                                                                                                  ")
    print("        ===================================================================================================      ")


def putMenu():
    print("")
    print("                      *     *       ********     *            *            ********                        ")
    print("                      *     *       *            *            *           *        *                       ")
    print("                      *******       *******      *            *           *        *                       ")
    print("                      *     *       *            *            *           *        *                       ")
    print("                      *     *       ********     *******      *******      ********                        ")
    print("                                                                                                   ")
    print("                                                                                                   ")
    print("       = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =")
    print("       = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =")
    print("       = =                                                                                   = =")
    print("       = =                           欢迎使用石家庄铁道大学校园导航系统                            = =")
    print("       = =                                                                                   = =")
    print("       = =                                   请选择服务:                                     = =")
    print("       = =                                                                                   = =")
    print("       = =             1.学校信息 可查询景点 当前地点和目的地           4.退出系统            = =")
    print("       = =                                                                                   = =")
    print("       = =             2.寻找两景点之间的最短路径                                          = =")
    print("       = =                                                                                   = =")
    print("       = =             3.寻找景点之间的最短路径                                              = =")
    print("       = =                                                                                   = =")
    print("       = =                                                                                   = =")
    print("       = =                                                                                   = =")
    print("       = =                                                                                   = =")
    print("       = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =")
    print("       = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =")
    print()
    op = input("             请根据你的需求选择操作:")
    return op




def getCampus(g):
    file = r'./map.txt'
    inff = r'./information.txt'
    for i in range(1,g.Nodes_Num+1):
        for j in range(1,g.Nodes_Num+1):
            if i == j:
                g.edges[i][j] = 0

    with open(file) as f:
        i = 0
        for line in f:
            if i == 0:
                tmp1,tmp2 = line.split(',')
                g.Nodes_Num = int(tmp1)
                g.edges_Num = int(tmp2)
                i = 1
            else:
                x, y, w = line.split(',')
                g.edges[int(x)][int(y)] = g.edges[int(y)][int(x)] = int(w)
    with open('information.txt','r', encoding='utf-8') as f:
        for line in f:
            tmp1,tmp2 = line.split(' ')
            poi = int(tmp1)
            g.inf[poi] = tmp2
def getinf(g):
    poi = int(input("请输入您想了解到景点,0结束:"))
    while poi != 0:
        print("以下是该景点信息:")
        print(g.inf[poi])
        poi = int(input("请输入您想了解到景点,0结束:"))

def check(ch):
    if ch < 0 or ch > 12:
        print("\n您的输入有误,请输入0~12之间的数字!")
        return False
    else:
        return True


def Search(g):
    number = 0
    while True:
        putMap()
        print("请问您想查看哪个景点(请输入景点编号,输入0结束):")
        input(number)
        # system("cls") #清空屏幕
        if(check(number)):
            if(number == 0):
                break
            else:
                print("景点编号:{}".format(g.att[number].num))
                print("景点名称:{}".format(g.att[number]))

class passing():
    pathStack = [[0]*M] ##路径栈
    top = 0
    count = 0  #栈顶位置,路径数
    visited = [[False]*M] #判断是否已经经过


#Floyd算法求两景点间的一条最短的路径
class DIS:
    distence = [[0] * M] * M                                                   #距离向量
    path = [[0] * M] * M

def shortPath(g,dis):
    for i in range(1,g.Nodes_Num+1):                                #初始化距离向量矩阵与路径向量矩阵
        for j in range(1,g.Nodes_Num+1):
            dis.distence[i][j] = g.edges[i][j]
            if i != j and dis.distence[i][j] != INF:
                dis.path[i][j] = i  #表示如果i和j相邻,i到j需要经过i
            else:
                dis.path[i][j] = -1                                 #否则用 -1代表当前两点不可达

    for k in range(1,g.Nodes_Num+1):                                #递推求解每两景点的最短路径
        for i in range(1,g.Nodes_Num+1):
            for j in range(1,g.Nodes_Num+1):
                if dis.distence[i][j] > (dis.distence[i][k] + dis.distence[k][j]):  #如果发现引入k点可以使得路径更短
                    dis.distence[i][j] = dis.distence[i][k] + dis.distence[k][j]    #更新最短路径长度
                    dis.path[i][j] = k                               # 更新最短路径上的经结点


# 递归实现打印两点间的最短路径(不包括起始点)
def Floyd_Print(g,dis,start,end):
                                                #递归基:如果两点相邻或者两点不可达,结束递归
    if dis.path[start][end] == -1 or dis.path[start][end] == end or dis.path[start][end] == start:
        return
    else:
        Floyd_Print(g,dis, start, dis.path[start][end])                 #将中间点作为终点继续打印路径
        print('{}->'.format(g.att[dis.path[start][end]]),end='')         #打印中间景点名字
        Floyd_Print(g, dis,dis.path[start][end], end)                 #将中间点作为起点继续打印路径

#输出并打印两点间的最短路径
def print_shortPath(g,dis):
    start = int(input("         请输入起点编号:"))
    end = int(input("         请输入终点编号:"))
    print("             {}到{}的最短距离是{}M".format(g.att[start],g.att[end],dis.distence[start][end]))
    print("             最佳游览路线:",end='')
    print('{}->'.format(g.att[start]),end='')                                       #输出路径上的起点
    Floyd_Print(g,dis,start, end)                                               #输出路径上的中间点
    print(g.att[end])                                     #输出路径上的终点



def bestPath(g,dis):
    vNum = [0,0,0,0,0,0,0,0,0,0,0,0,0]
    count = 1                                       #记录用户输入的编号信息
    len = 500                                                        #统计全程路径总长
    vNum[count] = int(input("             请输入你要游览的景点的编号(输入0结束输入):"))


    while vNum[count] != 0 and count <= 12:
        if vNum[count] == 0:
            break
        count += 1
        vNum[count] = int(input("             请输入你要游览的景点的编号(输入0结束输入):"))

    print("             已为您挑选最佳访问路径:")
    i = 1
    while vNum[i] > 0 and vNum[i + 1] > 0 :          #遍历所有输入的景点
        print("{}->".format( g.att[vNum[i]]),end='')                      #输出路径上的起点
        Floyd_Print(g,dis, vNum[i],vNum[i + 1])                      #利用Floyd算法得到这两点之间的最短路径
        len += dis.distence[vNum[i]][vNum[i + 1]]             #算出最短路长度
        i += 1
    print(g.att[vNum[count - 1]])                 #输出路径上的终点
    print("             全程总长为:{}m".format(len))


if __name__ == '__main__':

    g = Campus()
    dis = DIS()
    p = passing()
    getCampus(g)                                                               #从文件读取信息建立校园地图
    shortPath(g,dis)                                                               #通过Floyd求出distence表与path表
    while True :
        op = putMenu()
        while op != '0':                                                  #打印主菜单
            if op == '1':
                putMap()
                getinf(g)
                os.system("pause")
                os.system('cls')
                op = putMenu()
            elif op == "2":
                putMap()
                print_shortPath(g,dis)                                  #两景点间最短路径查询
                os.system("pause")
                os.system('cls')
                op = putMenu()
            elif op == "3":
                putMap()
                bestPath(g,dis)                #多景点间访问最优路线查询
                os.system("pause")
                os.system('cls')
                op = putMenu()
            elif op == '4':
                print("感谢使用!")
                exit()
            else:
                print("             对不起!没有该选项对应的操作.")
                os.system("pause")
                os.system('cls')
                op = putMenu()

 

posted @ 2024-04-14 17:55  记得关月亮  阅读(11)  评论(0编辑  收藏  举报