Guest User

Untitled

a guest
Dec 18th, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.55 KB | None | 0 0
  1. //
  2. // main.cpp
  3. // Template_Example
  4. //
  5. // Created by Lado on 15/12/17.
  6. // Copyright © 2017 Lado. All rights reserved.
  7. //
  8.  
  9. #include <iostream>
  10. #include <cstddef>
  11. #include <utility>
  12. #include <cstdlib>
  13. #include <vector>
  14. #include <functional>
  15. #include <iterator>
  16. #include <algorithm> // std::copy
  17. #include <memory>
  18. #include <random>
  19.  
  20. using namespace std;
  21.  
  22. template<typename F>
  23. class data
  24. {
  25. public:
  26. inline F dataa(F x)
  27. {
  28. F result;
  29. result = x;
  30. return result;
  31. }
  32. inline F datas(int &size)
  33. {
  34. int n = 10;
  35. size = n;
  36. F *array = new F[size];
  37. //TODO:using library random
  38. random_device rd; //Will be used to obtain a seed for the random number engine
  39. mt19937 gen(rd()); //Standard mersenne_twister_engine seeded with rd()
  40. uniform_int_distribution<> dis(1, 40);
  41. for(int i = 0; i < size; ++i)
  42. {
  43. //rand numbers decimal
  44. //array[i] = rand()%100+1;
  45. array[i] = dis(gen);
  46. }
  47. return *array;
  48. }
  49. };
  50.  
  51. int GreatestCommonDivisor(int m,int n) //Наибольший общий делитель
  52. {
  53. int r;
  54. do
  55. {
  56. r = m % n;
  57. m = n;
  58. n = r;
  59. } while ( r != 0 );
  60. return m;
  61. }
  62.  
  63. template<class T1, class T2>
  64. class Fraction{
  65. private:
  66. T1 fenzi; T2 fenmu; //чеслитель и знаминатель
  67. public:
  68. Fraction(){}
  69. ~Fraction(){}
  70. Fraction(T1 fenzi,T2 fenmu);
  71. double sever(T1 zi, T2 mu);
  72. double *maker();
  73. //friend ostream& operator<<(ostream& out,const Fraction& fra);//i/O must use a heavy overload
  74. //friend istream& operator>>(istream& in,const Fraction& fra);
  75. void setValue(T2 mu,T1 zi);
  76. };
  77. template<class T1, class T2>
  78. Fraction<T1, T2>::Fraction(T1 zi,T2 mu):fenzi(zi),fenmu(mu){
  79.  
  80. if(mu==0)
  81. {
  82. cout << "Error";
  83. exit(1);
  84. }
  85. }
  86.  
  87. template<class T1, class T2>
  88. void Fraction<T1, T2>::setValue(T2 mu,T1 zi){
  89. fenmu=mu;
  90. fenzi=zi;
  91. }
  92. template<class T1, class T2>
  93. double Fraction<T1, T2>::sever(T1 zi, T2 mu)
  94. {
  95. fenzi = zi;
  96. fenmu = mu;
  97. if(mu == 0)
  98. {
  99. cout << "Error";
  100. exit(1);
  101. }
  102.  
  103. //cout << fenzi << " " << fenmu;
  104. return (fenzi/fenmu);
  105. }
  106. template<class T1, class T2>
  107. double* Fraction<T1, T2>::maker()
  108. {
  109. int n = 10;
  110. //size_out = n;
  111. T1 *arr = new T1[n];
  112.  
  113. random_device rd;
  114. mt19937 gen(rd());
  115. uniform_real_distribution<> dis(1.0, 2.0);
  116.  
  117. for(int i = 0; i < n; ++i)
  118. {
  119.  
  120. arr[i] = dis(gen);
  121. //arr[i] = ((double)rand()) / ((double)RAND_MAX) * 10 + 0.5;
  122. }
  123. return arr;
  124. }
  125.  
  126. template<typename Type1, typename NN>
  127. class cocktailSort
  128. {
  129. public:
  130. void sort( Type1* arr, NN len)
  131. {
  132. bool notSorted = true;
  133. while( notSorted )
  134. {
  135. notSorted = false;
  136. for( int a = 0; a < len - 1; a++ )
  137. {
  138. if( arr[a] > arr[a + 1] )
  139. {
  140. sSwap( arr[a], arr[a + 1] );
  141. notSorted = true;
  142. }
  143. }
  144.  
  145. if( !notSorted ) break;
  146. notSorted = false;
  147.  
  148. for( int a = len - 1; a > 0; a-- )
  149. {
  150. if( arr[a - 1] > arr[a] )
  151. {
  152. sSwap( arr[a], arr[a - 1] );
  153. notSorted = true;
  154. }
  155. }
  156. }
  157. }
  158.  
  159. private:
  160. void sSwap( NN& a, NN& b )
  161. {
  162. NN t = a;
  163. a = b; b = t;
  164. }
  165. };
  166.  
  167. int main(int argc, const char * argv[]) {
  168. // insert code here...
  169. Fraction<double, double> f(2, 3);
  170. Fraction<double, double> n;
  171. //int size = 10;
  172. int sizer;
  173. //TODO:using smart pointer for arra 1
  174. double *arr1;
  175. arr1 = n.maker();
  176.  
  177. data <int> d;
  178. data <double> sw;
  179. //int sizer;
  180. //TODO:using smart pointer for arra 2
  181. double arr2;
  182. arr2 = sw.datas(sizer);
  183. double k = d.dataa(3);
  184. cout << k << endl;
  185.  
  186. //output array 1
  187. cout << "arr1n";
  188. for(int i=0; i < 10; i++)
  189. {
  190. cout << *(arr1+i) << " ";
  191. }
  192. //output array 2
  193. cout << "narr2n";
  194. for(int i = 0; i < 10; i++)
  195. {
  196. cout << (arr2+i) << " ";
  197. }
  198.  
  199. cocktailSort<double, double> cs;
  200.  
  201. //copy arr1 and arr2 into one general array
  202. double *mass = new double[20];
  203. for(int i =0; i < 10; i++)
  204. {
  205. mass[i] = *(arr1+i);
  206. mass[i + 10]=(arr2+i);
  207. }
  208. cout << "nKKKn";
  209. for(int i = 0; i < 20; i++)
  210. {
  211. cout << mass[i] << " ";
  212. }
  213.  
  214. //using sort function
  215. cs.sort(mass, 20);
  216.  
  217. cout << endl << "Sorted: " << endl << "========" << endl;
  218. for( int x = 0; x < 20; x ++ )
  219. {
  220. //for( int s = x; s < 20; s++ )
  221. cout << mass[x] << " ";
  222. }
  223. cout << endl;
  224.  
  225. return 0;
  226. }
Add Comment
Please, Sign In to add comment