Advertisement
coloriot

HA13_remake

Jul 12th, 2024
24
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.  
  3. // ООП и машины: с учетом комментариев
  4.  
  5. class Car {
  6. private:
  7.     char* make;
  8.     int cost;
  9.     int weight;
  10.  
  11.     void copyMake(const char* src) {
  12.         int length = 0;
  13.         while (src[length] != ' ' && src[length] != '\0') length++;
  14.         make = new char[length + 1];
  15.         for (int i = 0; i < length; ++i) {
  16.             make[i] = src[i];
  17.         }
  18.         make[length] = ' ';
  19.     }
  20.  
  21. public:
  22.     Car() : make(nullptr), cost(0), weight(0) {}
  23.  
  24.     Car(const char* make, int cost, int weight) : cost(cost), weight(weight) {
  25.         copyMake(make);
  26.     }
  27.  
  28.     Car(const Car& other) : cost(other.cost), weight(other.weight) {
  29.         copyMake(other.make);
  30.     }
  31.  
  32.     Car& operator=(const Car& other) {
  33.         if (this != &other) {
  34.             delete[] make;
  35.             copyMake(other.make);
  36.             this->cost = other.cost;
  37.             this->weight = other.weight;
  38.         }
  39.         return *this;
  40.     }
  41.  
  42.     ~Car() {
  43.         delete[] make;
  44.     }
  45.  
  46.     const char* getMake() const {
  47.         return make;
  48.     }
  49.  
  50.     int getCost() const {
  51.         return cost;
  52.     }
  53.  
  54.     bool compareMake(const Car& other) const {
  55.         int i = 0;
  56.         while (make[i] != ' ' && other.make[i] != ' ') {
  57.             if (make[i] != other.make[i]) {
  58.                 return false;
  59.             }
  60.             i++;
  61.         }
  62.         return make[i] == other.make[i];
  63.     }
  64. };
  65.  
  66. bool is_more_expensive(const Car& car1, const Car& car2) {
  67.     return car1.getCost() > car2.getCost();
  68. }
  69.  
  70. void find_most_expensive_cars(Car* cars, int n) {
  71.     Car** most_expensive = new Car*[n];
  72.     int brand_count = 0;
  73.  
  74.     for (int i = 0; i < n; ++i) {
  75.         bool found = false;
  76.         for (int j = 0; j < brand_count; ++j) {
  77.             if (cars[i].compareMake(*most_expensive[j])) {
  78.                 if (is_more_expensive(cars[i], *most_expensive[j])) {
  79.                     most_expensive[j] = &cars[i];
  80.                 }
  81.                 found = true;
  82.                 break;
  83.             }
  84.         }
  85.         if (!found) {
  86.             most_expensive[brand_count] = &cars[i];
  87.             ++brand_count;
  88.         }
  89.     }
  90.  
  91.     std::cout << "Ответ: Самые дорогие машины среди представленных марок:\n";
  92.     for (int i = 0; i < brand_count; ++i) {
  93.         std::cout << most_expensive[i]->getMake() << " - " << most_expensive[i]->getCost() << std::endl;
  94.     }
  95.  
  96.     delete[] most_expensive;
  97. }
  98.  
  99. int main() {
  100.     int n;
  101.     std::cout << "Введите количество машин: ";
  102.     std::cin >> n;
  103.  
  104.     Car* cars = new Car[n];
  105.  
  106.     for (int i = 0; i < n; ++i) {
  107.         char make[100];
  108.         int cost, weight;
  109.         std::cin >> make >> weight >> cost;
  110.         cars[i] = Car(make, cost, weight);
  111.     }
  112.  
  113.     find_most_expensive_cars(cars, n);
  114.  
  115.     delete[] cars;
  116.     return 0;
  117. }
  118.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement