Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <clocale>
- #include <fstream>
- #include <cstring>
- #include <string>
- #include <iostream>
- #include <Windows.h>
- #include <fstream>
- #include <conio.h>
- #include <cctype>
- #include <string>
- #include <cstdlib>
- #include <locale>
- using namespace std;
- #define SIZE 10
- unsigned int Country_Count = 0;
- struct Country
- {
- string name;
- int c_area;
- int c_lengh;
- int с_number;
- } Country[SIZE];
- void print(), input(), print_top(), print_down(), sort(), remove();
- void search(), display(), search_area(), search_name();
- int menu(), submenu();
- int main()
- {
- SetConsoleCP(1251);
- SetConsoleOutputCP(1251);
- char choice;
- for (;;) {
- choice = menu();
- switch (choice) {
- case 'e': input();
- break;
- case 'p': print();
- break;
- case 's': search();
- break;
- case 'a': sort();
- break;
- case 'r': remove();
- break;
- case 'd': display();
- break;
- case 'q': return 0;
- }
- }
- _getch();
- return 0;
- }
- int menu()
- {
- system("cls");
- system("color 0A");
- char ch;
- do {
- cout << "(E)Ввести\n";
- cout << "(p)Записати у файл\n";
- cout << "(s)Пошук\n";
- cout << "(d)Вивести на екран\n";
- cout << "(a)Сортувати\n";
- cout << "(r)Видалити\n";
- cout << "(Q)Вихід\n";
- cout << "Виберіть дію: ";
- cin >> ch;
- } while (!strchr("epsdrq", tolower(ch)));
- return tolower(ch);
- }
- int submenu()
- {
- system("cls");
- char ch;
- do {
- cout << "(1)Записати у початок файлу\n";
- cout << "(2)Записати у кінцеь файлу\n";
- cin >> ch;
- } while (!strchr("12", tolower(ch)));
- return tolower(ch);
- }
- int search_menu()
- {
- system("cls");
- char ch;
- do {
- cout << "(1)Пошук по назві країни\n";
- cout << "(2)Пошук по площі\n";
- cin >> ch;
- } while (!strchr("12", tolower(ch)));
- return tolower(ch);
- }
- void print()
- {
- system("cls");
- char choice_sub;
- choice_sub = submenu();
- switch (choice_sub) {
- case '1': print_top();
- break;
- case '2': print_down();
- break;
- }
- }
- void input()
- {
- system("cls");
- int i = Country_Count++;
- cout << "Назва країни: ";
- cin >> Country[i].name;
- cout << "Площа країни: ";
- cin >> Country[i].c_area;
- cout << "Довжина кордону країни: ";
- cin >> Country[i].c_lengh;
- cout << "Кількість населення країни: ";
- cin >> Country[i].с_number;
- }
- void search()
- {
- system("cls");
- char choice_sub;
- choice_sub = search_menu();
- switch (choice_sub) {
- case '1': search_name();
- break;
- case '2': search_area();
- break;
- }
- }
- void search_name()
- {
- system("cls");
- int i = 0;
- struct Country emp;
- string search_nam, in_line, name;
- cout << "Введіть дані пошуку: ";
- cin >> search_nam;
- ifstream in("file.txt");
- while (!in.eof())
- {
- getline(in, in_line);
- if (!in_line.empty())
- {
- name = in_line.substr(string("Назва країни: ").length());
- if (!name.empty() && name == search_nam)
- {
- emp.name = name;
- cout << in_line << endl;
- i++;
- break;
- }
- }
- }
- while (!in.eof())
- {
- getline(in, in_line);
- if (in_line.find("Назва країни: ") == 0)
- {
- break;
- }
- cout << in_line << endl;
- }
- if (i == 0)
- cout << "Нічого не знайдено." << endl;
- system("PAUSE");
- }
- void search_area()
- {
- system("cls");
- int i = 0;
- struct Country emp;
- int search_name;
- string in_line, names;
- cout << "Введіть дані пошуку: ";
- cin >> search_name;
- for (i = 0; i < SIZE; i++) {
- if (search_name == Country[i].c_area) {
- cout << Country[i].name << '\n';
- }
- }
- system("PAUSE");
- }
- void print_top()
- {
- int t;
- ofstream ut1("file.txt", ios_base::app);
- for (t = 0; t < SIZE; t++)
- {
- if (!Country[t].name.empty())
- {
- ut1 << "Назва країни: " << Country[t].name;
- ut1 << " Площа країни: " << Country[t].c_area;
- ut1 << " Довжина кордону країни: " << Country[t].c_lengh;
- ut1 << " Кількість населення країни: " << Country[t].с_number;
- ut1 << " Густина населення країни: " << Country[t].с_number / Country[t].c_area << endl;
- }
- }
- system("PAUSE");
- }
- void sort()
- {
- system("cls");
- cout << "працює";
- struct Country temp;
- for (int j = 0; j < SIZE - 1; j++)
- for (int i = 0; i < SIZE - 1; i++)
- if (Country[i].с_number > Country[i + 1].с_number)
- {
- temp = Country[i];
- Country[i] = Country[i + 1];
- Country[i + 1] = temp;
- }
- for (int i = 0; i < SIZE; i++)
- {
- cout << Country[i].name << "\t";
- }
- // упорядкувати за спаданням кількості населення (МЕТОД ВИБОРУ)
- system("PAUSE");
- }
- void print_down()
- {
- int t;
- ofstream ut2("file.txt", ios_base::app);
- for (t = 0; t < SIZE; t++)
- {
- if (!Country[t].name.empty())
- {
- ut2 << "Назва країни: " << Country[t].name << endl;
- ut2 << " Площа країни: " << Country[t].c_area << endl;
- ut2 << " Довжина кордону країни: " << Country[t].c_lengh << endl;
- ut2 << " Кількість населення країни: " << Country[t].с_number<< endl;
- ut2 << " Густина населення країни: " << Country[t].с_number / Country[t].c_area << endl;
- }
- }
- ut2.close();
- system("PAUSE");
- }
- void display()
- {
- system("cls");
- ifstream file("file.txt");
- cout << file.rdbuf();
- cout << endl;
- system("pause");
- }
- void remove()
- {
- string deleteline;
- string line,name;
- bool founded = false;
- ifstream fin;
- fin.open("file.txt");
- ofstream temp;
- temp.open("temp.txt");
- cout << "Яку саме країну видалити? ";
- cin >> deleteline;
- while (!fin.eof())
- {
- getline(fin, line);
- if (!line.empty())
- {
- name = line.substr(string("Назва країни: ").length());
- if (!name.empty() && name == deleteline)
- {
- line.replace(line.find(deleteline), line.length(), "");
- }
- }
- }
- temp.close();
- fin.close();
- remove("file.txt");
- rename("temp.txt", "file.txt");
- system("pause");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement