Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <iomanip>
- #include <string>
- #include <vector>
- #include <algorithm>
- #include <Windows.h>
- #include <tchar.h>
- using namespace std;
- struct region {
- wstring name;
- unsigned area;
- unsigned population;
- double density;
- region() :
- name(L""),
- area(0),
- population(0),
- density(0)
- { };
- bool operator()(const region & a, const region & b)const {
- return a.density < b.density;
- }
- };
- class country {
- public:
- country() : regions(), size(0) { };
- void add_region();
- void show_regions()const;
- void output_min_or_max_density(const size_t, bool increase = true);
- private:
- vector<region> regions;
- region reg;
- size_t size;
- void show(const size_t)const;
- void show_list(size_t);
- void increase();
- void decrease();
- };
- int main() {
- system("color 9B");
- ::SetConsoleTitleW(_T("Регионы России"));
- wcout.imbue(locale("rus_rus.866"));
- wcin.imbue(locale("rus_rus.866"));
- country russia;
- wcout << L"Сколько регионов желаете создать: ";
- size_t limit;
- cin >> limit;
- for (size_t n = 0; n < limit; n++) russia.add_region();
- russia.show_regions();
- wcout << L"\nСколько регионов с наименьшей плотностью вывести на экран: ";
- unsigned q;
- cin >> q;
- russia.output_min_or_max_density(q);
- wcout << L"\nСколько регионов с наибольшей плотностью вывести на экран: ";
- cin >> q;
- russia.output_min_or_max_density(q, false);
- cin.get(); cin.get();
- return 0;
- }
- void country::add_region() {
- wcout << L"Введите название региона: ";
- fflush(stdin);
- getline(wcin, reg.name);
- wcout << L"Введите площадь региона в квадратных километрах: ";
- cin >> reg.area;
- wcout << L"Количество жителей региона: ";
- cin >> reg.population;
- if (reg.area) reg.density = double(reg.population) / reg.area;
- if (reg.name.size() && reg.area && reg.population) {
- ++size;
- regions.push_back(reg);
- } else wcout << L"Неудача!";
- cout << endl;
- }
- void country::show(const size_t limit)const {
- wcout << endl << setw(25) << L"Регион" << setw(23) << L"Площадь" << setw(14) << L"Население" << setw(14) << L"Плотность" << endl << endl;
- for (size_t n = 0; n < limit; n++) {
- wcout << left << setw(40) << regions[n].name;
- cout << setw(8) << regions[n].area
- << setw(14) << regions[n].population
- << fixed << setprecision(2) << setw(14) << regions[n].density << endl;
- }
- wcout << resetiosflags(ios_base::left);
- }
- void country::show_regions()const {
- if (!size) wcout << L"Список регионов отсутствует" << endl;
- else show(size);
- }
- void country::output_min_or_max_density(const size_t limit, bool increase) {
- if (increase) country::increase();
- else country::decrease();
- show_list(limit);
- }
- void country::show_list(size_t limit) {
- if (limit > size) limit = size;
- show(limit);
- }
- void country::increase() {
- sort(regions.begin(), regions.end(), region());
- }
- void country::decrease() {
- increase();
- reverse(regions.begin(), regions.end());
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement