Advertisement
Guest User

Pomiary

a guest
Jan 21st, 2020
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.09 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4.  
  5. namespace KolokwiumNr22
  6. {
  7.     class Program
  8.     {
  9.         static string[,] pomiary = null;
  10.         static List<double> temperaturaMniejsza = new List<double>();
  11.         static string[,] pomiaryM = null;
  12.         static List<double> temperaturaWieksza = new List<double>();
  13.         static string[,] pomiaryW = null;
  14.  
  15.         public static void Wczytaj(string sciezka)
  16.  
  17.         {
  18.             FileStream fs = new FileStream(sciezka, FileMode.OpenOrCreate, FileAccess.Read);
  19.             StreamReader re = new StreamReader(fs);
  20.  
  21.             try
  22.             {
  23.                 if (File.Exists(sciezka))
  24.                 {
  25.                     string linia = null;
  26.                     string[] linie = File.ReadAllLines(sciezka);
  27.                     pomiary = new string[linie.Length, 6];
  28.                     int k = 0;
  29.  
  30.                     while ((linia = re.ReadLine()) != null)
  31.                     {
  32.  
  33.                         string[] param = linia.Split(';');
  34.                         for (int i = 0; i < param.Length; i++)
  35.                         {
  36.                             pomiary[k, i] = param[i];
  37.                         }
  38.  
  39.                         k++;
  40.                     }
  41.                 }
  42.             }
  43.             catch (IOException ex)
  44.             {
  45.                 Console.WriteLine(ex.Message);
  46.             }
  47.             catch (Exception ex)
  48.             {
  49.                 Console.WriteLine(ex.Message);
  50.             }
  51.             finally
  52.             {
  53.                 if (re != null)
  54.                 {
  55.                     re.Close();
  56.                 }
  57.             }
  58.         }
  59.  
  60.         static void ReduceToArrays()
  61.         {
  62.             //int u = 0;
  63.             //int p = 0;
  64.             for (int i = 1; i < pomiary.GetLength(0); i++)
  65.             {
  66.                 double x = double.Parse(pomiary[i, 5]);
  67.                 if (x <= 20)
  68.                 {
  69.                     temperaturaMniejsza.Add(x);
  70.                     //Console.WriteLine("M: " + temperaturaMniejsza[u]);
  71.                     //u++;
  72.                 }
  73.                 else
  74.                 {
  75.                     temperaturaWieksza.Add(x);
  76.                     //Console.WriteLine("W: " + temperaturaWieksza[p]);
  77.                     //p++;
  78.                 }
  79.  
  80.             }
  81.             temperaturaMniejsza.Add(19.999999); //NIE WIEM KOMPLETNIE CZEMU, ALE TO POMOGŁO Z WYJĄTKIEM "POZA ROZMIAREM TABLICY" XDD
  82.             temperaturaWieksza.Add(999999);     //NIE WIEM KOMPLETNIE CZEMU, ALE TO POMOGŁO Z WYJĄTKIEM "POZA ROZMIAREM TABLICY" XDD
  83.  
  84.             pomiaryM = new string[temperaturaMniejsza.Count, 6];
  85.             pomiaryW = new string[temperaturaWieksza.Count, 6];
  86.  
  87.             int m = 0;
  88.             int w = 0;
  89.  
  90.             Console.WriteLine("REDUCE");
  91.             Console.WriteLine("Pomiary: " + pomiary.GetLength(0));
  92.             Console.WriteLine("PomiaryM: " + pomiaryM.GetLength(0));
  93.             Console.WriteLine("PomiaryW: " + pomiaryW.GetLength(0));
  94.  
  95.             for (int i = 1; i < pomiary.GetLength(0); i++)
  96.             {
  97.                 double y = double.Parse(pomiary[i, 5]);
  98.                 //Console.WriteLine(i);
  99.                 //Console.Write(y + " = " + temperaturaMniejsza[m] + "\t");
  100.                 if (y == temperaturaMniejsza[m])
  101.                 {
  102.                     //Console.WriteLine("M: " + m);
  103.                     //Console.WriteLine("Mniejsza");
  104.                     for (int j = 0; j < 6; j++)
  105.                     {
  106.                         pomiaryM[m, j] = pomiary[i, j];
  107.                         //Console.WriteLine(pomiaryM[m, j] + "=" + pomiary[i, j]);
  108.                     }
  109.                     m++;
  110.                 }
  111.                 else
  112.                 {
  113.                     //Console.WriteLine("W: " + w);
  114.                     //Console.WriteLine("Wieksza");
  115.                     for (int j = 0; j < 6; j++)
  116.                     {
  117.                         pomiaryW[w, j] = pomiary[i, j];
  118.                         //Console.WriteLine(pomiaryW[w, j] + "=" + pomiary[i, j]);
  119.                     }
  120.                     w++;
  121.                 }
  122.             }
  123.         }
  124.  
  125.         public static void Zapisz(string sciezka, string[,] arr)
  126.         {
  127.             try
  128.             {
  129.                 using(StreamWriter readStream = new StreamWriter(sciezka))
  130.                 {
  131.                     for (int i = 0; i < arr.GetLength(0); i++)
  132.                     {
  133.                         for (int j = 0; j < 6; j++)
  134.                         {
  135.                             readStream.Write(arr[i, j]  + ";");
  136.                         }
  137.                         readStream.WriteLine();
  138.                     }
  139.                 }
  140.             }
  141.             catch (IOException ex)
  142.             {
  143.                 Console.WriteLine(ex.Message);
  144.             }
  145.             catch (Exception ex)
  146.             {
  147.                 Console.WriteLine(ex.Message);
  148.             }
  149.         }
  150.  
  151.         static void ShowArray(string[,] arr)
  152.         {
  153.             for (int i = 0; i < arr.GetLength(0); i++)
  154.             {
  155.                 for (int j = 0; j < arr.GetLength(1); j++)
  156.                 {
  157.                     Console.Write("{0,-13} ", arr[i, j]);
  158.                 }
  159.                 Console.WriteLine();
  160.             }
  161.         }
  162.  
  163.         static void Main(string[] args)
  164.         {
  165.             string path = @"..\..\Dane\pomiaryVT.csv";
  166.  
  167.             string path1 = @"..\..\Dane\pomiaryM.csv";
  168.             if (!File.Exists(path1))
  169.                 File.Create(path1);
  170.  
  171.             string path2 = @"..\..\Dane\pomiaryW.csv";
  172.             if (!File.Exists(path2))
  173.                 File.Create(path2);
  174.  
  175.             try
  176.             {
  177.                 Wczytaj(path);
  178.  
  179.                 ReduceToArrays();
  180.                 //ShowArray(pomiary);
  181.  
  182.                 Zapisz(path1, pomiaryM);
  183.                 Zapisz(path2, pomiaryW);
  184.             }
  185.             catch (Exception e)
  186.             {
  187.                 Console.WriteLine(e.ToString());
  188.             }
  189.  
  190.             Console.WriteLine("KONIEC");
  191.             Console.ReadKey();
  192.         }
  193.     }
  194. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement