Допоможіть, потрібно створити клас, задача на фото, 50 баллов
А) список коммутаторов которые имеют максимальное количество портов
Б) список коммутаторов которые имеют минимальную цену
Ответы
Ответ:
#include <iostream>
#include <algorithm>
const int N=10;
class Manufacturer
{
public:
int portCounter;
int portSpeed;
int matrixSpeedCommunication;
float price;
Manufacturer()
{
};
Manufacturer(int portCounter_, int portSpeed_, int matrixSpeedCommunication_, float price_)
{
portCounter = portCounter_;
portSpeed = portSpeed_;
matrixSpeedCommunication = matrixSpeedCommunication_;
price = price_;
}
};
int main()
{
srand(time(nullptr));
Manufacturer manufacturerArray[N];
for (int i = 0; i < N; ++i)
{
manufacturerArray[i] = *new Manufacturer(rand() % 20 + 5, rand() % 4000 + 2000, rand() % 100 + 20, 0);
manufacturerArray[i].price = manufacturerArray[i].portCounter * 2.0 + manufacturerArray[i].matrixSpeedCommunication * 1.5 + manufacturerArray[i].portSpeed * 1.2;
std::cout <<"["<< i << "] " << manufacturerArray[i].portCounter << "; " << manufacturerArray[i].matrixSpeedCommunication << "; " << manufacturerArray[i].portSpeed << "; " << manufacturerArray[i].price << "\n";
}
Manufacturer bestPriceArray[N];
Manufacturer bestPortCounter[N];
std::copy(manufacturerArray, manufacturerArray+N, bestPriceArray);
std::copy(manufacturerArray, manufacturerArray+N, bestPortCounter);
//Sort by price
std::sort(bestPriceArray, bestPriceArray+N, [] (const Manufacturer & a, const Manufacturer & b) -> bool
{
return a.price > b.price;
});
//Sort by portNumber
std::sort(bestPortCounter, bestPortCounter+N, [] (const Manufacturer & a, const Manufacturer & b) -> bool
{
return a.portCounter < b.portCounter;
});
std::cout<<"Best price:\n";
for (int i = 1; i < 6; ++i)
{
std::cout << "[" << i << "] " << bestPriceArray[N-i].portCounter << "; " << bestPriceArray[N-i].matrixSpeedCommunication << "; " << bestPriceArray[N-i].portSpeed << "; " << bestPriceArray[N-i].price << "\n";
}
std::cout<<"Best port counter:\n";
for (int i = 1; i < 6; ++i)
{
std::cout << "[" << i << "] " << bestPortCounter[N-i].portCounter << "; " << bestPortCounter[N-i].matrixSpeedCommunication << "; " << bestPortCounter[N-i].portSpeed << "; " << bestPortCounter[N-i].price << "\n";
}
}
[0] 7; 37; 4739; 5756.3
[1] 7; 92; 4711; 5805.2
[2] 21; 24; 5997; 7274.4
[3] 15; 116; 3223; 4071.6
[4] 16; 33; 5934; 7202.3
[5] 11; 94; 3180; 3979
[6] 12; 64; 2257; 2828.4
[7] 18; 87; 5669; 6969.3
[8] 20; 26; 2389; 2945.8
[9] 7; 35; 2120; 2610.5
Best price:
[1] 7; 35; 2120; 2610.5
[2] 12; 64; 2257; 2828.4
[3] 20; 26; 2389; 2945.8
[4] 11; 94; 3180; 3979
[5] 15; 116; 3223; 4071.6
Best port counter:
[1] 21; 24; 5997; 7274.4
[2] 20; 26; 2389; 2945.8
[3] 18; 87; 5669; 6969.3
[4] 16; 33; 5934; 7202.3
[5] 15; 116; 3223; 4071.6
Объяснение:
Сделал расчёт стоимости в зависимости от параметров (рандомно делать её вроде как нелогично)
https://pastebin.com/19WbRz0p