hpnq

классы ka

Jun 1st, 2024 (edited)
653
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.29 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3.  
  4. class TElement {
  5. private:
  6.     char* name;
  7.     int numInputs;
  8.     double* inputValues;
  9.  
  10. public:
  11.     // Конструктор без параметров
  12.     TElement() : name(nullptr), numInputs(0), inputValues(nullptr) {}
  13.  
  14.     // Конструктор с параметрами
  15.     TElement(const char* name, int numInputs, double* inputValues) {
  16.         this->name = new char[strlen(name) + 1];
  17.         strcpy(this->name, name);
  18.         this->numInputs = numInputs;
  19.         this->inputValues = new double[numInputs];
  20.         for (int i = 0; i < numInputs; ++i) {
  21.             this->inputValues[i] = inputValues[i];
  22.         }
  23.     }
  24.  
  25.     // Конструктор копирования
  26.     TElement(const TElement& other) {
  27.         name = new char[strlen(other.name) + 1];
  28.         strcpy(name, other.name);
  29.         numInputs = other.numInputs;
  30.         inputValues = new double[numInputs];
  31.         for (int i = 0; i < numInputs; ++i) {
  32.             inputValues[i] = other.inputValues[i];
  33.         }
  34.     }
  35.  
  36.     // Метод для вывода имени элемента на экран
  37.     void printName() const {
  38.         if (name != nullptr) {
  39.             std::cout << "Name: " << name << std::endl;
  40.         } else {
  41.             std::cout << "Name is not set." << std::endl;
  42.         }
  43.     }
  44.  
  45.     // Метод для распечатки значений на всех входах на экран
  46.     void printInputValues() const {
  47.         std::cout << "Input values: ";
  48.         for (int i = 0; i < numInputs; ++i) {
  49.             std::cout << inputValues[i] << " ";
  50.         }
  51.         std::cout << std::endl;
  52.     }
  53.  
  54.     // Метод для распечатки значения на заданном входе
  55.     void printInputValue(int index) const {
  56.         if (index >= 0 && index < numInputs) {
  57.             std::cout << "Input value at index " << index << ": " << inputValues[index] << std::endl;
  58.         } else {
  59.             std::cout << "Index out of range." << std::endl;
  60.         }
  61.     }
  62.  
  63.     // Метод для вычисления значения на выходе элемента
  64.     void printOutputValue() const {
  65.         if (numInputs == 0) {
  66.             std::cout << "No inputs available." << std::endl;
  67.             return;
  68.         }
  69.  
  70.         double sum = 0;
  71.         for (int i = 0; i < numInputs; ++i) {
  72.             sum += inputValues[i];
  73.         }
  74.         double average = sum / numInputs;
  75.         std::cout << "Output value (average of inputs): " << average << std::endl;
  76.     }
  77.  
  78.     // Метод для изменения имени элемента
  79.     void setName(const char* newName) {
  80.         if (name != nullptr) {
  81.             delete[] name;
  82.         }
  83.         name = new char[strlen(newName) + 1];
  84.         strcpy(name, newName);
  85.     }
  86.  
  87.     // Метод для сравнения количества входов с другим элементом
  88.     int compareNumInputs(const TElement& other) const {
  89.         if (numInputs > other.numInputs) return 1;
  90.         if (numInputs < other.numInputs) return -1;
  91.         return 0;
  92.     }
  93.  
  94.     // Метод для сравнения значения на выходе с другим элементом
  95.     int compareOutputValue(const TElement& other) const {
  96.         double thisOutput = 0, otherOutput = 0;
  97.  
  98.         for (int i = 0; i < numInputs; ++i) {
  99.             thisOutput += inputValues[i];
  100.         }
  101.         thisOutput /= numInputs;
  102.  
  103.         for (int i = 0; i < other.numInputs; ++i) {
  104.             otherOutput += other.inputValues[i];
  105.         }
  106.         otherOutput /= other.numInputs;
  107.  
  108.         if (thisOutput > otherOutput) return 1;
  109.         if (thisOutput < otherOutput) return -1;
  110.         return 0;
  111.     }
  112.  
  113.     // Деструктор
  114.     ~TElement() {
  115.         delete[] name;
  116.         delete[] inputValues;
  117.     }
  118. };
  119.  
  120. // Демонстрация работы всех методов
  121. int main() {
  122.     // Объект с использованием конструктора без параметров
  123.     TElement elem1;
  124.  
  125.     // Установка имени и значений входов
  126.     double inputs1[] = {1.0, 2.0, 3.0};
  127.     elem1.setName("Element 1");
  128.  
  129.     // Вывод информации об элементе
  130.     elem1.printName();
  131.     elem1.printInputValues();
  132.     elem1.printOutputValue();
  133.  
  134.     // Объект с использованием конструктора с параметрами
  135.     double inputs2[] = {4.0, 5.0, 6.0};
  136.     TElement elem2("Element 2", 3, inputs2);
  137.     elem2.printName();
  138.     elem2.printInputValues();
  139.     elem2.printOutputValue();
  140.     // Объект с использованием конструктора копирования
  141.     TElement elem3(elem2);
  142.     elem3.setName("Element 3");
  143.     elem3.printName();
  144.     elem3.printInputValues();
  145.     elem3.printOutputValue();
  146.  
  147.     // Сравнение количества входов
  148.     std::cout << "Comparison of number of inputs between elem1 and elem2: "
  149.               << elem1.compareNumInputs(elem2) << std::endl;
  150.  
  151.     // Сравнение значений на выходе
  152.     std::cout << "Comparison of output values between elem1 and elem2: "
  153.               << elem1.compareOutputValue(elem2) << std::endl;
  154.  
  155.     return 0;
  156. }
Advertisement
Add Comment
Please, Sign In to add comment