Petro_zzz

lesson322_23

Oct 13th, 2023
842
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.07 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. inline void my_swap(int& a, int& b)  {
  6.     int tmp = a;
  7.     a = b;
  8.     b = tmp;
  9. }
  10.  
  11. void show_arr(int size, int* arr) {
  12.     for (int k = 0; k < size; k++)
  13.         cout << arr[k] << " ";
  14.     cout << endl;
  15. }
  16.  
  17. void work_1pp() {
  18.     int arr[]{ 1,2,3,4,5,6,7,8 };
  19.     show_arr(size(arr), arr);
  20.     for (int k = 0; k < size(arr); k += 2) {
  21.         my_swap(arr[k], arr[k + 1]);
  22.     }
  23.     show_arr(size(arr), arr);
  24. }
  25.  
  26. int bad_sum(int a, int b) {
  27.     int sum = 0;
  28.     if (a > b)
  29.         my_swap(a, b);
  30.     while (a <= b)
  31.         sum += a++;
  32.     return sum;
  33. }
  34.  
  35. int sum1(int a, int b) {
  36.     a = a + b;
  37.     b = 12 + a;
  38.     return a + b;
  39. }
  40.  
  41. int sum2(int* pa, int* pb) {
  42.     return *pa + *pb;
  43. }
  44.  
  45.  
  46. void pointer_vs_const() {
  47.     const int cx = 56;
  48.     cout << cx << endl;
  49.     int x = cx;
  50.     x = x + 10;
  51.     int s = x + (double)cx;
  52.     cout << sum1(x, cx) << endl;
  53.        
  54.     const int* pCx;
  55.     pCx = &cx;
  56.     //*pCx = 45;
  57.     x = *pCx;
  58.    
  59.     const int cy = 16;
  60.     pCx = &cy;
  61.    
  62.     int r{ 7 };
  63.     cout << r << endl;
  64.  
  65.     const int arr[3]{ 3,4,5 };
  66.  
  67.     const int* pa = arr;
  68.     cout << *pa << endl;
  69.     pa += 2; // я изменил адрес, а не значение
  70.     cout << *pa << endl;
  71.     //arr[1] = 3;
  72.  
  73.     int w = 14;
  74.     // сделали неизменным адрес cpw
  75.     int* const cpw = &w;
  76.     w = 15;
  77.     *cpw = 16; 
  78.     //cpw += 1;
  79.     //cpw = &x;
  80.     const int rr = 17;
  81.     const int* const super_const = &rr;
  82.  
  83.     //cout << sum2(&x, &cx) << endl;
  84.     //cx = 14;
  85.     //my_swap(cx, x);
  86. }
  87.  
  88. inline int sum(int a, int b) {
  89.     return a + b;
  90. }
  91.  
  92. inline int sub(int a, int b) {
  93.     return a - b;
  94. }
  95.  
  96. inline int mul(int a, int b) {
  97.     return a * b;
  98. }
  99.  
  100. inline int divide(int a, int b) {
  101.     return a / b;
  102. }
  103.  
  104. void test_calc() {
  105.     int a, b;
  106.     char op;
  107.     cin >> a >> op >> b;
  108.     int (*pOp)(int, int);
  109.     pOp = sum;
  110.     switch (op) {
  111.         case '+': pOp = sum;  break;
  112.         case '-': pOp = sub;  break;
  113.         case '*': pOp = mul;  break;
  114.         case '/': pOp = divide;  break;
  115.         default:break;
  116.     }  
  117.     cout << " = " << pOp(a, b) << endl;
  118. }
  119.  
  120.  
  121. int main() {
  122.     //work_1pp();
  123.     //cout << bad_sum(3, 7) << " " << bad_sum(7, 3) << endl;
  124.     //pointer_vs_const();
  125.     test_calc();
  126. }
  127.  
  128.  
Advertisement
Add Comment
Please, Sign In to add comment