首页 文章

如何输入矩阵样式的txt文件,而不是为C定义我自己的int 2D数组

提问于
浏览
0

所以我对C很新,但我想我已经开始了解它 .

作为excersize的一部分,我必须采用输入文本文件并将其应用于“最短距离算法”,最终我想输出所有最短距离和路线,但我还没有那么远 . 我使用了Floyd Warshall算法 . 现在我的问题是,我如何用文本输入替换自编写的int数组 . 输入数组只是数字,但实际上表示节点之间的距离 . 我现在使用的测试阵列只有3个节点,但我希望能够将它扩展到更大的节点amout,比如100 .

示例测试矩阵:

0 1234567 100

1234567 0 400

100 400 0

应该读作:

node1      node2       node3
  node 1    0          999999     100
  node 2   999999       0         400
  node 3    100        400         0

大数字:999999表示太大的距离太多算作边缘 .

到目前为止,我的代码看起来像这样:

#include<stdio.h>

// Number of vertices
#define V 3

// Define 999999 as a distance that is too large to represent a edge connection
#define TooLarge 999999

// The print function
void printSolution(int dist[][V]);

        // Distance algorithm
    void Distance (int distgraph[][V])
    {
        // output matrix that will have the shortest distance for every vertice
        int dist[V][V], i, j, k;

        // initial values for shortest distance are based on shortest paths.
        for (i = 0; i < V; i++)
            for (j = 0; j < V; j++)
                dist[i][j] = distgraph[i][j];

        // Add all vertices to the set of intermediate vertices.
             for (k = 0; k < V; k++)
        {
            // use all vertices as seperate source
            for (i = 0; i < V; i++)
        {
            // use all vertices as destination for the earlier determined source

            for (j = 0; j < V; j++)
            {
                // If vertex k is on the shortest path from i to j, then update the value of dist[i][j]
                if (dist[i][k] + dist[k][j] < dist[i][j])
                    dist[i][j] = dist[i][k] + dist[k][j];
            }
        }
    }

    // Print the shortest distance matrix
    printSolution(dist);
}

// The print function
void printSolution(int dist[][V])
{
    printf ("Shortest distance matrix \n");
    for (int i = 0; i < V; i++)
    {
        for (int j = 0; j < V; j++)
        {
            if (dist[i][j] == 999999)
                printf("%7s", "TooLarge");
            else
                printf ("%7d", dist[i][j]);
        }
        printf("\n");
    }
}

// driver program to test above function
int main()
{
      int distgraph[V][V] = { {0, 1234567, 100},
                              {1234567, 0, 400},
                              {100, 400, 0,},
                            };

    // Print the solution
    Distance(distgraph);
    return 0;
}

希望有人可以帮助我,我感觉我只是忘记了一些愚蠢的事情 . 我试图使用这种类型的代码输入文本文件:

using namespace std;

double distances [3][3];

int main () {
  int x, y;
  ifstream in("citytest.txt");

  if (!in) {
    cout << "Cannot open file.\n";
    return 0;
  }

  for (y = 0; y < 3; y++) {
    for (x = 0; x < 3; x++) {
      in >> distances[x][y];
    }
  }
    cout << distances[3][3] << " " << endl;
      in.close();

我知道哪些有效,但只是在输入矩阵的预定部分,而我想输入整个数组 . (cout函数就在那里测试是否给出了正确的距离作为输入)

1 回答

  • 1

    除非您知道外部数据文件中的工作负载很大,否则无法有效地分配容器 .

    从而:

    • 将文件的第一行标记,并从中获取维度N.

    • 相应地分配您的容器

    • 然后使用文件的其余部分并将数据放入容器中;如果行's length doesn' t与N匹配,或者如果没有N行,则可能 throw .

    你可以考虑一下

    • 用完全邻接矩阵表示图是一个值得商榷的概念;对于稀疏图,它的空间效率低且时间效率低

    • 2D c阵列不是矩阵的唯一可能表示;您可以考虑使用平面std容器并在其上实现slice样式的访问

    • 最后一点,你可能想看看boost::graph

相关问题