Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/124.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++;图形数据结构 我在C++中写了一个图形实现,其中城市是顶点,从一个城市到另一个城市的飞行代表一个边缘,权重是这些城市之间的距离。顶点、边和权重存储在文件中,当程序运行时,它会将顶点、边和权重加载到程序中。我使用一个邻接矩阵来表示边_C++_Algorithm_Graph_Implementation - Fatal编程技术网

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

C++;图形数据结构 我在C++中写了一个图形实现,其中城市是顶点,从一个城市到另一个城市的飞行代表一个边缘,权重是这些城市之间的距离。顶点、边和权重存储在文件中,当程序运行时,它会将顶点、边和权重加载到程序中。我使用一个邻接矩阵来表示边,c++,algorithm,graph,implementation,C++,Algorithm,Graph,Implementation,现在,当程序运行时,它将提示用户: 选择出发城市 退出。选项二只是终止程序 如果用户选择选项一,那么它将列出文件中的所有城市。我选择了七个城市。所以它看起来像1。)洛杉矶2。)纽约3。)迈阿密等等,直到选项7。当用户选择一个选项时,它将列出除用户选择的出发城市之外的所有目的地城市。一旦用户选择了目的地城市,将有三种可能性 现在第一种可能是A市和B市之间没有直接或通过连接,程序将输出,[出发城市]和[目的地城市]之间没有目的地,按任意键返回。一旦用户按下任何键,菜单将再次显示。第二种可能性是,如果

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

  • 选择出发城市
  • 退出。选项二只是终止程序
  • 如果用户选择选项一,那么它将列出文件中的所有城市。我选择了七个城市。所以它看起来像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();
    }
    
    包括
    #包括
    #布拉格语一次
    常数int NULL_边=0;
    typedef std::string字符串;
    类图形类型
    {
    私人:
    整数边[50][50];
    国际货币基金组织;
    int m_最大顶点;
    int m_距离;
    字符串*m_顶点;
    bool*marks;//marks[i]是顶点的标记[i]
    int索引(字符串*,字符串);
    公众:
    GraphType();
    ~GraphType();
    bool isEmpty()常量;
    bool isFull();//要做的事
    int GetWeight(String,String);//要执行的操作
    void ClearMarks();//要执行的操作
    void MarkVertex(字符串)//要执行的操作
    bool isMarked(String)//要做
    void addVertex(字符串);
    void addEdge(字符串、字符串、整数);
    无效显示城市();
    };
    #包括“GraphType.h”
    GraphType::GraphType()
    {
    m_max=50;
    m_距离=0;
    m_顶点=新字符串[m_最大顶点];
    马克=新布尔[50];
    std::ifstream加载文件;
    loadFile.open(“load.txt”);
    if(loadFile.fail())
    
    std::cout根据我得到的信息,您有两个问题:

  • 从文件读/写:最简单的解决方案是freopen


  • 现在,每个
    cin&cout
    操作都将在您在
    freopen

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

  • bool访问[MAX_CITY+10];
    无效DFS(整数x){
    访问[x]=1;
    对于(int i=1;i
    
    freopen("input","r",stdin) 
    freopen("output","w",stdout)
    
    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);
        }
    }