x3c2c5

cpps

Mar 15th, 2023
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.36 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <algorithm>
  4.  
  5. using namespace std;
  6.  
  7. class Integer {
  8. private:
  9.     int value;
  10. public:
  11.     Integer(int val = 0) : value(val) {}
  12.  
  13.     int getValue() const { return value; }
  14.  
  15.     bool operator <= (const Integer& other) const {
  16.         // Порівнюю значення на основі системи числення
  17.         if (value < 0 && other.value >= 0) {
  18.             return true;
  19.         } else if (value >= 0 && other.value < 0) {
  20.             return false;
  21.         } else {
  22.             return value <= other.value;
  23.         }
  24.     }
  25.  
  26.     Integer operator * (const Integer& other) const {
  27.         // Обчислюю добуток на основі системи числення
  28.         int product = (value < 0 && other.value >= 0) ||
  29.                       (value >= 0 && other.value < 0)
  30.                       ? -abs(value) * abs(other.value)
  31.                       : abs(value) * abs(other.value);
  32.         return Integer(product);
  33.     }
  34. };
  35.  
  36. void selectionSort(Integer arr[], int n) {
  37.     for (int i = 0; i < n - 1; i++) {
  38.         int minIndex = i;
  39.         for (int j = i + 1; j < n; j++) {
  40.             if (arr[j] <= arr[minIndex]) {
  41.                 minIndex = j;
  42.             }
  43.         }
  44.         swap(arr[i], arr[minIndex]);
  45.     }
  46. }
  47.  
  48. double geometricMean(Integer arr[], int n) {
  49.     double product = 1.0;
  50.     for (int i = 0; i < n; i++) {
  51.         product *= arr[i].getValue();
  52.     }
  53.     return pow(product, 1.0/n);
  54. }
  55.  
  56. int main() {
  57.     Integer arr[] = { Integer(-5), Integer(3), Integer(0), Integer(2), Integer(-1) };
  58.     int n = sizeof(arr) / sizeof(arr[0]);
  59.  
  60.     // Сортування масиву сортуванням вибором
  61.     selectionSort(arr, n);
  62.  
  63.     // Вивiд відсортованого масиву
  64.     cout << "Відсортований масив: ";
  65.     for (int i = 0; i < n; i++) {
  66.         cout << arr[i].getValue() << " ";
  67.     }
  68.     cout << endl;
  69.  
  70.     // обчислюю середнє геометричне значення масиву
  71.     double geoMean = geometricMean(arr, n);
  72.     cout << "Geometric Mean: " << geoMean << endl;
  73.  
  74.     // тестування
  75.     Integer a = Integer(-2);
  76.     Integer b = Integer(3);
  77.     Integer c = a * b;
  78.     cout << a.getValue() << " i " << b.getValue() << " це " << c.getValue() << endl;
  79.  
  80.     return 0;
  81. }
  82.  
Advertisement
Add Comment
Please, Sign In to add comment