peterzig

[AISD] Sortowanie z pliku

Sep 14th, 2016
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.03 KB | None | 0 0
  1. //////////////////////////////////////sortowanie.txt///////////////////////////////////
  2. 9 // ilosc elementow tablicy
  3. 1.0
  4. 4.0
  5. 3.0
  6. 2.0
  7. 8.0
  8. 15.0
  9. 2.5
  10. 3.5
  11. 5.5
  12. ///////////////////////////////////////////////////////////////////////////////////////
  13. // [AISD] Sortowanie z pliku.cpp : Defines the entry point for the console application.
  14. //
  15.  
  16. #include "stdafx.h"
  17. #include <conio.h>
  18. #include <malloc.h>
  19.  
  20. FILE *plik;
  21.  
  22. bool Test(const float * const tab, const int n)
  23. {
  24.     if (tab == 0) return false;
  25.     if (n <= 0) return false;
  26.     if (n > 100000) return false;
  27.     for (int i = 0; i < n - 1; ++i)
  28.     {
  29.         if (tab[i] > tab[i + 1]) return false;
  30.     }
  31.     return true;
  32. }
  33.  
  34. float* WczytajDane(const char *fileName, int *n)
  35. {
  36.     if ((plik = fopen(fileName, "r")) == NULL)
  37.     {
  38.         printf("Blad otwarcia pliku %s!", fileName);
  39.         return false;
  40.     }
  41.     fscanf(plik, "%d", n);
  42.     float *tab = (float*)malloc(sizeof(float)*(*n));
  43.  
  44.     for (int i = 0; i < (*n); i++)
  45.     {
  46.         fscanf(plik, "%f", &tab[i]);
  47.     }
  48.     return tab;
  49. }
  50.  
  51. void SortujBabelkowo(float *Dane, const int n)
  52. {
  53.     float x;
  54.     for (int i = 0; i < n - 1; i++)
  55.     {
  56.         for (int j = 0; j < n - 1; j++)
  57.         {
  58.             if (Dane[j] > Dane[j + 1])
  59.             {
  60.                 x = Dane[j + 1];
  61.                 Dane[j + 1] = Dane[j];
  62.                 Dane[j] = x;
  63.             }
  64.         }
  65.     }
  66. }
  67.  
  68. void SortujPrzezWybieranie(float *Dane, const int n)
  69. {
  70.     int k;
  71.     float x;
  72.     for (int i = 0; i < n; i++)
  73.     {
  74.         k = i;
  75.         for (int j = i + 1; j < n; j++)
  76.         {
  77.             if (Dane[j] < Dane[k]) k = j;
  78.         }
  79.         x = Dane[k];
  80.         Dane[k] = Dane[i];
  81.         Dane[i] = x;
  82.     }
  83. }
  84.  
  85. int main()
  86. {
  87.     int n;
  88.  
  89.     float *tab = WczytajDane("sortowanie.txt", &n);
  90.     printf("Liczba danych: %d\n", n);
  91.     SortujBabelkowo(tab, n);
  92.     printf("Metoda sortowania babelkowego\n");
  93.     if (Test(tab, n)) printf("Dane posortowane prawidlowo\n"); else printf("Dane posortowane nieprawidlowo\n");
  94.     SortujPrzezWybieranie(tab, n);
  95.     printf("Metoda sortowania przez wybieranie.\n");
  96.     if (Test(tab, n)) printf("Dane posortowane prawidlowo\n"); else printf("Dane posortowane nieprawidlowo\n");
  97.     free(tab);
  98.     _getch();
  99.     return 0;
  100. }
Add Comment
Please, Sign In to add comment