Advertisement
Guest User

Informatikos VBE 2013, 2 užduotis

a guest
Jun 14th, 2015
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.89 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <algorithm>
  5.  
  6. using namespace std;
  7.  
  8. int total_cities = 0,
  9.     total_regions = 0;
  10.  
  11. struct City {
  12.     string name;
  13.     string region;
  14.     int population;
  15. } cities[103];
  16.  
  17. struct Region {
  18.     string name = "";
  19.     int population = 0;
  20.     int smallest_population = 600000; // 600 000 is highest possible value
  21. } regions[10];
  22.  
  23. void read_data();
  24. void write_data();
  25. void add_region_city(int index, int population);
  26. bool region_compare(Region region1, Region region2);
  27.  
  28. int main()
  29. {
  30.     read_data();
  31.  
  32.     for (int i = 0; i < total_cities; i++) {
  33.  
  34.         for (int r = 0; r < 10; r++) {
  35.             // if region name is already in list, skip loop
  36.             if (regions[r].name == cities[i].region) {
  37.                 add_region_city(r, cities[i].population);
  38.                 break;
  39.             }
  40.  
  41.             // if region's name isn't set and current index is free, set region name
  42.             if (regions[r].name == "") {
  43.                 total_regions++;
  44.                 regions[r].name = cities[i].region;
  45.                 add_region_city(r, cities[i].population);
  46.                 break;
  47.             }
  48.         }
  49.  
  50.     }
  51.  
  52.     sort(regions, regions + total_regions, region_compare);
  53.  
  54.     write_data();
  55.     return 0;
  56. }
  57.  
  58. /**
  59.  * Reads data from input file
  60.  */
  61. void read_data() {
  62.     ifstream datafile;
  63.     datafile.open("U2.txt");
  64.  
  65.     if (datafile.is_open()) {
  66.         datafile >> total_cities;
  67.  
  68.         for (int i = 0; i < total_cities; i++) {
  69.             datafile >> cities[i].name >> cities[i].region >> cities[i].population;
  70.         }
  71.     }
  72. }
  73.  
  74. /**
  75.  * Writes results to output file
  76.  */
  77. void write_data() {
  78.     ofstream outputfile;
  79.     outputfile.open("U2Rez.txt");
  80.  
  81.     if (outputfile.is_open()) {
  82.         outputfile << total_regions << endl;
  83.         for (int i = 0; i < total_regions; i++) {
  84.             outputfile << regions[i].name << " ";
  85.             outputfile << regions[i].smallest_population << " ";
  86.             outputfile << regions[i].population << " ";
  87.             outputfile << endl;
  88.         }
  89.     }
  90. }
  91.  
  92. /**
  93.   * Adds city population to region population, and checks,
  94.   * if it's smallest city population  
  95.   *
  96.   * int index - array index of region
  97.   * int population - population of city  
  98.   */
  99. void add_region_city(int index, int population) {
  100.     regions[index].population += population;
  101.  
  102.     if (regions[index].smallest_population > population)
  103.         regions[index].smallest_population = population;
  104. }
  105.  
  106. /**
  107.   * Comparing function for sorting regions by smallest city
  108.   * and alphabet (sort function's third argument)
  109.   */
  110. bool region_compare(Region region1, Region region2) {
  111.     if (region1.smallest_population == region2.smallest_population) {
  112.         return region1.name < region2.name;
  113.     } else {
  114.         return region1.smallest_population < region2.smallest_population;
  115.     }
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement