Advertisement
edensheiko

Tragil 1,2 c++ HIT

Feb 26th, 2021
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.40 KB | None | 0 0
  1. // just c++ small traning
  2. #include <iostream>
  3. #include <iomanip> //not needed
  4. #include <assert.h>
  5.  
  6. using namespace std;
  7.  
  8. void swapPoint(int*a, int*b);
  9. void swapRef(int & a, int & b);
  10.  
  11. struct Time
  12. {
  13.     int s;
  14.     int m;
  15.     int h;
  16. };
  17.  
  18.  Time clon(const Time& n);
  19.  
  20.  
  21. void Time_modef(Time& ref) // copy by ref (c++)
  22. {
  23.     int sec_to_min = (ref.s / 60);
  24.     int min_to_hour = (ref.m + sec_to_min) / 60;
  25.  
  26.     ref.s = ref.s % 60;
  27.     ref.m = (ref.m + sec_to_min) % 60;
  28.     ref.h = (ref.h + min_to_hour) % 24;
  29.     /*print*/
  30.     cout << ref.s << "," << ref.m << "," << ref.h << endl;
  31.  
  32. }
  33.  
  34. void Time_modef2(Time& ref)
  35. {
  36.     int sec_to_min = (ref.s / 60);
  37.     int min_to_hour = (ref.m + sec_to_min) / 60;
  38.  
  39.     ref.s = ref.s % 60;
  40.     ref.m = (ref.m + sec_to_min) % 60;
  41.     ref.h = (ref.h + min_to_hour) % 24;
  42.  
  43. }
  44.  
  45. Time* alloc_time(int ptr2)
  46. {
  47.     int n;
  48.     cout << "Enter how many values:" << endl;
  49.     cin >> n;
  50.     Time* array_of_structs = new Time[n];
  51.     assert(array_of_structs);
  52.     for (int i = 0; i < n; i++)
  53.     {
  54.         cout << "enter hour,min,sec:";
  55.         cin >> array_of_structs[i].h >> array_of_structs[i].m >> array_of_structs[i].s;
  56.         Time_modef2(array_of_structs[i]);
  57.         ptr2 = n;
  58.         //return array_of_structs;
  59.     }
  60.     for (int i = 0; i < n; i++)
  61.     {
  62.         cout << array_of_structs[i].h << "," << array_of_structs[i].m << "," << array_of_structs[i].s << endl;
  63.     }
  64.     return array_of_structs;
  65. }
  66.  
  67. void main()
  68. {
  69.     int temp = NULL;
  70.     Time v1;
  71.     v1.s = 70;
  72.     v1.m = 128;
  73.     v1.h = 3;
  74.     Time_modef(v1);
  75.     Time* v2 = alloc_time(temp);
  76.     /* new targil 2*/
  77.     int a = 5;
  78.     int b = 10;
  79.     cout << "before:" << a << "," << b << endl;
  80.     swapPoint(&a, &b);
  81.     cout << "after:" << a << "," << b << endl;
  82.     int x = 5;
  83.     int y = 10;
  84.     cout << "before:" << x << "," << y << endl;
  85.     swapRef(x, y);
  86.     cout << "after:" << x << "," << y << endl;
  87.     /*targil 2_ver b */
  88.     Time v55;
  89.     v55.h = 22;
  90.     v55.m = 77;
  91.     v55.s = 22;
  92.     clon(v1);
  93.     clon(v55);
  94.     cout << "time clons:v1 & (v55 no time manipulation)" << endl;
  95.     cout << v55.h << ":" << v55.m << ":" << v55.s << endl;
  96.     cout << v1.h << ":" << v1.m << ":" << v1.s << endl;
  97.     Time_modef(v55);
  98.     cout << "time clons: (v55 with time manipulation)" << endl;
  99.     cout << v55.h << ":" << v55.m << ":" << v55.s << endl;
  100. }
  101.  
  102. void swapPoint(int* a, int* b)
  103. {
  104.     int temp = *a;
  105.     *a = *b;
  106.     *b = temp;
  107.  
  108. }
  109.  
  110. void swapRef(int& a, int& b)
  111. {
  112.     int temp = a;
  113.     a = b;
  114.     b = temp;
  115.  
  116. }
  117.  
  118. Time clon(const Time& n)
  119. {
  120.     Time temp_clone;
  121.     temp_clone = n;
  122.     return n;
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement