Advertisement
Guest User

File

a guest
Dec 13th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.31 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.IO;
  7.  
  8. namespace ConsoleApp5
  9. {   /// <summary>
  10.     /// Loads data from file and puts it into 2d int array[,] where first line is size of array
  11.     /// </summary>
  12.     public class LoadFile
  13.     {
  14.         public static int[,] pathsLenght { get; set; }
  15.         public static int size { get; set; }
  16.  
  17.         // example of loaded txt file
  18.         //  3
  19.         //  0
  20.         //  3 0
  21.         //  6 5 0
  22.         //
  23.         //  3 -> size of towns (A, B, C)
  24.         //     A B C  (Cities)
  25.         //  A  0      (path from A to A equals 0)
  26.         //  B  3 0    (path from A to B equals 3), (B to B = 0)
  27.         //  C  6 5 0  (A to C = 6) , (B to C = 5), (C to C = 0) etc.
  28.  
  29.         public LoadFile(string fileName)
  30.         {
  31.             string[] lines = File.ReadAllLines(fileName);
  32.            
  33.             Int32.TryParse((lines[0].Trim()), out int x);
  34.             size = x;
  35.             pathsLenght = new int[x, x];
  36.  
  37.             for (int i = 1; i <= lines.Length - 1; i++)
  38.             {
  39.                 string[] currentLenghts = lines[i].Trim().Split(' ');
  40.                 for (int j = 0; j < currentLenghts.Length; j++)
  41.                 {
  42.                     pathsLenght[i - 1, j] = int.Parse(currentLenghts[j]);
  43.  
  44.                 }
  45.             }
  46.  
  47.             for (int i = 0; i < x; i++)
  48.             {
  49.                 for (int j = 0; j < x; j++)
  50.                 {
  51.                     if (i <= j)
  52.                     {
  53.                         continue;
  54.                     }
  55.                     else
  56.                     {
  57.                         int tmp = pathsLenght[i, j];
  58.                         pathsLenght[j, i] = tmp;
  59.                     }
  60.                 }
  61.             }
  62.         }
  63.  
  64.         public void ReadFile()
  65.         {
  66.             Console.WriteLine("Wczytane danych z pliku: ");
  67.             for (int i = 0; i < pathsLenght.GetLength(0); i++)
  68.             {
  69.                 Console.WriteLine("-" + i + "-x-");
  70.                 for (int j = 0; j < pathsLenght.GetLength(1); j++)
  71.                 {
  72.  
  73.                     Console.Write(pathsLenght[i, j] + " ");
  74.                 }
  75.                 Console.WriteLine("-" + i + "-x-");
  76.  
  77.             }
  78.         }
  79.  
  80.     }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement