hpnq

123

Jun 28th, 2024 (edited)
707
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.95 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <fstream>
  4. #include <string.h>
  5. #include <windows.h>
  6. using namespace std;
  7.  
  8.  
  9. struct Countries {
  10.     string name; //название страны
  11.     string capital; //столица
  12.     double population; //численность населения
  13.     double populationPercent; //процент населения от общемирового
  14.     double area; //площадь
  15. };
  16.  
  17.  
  18. struct node {
  19.     Countries countries;
  20.     node* left, * right;
  21. };
  22.  
  23.  
  24. //очередь
  25. struct qnode {
  26.     Countries info;
  27.     qnode* next;
  28. };
  29.  
  30.  
  31. //добавляем элемент в очередь
  32. void push(qnode** start, qnode** end, Countries& d) {
  33.     qnode* p = new qnode;
  34.     p->info = d;
  35.     p->next = nullptr;
  36.     if (*end) {
  37.         (*end)->next = p;
  38.     }
  39.     else {
  40.         (*start) = p;
  41.     }
  42.     *end = p;
  43. }
  44.  
  45. void inputcountries(Countries& countries)//ввод данных о студенте
  46. {
  47.     cout << "Info aboit one country" << endl;
  48. //    cout << "Введите название страны: " << endl;
  49.     cin >> countries.name;
  50. //    cout << "Введите столицу:" << endl;
  51.     cin >> countries.capital;
  52. //    cout << "Введите численность населения:" << endl;
  53.     cin >> countries.population;
  54. //    cout << "Введите процент населения от общемирового:" << endl;
  55.     cin >> countries.populationPercent;
  56. //    cout << "Введите площадь:" << endl;
  57.     cin >> countries.area;
  58.  
  59. }
  60.  
  61.  
  62. //ввод массива с клавиатуры
  63. void inputMasFromKeyboard(qnode*& s, qnode*& e, unsigned int& size)
  64. {
  65.     cout << "size" << endl;
  66.     cin >> size;
  67.     if (size == 0) return;
  68.     int i;
  69.     for (i = 0; i < size; i++)
  70.     {
  71.  
  72.         cout << "info:" << endl;
  73.         Countries cs;
  74.         inputcountries(cs);
  75.         push(&s, &e, cs);
  76. //        s = s->next;
  77.     }
  78. }
  79.  
  80. void printStudent(Countries countries)//печать данных о стране
  81. {
  82.     cout << left << setw(10) << countries.name << setw(15) << countries.capital << setw(10) << countries.population << setw(9)
  83.          << countries.populationPercent << setw(5) << countries.area << endl;;
  84.  
  85. }
  86.  
  87. void printMas(qnode* q1, unsigned int size)
  88. {
  89.     if (!size) return;
  90.     cout << "our country array:" << endl;
  91.     cout << left << setw(10) << "a" << setw(15) << "b" << setw(10) << "c" << setw(9)
  92.          << "d" << setw(5) << "e" << endl;
  93.     for (int i = 0; i < size; i++)
  94.     {
  95.         printStudent(q1->info);
  96.         q1 = q1->next;
  97.     }
  98. }
  99.  
  100.  
  101. void insert(node** p, Countries countries) // добавление узла в дерево поиска
  102. {
  103.     if (*p == NULL)
  104.     { //создаём новый узел дерева на найденном месте
  105.         *p = new node;
  106.         (*p)->countries = countries;
  107.         (*p)->left = NULL;
  108.         (*p)->right = NULL;
  109.     }
  110.     else
  111.     {
  112.         if (countries.name < (*p)->countries.name) insert(&(*p)->left, countries);
  113.         if (countries.name > (*p)->countries.name) insert(&(*p)->right, countries);
  114.     }
  115. }
  116.  
  117.  
  118. // распечатка дерева в симметричном порядке
  119. void printsim(const node* p)
  120. {
  121.     if (p != NULL)
  122.     {
  123.         printsim(p->left);
  124.         printStudent(p->countries);
  125.         printsim(p->right);
  126.     }
  127. }
  128. void clear(node** p) // очистка дерева
  129. {
  130.     if ((*p) != NULL)
  131.     {
  132.         clear(&(*p)->left);
  133.         clear(&(*p)->right);
  134.         delete* p;
  135.         *p = NULL;
  136.     }
  137. }
  138.  
  139.  
  140. //функция создания деревьев из массива
  141. void createTrees(qnode* q1, unsigned int size, node** root1, node** root2)
  142. {
  143.     if (!size) return;
  144.     for (int i = 0; i < size; i++)
  145.     {
  146.         if (q1->info.population > 100000000) insert(root1, q1->info);
  147.         else insert(root2, q1->info);
  148.         q1 = q1->next;
  149.     }
  150. }
  151.  
  152.  
  153.  
  154. // высота дерева
  155. int height(const node* p)
  156. {
  157.     if (p == NULL) return 0;
  158.     int h1 = height(p->left);//считаем высоту левого поддерева
  159.     int h2 = height(p->right);//считаем высоту правого поддерева
  160.     if (h1 >= h2) return h1 + 1;//возвращаем высоту большего из них+1 - так как добавляем наш узел
  161.     return h2 + 1;
  162. }
  163.  
  164.  
  165.  
  166.  
  167.  
  168. double sayminarea(const node* p) {
  169.     if (p != NULL)
  170.     {
  171.  
  172.         double s1 = sayminarea(p->left); // печатаем левое поддерево
  173.         double s2 = sayminarea(p->right); // печатаем правое поддерево
  174.         return min(p->countries.area, min(s1, s2)); // минимальная площадь поддеревьев.
  175.     }
  176.  
  177.     return 100000000; //
  178. }
  179. int saymaxlevel(const node* p, string s) {
  180.     //height
  181.  
  182.     if (p != NULL)
  183.     {
  184.  
  185.         int s1 = saymaxlevel(p->left, s); // печатаем левое поддерево
  186.         int s2 = saymaxlevel(p->right, s); // печатаем правое поддерево
  187.         if (p->countries.name[0] == s[0]) {
  188.             return max(height(p), max(s1, s2)); // минимальная площадь поддеревьев
  189.         }
  190.         return max(s1, s2);
  191.     }
  192.     return 0;
  193.  
  194. }
  195.  
  196. // очистка очереди
  197. void clear(qnode** pbeg)
  198. {
  199.     qnode* temp;
  200.     while (*pbeg != nullptr)
  201.     {
  202.         temp = (*pbeg)->next; //устанавливаем temp на следующий узел
  203.         delete* pbeg; // удаляем текущий узел
  204.         *pbeg = temp; // устанавливаем указатель начала pbeg на temp
  205.     }
  206. }
  207.  
  208. int main() {
  209.     SetConsoleCP(1251);
  210.     SetConsoleOutputCP(1251);
  211.     unsigned int size = 0;
  212.     qnode* s1 = NULL, * e1 = NULL;
  213.     inputMasFromKeyboard(s1, e1, size);
  214.  
  215.     if (size) {
  216.  
  217.         node* root1 = NULL, * root2 = NULL;
  218.         printMas(s1, size);
  219.         cout << endl;
  220.  
  221.         createTrees(s1, size, &root1, &root2);
  222.  
  223.         cout << "first in simm" << endl;
  224.         printsim(root1);
  225.         cout << endl;
  226.         cout << "second in simm" << endl;
  227.         printsim(root2);
  228.         cout << endl;
  229.         // ------------ задача 5--------------
  230.  
  231.         cout << "min array from 1 tree: " << endl;
  232.         sayminarea(root1);
  233.         // ------------ задача 6--------------
  234.         string s;
  235.         cout << "insert letter  " << endl;
  236.         cin >> s;
  237.         cout << "max level from 2 tree:  " << endl;
  238.         cout << saymaxlevel(root2, s) << endl;
  239.  
  240.  
  241.         // ------------ задача 7--------------
  242.  
  243.         clear(&root1);
  244.         if (!root1) cout << "cleared!" << endl;
  245.         clear(&root2);
  246.         if (!root2) cout << "cleared!" << endl;
  247.  
  248.  
  249.         clear(&s1);
  250.     }
  251.     else cout << "Размер массива равен 0" << endl;
  252.     return 0;
  253. }
  254. /*
  255.  *
  256.  *
  257.  *
  258.  *
  259.  rsssia    m1skow         1.488e+081.94     1705.2rsssia    m1skow         1.488e+081.94     1705.2
  260.  
  261.  */
Advertisement
Add Comment
Please, Sign In to add comment