Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- #include <algorithm>
- using namespace std;
- class Integer {
- private:
- int value;
- public:
- Integer(int val = 0) : value(val) {}
- int getValue() const { return value; }
- bool operator <= (const Integer& other) const {
- // Порівнюю значення на основі системи числення
- if (value < 0 && other.value >= 0) {
- return true;
- } else if (value >= 0 && other.value < 0) {
- return false;
- } else {
- return value <= other.value;
- }
- }
- Integer operator * (const Integer& other) const {
- // Обчислюю добуток на основі системи числення
- int product = (value < 0 && other.value >= 0) ||
- (value >= 0 && other.value < 0)
- ? -abs(value) * abs(other.value)
- : abs(value) * abs(other.value);
- return Integer(product);
- }
- };
- void selectionSort(Integer arr[], int n) {
- for (int i = 0; i < n - 1; i++) {
- int minIndex = i;
- for (int j = i + 1; j < n; j++) {
- if (arr[j] <= arr[minIndex]) {
- minIndex = j;
- }
- }
- swap(arr[i], arr[minIndex]);
- }
- }
- double geometricMean(Integer arr[], int n) {
- double product = 1.0;
- for (int i = 0; i < n; i++) {
- product *= arr[i].getValue();
- }
- return pow(product, 1.0/n);
- }
- int main() {
- Integer arr[] = { Integer(-5), Integer(3), Integer(0), Integer(2), Integer(-1) };
- int n = sizeof(arr) / sizeof(arr[0]);
- // Сортування масиву сортуванням вибором
- selectionSort(arr, n);
- // Вивiд відсортованого масиву
- cout << "Відсортований масив: ";
- for (int i = 0; i < n; i++) {
- cout << arr[i].getValue() << " ";
- }
- cout << endl;
- // обчислюю середнє геометричне значення масиву
- double geoMean = geometricMean(arr, n);
- cout << "Geometric Mean: " << geoMean << endl;
- // тестування
- Integer a = Integer(-2);
- Integer b = Integer(3);
- Integer c = a * b;
- cout << a.getValue() << " i " << b.getValue() << " це " << c.getValue() << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment