欢迎使用CSDN-markdown编辑器

来源:互联网 发布:美工薪资待遇 编辑:程序博客网 时间:2024/05/16 17:46

graph.h

#ifndef GRAPH_H#define GRAPH_H#define MAX_STATION_NUM 30#define MAX_STATION_NAME_LENGTH 30#define MAX_LINE_NUM 30typedef struct Edge{    int adjVertex;    int lineNo;    int dis;    struct Edge *next;}Edge;typedef struct Vertex{    bool usrFlag;    int stationNo;    struct Edge* edgeList;}Vertex, AdjList[MAX_STATION_NUM];typedef struct Graph{    int nVertex, nEdges;    AdjList stationList;}Graph;/**/void addStation(int lineNo, char station[MAX_STATION_NAME_LENGTH], int distance);/* @ 入参s1, s2 @ 出参distance*/void getDistance(char s1[MAX_STATION_NAME_LENGTH], char s2[MAX_STATION_NAME_LENGTH], int &distance);int getStationIndexByNo(int stationNo);int getLineLastStation(int lineNo);int getUnusedStationIndex(Graph &g);void initStation(int i, Graph &g, int stationNo);void addStation2Line(Graph &g, int preInex, int nextIndex, int dis, int lineNo);void addList(Edge *&list, Edge *&node);void initGraph(Graph &g);void printLines(int lineNo);void printGraph(Graph &g);#endif

graph.cpp

#include <iostream>#include <map>#include <vector>#include "graph.h"std::map<int, int> lineNo2StaionNo;std::vector<std::vector<int>> lines;std::map<int, int> stationNo2StationIndex;Graph g;int getStationIndexByNo(int stationNo){    if (stationNo2StationIndex.find(stationNo) == stationNo2StationIndex.end())    {        return -1;    }    return stationNo2StationIndex[stationNo];}void initGraph(Graph &g){    g.nEdges = g.nVertex = 0;    for (int i = 0; i < MAX_STATION_NUM; ++i)    {        g.stationList[i].edgeList = NULL;        g.stationList[i].usrFlag = false;        g.stationList[i].stationNo = -1;    }    lines.resize(MAX_LINE_NUM);}int getUnusedStationIndex(Graph &g){    for (int i = 0; i < MAX_STATION_NUM; ++i)    {        if (!g.stationList[i].usrFlag)            return i;    }    return -1;}int getLineLastStation(int lineNo){    if (lineNo2StaionNo.find(lineNo) == lineNo2StaionNo.end())        return -1;    return lineNo2StaionNo[lineNo];}void addList(Edge *&list, Edge *&node){    if (list)    {        node->next = list;        list = node;    }    else    {        list = node;    }}void addStation2Line(Graph &g, int preInex, int nextIndex, int dis, int lineNo){    Edge *e1 = NULL, *e2 = NULL;    e1 = (Edge *)malloc(sizeof(Edge));    e2 = (Edge *)malloc(sizeof(Edge));    e1->adjVertex = preInex; e2->adjVertex = nextIndex;    e1->dis = e2->dis = dis;    e1->lineNo = e2->lineNo = lineNo;    e1->next = e2->next = NULL;    addList(g.stationList[preInex].edgeList, e2);    addList(g.stationList[nextIndex].edgeList, e1);    g.nEdges += 2;}void addStation(int lineNo, char station[MAX_STATION_NAME_LENGTH], int distance){    int nextStationNo = atoi(&station[1]);    int preStationNo = getLineLastStation((lineNo));    int preStationIndex = getStationIndexByNo(preStationNo);    int nextStationIndex = getStationIndexByNo(nextStationNo);    if (preStationIndex == -1 && nextStationIndex == -1)  /* 无线无站 */    {        int newStationIndex = getUnusedStationIndex(g);        stationNo2StationIndex[nextStationNo] = newStationIndex;        lineNo2StaionNo[lineNo] = nextStationNo; /* 建线 */        initStation(newStationIndex, g, nextStationNo);  /* 建站 */        lines[lineNo].push_back(newStationIndex);    }    else if (preStationIndex == -1 && nextStationIndex != -1) /* 无线有站 */    {        lineNo2StaionNo[lineNo] = nextStationNo;        lines[lineNo].push_back(nextStationIndex);    }    else if (preStationIndex != -1 && nextStationIndex == -1) /* 有线无站*/    {        int newStationIndex = getUnusedStationIndex(g);        stationNo2StationIndex[nextStationNo] = newStationIndex;        initStation(newStationIndex, g, nextStationNo);        lineNo2StaionNo[lineNo] = nextStationNo;        addStation2Line(g, preStationIndex, newStationIndex, distance, lineNo);        lines[lineNo].push_back(newStationIndex);    }    else /* 有线有站 */    {        lineNo2StaionNo[lineNo] = nextStationNo;        addStation2Line(g, preStationIndex, nextStationIndex, distance, lineNo);        lines[lineNo].push_back(nextStationIndex);    }}void initStation(int i, Graph &g, int stationNO){    g.stationList[i].stationNo = stationNO; /* 建站*/    g.stationList[i].edgeList = NULL;    g.stationList[i].usrFlag = true;    g.nVertex++;}void printLines(int lineNo){    if (lineNo == -1)    {        for (int i = 0; i < MAX_LINE_NUM; ++i)        {            if (lines[i].empty()) continue;            for (int j = 0; j < lines[i].size(); ++j)            {                printf("%d-->", lines[i][j]);            }            printf("\n");        }    }    else    {        for (int j = 0; j < lines[lineNo].size(); ++j)        {            printf("%d-->", lines[lineNo][j]);        }        printf("\n");    }}void getDistance(char s1[MAX_STATION_NAME_LENGTH], char s2[MAX_STATION_NAME_LENGTH], int &distance){    int s1no = atoi(&s1[1]), s2no = atoi(&s2[1]);}void printGraph(Graph &g){    printf("定点数:%d\n", g.nVertex);    printf("边数:%d\n", g.nEdges / 2);    for (int i = 0; i < MAX_STATION_NUM; ++i)    {        if (!g.stationList[i].usrFlag) continue;        printf("站点编号:%d\n", g.stationList[i].stationNo);        printf("边表:\n\t");        for (Edge *p = g.stationList[i].edgeList; p != NULL; p = p->next)        {            printf("边<%d--%d>,距离<%d>,地铁线号<%d>\n\t", g.stationList[i].stationNo, g.stationList[p->adjVertex].stationNo, p->dis, p->lineNo);        }        printf("\n");    }}int main(){    FILE *f = fopen("test.txt", "r");    initGraph(g);    int lineNo, distance;    lineNo = -1; distance = -1;    char str[MAX_STATION_NAME_LENGTH];    memset(str, 0, sizeof(str));    while (fscanf(f, "%d %s %d", &lineNo, str, &distance))    {        if (str[1] == 'q') break;        addStation(lineNo, str, distance);        lineNo = -1; distance = -1;        memset(str, 0, sizeof(str));    }    printf("******图******\n");    printGraph(g);    printf("*********11退出**************\n");    while(scanf("%d", &lineNo), lineNo != 11)    {        printLines(lineNo);    }    system("pause");    return 0;}
0 0