Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.IO;
- namespace ConsoleApp5
- { /// <summary>
- /// Loads data from file and puts it into 2d int array[,] where first line is size of array
- /// </summary>
- public class LoadFile
- {
- public static int[,] pathsLenght { get; set; }
- public static int size { get; set; }
- // example of loaded txt file
- // 3
- // 0
- // 3 0
- // 6 5 0
- //
- // 3 -> size of towns (A, B, C)
- // A B C (Cities)
- // A 0 (path from A to A equals 0)
- // B 3 0 (path from A to B equals 3), (B to B = 0)
- // C 6 5 0 (A to C = 6) , (B to C = 5), (C to C = 0) etc.
- public LoadFile(string fileName)
- {
- string[] lines = File.ReadAllLines(fileName);
- Int32.TryParse((lines[0].Trim()), out int x);
- size = x;
- pathsLenght = new int[x, x];
- for (int i = 1; i <= lines.Length - 1; i++)
- {
- string[] currentLenghts = lines[i].Trim().Split(' ');
- for (int j = 0; j < currentLenghts.Length; j++)
- {
- pathsLenght[i - 1, j] = int.Parse(currentLenghts[j]);
- }
- }
- for (int i = 0; i < x; i++)
- {
- for (int j = 0; j < x; j++)
- {
- if (i <= j)
- {
- continue;
- }
- else
- {
- int tmp = pathsLenght[i, j];
- pathsLenght[j, i] = tmp;
- }
- }
- }
- }
- public void ReadFile()
- {
- Console.WriteLine("Wczytane danych z pliku: ");
- for (int i = 0; i < pathsLenght.GetLength(0); i++)
- {
- Console.WriteLine("-" + i + "-x-");
- for (int j = 0; j < pathsLenght.GetLength(1); j++)
- {
- Console.Write(pathsLenght[i, j] + " ");
- }
- Console.WriteLine("-" + i + "-x-");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement