Advertisement
NickAndNick

Классы. Vector. Заполнение, сортировка, вывод

Nov 19th, 2012
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.25 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <string>
  4. #include <vector>
  5. #include <algorithm>
  6. #include <Windows.h>
  7. #include <tchar.h>
  8.  
  9. using namespace std;
  10.  
  11. struct region {
  12.  
  13.     wstring name;
  14.     unsigned area;
  15.     unsigned population;
  16.     double density;
  17.  
  18.     region() :
  19.         name(L""),
  20.         area(0),
  21.         population(0),
  22.         density(0)
  23.     { };
  24.  
  25.     bool operator()(const region & a, const region & b)const {
  26.         return a.density < b.density;
  27.     }
  28.  
  29. };
  30.  
  31. class country {
  32.  
  33. public:
  34.  
  35.     country() : regions(), size(0) { };
  36.  
  37.     void add_region();
  38.     void show_regions()const;
  39.     void output_min_or_max_density(const size_t, bool increase = true);
  40.  
  41. private:
  42.  
  43.     vector<region> regions;
  44.     region reg;
  45.     size_t size;
  46.  
  47.     void show(const size_t)const;
  48.     void show_list(size_t);
  49.     void increase();
  50.     void decrease();
  51.  
  52. };
  53.  
  54. int main() {
  55.  
  56.     system("color 9B");
  57.     ::SetConsoleTitleW(_T("Регионы России"));
  58.  
  59.     wcout.imbue(locale("rus_rus.866"));
  60.     wcin.imbue(locale("rus_rus.866"));
  61.  
  62.     country russia;
  63.  
  64.     wcout << L"Сколько регионов желаете создать: ";
  65.     size_t limit;
  66.     cin >> limit;
  67.  
  68.     for (size_t n = 0; n < limit; n++) russia.add_region();
  69.     russia.show_regions();
  70.  
  71.     wcout << L"\nСколько регионов с наименьшей плотностью вывести на экран: ";
  72.     unsigned q;
  73.     cin >> q;
  74.     russia.output_min_or_max_density(q);
  75.  
  76.     wcout << L"\nСколько регионов с наибольшей плотностью вывести на экран: ";
  77.     cin >> q;
  78.     russia.output_min_or_max_density(q, false);
  79.  
  80.     cin.get(); cin.get();
  81.     return 0;
  82.  
  83. }
  84.  
  85. void country::add_region() {
  86.  
  87.     wcout << L"Введите название региона: ";
  88.     fflush(stdin);
  89.     getline(wcin, reg.name);
  90.  
  91.     wcout << L"Введите площадь региона в квадратных километрах: ";
  92.     cin >> reg.area;
  93.  
  94.     wcout << L"Количество жителей региона: ";
  95.     cin >> reg.population;
  96.  
  97.     if (reg.area) reg.density = double(reg.population) / reg.area;
  98.  
  99.     if (reg.name.size() && reg.area && reg.population) {
  100.         ++size;
  101.         regions.push_back(reg);
  102.     } else wcout << L"Неудача!";
  103.  
  104.     cout << endl;
  105.  
  106. }
  107.  
  108. void country::show(const size_t limit)const {
  109.  
  110.     wcout << endl << setw(25) << L"Регион" << setw(23) << L"Площадь" << setw(14) << L"Население" << setw(14) << L"Плотность" << endl << endl;
  111.  
  112.     for (size_t n = 0; n < limit; n++) {
  113.         wcout << left << setw(40) << regions[n].name;
  114.         cout  << setw(8) << regions[n].area
  115.               << setw(14) << regions[n].population
  116.               << fixed << setprecision(2) << setw(14) << regions[n].density << endl;
  117.     }
  118.  
  119.     wcout << resetiosflags(ios_base::left);
  120. }
  121.  
  122. void country::show_regions()const {
  123.     if (!size) wcout << L"Список регионов отсутствует" << endl;
  124.     else show(size);
  125. }
  126.  
  127. void country::output_min_or_max_density(const size_t limit, bool increase) {
  128.     if (increase) country::increase();
  129.     else country::decrease();
  130.     show_list(limit);
  131. }
  132.  
  133. void country::show_list(size_t limit) {
  134.     if (limit > size) limit = size;
  135.     show(limit);
  136. }
  137.  
  138. void country::increase() {
  139.     sort(regions.begin(), regions.end(), region());
  140. }
  141.  
  142. void country::decrease() {
  143.     increase();
  144.     reverse(regions.begin(), regions.end());
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement