Advertisement
Tavxela

Untitled

Apr 24th, 2021
696
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.45 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. #include <charconv>
  5. #include <fstream>
  6.  
  7.  
  8. using namespace std;
  9.  
  10. class Island
  11. {
  12. public:
  13.     string name = "";
  14.     int area = NULL;
  15.     string location;
  16.     Island() {};
  17.     Island(istream& input)
  18.     {
  19.         this->operator>>(input);
  20.     }
  21.     int get_area() const { return area; }
  22.     ostream& display(ostream& output) const
  23.     {
  24.         return output << name << ' ' << area << ' ' << location;
  25.     }
  26.  
  27.     void operator >> (istream& input)
  28.     {
  29.         string value;
  30.         input >> name;
  31.         input >> value;
  32.         from_chars(value.data(), value.data() + value.size(), area);
  33.         input >> location;
  34.     }
  35.  
  36.     bool operator < (const Island& object)
  37.     {
  38.         return this->area < object.area;
  39.     }
  40.  
  41.     bool operator >(const Island& object)
  42.     {
  43.         return this->area > object.area;
  44.     }
  45. };
  46.  
  47. ostream& operator <<(ostream& output, const Island& object)
  48. {
  49.     return object.display(output);
  50. }
  51.  
  52.  
  53. int main()
  54. {
  55.     const int N = 12;
  56.     Island islands[N];
  57.     fstream file;
  58.     int max;
  59.     string max_name;
  60.     string max_loc;
  61.  
  62.     file.open("./island.txt");
  63.     if (file.is_open())
  64.     {
  65.         int i = 0;
  66.         while (not file.eof())
  67.         {
  68.             islands[i++] = Island(file);
  69.         }
  70.  
  71.         max = islands[0].get_area();
  72.         for (int i = 0; i < N; i++)
  73.             if (islands[i].get_area() > max)
  74.             {
  75.                 max = islands[i].get_area();
  76.                 max_name = islands[i].name;
  77.                 max_loc = islands[i].location;
  78.             }
  79.     }
  80.     file.close();
  81.     cout << "Max: " << max_name << ' ' << max_loc << endl;
  82.     return EXIT_SUCCESS;
  83. }
  84.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement