Advertisement
Mike_be

Just shit programm

Oct 28th, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.17 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <ctime>
  4. #include <stdlib.h>
  5. #include <vector>
  6. #include <string>
  7. #include <algorithm>
  8. #include <iomanip>
  9. #include "mikelib.h"
  10.  
  11. using namespace std;
  12.  
  13. vector<int> vec;
  14. vector<int> vec_custom_sort;
  15.  
  16. void vec_ran(int n, int x) //Randomly generates numbers and fills vector
  17. {
  18.     int i = 0;
  19.     if (x == 0) while (i < n) //Fills vector with absolutely random numbers
  20.     {
  21.         vec.push_back(rand());
  22.         vec_custom_sort.push_back(vec[i]);
  23.         i++;
  24.     }
  25.     else while (i < n)//Fills vector with random numbers with x interval
  26.     {
  27.         vec.push_back(rand() % x);
  28.         vec_custom_sort.push_back(vec[i]);
  29.         i++;
  30.     }
  31. }
  32.  
  33. void vec_print(int n) // Prints whole vector if it's smaller than 50 integers or prints first 50 integers if vector is longer
  34. {
  35.     int i = 0;
  36.     if (n <= 50)
  37.     {
  38.         while (i < n)
  39.         {
  40.             cout << vec[i] << ' ';
  41.             i++;
  42.         }
  43.     }
  44.     else
  45.     {
  46.         while (i < 50)
  47.         {
  48.             cout << vec[i] << ' ';
  49.             i++;
  50.         }
  51.     }
  52.     cout << endl;
  53. }
  54.  
  55. void vec_print_custom(int n) // Does the same thing as function before, but for other vector
  56. {
  57.     cout << setw(55) << "Sorted vector by custom function" << endl;
  58.     int i = 0;
  59.     if (n <= 50)
  60.     {
  61.         while (i < n)
  62.         {
  63.             cout << vec_custom_sort[i] << ' ';
  64.             i++;
  65.         }
  66.     }
  67.     else
  68.     {
  69.         while (i < 50)
  70.         {
  71.             cout << vec_custom_sort[i] << ' ';
  72.             i++;
  73.         }
  74.     }
  75.     cout << endl;
  76. }
  77.  
  78. void vec_type(int n) //Function for typing numbers into vector
  79. {
  80.     int i = 0, num;
  81.     while (i < n)
  82.     {
  83.         cout << "Type " << i+1 << "th element of vector: ";
  84.         cin >> num;
  85.         vec.push_back(num);
  86.         vec_custom_sort.push_back(num);
  87.         i++;
  88.     }
  89. }
  90.  
  91. int check(string temp) // Check for input
  92. {
  93.     if (temp == "Yes" || temp == "yes" || temp == "y") return 1;
  94.     else if (temp == "No" || temp == "no" || temp == "n") return 2;
  95.     else return 0;
  96. }
  97.  
  98. void choice(int n, string temp, int x) //Compilation of several functions in one function where it also decides
  99. {                                      //the path depending on output from previous function
  100.     if (check(temp) == 1)
  101.     {
  102.         vec_ran(n, x);
  103.         vec_print(n);
  104.         cout << endl;
  105.     }
  106.     else if (check(temp) == 2)
  107.     {
  108.         vec_type(n);
  109.         vec_print(n);
  110.         cout << endl;
  111.     }
  112. }
  113.  
  114. void ShellsSort(unsigned N)//Shell sorting algorithm
  115. {
  116.     unsigned i, j, d;
  117.     int t;
  118.     for (d = N / 2; d > 0; d /= 2)
  119.         for (i = d; i < N; i++)
  120.         {
  121.             t = vec_custom_sort[i];
  122.             for (j = i; j >= d; j -= d)
  123.             {
  124.                 if (t < vec_custom_sort[j - d])
  125.                     vec_custom_sort[j] = vec_custom_sort[j - d];
  126.                 else
  127.                     break;
  128.             }
  129.             vec_custom_sort[j] = t;
  130.         }
  131. }
  132.  
  133. int main()
  134. {
  135.  
  136.     cout << "Hello! This is program for sorting vectors.\n";//Welcome message
  137.     string temp;
  138.     cout << "Do you want to generate vector with random numbers? (If no, you will need to write in manually) [Yes/No]: ";
  139.     do //Cycle for asking user to decide future path, has input check because this step is important
  140.     {
  141.         cin >> temp;
  142.         if (check(temp) == 0) cout << "Type [Yes] or [No] without brackets: ";
  143.     } while (check(temp) == 0);
  144.     int n = read_uint("Type the amount of variable in vector");
  145.     //cout << "Type the amount of variable in vector: ";
  146.     //cin >> n;
  147.     int x = 0;
  148.     if (check(temp) == 1) {//Small "if" for additional input if user said "Yes" in previous step
  149.         //cout << "Type the amount of unique integers(0 if you don't need it): ";
  150.         x = read_uint("Type the amount of unique integers(0 if you don't need it)");
  151.         cout << endl << setw(70) << "Default vector (or a part of it if it's bigger than 50 integers)" << endl;
  152.     }
  153.     choice(n, temp, x);
  154.     double t = clock(); //Clock function from ctime, gets current clock time and logs it into "t" integer
  155.     sort(vec.begin(), vec.end());
  156.     t = (clock() - (double)t)/CLOCKS_PER_SEC; //Gets current time minus previous logged time
  157.     cout << setw(55) << "Sorted vector by library function" << endl;
  158.     vec_print(n);
  159.     cout << "Computer sorted vector in " << t << " seconds." << endl << endl;
  160.     double t_custom = clock();
  161.     ShellsSort(n);
  162.     t_custom = (clock() - t_custom)/CLOCKS_PER_SEC;
  163.     vec_print_custom(n);
  164.     cout << "Computer sorted vector in " << t_custom << " seconds." << endl << endl;
  165.     cout << "Time difference between default sort function and custom is: " << t_custom - t << " seconds" << endl;
  166.     system("pause");
  167.     return 0;
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement