Petro_zzz

10_12

Oct 14th, 2022
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.05 KB | None | 0 0
  1. #include <iostream>;
  2.  
  3. using namespace std;
  4.  
  5. struct Date {
  6.     int day;
  7.     int month;
  8.     int year;
  9.  
  10.     void show_in() {
  11.         cout << day << ".";
  12.         if (month < 10)
  13.             cout << 0 << month << ".";
  14.         else
  15.             cout << month << ".";
  16.         cout << year;
  17.     }
  18.  
  19.     void show2() {
  20.         cout << day << "-";
  21.         cout << convert_month(month) << "-";
  22.         cout << year;
  23.     }
  24.  
  25.  
  26.     string convert_month(int num) {
  27.         string str[] = { "Jan.", "Fev.",
  28.                        "Mar.", "Apr.",
  29.                        "May", "Jun.",
  30.                        "Jul.", "Aug.",
  31.                        "Sep.", "Oct.",
  32.                        "Nov.", "Dec." };
  33.         if (num > 0 && num < 12)
  34.             return str[num - 1];
  35.         else
  36.             return "Error month.";
  37.     }
  38. };
  39.  
  40. void show(const Date& date) {  
  41.     cout << date.day << ".";
  42.     if(date.month < 10)
  43.         cout << 0 << date.month << ".";
  44.     else
  45.         cout << date.month << ".";
  46.     cout << date.year;
  47. }
  48.  
  49.  
  50. void mem2DdinamicArray() {
  51.     int szarr[]{3, 4 , 6, 19, 1 };
  52.     int** pArr2D = new int* [5];
  53.     for (int k = 0; k < 5; ++k) {
  54.         pArr2D[k] = new int[szarr[k]];
  55.     }
  56.     cout << "sz(0):" << _msize(pArr2D[0]) / sizeof(pArr2D[0][0]) << endl;
  57.     cout << "sz   :" << _msize(pArr2D) / sizeof(pArr2D[0]);
  58.  
  59.     pArr2D[2][3] = 16; // тут какие-то операции
  60.     for (int k = 0; k < 5; k++) {
  61.         delete[] pArr2D[k];
  62.     }
  63.     delete[] pArr2D;
  64. }
  65.  
  66. void test_structDate() {
  67.     Date date /*{12, 10, 2022}*/;
  68.  
  69.     date.day = 12;
  70.     date.month = 2;
  71.     date.year = 2022;
  72.  
  73.     cin >> date.day;
  74.     cin >> date.month;
  75.     cin >> date.year;
  76.  
  77.     int& x = date.day;
  78.     x = 17;
  79.  
  80.     show(date); cout << endl;
  81.     date.show_in();
  82.     cout << endl;
  83.     date.show2();
  84. }
  85.  
  86. int* create(int sz) {
  87.     // int* x = new int[sz];
  88.     return new int[sz]; //x;
  89. }
  90.  
  91. void remove(int* &arr) {
  92.     delete[] arr;
  93. }
  94.  
  95. void create(int*& arr, int sz) {
  96.     if (arr) remove(arr); // если память выделена, то чистим память
  97.     arr = new int[sz];
  98. }
  99.  
  100. void main() {
  101.     //mem2DdinamicArray();
  102.     int* arr = create(7);// делаем ф-и для работы с памятью
  103.     remove(arr);
  104.     create(arr, 17); // другая ф-я для создания массива в куче
  105.     remove(arr);
  106. }
  107.  
  108.  
Advertisement
Add Comment
Please, Sign In to add comment