Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- // ООП и машины: с учетом комментариев
- class Car {
- private:
- char* make;
- int cost;
- int weight;
- void copyMake(const char* src) {
- int length = 0;
- while (src[length] != ' ' && src[length] != '\0') length++;
- make = new char[length + 1];
- for (int i = 0; i < length; ++i) {
- make[i] = src[i];
- }
- make[length] = ' ';
- }
- public:
- Car() : make(nullptr), cost(0), weight(0) {}
- Car(const char* make, int cost, int weight) : cost(cost), weight(weight) {
- copyMake(make);
- }
- Car(const Car& other) : cost(other.cost), weight(other.weight) {
- copyMake(other.make);
- }
- Car& operator=(const Car& other) {
- if (this != &other) {
- delete[] make;
- copyMake(other.make);
- this->cost = other.cost;
- this->weight = other.weight;
- }
- return *this;
- }
- ~Car() {
- delete[] make;
- }
- const char* getMake() const {
- return make;
- }
- int getCost() const {
- return cost;
- }
- bool compareMake(const Car& other) const {
- int i = 0;
- while (make[i] != ' ' && other.make[i] != ' ') {
- if (make[i] != other.make[i]) {
- return false;
- }
- i++;
- }
- return make[i] == other.make[i];
- }
- };
- bool is_more_expensive(const Car& car1, const Car& car2) {
- return car1.getCost() > car2.getCost();
- }
- void find_most_expensive_cars(Car* cars, int n) {
- Car** most_expensive = new Car*[n];
- int brand_count = 0;
- for (int i = 0; i < n; ++i) {
- bool found = false;
- for (int j = 0; j < brand_count; ++j) {
- if (cars[i].compareMake(*most_expensive[j])) {
- if (is_more_expensive(cars[i], *most_expensive[j])) {
- most_expensive[j] = &cars[i];
- }
- found = true;
- break;
- }
- }
- if (!found) {
- most_expensive[brand_count] = &cars[i];
- ++brand_count;
- }
- }
- std::cout << "Ответ: Самые дорогие машины среди представленных марок:\n";
- for (int i = 0; i < brand_count; ++i) {
- std::cout << most_expensive[i]->getMake() << " - " << most_expensive[i]->getCost() << std::endl;
- }
- delete[] most_expensive;
- }
- int main() {
- int n;
- std::cout << "Введите количество машин: ";
- std::cin >> n;
- Car* cars = new Car[n];
- for (int i = 0; i < n; ++i) {
- char make[100];
- int cost, weight;
- std::cin >> make >> weight >> cost;
- cars[i] = Car(make, cost, weight);
- }
- find_most_expensive_cars(cars, n);
- delete[] cars;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement