Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstring>
- class TElement {
- private:
- char* name;
- int numInputs;
- double* inputValues;
- public:
- // Конструктор без параметров
- TElement() : name(nullptr), numInputs(0), inputValues(nullptr) {}
- // Конструктор с параметрами
- TElement(const char* name, int numInputs, double* inputValues) {
- this->name = new char[strlen(name) + 1];
- strcpy(this->name, name);
- this->numInputs = numInputs;
- this->inputValues = new double[numInputs];
- for (int i = 0; i < numInputs; ++i) {
- this->inputValues[i] = inputValues[i];
- }
- }
- // Конструктор копирования
- TElement(const TElement& other) {
- name = new char[strlen(other.name) + 1];
- strcpy(name, other.name);
- numInputs = other.numInputs;
- inputValues = new double[numInputs];
- for (int i = 0; i < numInputs; ++i) {
- inputValues[i] = other.inputValues[i];
- }
- }
- // Метод для вывода имени элемента на экран
- void printName() const {
- if (name != nullptr) {
- std::cout << "Name: " << name << std::endl;
- } else {
- std::cout << "Name is not set." << std::endl;
- }
- }
- // Метод для распечатки значений на всех входах на экран
- void printInputValues() const {
- std::cout << "Input values: ";
- for (int i = 0; i < numInputs; ++i) {
- std::cout << inputValues[i] << " ";
- }
- std::cout << std::endl;
- }
- // Метод для распечатки значения на заданном входе
- void printInputValue(int index) const {
- if (index >= 0 && index < numInputs) {
- std::cout << "Input value at index " << index << ": " << inputValues[index] << std::endl;
- } else {
- std::cout << "Index out of range." << std::endl;
- }
- }
- // Метод для вычисления значения на выходе элемента
- void printOutputValue() const {
- if (numInputs == 0) {
- std::cout << "No inputs available." << std::endl;
- return;
- }
- double sum = 0;
- for (int i = 0; i < numInputs; ++i) {
- sum += inputValues[i];
- }
- double average = sum / numInputs;
- std::cout << "Output value (average of inputs): " << average << std::endl;
- }
- // Метод для изменения имени элемента
- void setName(const char* newName) {
- if (name != nullptr) {
- delete[] name;
- }
- name = new char[strlen(newName) + 1];
- strcpy(name, newName);
- }
- // Метод для сравнения количества входов с другим элементом
- int compareNumInputs(const TElement& other) const {
- if (numInputs > other.numInputs) return 1;
- if (numInputs < other.numInputs) return -1;
- return 0;
- }
- // Метод для сравнения значения на выходе с другим элементом
- int compareOutputValue(const TElement& other) const {
- double thisOutput = 0, otherOutput = 0;
- for (int i = 0; i < numInputs; ++i) {
- thisOutput += inputValues[i];
- }
- thisOutput /= numInputs;
- for (int i = 0; i < other.numInputs; ++i) {
- otherOutput += other.inputValues[i];
- }
- otherOutput /= other.numInputs;
- if (thisOutput > otherOutput) return 1;
- if (thisOutput < otherOutput) return -1;
- return 0;
- }
- // Деструктор
- ~TElement() {
- delete[] name;
- delete[] inputValues;
- }
- };
- // Демонстрация работы всех методов
- int main() {
- // Объект с использованием конструктора без параметров
- TElement elem1;
- // Установка имени и значений входов
- double inputs1[] = {1.0, 2.0, 3.0};
- elem1.setName("Element 1");
- // Вывод информации об элементе
- elem1.printName();
- elem1.printInputValues();
- elem1.printOutputValue();
- // Объект с использованием конструктора с параметрами
- double inputs2[] = {4.0, 5.0, 6.0};
- TElement elem2("Element 2", 3, inputs2);
- elem2.printName();
- elem2.printInputValues();
- elem2.printOutputValue();
- // Объект с использованием конструктора копирования
- TElement elem3(elem2);
- elem3.setName("Element 3");
- elem3.printName();
- elem3.printInputValues();
- elem3.printOutputValue();
- // Сравнение количества входов
- std::cout << "Comparison of number of inputs between elem1 and elem2: "
- << elem1.compareNumInputs(elem2) << std::endl;
- // Сравнение значений на выходе
- std::cout << "Comparison of output values between elem1 and elem2: "
- << elem1.compareOutputValue(elem2) << std::endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment