Advertisement
Guest User

Untitled

a guest
Dec 7th, 2016
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.10 KB | None | 0 0
  1. // Example program
  2. #include <iostream>
  3. #include <string>
  4. #include <vector>
  5.  
  6. using namespace std;
  7.  
  8. //struct MyVector for Task 10
  9. template <typename T>
  10. struct MyVector
  11. {
  12. vector<T> v1 = {};
  13. };
  14. //pair fot test in Task 10
  15. struct Pair
  16. {
  17. int x = 0;
  18. int y = 0;
  19. };
  20. //Overloading operator < and << for struct Pair
  21. bool operator<(const Pair& left, const Pair& right) {
  22. return (left.x + left.y) < (right.x + right.y);
  23. }
  24. ostream& operator<<(ostream& o, const Pair& p)
  25. {
  26. o << p.x << " " << p.y << " | " << endl;
  27. return o;
  28. }
  29. //value of x makes no sense because it is always takse y value
  30. void task1(bool x, bool y)
  31. {
  32. cout <<"Case: x = " << x << "| y = " << y << endl;
  33. cout <<"Just y result " << y << endl;
  34. cout <<"x = y ? x : y result " << (x = y ? x : y) << endl;
  35. }
  36. //in case that next element always is prev element ^ 2
  37. double task2 (int x, double n)
  38. {
  39. double res = x;
  40. double prev = x;
  41. for(int i = 0; i < n; i++)
  42. {
  43. prev *= prev;
  44. res += prev;
  45. }
  46. return res;
  47. }
  48. //recorsive function that implements i + 1/((i+1)/...)
  49. double task3(int n, int i)
  50. {
  51. i < 1? i = 1 : false;
  52. if(i <= n)
  53. {
  54. return i + 1/(task3(n, i + 1));
  55. }
  56. else
  57. {
  58. return n + 1;
  59. }
  60. }
  61. //calculate next element while not n
  62. int task4(int n, int a1, int a2, int a3)
  63. {
  64. int res = 0;
  65. for(int i = 3; i < n; i++ ) {
  66. res = -2 * a3 + a2 + 2 * a1;
  67. a1 = a2;
  68. a2 = a3;
  69. a3 = res;
  70.  
  71. }
  72. return res;
  73. }
  74. //fast pow for task 5
  75. double fastPow(int x, int n)
  76. {
  77. double result = 1;
  78.  
  79. for(; n >= 1; n/=2)
  80. if(n%2 == 1)
  81. {
  82. result*=x;
  83. x*=x;
  84. }
  85. else x*=x;
  86.  
  87. return result;
  88. }
  89. // calculate n-th element with fastPow speed
  90. int task5(int n)
  91. {
  92. int y;
  93. n % 2 == 0 ? y = 1 : y = -1;
  94. return fastPow(-2, n) + y + 1;
  95. }
  96. // recursive calculation of n-th element
  97. int task6(int n, int a1, int a2, int a3)
  98. {
  99. if(n <= 3) return a3;
  100. return task6(n-1, a2, a3, -2*a3 + a2 + 2 *a1);
  101. }
  102. //multiply i-th element on x
  103. void *task8(int a[], int n, int x)
  104. {
  105. for(int i = 0; i < n; i++)
  106. a[i] *= x;
  107. }
  108. //sum of i-th elemnts form a[] and b[]
  109. void *task9(int a[], int n, int b[], int m)
  110. {
  111. n < m ? n : n = m;
  112. for(int i = 0; i < n; i++)
  113. a[i] += b[i];
  114. }
  115. //function to print array
  116. void outArray(int a[], int n)
  117. {
  118. for(int i = 0; i < n; i++)
  119. cout << a[i] << " ";
  120. cout << endl;
  121. }
  122. //bubl sort
  123. template <typename T>
  124. void sort(MyVector<T>& v)
  125. {
  126. for(int i = 0; i < v.v1.size(); i++)
  127. for(int j = 0; j < v.v1.size(); j++)
  128. if(v.v1[i] < v.v1[j])
  129. {
  130. T temp = v.v1[i];
  131. v.v1[i] = v.v1[j];
  132. v.v1[j] = temp;
  133. }
  134.  
  135. }
  136. //out vector
  137. template <typename T>
  138. void outVector(MyVector<T> mv)
  139. {
  140. for(int i = 0; i < mv.v1.size(); i++)
  141. cout << mv.v1[i] << " ";
  142. cout << endl;
  143. }
  144. int main()
  145. {
  146. cout << "Task1" << endl;
  147. task1(true, false);
  148. task1(false, true);
  149. task1(false, false);
  150. task1(true, true);
  151. cout << "Task3: " << task3(3, 10) << endl;
  152. cout << "Task3: " << task3(1, 1) << endl;
  153. cout << "Task4: " << task4(4, -2, 6, -8) << endl;
  154. cout << "Task5: " << task5(4) << endl;
  155. cout <<"Task6: " << task6(4, -2, 6, -8) << endl;
  156. cout << "Task8: " << endl;
  157. int test[] = {3, 4, 5};
  158. task8(test, 3, 2); outArray(test, 3);
  159. cout << "Task9: " << endl;
  160. int a[] = {1, 2, 3}, b[] = {4, 5, 6};
  161. task9(a, 3, b, 3); outArray(a, 3);
  162. cout << "Task10: " << endl;
  163. MyVector<int> mv1;
  164. MyVector<string> mv2;
  165. MyVector<Pair> mv3;
  166. cout << "Integer test: " << endl;
  167. mv1.v1 = {7, 5, 16, 8};
  168. sort(mv1);
  169. outVector(mv1);
  170. cout << "Pair test: " << endl;
  171. mv2.v1 = {"ab", "bb", "ba", "aa"};
  172. sort(mv2);
  173. outVector(mv2);
  174. cout << "Pair test: " << endl;
  175. Pair a1, b1, c1, d1;
  176. a1.x = 2; a1.y = 2;
  177. b1.x = 2; b1.y = 3;
  178. c1.x = 3; c1.y = 4;
  179. d1.x = 5; d1.y = 6;
  180. mv3.v1 = {b1, a1, d1, c1};
  181. sort(mv3);
  182. outVector(mv3);
  183. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement