valenki13

lesson_321_22

Oct 18th, 2023
924
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.47 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. void test_myswap();
  6. void test_myswap_by_pc();
  7. void myswap(int& x, int& y);
  8. void test_checsign();
  9. bool checsign(int, int*, int&, int&);
  10.  
  11. int sum(int a, int b) {
  12.     return a + b;
  13. }
  14.  
  15. void pointer_vs_constant() {
  16.     /*
  17.     c - const
  18.     p - pointer
  19.     r - reference
  20.     */
  21.  
  22.     int x = 17;
  23.     int y = 22;
  24.     x = x + 12;
  25.     cout << x << endl;
  26.  
  27.     const int cx = x;
  28.     cout << cx << endl;
  29.     x = x + 1000;
  30.     cout << cx << endl;
  31.     cout << x << endl;
  32.     cout << "sum(x, cx) : " << sum(x, cx) << endl;
  33.  
  34.     const int* pcx = &cx;
  35.     cout << "*pcx: " << *pcx << endl;
  36.  
  37.     const int cArr[]{5,6,7,8};
  38.     const int* pca = cArr;
  39.     //cArr[0] = 6;
  40.     //*pca = 6;
  41.     cout << "Const1 " << *pca << endl;
  42.     pca = pca + 1;
  43.     cout << "Const2 " << *pca << endl;
  44.     pca = &x; // это замороченный момент
  45.    
  46.     const int* const cpcx = &(cArr[2]);
  47.     //cpcx = cpcx + 1;
  48.     //*cpcx = 888;
  49.  
  50.     int z = 33;
  51.     int* const cpz = &z;
  52.     cout << z << " " << *cpz << endl;
  53.     //cpz++;
  54.     *cpz = 44;
  55.     cout << z << " " << *cpz << endl;
  56.     z = 55;
  57.     cout << z << " " << *cpz << endl;
  58.  
  59.     //*pcx = 14;     - даже через разъименование нельзя
  60.     //myswap(x, cx); - ссылка привела бы к изменению - так нельзя
  61.     //cx++;          - константы нельзя менять
  62.  
  63. }
  64.  
  65. int main() {
  66.     //test_checsign();
  67.     //test_myswap();
  68.     //test_myswap_by_pc();
  69.     pointer_vs_constant();
  70.     return 0;
  71. }
  72.  
  73.  
  74. /// <summary>
  75. /// Count numbers of positive and negative elements in array
  76. /// </summary>
  77. /// <param name="size">A integer size of array</param>
  78. /// <param name="arr">A array of int</param>
  79. /// <param name="n_neg">Nuber of negative</param>
  80. /// <param name="n_pos">Number of positive</param>
  81. /// <returns>Is error in the function</returns>
  82. bool checsign(int size,   // size
  83.     int* arr,   //
  84.     int& n_neg, //
  85.     int& n_pos  //
  86. ) {
  87.     n_neg = 0;
  88.     n_pos = 0;
  89.     for (int k = 0; k < size; k++) {
  90.         if (arr[k] < 0) n_neg++;
  91.         else if (arr[k] > 0) n_pos++;
  92.     }
  93.     return false;
  94. }
  95.  
  96. void test_checsign() {
  97.     int arr[]{ 1, 3, 0, -2, 1,-1, 12, 7 };
  98.     int n_neg = 0;
  99.     int n_pos = 0;
  100.     bool is_err = checsign(size(arr), arr, n_neg, n_pos);
  101.     if (is_err) {
  102.         cout << "BAD error" << endl;
  103.     }
  104.     if (n_pos != 5) {
  105.         cout << "BAD value positive wrong" << endl;
  106.     }
  107.     if (n_neg != 2) {
  108.         cout << "BAD value negative wrong" << endl;
  109.     }
  110.     if (!is_err && (n_pos == 5) && (n_neg == 2)) {
  111.         cout << "OK";
  112.     }
  113. }
  114.  
  115. void myswap4(int x, int y) {
  116.     // BAD
  117.     int temp = x;
  118.     x = y;
  119.     y = temp;
  120.     cout << "In: " << x << " " << y << endl;
  121. }
  122.  
  123. void myswap3(int* px, int* py) {
  124.     // BAD
  125.     int* temp = px;
  126.     px = py;
  127.     py = temp;
  128.     cout << "In: " << *px << " " << *py << endl;
  129. }
  130.  
  131. void myswap2(int* px, int* py) {
  132.     // SO SO
  133.     int temp = *px;
  134.     *px = *py;
  135.     *py = temp;
  136.     cout << "In: " << *px << " " << *py << endl;
  137. }
  138.  
  139. void myswap(int& x, int& y) {
  140.     int temp = x;
  141.     x = y;
  142.     y = temp;
  143.     cout << "In: " << x << " " << y << endl;
  144. }
  145.  
  146. void myswap_alg(int& x, int& y) {
  147.     x = x + y;
  148.     y = x - y;
  149.     x = x - y;
  150. }
  151.  
  152. void test_myswap() {
  153.     int x = 4;
  154.     int y = 5;
  155.     swap(x, y);
  156.  
  157.     cout << x << " " << y << endl;
  158.     // out: 4 5
  159.     myswap(x, y);
  160.     cout << "Out: " << x << " " << y << endl;
  161.     // out: 5 4
  162. }
  163.  
  164. void test_myswap_by_pc() {
  165.     int x = 4;
  166.     int y = 5;
  167.     cout << "Part 1: ";
  168.     if (x == 4 && y == 5)
  169.         cout << "OK" << endl;
  170.     else
  171.         cout << "BAD" << endl;
  172.     myswap(x, y);
  173.     cout << "Part 2: ";
  174.     if (x == 5 && y == 4)
  175.         cout << "OK" << endl;
  176.     else
  177.         cout << "BAD" << endl;
  178. }
Advertisement
Add Comment
Please, Sign In to add comment