• Предмет: Информатика
  • Автор: apelsinn00
  • Вопрос задан 5 месяцев назад

Есть вот такой код, нужно в него добавить каким либо образом Изготовителя(чтобы в массиве на каждой строке первым было слово и чтобы было хотя бы 3 разных) хоть рандомный набор букв, хоть самому записывать, хоть записать варианты и потом сделать рандомный выбор из них, как угодно главное тоже через класс
Даю 50 баллов
#include
#include

const int N=10;

class Commutator
{
public:
int portCounter;
int portSpeed;
int matrixSpeedCommunication;
float price;

Commutator ()
{
};

Commutator (int portCounter_, int portSpeed_, int matrixSpeedCommunication_, float price_)
{
portCounter = portCounter_;
portSpeed = portSpeed_;
matrixSpeedCommunication = matrixSpeedCommunication_;
price = price_;
}
};

int main()
{
srand(time(nullptr));

Commutator commutatorArray[N];
for (int i = 0; i < N; ++i)
{
commutatorArray[i] = *new Commutator (rand() % 20 + 5, rand() % 4000 + 2000, rand() % 100 + 20, 0);
commutatorArray[i].price = commutatorArray[i].portCounter * 2.0 + commutatorArray[i].matrixSpeedCommunication * 1.5 + commutatorArray[i].portSpeed * 1.2;
std::cout <<"["<< i << "] " << commutatorArray[i].portCounter << "; " << commutatorArray[i].matrixSpeedCommunication << "; " << commutatorArray[i].portSpeed << "; " << commutatorArray[i].price << "\n";
}
Commutator bestPriceArray[N];
Commutator bestPortCounter[N];
std::copy(commutatorArray, commutatorArray+N, bestPriceArray);
std::copy(commutatorArray, commutatorArray+N, bestPortCounter);

//Sort by price
std::sort(bestPriceArray, bestPriceArray+N, [] (const Commutator & a, const Commutator & b) -> bool
{
return a.price > b.price;
});

//Sort by portNumber
std::sort(bestPortCounter, bestPortCounter+N, [] (const Commutator & a, const Commutator & 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";
}
}

Ответы

Ответ дал: honvert
1

В класс Commutator можно добавить поле для хранения изготовителя с названием manufacturer. Далее в конструкторе класса можно добавить параметр для передачи значения изготовителя и инициализировать соответствующее поле.Например:

class Commutator

{

public:

int portCounter;

int portSpeed;

int matrixSpeedCommunication;

float price;

std::string manufacturer;

Commutator (int portCounter_, int portSpeed_, int matrixSpeedCommunication_, float price_, std::string manufacturer_)

{

portCounter = portCounter_;

portSpeed = portSpeed_;

matrixSpeedCommunication = matrixSpeedCommunication_;

price = price_;

manufacturer = manufacturer_;

}

};

Затем в цикле в методе main можно использовать этот конструктор и передать нужное значение изготовителя:

for (int i = 0; i < N; ++i)

{

commutatorArray[i] = *new Commutator (rand() % 20 + 5, rand() % 4000 + 2000, rand() % 100 + 20, 0, "Manufacturer" + std::to_string(i));

commutatorArray[i].price = commutatorArray[i].portCounter * 2.0 + commutatorArray[i].matrixSpeedCommunication * 1.5 + commutatorArray[i].portSpeed * 1.2;

std::cout <<"["<< i << "] " << commutatorArray[i].portCounter << "; " << commutatorArray[i].matrixSpeedCommunication << "; " << commutatorArray[i].portSpeed << "; " << commutatorArray[i].price << "; " << commutatorArray[i].manufacturer << "\n";

}

Вы можете также создать несколько разных вариантов изготовителей и сделать рандомный выбор из них при инициализации элементов массива. Для этого можно создать массив строк с разными изготовителями и использовать рандомный индекс для выбора одного из них.Например:

std::string manufacturers[] = {"Manufacturer1", "Manufacturer2", "Manufacturer3"};

for (int i = 0; i < N; ++i)

{

int manufacturerIndex = rand() % 3;

commutatorArray[i] = *new Commutator (rand() % 20 + 5, rand() % 4000 + 2000, rand() % 100 + 20, 0, manufacturers[manufacturerIndex]);

commutatorArray[i].price = commutatorArray[i].portCounter * 2.0 + commutatorArray[i].matrixSpeedCommunication * 1.5 + commutatorArray[i].portSpeed * 1.2;

std::cout <<"["<< i << "] " << commutatorArray[i].portCounter << "; " << commutatorArray[i].matrixSpeedCommunication << "; " << commutatorArray[i].portSpeed << "; " << commutatorArray[i].price << "; " << commutatorArray[i].manufacturer << "\n";

}

Этот код создаст рандомный выбор из трех разных изготовителей.Вы также можете самостоятельно задать изготовителей для каждого элемента массива, используя соответствующий конструктор класса Commutator.


honvert: Вы можете также создать несколько разных вариантов изготовителей и сделать рандомный выбор из них при инициализации элементов массива. Для этого можно создать массив строк с разными изготовителями и использовать рандомный индекс для выбора одного из них.
Например:
Вас заинтересует