Advertisement
Guest User

Zadanie3

a guest
Jan 21st, 2020
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.35 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7.  
  8. namespace ConsoleApp9
  9. {
  10.     class Program
  11.     {
  12.         static void Wczytaj(string sciezka)
  13.         {
  14.             try
  15.             {
  16.                 using (StreamReader streamReader = new StreamReader(sciezka))
  17.                 {
  18.                     string linia = null;
  19.                     double x;
  20.                     int licznik = 0;
  21.                     while ((linia = streamReader.ReadLine()) != null)
  22.                     {
  23.                         licznik++;
  24.                         try
  25.                         {
  26.                             x = Convert.ToDouble(linia);
  27.                             liczba.Add(x);
  28.                         }
  29.                         catch(FormatException er)
  30.                         {
  31.                             Console.WriteLine("Blad w {0} linii", licznik);
  32.                         }
  33.                         catch(Exception er)
  34.                         {
  35.                             Console.WriteLine(er.ToString());
  36.                         }
  37.                     }
  38.                 }
  39.             }
  40.             catch (IOException error)
  41.             {
  42.                 Console.WriteLine(error.ToString());
  43.             }
  44.             catch (Exception error)
  45.             {
  46.                 Console.WriteLine(error.ToString());
  47.             }
  48.         }
  49.  
  50.         static void Zapisz(string sciezka)
  51.         {
  52.             try
  53.             {
  54.                 using (StreamWriter sw = new StreamWriter(sciezka))
  55.                 {
  56.                     for (int i = 0; i < 100; i++)
  57.                     {  
  58.                         if(i % 20 == 0)
  59.                             sw.WriteLine("ERROR");
  60.                         else
  61.                             sw.WriteLine(rand.Next(200));
  62.                     }
  63.                 }
  64.             }
  65.             catch (IOException error)
  66.             {
  67.                 Console.WriteLine(error.ToString());
  68.             }
  69.             catch (Exception error)
  70.             {
  71.                 Console.WriteLine(error.ToString());
  72.             }
  73.             finally
  74.             {
  75.                 Console.WriteLine("Zapisano");
  76.             }
  77.         }
  78.  
  79.         static void Sortuj(List<double> lista)
  80.         {
  81.             double x;
  82.             int j;
  83.             for (int i = 1; i < lista.Count(); i++)
  84.             {
  85.                 x = lista[i];
  86.                 j = i - 1;
  87.                 while(j >= 0 && lista[j] > x)
  88.                 {
  89.                     lista[j + 1] = lista[j];
  90.                     j--;
  91.                 }
  92.                 lista[j + 1] = x;
  93.             }
  94.         }
  95.  
  96.         static void Wyswietl(List<double> lista)
  97.         {
  98.             for (int i = 0; i < lista.Count(); i++)
  99.             {
  100.                 Console.WriteLine(lista[i]);
  101.             }
  102.         }
  103.  
  104.         static Random rand = new Random();
  105.         static List<double> liczba = new List<double>();
  106.  
  107.         static void Main(string[] args)
  108.         {
  109.             string sciezka = @"..\..\Dane\Zadanie3.csv";
  110.             if (!File.Exists(sciezka))
  111.                 File.Create(sciezka);
  112.  
  113.             Zapisz(sciezka);
  114.             Wczytaj(sciezka);
  115.             Sortuj(liczba);
  116.             Wyswietl(liczba);
  117.  
  118.             Console.ReadKey();
  119.         }
  120.     }
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement