Advertisement
aw0lf

Alg-tester

Jun 25th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.84 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <time.h>
  4. #include<fstream>
  5.  
  6. using namespace std;
  7. int* Sort(int [],int); //funkcje sortujace, potrzebne by uzyskac dane rodzaje zbiorów
  8. int* ReverseSort ( int [], int);
  9. void generateData();
  10. void loadData();
  11. void stoper(char *);
  12.  
  13. int main(){
  14.     generateData();
  15.     loadData();
  16. return 0;
  17. }
  18. void stoper(){
  19.     clock_t t;
  20.     for(int i=0;i<5;i++){
  21.     t = clock();
  22. //algorytm
  23.     t = clock() - t;
  24.     double time_taken = ((double)t)/CLOCKS_PER_SEC; // in seconds
  25.     cout<<"Pomiar "<<i<<" czas: "<<time_taken<<endl;
  26.     }
  27.  
  28.     }
  29. void loadData(){
  30. char *file_name=new char[7];
  31. int tab_size;
  32. file_name[2]='.';
  33. file_name[3]='t';
  34. file_name[4]='x';
  35. file_name[5]='t';
  36. int tab_sizes[]={10,20,30,50,75,100,200,300,400,500};
  37. for(int j=0;j<10;j++){
  38.     int temp[]={'0','1','2','3','4','5','6','7','8','9'};
  39.     file_name[1]=temp[j];
  40.     tab_size=tab_sizes[j];
  41.     int *tab=new int[tab_size];
  42.     fstream file; //otwieram plik
  43.     //pesymistyczne
  44.     cout<<"Losowy zbior: "<<tab_size<<" elementowy"<<endl;
  45.     file_name[0]='r';
  46.     file.open(file_name);
  47.     while(file.good()){
  48.         for(int i=0;i<tab_size;i++)
  49.             file>>tab[i]; //wczytuje dane do tab
  50.     }
  51.     file.close();
  52.     stoper();
  53.  
  54.     //optymistyczne
  55.     cout<<"Optymistyczny zbior: "<<tab_size<<" elementowy"<<endl;
  56.         file_name[0]='o';
  57.     file.open(file_name);
  58.     while(file.good()){
  59.         for(int i=0;i<tab_size;i++)
  60.             file>>tab[i]; //wczytuje dane do tab
  61.     }
  62.     file.close();
  63.     stoper();
  64.     //pesymistyczne
  65.     cout<<"Pesymistyczne zbior: "<<tab_size<<" elementowy"<<endl;
  66.         file_name[0]='p';
  67.     file.open(file_name);
  68.     while(file.good()){
  69.         for(int i=0;i<tab_size;i++)
  70.             file>>tab[i]; //wczytuje dane do tab
  71.     }
  72.     file.close();
  73.     stoper();
  74.  
  75.     delete[] tab;
  76.     }
  77.     delete[] file_name;
  78. }
  79.  
  80. void generateData()
  81. {
  82.     srand(time(NULL));
  83.  
  84.     char *file_name=new char[7];
  85.     int tab_size;
  86.     file_name[2]='.';
  87.     file_name[3]='t';
  88.     file_name[4]='x';
  89.     file_name[5]='t';
  90.  
  91.     for(int j=0;j<10;j++){
  92.     cout<<"Podaj wielkosc "<<endl;
  93.     cin>>tab_size;
  94.     int *tab = new int[tab_size]; //generowanie tablicy dynamicznej i wypelnianie jej liczbami losowymi
  95.         for(int i=0; i<tab_size; i++){
  96.             tab[i] = (rand() % tab_size)+7;
  97.         }
  98.  
  99.     int temp[]={'0','1','2','3','4','5','6','7','8','9'};
  100.     file_name[1]=temp[j];
  101.     ofstream file;
  102.     //losowe, wypenianie petla tablicy charow
  103.         file_name[0]='r';
  104.     file.open(file_name);
  105.             for(int i=0; i<tab_size; i++){
  106.         file<<tab[i]<<endl;
  107.             }
  108.     file.close();
  109.  
  110.     //
  111.         //zapis zbioru optymistycznego
  112.     Sort(tab,tab_size/2);
  113.     file_name[0]='o';
  114.     file.open(file_name);
  115.     for(int i=0;i<tab_size;i++){
  116.         file<<tab[i]<<endl;
  117.     }
  118.     file.close();
  119.  
  120.         //zapis zbioru pesymistycznego
  121.     ReverseSort(tab,tab_size);
  122.     file_name[0]='p';
  123.     file.open(file_name);
  124.     for(int i=0;i<tab_size;i++){
  125.         file<<tab[i]<<endl;
  126.     }
  127.     file.close();
  128.     delete[] tab;
  129.     }
  130.  
  131.  
  132.     delete[] file_name;
  133.  
  134. }
  135. int* Sort( int tab[], int size ) //sortuje liczby, potrzebna by stworzyc optymistyczny- czesciowo uporzadkowany zbior
  136. {
  137.     int temp, j;
  138.     for( int i = 1; i < size; i++ )
  139.     {
  140.         temp = tab[ i ];
  141.         for( j = i - 1; j >= 0 && tab[ j ] > temp; j-- )
  142.              tab[ j + 1 ] = tab[ j ];
  143.         tab[ j + 1 ] = temp;
  144.     }
  145.     return tab;
  146. }
  147.  
  148. int* ReverseSort ( int tab[], int size ) //generuje zbior pesymistyczny, liczby posortowane odwrotnie
  149. {
  150.     int temp, j;
  151.     for( int i = 1; i < size; i++ )
  152.     {
  153.         temp = tab[ i ];
  154.         for( j = i - 1; j >= 0 && tab[ j ] < temp; j-- )
  155.              tab[ j + 1 ] = tab[ j ];
  156.         tab[ j + 1 ] = temp;
  157.     }
  158.     return tab;
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement