【问题标题】:C++ Graph data StructureC++ 图形数据结构
【发布时间】:2018-03-05 06:35:33
【问题描述】:

我正在用 C++ 编写一个图形实现,其中城市是顶点,从一个城市到另一个城市的航班代表一条边,权重是这些城市之间的距离。顶点、边和权重存储在一个文件中,当程序运行时,它会将顶点、边和权重加载到程序中。我正在使用一个表示边缘的邻接矩阵。

现在当程序运行时,它会提示用户:

  1. 选择出发城市
  2. 退出。选项二只是终止程序。

如果用户选择选项一,那么它将列出文件中的所有城市。我选择了七个城市。所以它看起来像 1.) 洛杉矶 2.) 纽约 3.) 迈阿密等等,直到选项 7。当用户选择一个选项时,它将列出除用户选择的出发城市之外的所有目的地城市。一旦用户选择了他的目的地城市,将会有三种可能性。

现在第一种可能是A市和B市之间没有直接或直连,程序会输出,[出发城市]和[目的地城市]之间没有目的地,按任意键返回。一旦用户按下任意键,菜单将再次显示。第二种可能性是如果城市之间有直接连接,那么程序将输出[出发城市]-[目的地城市] = [英里]和城市之间的英里之间的直接连接,或者如果没有直接连接它会说没有直接连接,用户可以返回菜单。

第三种可能是直通连接,它将显示出发城市和目的地城市以及它们之间的所有城市以及它们之间覆盖的总里程,用户可以按任意键返回到菜单。

现在我遇到的问题是从文件中获取信息,我不知道如何从文件中获取信息或如何编写文件以便程序知道哪些是顶点、边和权重。另外,如何显示有直接连接、通过连接或完全没有连接的城市。

include <iostream>
#include <fstream>
#pragma once

const int NULL_EDGE = 0;
typedef std::string String;

class GraphType
{

private:
    int edges[50][50];
    int m_numVertices;
    int m_maxVertices;
    int m_distance;
    String* m_vertices;
    bool* marks; // marks[i] is the mark for vertices[i]
    int IndexIs(String*, String);

public:
    GraphType();
    ~GraphType();
    bool isEmpty() const;
    bool isFull(); //to do
    int GetWeight(String, String); // to do
    void ClearMarks(); // to do
    void MarkVertex(String) // to do
    bool isMarked(String) // to do
    void addVertex(String);
    void addEdge(String, String, int);
    void displayCities();
};


#include "GraphType.h"

GraphType::GraphType()
{

    m_maxVertices = 50;
    m_distance = 0;
    m_vertices = new String[m_maxVertices];
    marks = new bool[50];

    std::ifstream loadFile;
    loadFile.open("load.txt");

    if (loadFile.fail())
        std::cout << " Error opening load.txt\n";
    else
    {
        //stuck here
    }

    loadFile.close();
}

GraphType::~GraphType()
{
    delete[] m_vertices;
    delete[] marks;
}

int GraphType::IndexIs(String* vertices, String vertex)
{
    int index = 0;

    while (!(vertex == m_vertices[index]) == 0)
        index++;

    return index;
}

void GraphType::addVertex(String vertex)
{
    m_vertices[m_numVertices] = vertex;

    for (int i = 0; i < m_numVertices; i++)
    {
        edges[m_numVertices][i] = NULL_EDGE;
        edges[i][m_numVertices] = NULL_EDGE;
    }

    m_numVertices++;
}

void GraphType::addEdge(String startVertex, String destVertex, int weight)
{
    int row;
    int col;

    row = IndexIs(m_vertices, startVertex);
    col = IndexIs(m_vertices, destVertex);
    edges[row][col] = weight;
}

void GraphType::displayCities()
{
    //stuck here
}

bool GraphType::isEmpty() const
{
    return (m_numVertices == 0);
}

#include "GraphType.h"

int FlyMenu();
void CitiesMenu(GraphType&);

int main()
{
    int choose;
    GraphType gt;

    do
    {
        choose = FlyMenu();

        switch (choose)
        {
            case 1: CitiesMenu(gt);
            break;

            case 2:
            break;

            default: std::cout << " Invalid Input\n";
            break;
        }

    } while (choose != 2);

    return 0;
}

int FlyMenu()
{
    int option;

    std::cout << " 1.) Choose Depature City\n";
    std::cout << " 2.) Exit\n";
    std::cout << " Enter option: ";
    std::cin >> option;

    return option;
}

void CitiesMenu(GraphType& gt)
{
    gt.displayCities();
}

我知道深度遍历和广度遍历算法,以查看城市之间是否存在联系,但我不知道如何在这种情况下实现它们。我不能使用标准模板库,只能使用 std::vector。我正在考虑写另一门课,但我不知道那门课对我有什么帮助。

【问题讨论】:

    标签: c++ algorithm graph implementation


    【解决方案1】:

    据我所知,您有两个问题:

    1. 从文件读取/写入:最简单的解决方案是 freopen。

    freopen("input","r",stdin) 
    freopen("output","w",stdout)
    

    现在每个cin &amp; cout 操作都将在您在freopen 上定义的文件上完成

    1. 实现 DFS / BFS:我将为您编写最简单的 DFS 类型,您必须对其进行编辑以适应您的程序。

    bool visit[MAX_CITY + 10];
    void DFS(int x){
        visit[x] = 1;
        for (int i=1;i<=MAX_CITY;i++){
            int nx = i;
            if (adj[x][i] == -1) continue; // a cost of -1 means no flight
            if (visit[nx]) continue;
            DFS(nx);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-05-30
      • 1970-01-01
      • 2011-11-23
      • 2021-08-05
      • 1970-01-01
      • 1970-01-01
      • 2012-06-08
      相关资源
      最近更新 更多