简体   繁体   English

从c#中的文本文件中读取矩阵

[英]Read a matrix from a text file in c#

I need to read a text file that contains the matrix, instead of having it on the code like I have, but I tried and I can't figure it out. 我需要读取一个包含矩阵的文本文件,而不是像我一样在代码上使用它,但我试过了,我无法弄明白。 I have the matrix on a text file like I have on the code, but without the comas 我在文本文件上有矩阵,就像我在代码上一样,但是没有coma

This is a dijkstra program that reads the matrix in order to find the shortest path, thank you 这是一个dijkstra程序,它读取矩阵以找到最短路径,谢谢

{

    private static int MinimumDistance(int[] distance, bool[] shortestPathTreeSet, int verticesCount)
    {
        int min = int.MaxValue;
        int minIndex = 0;

        for (int v = 0; v < verticesCount; ++v)
        {
            if (shortestPathTreeSet[v] == false && distance[v] <= min)
            {
                min = distance[v];
                minIndex = v;
            }
        }

        return minIndex;
    }

    private static void Print(int[] distance, int verticesCount)
    {
        Console.WriteLine("Vertex    Distance from source");

        for (int i = 0; i < verticesCount; ++i)
            Console.WriteLine("{0}\t  {1}", i, distance[i]);
    }

    public static void DijkstraAlgo(int[,] graph, int source, int verticesCount)
    {
        int[] distance = new int[verticesCount];
        bool[] shortestPathTreeSet = new bool[verticesCount];

        for (int i = 0; i < verticesCount; ++i)
        {
            distance[i] = int.MaxValue;
            shortestPathTreeSet[i] = false;
        }

        distance[source] = 0;

        for (int count = 0; count < verticesCount - 1; ++count)
        {
            int u = MinimumDistance(distance, shortestPathTreeSet, verticesCount);
            shortestPathTreeSet[u] = true;

            for (int v = 0; v < verticesCount; ++v)
                if (!shortestPathTreeSet[v] && Convert.ToBoolean(graph[u, v]) && distance[u] != int.MaxValue && distance[u] + graph[u, v] < distance[v])
                    distance[v] = distance[u] + graph[u, v];
        }

        Print(distance, verticesCount);
    }

    static void Main(string[] args)
    {
        int[,] graph =  {
                     { 0, 6, 0, 0, 0, 0, 0, 9, 0 },
                     { 6, 0, 9, 0, 0, 0, 0, 11, 0 },
                     { 0, 9, 0, 5, 0, 6, 0, 0, 2 },
                     { 0, 0, 5, 0, 9, 16, 0, 0, 0 },
                     { 0, 0, 0, 9, 0, 10, 0, 0, 0 },
                     { 0, 0, 6, 0, 10, 0, 2, 0, 0 },
                     { 0, 0, 0, 16, 0, 2, 0, 1, 6 },
                     { 9, 11, 0, 0, 0, 0, 1, 0, 5 },
                     { 0, 0, 2, 0, 0, 0, 6, 5, 0 }
                        };

        DijkstraAlgo(graph, 0, 9);
    }
}

} }

and here is the text file matrix: 这是文本文件矩阵:

0 6 8 12 0 0 0 0 0 0 0 6 8 12 0 0 0 0 0 0
6 0 0 5 0 0 0 0 0 0 6 0 0 5 0 0 0 0 0 0
8 0 0 1 0 0 0 0 0 0 8 0 0 1 0 0 0 0 0 0
12 5 1 0 9 10 14 16 15 0 12 5 1 0 9 10 14 16 15 0
0 0 0 9 0 0 3 0 0 0 0 0 0 9 0 0 3 0 0 0
0 0 0 10 0 0 0 0 13 0 0 0 0 10 0 0 0 0 13 0
0 0 0 14 3 0 0 3 0 6 0 0 0 14 3 0 0 3 0 6
0 0 0 16 0 0 3 0 1 4 0 0 0 16 0 0 3 0 1 4
0 0 0 15 0 13 0 1 0 7 0 0 0 15 0 13 0 1 0 7
0 0 0 0 0 0 6 4 7 0 0 0 0 0 0 0 6 4 7 0

If i understand you correctly. 如果我理解正确的话。

You are after a way to read a space deliminated int s from a file and convert it into a multidimensional (square) array of int . 您正在寻找一种从文件中读取空间分隔的int并将其转换为多维(方形) int数组的方法。 eg int[,] 例如int[,]

You could do something like this 你可以这样做

Given 特定

public static class Extensions
{
   public static T[,] ToRectangularArray<T>(this IReadOnlyList<T[]> arrays)
   {
      var ret = new T[arrays.Count, arrays[0].Length];
      for (var i = 0; i < arrays.Count; i++)
         for (var j = 0; j < arrays[0].Length; j++)           
            ret[i, j] = arrays[i][j];
      return ret;
   }
}

Usage 用法

var someArray = File.ReadAllLines("myAwesomeFile")   // read from File
                    .Select(x => x.Split(' ').Select(int.Parse).ToArray()) // split line into array of int
                    .ToArray()                       // all to array
                    .ToRectangularArray();           // send to multidimensional array

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM