Advertisement
Guest User

sharpec

a guest
Jun 18th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.83 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. using System.Xml.Serialization;
  8.  
  9. namespace ConsoleApp6
  10. {
  11.     class Program
  12.     {
  13.         static void Main(string[] args)
  14.         {
  15.             FileStream fS = new FileStream("input.csv", FileMode.Open);
  16.             StreamReader sR = new StreamReader(fS);
  17.             List<int[,]> Matr = new List<int[,]>();
  18.             string cur = "";
  19.             int ind = 0;
  20.             int indstr = 0;
  21.             while ((cur = sR.ReadLine()) != null)
  22.             {
  23.                 string[] str = cur.Split(',');
  24.                 int[] num = new int[str.Length];
  25.                 for (int i = 0; i < str.Length; i++)
  26.                     if (!int.TryParse(str[i], out num[i]))
  27.                     {
  28.                         string cur1 = "";
  29.                         for (int j = 0; j < str[i].Length; j++)
  30.                             if (str[i][j] >= '0' & str[i][j] <= '9')
  31.                                 cur1 += str[i][j];
  32.                         int.TryParse(cur1, out num[i]);
  33.  
  34.                     }
  35.                 if (Matr.Count == 0)
  36.                 {
  37.                     Matr.Add(new int[num.Length, num.Length]);
  38.                     indstr = num.Length;
  39.                     for (int i = 0; i < Matr[ind].GetLength(1); i++)
  40.                         Matr[ind][num.Length - indstr, i] = num[i];
  41.                     indstr = num.Length - 1;
  42.  
  43.                     continue;
  44.                 }
  45.                 if (indstr > 0 & Matr[ind].GetLength(0) != num.Length)
  46.                 {
  47.  
  48.                     for (int i = Matr[ind].GetLength(0) - indstr; i < Matr[ind].GetLength(1); i++)
  49.                         for (int j = 0; j < Matr[ind].GetLength(1); j++)
  50.                             Matr[ind][i, j] = 0;
  51.                     indstr = 0;
  52.                 }
  53.                 if (indstr == 0)
  54.                 {
  55.                     ind++;
  56.  
  57.                     Matr.Add(new int[num.Length, num.Length]);
  58.                     indstr = num.Length;
  59.                     for (int i = 0; i < Matr[ind].GetLength(1); i++)
  60.                         Matr[ind][num.Length - indstr, i] = num[i];
  61.                     indstr = num.Length - 1;
  62.  
  63.                 }
  64.                 else
  65.                 {
  66.                     for (int i = 0; i < Matr[ind].GetLength(1); i++)
  67.                         Matr[ind][num.Length - indstr, i] = num[i];
  68.                     indstr--;
  69.                 }
  70.  
  71.  
  72.             }
  73.             foreach (var a in Matr)
  74.             {
  75.                 for (int i = 0; i < a.GetLength(1); i++)
  76.                     for (int j = 0; j < a.GetLength(1); j++)
  77.                     {
  78.                         Console.Write(a[i, j]);
  79.                         if (j + 1 == a.GetLength(1))
  80.                             Console.WriteLine();
  81.                         else
  82.                         {
  83.                             Console.Write(" ");
  84.                         }
  85.                     }
  86.                 Console.WriteLine("-------------------------");
  87.             }
  88.             Console.WriteLine();
  89.             IEnumerable<int[,]> mat = from a in Matr
  90.                                       where MainDiag(a)
  91.                                       select a;
  92.             foreach (var a in mat)
  93.             {
  94.                 for (int i = 0; i < a.GetLength(1); i++)
  95.                     for (int j = 0; j < a.GetLength(1); j++)
  96.                     {
  97.                         Console.Write(a[i, j]);
  98.                         if (j + 1 == a.GetLength(1))
  99.                             Console.WriteLine();
  100.                         else
  101.                         {
  102.                             Console.Write(" ");
  103.                         }
  104.                     }
  105.                 Console.WriteLine("-------------------------");
  106.             }
  107.             IEnumerable<int[,]> mat2 = from a in Matr
  108.                                       where MainElement(a)
  109.                                       select a;
  110.             Console.WriteLine();
  111.             foreach (var a in mat2)
  112.             {
  113.                 for (int i = 0; i < a.GetLength(1); i++)
  114.                     for (int j = 0; j < a.GetLength(1); j++)
  115.                     {
  116.                         Console.Write(a[i, j]);
  117.                         if (j + 1 == a.GetLength(1))
  118.                             Console.WriteLine();
  119.                         else
  120.                         {
  121.                             Console.Write(" ");
  122.                         }
  123.                     }
  124.                 Console.WriteLine("-------------------------");
  125.             }
  126.             XmlSerializer xml = new XmlSerializer(typeof(int[,]));
  127.             FileStream newfS = new FileStream("text.xml",FileMode.Create);
  128.             foreach (var a in mat2)
  129.                 xml.Serialize(newfS,a);
  130.  
  131.  
  132.         }
  133.         static public bool MainDiag(int[,] a)
  134.         {
  135.             int z = 0;
  136.             for(int i=0;i<a.GetLength(1);i++)
  137.             {
  138.                 if (a[i, i] == 0)
  139.                     z++;
  140.             }
  141.             return z >= 3;
  142.         }
  143.         static public bool MainElement(int[,] a)
  144.         {
  145.             int z = -100;
  146.             int ind1 = -1;
  147.             int ind2 = 0;
  148.             for (int i = 0; i < a.GetLength(1); i++)
  149.                 for (int j = 0; j < a.GetLength(1); j++)
  150.                     if (a[i, j] > z)
  151.                     {
  152.                         z = a[i, j];
  153.                         ind1 = i;
  154.                         ind2 = j;
  155.                     }
  156.                      else if(a[i, j] == z)
  157.                     {
  158.                         if(j>i)
  159.                         {
  160.                             ind1 = i;
  161.                             ind2 = j;
  162.                         }
  163.                     }
  164.             return ind2 > ind1;
  165.  
  166.  
  167.  
  168.         }
  169.     }
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement