Advertisement
Guest User

Untitled

a guest
Jan 17th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.50 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7.  
  8. namespace Sortowanie {
  9.  class Program {
  10.   //JAKIEŚ ZMIENNE
  11.   static int ile = 0, jeszcze;
  12.   static bool jesioraz = false;
  13.   static bool ladowanie = true;
  14.   static string linia = "0";
  15.   static bool manual = false;
  16.  
  17.   //FUNKCJE
  18.   static void sort(int[] tablica) {
  19.    int n = tablica.Length;
  20.    do {
  21.     for (int i = 0; i < n - 1; i++) {
  22.      if (tablica[i] > tablica[i + 1]) {
  23.       int tmp = tablica[i];
  24.       tablica[i] = tablica[i + 1];
  25.       tablica[i + 1] = tmp;
  26.      }
  27.     }
  28.     n--;
  29.    }
  30.    while (n > 1);
  31.   }
  32.   static void QuickSort(int[] array, int lewo, int prawo) {
  33.    var i = lewo;
  34.    var j = prawo;
  35.    var strona = array[(lewo + prawo) / 2];
  36.    while (i < j) {
  37.     while (array[i] < strona) i++;
  38.     while (array[j] > strona) j--;
  39.     if (i <= j) {
  40.      var tmp = array[i];
  41.      array[i++] = array[j];
  42.      array[j--] = tmp;
  43.     }
  44.    }
  45.    if (lewo < j) QuickSort(array, lewo, j);
  46.    if (i < prawo) QuickSort(array, i, prawo);
  47.   }
  48.   static void Fajne_cos() {
  49.    //System.Threading.Thread.Sleep(5000);
  50.    Console.Write("[|||       ]25%");
  51.    System.Threading.Thread.Sleep(500);
  52.    Console.Clear();
  53.    Console.Write("[||||      ]35%");
  54.    System.Threading.Thread.Sleep(500);
  55.    Console.Clear();
  56.    Console.Write("[||||||    ]60%");
  57.    System.Threading.Thread.Sleep(500);
  58.    Console.Clear();
  59.    Console.Write("[||||||||  ]80%");
  60.    System.Threading.Thread.Sleep(500);
  61.    Console.Clear();
  62.    Console.Write("[||||||||||]100%");
  63.    ladowanie = false;
  64.   }
  65.   static int sprawdzenie(string liczba) {
  66.    int ilosc_liczb;
  67.  
  68.    while (!Int32.TryParse(liczba, out ilosc_liczb) || liczba.ElementAt(0) == '-') {
  69.     Console.Write("Podaj ilość liczb do posortowania: ");
  70.     liczba = Console.ReadLine();
  71.    }
  72.  
  73.    return ilosc_liczb;
  74.   }
  75.  
  76.  
  77.   static void Main(string[] args) {
  78.    do {
  79.     if (ladowanie == true) {
  80.      Fajne_cos();
  81.      ladowanie = false;
  82.     }
  83.     int opcja = 0;
  84.     do {
  85.      Console.Clear();
  86.      Console.Write("Jaką metode sortowania chcesz wybrać?");
  87.      if (manual == false) {
  88.       Console.Write("(Manual Wyłączony)\n");
  89.      } else {
  90.       Console.Write("(Manual Włączony)\n");
  91.      }
  92.      Console.Write("1. Sortowanie bąbelkowe\n2. Sortowanie szybkie (quicksort)\n3. Sortowanie przez wstawianie \n4. Opcje\n Wybór: ");
  93.      opcja = Convert.ToInt32(Console.ReadLine());
  94.     } while (opcja <= 0 || opcja >= 5);
  95.  
  96.     switch (opcja) {
  97.      case 1:
  98.       jesioraz = false;
  99.       Console.Clear();
  100.       Console.Write("Ile liczb chcesz posortować?");
  101.       ile = Convert.ToInt32(Console.ReadLine());
  102.       int[] tablica = new int[ile];
  103.       if (ile == 0) {
  104.        jesioraz = true;
  105.        break;
  106.       }
  107.       if (manual == true) {
  108.        tablica = new int[ile];
  109.        Random rnd = new Random();
  110.        Console.Write("\nPrzed: ");
  111.        for (int i = 0; i < ile; i++) {
  112.         tablica[i] = rnd.Next(0, 100);
  113.         Console.Write(tablica[i] + " ");
  114.        }
  115.       }
  116.       if (manual == false) {
  117.        Console.Write("Podaj liczby do tablicy: \n");
  118.  
  119.        for (int i = 0; i < ile; i++) {
  120.         Console.Write((i + 1) + " ");
  121.         tablica[i] = Convert.ToInt32(Console.ReadLine());
  122.        }
  123.  
  124.        Console.Write("\nPrzed: ");
  125.        for (int l = 0; l < ile; l++) {
  126.         Console.Write(tablica[l] + " ");
  127.        }
  128.       }
  129.       sort(tablica);
  130.       Console.Write("\nPo: ");
  131.       for (int j = 0; j < ile; j++) {
  132.        Console.Write(tablica[j] + " ");
  133.       }
  134.       Console.Write("\n\n\nPowrót do menu?\n1. Tak\n2. Nie\n Opcja: ");
  135.       jeszcze = Convert.ToInt32(Console.ReadLine());
  136.       if (jeszcze == 1) {
  137.        jesioraz = true;
  138.        break;
  139.       }
  140.       break;
  141.      case 2:
  142.       jesioraz = false;
  143.       Console.Clear();
  144.       Console.Write("Ile liczb chcesz posortować?");
  145.       ile = Convert.ToInt32(Console.ReadLine());
  146.       if (ile == 0) {
  147.        jesioraz = true;
  148.        break;
  149.       }
  150.       var rnd2 = new Random();
  151.       int[] tablica2 = new int[ile];
  152.       for (int i = 0; i < tablica2.Length; i++) {
  153.        tablica2[i] = rnd2.Next(0, 100);
  154.       }
  155.       Console.WriteLine("\nPrzed: ");
  156.       Console.WriteLine(string.Join(" ", tablica2));
  157.       QuickSort(tablica2, 0, tablica2.Length - 1);
  158.       Console.WriteLine("Po: ");
  159.       Console.WriteLine(string.Join(" ", tablica2));
  160.       Console.Write("\n\n\nPowrót do menu?\n1. Tak\n2. Nie\n Opcja: ");
  161.       jeszcze = Convert.ToInt32(Console.ReadLine());
  162.       if (jeszcze == 1) {
  163.        jesioraz = true;
  164.        break;
  165.       }
  166.       break;
  167.      case 3:
  168.       jesioraz = false;
  169.       Console.Clear();
  170.       Console.Write("Ile liczb chcesz posortować?");
  171.       ile = Convert.ToInt32(Console.ReadLine());
  172.       if (ile == 0) {
  173.        jesioraz = true;
  174.        break;
  175.       }
  176.       int ilosc_liczb, element, bufor;
  177.       string liczba = "";
  178.  
  179.       Random randomizer = new Random();
  180.  
  181.       ilosc_liczb = sprawdzenie(liczba);
  182.  
  183.       int[] do_posortowania = new int[ilosc_liczb];
  184.       int[] posortowane = new int[ilosc_liczb];
  185.  
  186.       Console.WriteLine("");
  187.  
  188.       for (int i = 0; i < ilosc_liczb; i++) {
  189.        element = randomizer.Next(101);
  190.        do_posortowania[i] = element;
  191.        Console.Write(do_posortowania[i] + " ");
  192.       }
  193.  
  194.       Console.WriteLine("");
  195.       posortowane[0] = do_posortowania[0];
  196.  
  197.       for (int i = 1; i < ilosc_liczb; i++) {
  198.        for (int j = i; j > 0; j--) {
  199.  
  200.         if (posortowane[j - 1] > do_posortowania[i]) {
  201.          bufor = posortowane[j - 1];
  202.          posortowane[j - 1] = do_posortowania[i];
  203.          posortowane[j] = bufor;
  204.         } else {
  205.          posortowane[j] = do_posortowania[i];
  206.          break;
  207.         }
  208.        }
  209.       }
  210.  
  211.       Console.WriteLine("Posortowane liczby:");
  212.  
  213.       for (int i = 0; i < ilosc_liczb; i++) {
  214.        Console.Write(posortowane[i] + " ");
  215.       }
  216.       Console.Read();
  217.       break;
  218.      case 4:
  219.       jesioraz = true;
  220.       int zmiana = 0;
  221.  
  222.  
  223.       do {
  224.        try {
  225.         using(StreamReader streamR = new StreamReader("dane.txt")) {
  226.          linia = streamR.ReadLine();
  227.          streamR.Close();
  228.         }
  229.        } catch (Exception e) {
  230.         Console.WriteLine("Nie mozna otworzyc pliku");
  231.         Console.WriteLine(e.Message);
  232.        }
  233.  
  234.        Console.Clear();
  235.        zmiana = 0;
  236.        Console.Write("===============\n");
  237.        Console.Write("     Opcje     \n");
  238.        Console.Write("===============\n");
  239.        Console.Write("1. Automatyczne uzupełnianie tablicy(Randomowe Liczby) = ");
  240.        if (manual == false) {
  241.         Console.Write("Nie aktywne!");
  242.        }
  243.        if (manual == true) {
  244.         Console.Write("Aktywne!");
  245.        }
  246.  
  247.        Console.Write("\n\nCzy chcesz zmienić opcje numer 1?\n 1. Tak\n 2. Nie\nOpcja: ");
  248.        zmiana = Convert.ToInt16(Console.ReadLine());
  249.  
  250.        if (zmiana == 1) {
  251.         if (linia == "0")
  252.          using(StreamWriter StreamW = new StreamWriter(("dane.txt"), false)) {
  253.           StreamW.WriteLine("1");
  254.           manual = true;
  255.           StreamW.Close();
  256.          }
  257.         if (linia == "1") {
  258.          using(StreamWriter StreamW = new StreamWriter(("dane.txt"), false)) {
  259.           StreamW.WriteLine("0");
  260.           manual = false;
  261.           StreamW.Close();
  262.          }
  263.         }
  264.  
  265.        } else {
  266.         break;
  267.        }
  268.  
  269.       }
  270.       while (zmiana >= 0 || zmiana <= 3);
  271.       break;
  272.     }
  273.     opcja = 0;
  274.    } while (jesioraz == true);
  275.   }
  276.  }
  277. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement