Petro_zzz

30_09_pointers_struct

Sep 30th, 2022
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.45 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. double hipotenuse(double a, double b) {
  5.     return sqrt(a * a + b * b);
  6. }
  7.  
  8. void test_pointers() {
  9.     int val = 12;
  10.     cout << val << " " << &val << endl;
  11.     int* pval = &val;
  12.     cout << pval << " " << *pval << endl;
  13.  
  14.     const int val2 = 13;
  15.     //val2++; // константы менять нельзя
  16.     cout << val2 << " " << &val2 << endl;
  17.     //int* const pval2 = &val2;
  18.     //cout << "before: " << pval2 << " " << *pval2 << endl;
  19.     //pval2 = &val;
  20.     val += 100; // через эту переменную можно менять
  21.     //*pval2 += 100; // через эту нельзя - она const
  22.     //cout << "arfter: " << pval2 << " " << *pval2 << endl;
  23. }
  24.  
  25. void my_swap0(int a, int b) {
  26.     cout << "in my swap0 before" << endl;
  27.     cout << "a = " << a << " " << "b =" << b << endl;
  28.     cout << "a = " << &a << " " << "b =" << &b << endl;
  29.     int tmp = a;
  30.     a = b;
  31.     b = tmp;
  32.     cout << "in my swap0 after" << endl;
  33.     cout << "a = " << a << " " << "b =" << b << endl;
  34.     cout << "a = " << &a << " " << "b =" << &b << endl;
  35. }
  36.  
  37.  
  38. void my_swap1(int *a, int *b) {
  39.     cout << "in my swap0 before" << endl;
  40.     cout << "a = " << *a << " " << "b =" << *b << endl;
  41.     cout << "a = " << a << " " << "b =" << b << endl;
  42.     int tmp = *a;
  43.     *a = *b;
  44.     *b = tmp;
  45.     cout << "in my swap0 after" << endl;
  46.     cout << "a = " << *a << " " << "b =" << *b << endl;
  47.     cout << "a = " << a << " " << "b =" << b << endl;
  48. }
  49.  
  50. void my_swap2(int &a, int &b) {
  51.     cout << "in my swap0 before" << endl;
  52.     cout << "a = " << a << " " << "b =" << b << endl;
  53.     cout << "a = " << &a << " " << "b =" << &b << endl;
  54.     int tmp = a;
  55.     a = b;
  56.     b = tmp;
  57.     cout << "in my swap0 after" << endl;
  58.     cout << "a = " << a << " " << "b =" << b << endl;
  59.     cout << "a = " << &a << " " << "b =" << &b << endl;
  60. }
  61.  
  62. int* create_arr(int n) {
  63.     return new int[n] {};
  64. }
  65.  
  66. int my_arr[]{ 1,2,3,4,5 };
  67.  
  68. int& find(int val){
  69.     for (int k = 0; k < 5; k++) {
  70.         if (my_arr[k] == val) {
  71.             return my_arr[k];
  72.         }
  73.     }
  74. }
  75.  
  76. void test_swap() {
  77.     //cout << hipotenuse(3, 4) << endl;
  78.     //test_pointers();
  79.     /*
  80.     int a = 3;
  81.     int b = 4;
  82.     cout << "in main before" << endl;
  83.     cout << "a = " << a << " " << "b =" << b << endl;
  84.     cout << "a = " << &a << " " << "b =" << &b << endl;
  85.     //my_swap1(&a, &b);
  86.     my_swap2(a, b);
  87.     cout << "in main after" << endl;
  88.     cout << "a = " << a << " " << "b =" << b << endl;
  89.     cout << "a = " << &a << " " << "b =" << &b << endl;
  90.     int* arr = create_arr(5);
  91.     for(int k =0; k < 5; ++k)
  92.         cout << arr[k] << endl;
  93. */
  94. }
  95.  
  96. void test_fun_pointers() {
  97.     for (int k = 0; k < 5; ++k)
  98.         cout << my_arr[k] << endl;
  99.     find(2) = 15;
  100.     for (int k = 0; k < 5; ++k)
  101.         cout << my_arr[k] << endl;
  102.     cout << endl;
  103.     cout << *(my_arr + 1) << my_arr[1] << endl;
  104.     cout << *create_arr << endl;
  105. }
  106.  
  107. int sum(int a, int b) {
  108.     cout << "Sum: ";
  109.     return a + b;
  110. }
  111.  
  112. int mul(int a, int b) {
  113.     cout << "Mult: ";
  114.     return a * b;
  115. }
  116.  
  117. void function_pointers() {
  118.     cout << sum << endl;
  119.     cout << mul << endl;
  120.     int (*pfun)(int, int) = sum; // это указатель на функцию
  121.     cout << pfun(3, 4) << endl;
  122.     pfun = mul;
  123.     cout << pfun(3, 4) << endl;
  124.     int((*pfun2[2])(int, int)) { mul, sum };
  125.     cout << "Enter 0 - for mult calc" << endl;
  126.     cout << "Enter 1 - for sum calc" << endl;
  127.     int c;
  128.     cin >> c;
  129.     cout << pfun2[c](13, 15) << endl;
  130. }
  131.  
  132. struct Point {
  133.     int x = 0;
  134.     int y = 0;
  135. };
  136.  
  137. struct Line {
  138.     Point pt1;
  139.     Point pt2;
  140. };
  141.  
  142. struct Man {
  143.     const char* name;
  144.     int age;
  145.     const char* workplace;
  146.     int salary;
  147.  
  148.     void show() {
  149.         cout << name << "   "
  150.             << age << "   "
  151.             << workplace << "   "
  152.             << salary << endl;
  153.     }
  154. };
  155.  
  156. void show(Man& man) {
  157.     cout << man.name << "   "
  158.          << man.age << "   "
  159.          << man.workplace << "   "
  160.          << man.salary << endl;
  161. }
  162.  
  163. void show(Point &pt) {
  164.     cout << "(" << pt.x << ", " << pt.y << ")" << endl;
  165. }
  166.  
  167. double distance(Point& pt1, Point& pt2) {
  168.     int dx = (pt1.x - pt2.x);
  169.     int dy = (pt1.y - pt2.y);
  170.     return sqrt(dx * dx + dy * dy);
  171. }
  172.  
  173. void main() {  
  174.     Point pt1{0, 0};
  175.     Point pt2{3, 4};
  176.     //pt.x = 10;
  177.     //pt.y = 11;
  178.     Point arr_points[]{ pt1, pt2 };
  179.     show(pt1);
  180.     show(pt2);
  181.     cout << distance(pt1, pt2) << endl;
  182.     cout << distance(arr_points[0], arr_points[1]) << endl;
  183.  
  184.  
  185.     Man man{ "Egor", 42, "Agent", 95000 };
  186.     show(man);
  187.     man.salary = 87000;
  188.     show(man);
  189.     man.show();
  190.  
  191.     struct Point2 {
  192.         double d1;
  193.         int d2;
  194.     };
  195.  
  196.     Point2 pt3;
  197.  
  198.     cout << sizeof(pt1) << " " << sizeof(pt1.x) << endl;
  199.     cout << sizeof(pt3) << " " << sizeof(pt3.d1) + sizeof(pt3.d2) << endl;
  200.  
  201.     pt1 > pt2;
  202. }
Advertisement
Add Comment
Please, Sign In to add comment