Advertisement
Guest User

Untitled

a guest
Oct 21st, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.19 KB | None | 0 0
  1. #include <iostream>
  2. #include <clocale>
  3. #include <fstream>
  4. #include <cstring>
  5. #include <string>
  6. #include <iostream>
  7. #include <Windows.h>
  8. #include <fstream>
  9. #include <conio.h>
  10. #include <cctype>
  11. #include <string>
  12. #include <cstdlib>
  13. #include <locale>
  14.  
  15. using namespace std;
  16. #define SIZE 10
  17.  
  18. unsigned int Country_Count = 0;
  19.  
  20. struct Country
  21. {
  22.     string name;
  23.     int c_area;
  24.     int c_lengh;
  25.     int с_number;
  26. } Country[SIZE];
  27.  
  28. void print(), input(), print_top(), print_down(), sort(), remove();
  29. void search(), display(), search_area(), search_name();
  30. int menu(), submenu();
  31.  
  32. int main()
  33. {
  34.     SetConsoleCP(1251);
  35.     SetConsoleOutputCP(1251);
  36.  
  37.     char choice;
  38.  
  39.     for (;;) {
  40.         choice = menu();
  41.         switch (choice) {
  42.         case 'e': input();
  43.             break;
  44.         case 'p': print();
  45.             break;
  46.         case 's': search();
  47.             break;
  48.         case 'a': sort();
  49.             break;
  50.         case 'r': remove();
  51.             break;
  52.         case 'd': display();
  53.             break;
  54.         case 'q': return 0;
  55.         }
  56.     }
  57.  
  58.     _getch();
  59.     return 0;
  60. }
  61. int menu()
  62. {
  63.     system("cls");
  64.     system("color 0A");
  65.     char ch;
  66.     do {
  67.         cout << "(E)Ввести\n";
  68.         cout << "(p)Записати у файл\n";
  69.         cout << "(s)Пошук\n";
  70.         cout << "(d)Вивести на екран\n";
  71.         cout << "(a)Сортувати\n";
  72.         cout << "(r)Видалити\n";
  73.         cout << "(Q)Вихід\n";
  74.         cout << "Виберіть дію: ";
  75.         cin >> ch;
  76.     } while (!strchr("epsdrq", tolower(ch)));
  77.  
  78.     return tolower(ch);
  79.  
  80. }
  81. int submenu()
  82. {
  83.     system("cls");
  84.     char ch;
  85.     do {
  86.         cout << "(1)Записати у початок файлу\n";
  87.         cout << "(2)Записати у кінцеь файлу\n";
  88.         cin >> ch;
  89.     } while (!strchr("12", tolower(ch)));
  90.  
  91.     return tolower(ch);
  92.  
  93. }
  94. int search_menu()
  95. {
  96.     system("cls");
  97.     char ch;
  98.     do {
  99.         cout << "(1)Пошук по назві країни\n";
  100.         cout << "(2)Пошук по площі\n";
  101.         cin >> ch;
  102.     } while (!strchr("12", tolower(ch)));
  103.  
  104.     return tolower(ch);
  105.  
  106. }
  107. void print()
  108. {
  109.     system("cls");
  110.     char choice_sub;
  111.  
  112.     choice_sub = submenu();
  113.     switch (choice_sub) {
  114.     case '1': print_top();
  115.         break;
  116.     case '2': print_down();
  117.         break;
  118.     }
  119. }
  120. void input()
  121. {
  122.     system("cls");
  123.     int i = Country_Count++;
  124.  
  125.     cout << "Назва країни: ";
  126.     cin >> Country[i].name;
  127.  
  128.     cout << "Площа країни: ";
  129.     cin >> Country[i].c_area;
  130.  
  131.     cout << "Довжина кордону країни: ";
  132.     cin >> Country[i].c_lengh;
  133.  
  134.     cout << "Кількість населення країни: ";
  135.     cin >> Country[i].с_number;
  136.  
  137. }
  138. void search()
  139. {
  140.     system("cls");
  141.     char choice_sub;
  142.  
  143.     choice_sub = search_menu();
  144.     switch (choice_sub) {
  145.     case '1': search_name();
  146.         break;
  147.     case '2': search_area();
  148.         break;
  149.     }
  150. }
  151. void search_name()
  152. {
  153.     system("cls");
  154.     int i = 0;
  155.     struct Country emp;
  156.     string search_nam, in_line, name;
  157.  
  158.     cout << "Введіть дані пошуку: ";
  159.     cin >> search_nam;
  160.  
  161.     ifstream in("file.txt");
  162.     while (!in.eof())
  163.     {
  164.         getline(in, in_line);
  165.  
  166.         if (!in_line.empty())
  167.         {
  168.             name = in_line.substr(string("Назва країни: ").length());
  169.             if (!name.empty() && name == search_nam)
  170.             {
  171.                 emp.name = name;
  172.                 cout << in_line << endl;
  173.                 i++;
  174.                 break;
  175.             }
  176.         }
  177.     }
  178.  
  179.     while (!in.eof())
  180.     {
  181.         getline(in, in_line);
  182.         if (in_line.find("Назва країни: ") == 0)
  183.         {
  184.             break;
  185.         }
  186.         cout << in_line << endl;
  187.     }
  188.  
  189.     if (i == 0)
  190.         cout << "Нічого не знайдено." << endl;
  191.  
  192.     system("PAUSE");
  193.  
  194. }
  195. void search_area()
  196. {
  197.     system("cls");
  198.     int i = 0;
  199.     struct Country emp;
  200.     int search_name;
  201.     string in_line, names;
  202.     cout << "Введіть дані пошуку: ";
  203.     cin >> search_name;
  204.     for (i = 0; i < SIZE; i++) {
  205.         if (search_name == Country[i].c_area) {
  206.             cout << Country[i].name << '\n';
  207.         }
  208.  
  209.     }
  210.  
  211.     system("PAUSE");
  212. }
  213. void print_top()
  214. {
  215.     int t;
  216.     ofstream ut1("file.txt", ios_base::app);
  217.     for (t = 0; t < SIZE; t++)
  218.     {
  219.         if (!Country[t].name.empty())
  220.         {
  221.             ut1 << "Назва країни: " << Country[t].name;
  222.             ut1 << " Площа країни: " << Country[t].c_area;
  223.             ut1 << " Довжина кордону країни: " << Country[t].c_lengh;
  224.             ut1 << " Кількість населення країни: " << Country[t].с_number;
  225.             ut1 << " Густина населення країни: " << Country[t].с_number / Country[t].c_area << endl;
  226.         }
  227.     }
  228.     system("PAUSE");
  229. }
  230. void sort()
  231. {
  232.     system("cls");
  233.     cout << "працює";
  234.     struct Country temp;
  235.     for (int j = 0; j < SIZE - 1; j++)
  236.         for (int i = 0; i < SIZE - 1; i++)
  237.             if (Country[i].с_number > Country[i + 1].с_number)
  238.             {
  239.                 temp = Country[i];
  240.                 Country[i] = Country[i + 1];
  241.                 Country[i + 1] = temp;
  242.             }
  243.     for (int i = 0; i < SIZE; i++)
  244.     {
  245.         cout << Country[i].name << "\t";
  246.     }
  247.     // упорядкувати за спаданням кількості населення (МЕТОД ВИБОРУ)
  248.     system("PAUSE");
  249. }
  250. void print_down()
  251. {
  252.     int t;
  253.     ofstream ut2("file.txt", ios_base::app);
  254.     for (t = 0; t < SIZE; t++)
  255.     {
  256.         if (!Country[t].name.empty())
  257.         {
  258.             ut2 << "Назва країни: " << Country[t].name << endl;
  259.             ut2 << " Площа країни: " << Country[t].c_area << endl;
  260.             ut2 << " Довжина кордону країни: " << Country[t].c_lengh << endl;
  261.             ut2 << " Кількість населення країни: " << Country[t].с_number<< endl;
  262.             ut2 << " Густина населення країни: " << Country[t].с_number / Country[t].c_area << endl;
  263.         }
  264.     }
  265.     ut2.close();
  266.     system("PAUSE");
  267. }
  268.  
  269. void display()
  270. {
  271.     system("cls");
  272.     ifstream file("file.txt");
  273.     cout << file.rdbuf();
  274.     cout << endl;
  275.     system("pause");
  276. }
  277. void remove()
  278. {
  279.     string deleteline;
  280.     string line,name;
  281.     bool founded = false;
  282.     ifstream fin;
  283.     fin.open("file.txt");
  284.     ofstream temp;
  285.     temp.open("temp.txt");
  286.     cout << "Яку саме країну видалити? ";
  287.     cin >> deleteline;
  288.     while (!fin.eof())
  289.     {
  290.         getline(fin, line);
  291.  
  292.         if (!line.empty())
  293.         {
  294.             name = line.substr(string("Назва країни: ").length());
  295.             if (!name.empty() && name == deleteline)
  296.             {
  297.                 line.replace(line.find(deleteline), line.length(), "");
  298.             }
  299.         }
  300.     }
  301.     temp.close();
  302.     fin.close();
  303.     remove("file.txt");
  304.     rename("temp.txt", "file.txt");
  305.     system("pause");
  306. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement