Untitled

 avatar
unknown
plain_text
2 years ago
2.2 kB
3
Indexable
#include <iostream>
using namespace std;

void displayMenu(int& times) {
    times++;
    cout << "Menu Options:" << endl;
    cout << "1. Sort numbers in descending order" << endl;
    cout << "2. Permute numbers" << endl;
    cout << "3. Simplify numbers" << endl;
    cout << "4. Show median" << endl;
    cout << "5. Show times called: " << times << endl;
    cout << "Q. Quit" << endl;
}

int median(int a, int b, int c) {
    if ((a >= b && a <= c) || (a >= c && a <= b))
        return a;
    else if ((b >= a && b <= c) || (b >= c && b <= a))
        return b;
    else
        return c;
}

void mySort(int& a, int& b, int& c) {
    if (a < b) swap(a, b);
    if (a < c) swap(a, c);
    if (b < c) swap(b, c);
}

void myPermutation(int& a, int& b, int& c) {
    int temp = c;
    c = b;
    b = a;
    a = temp;
}

int findGCD(int a, int b) {
    if (b == 0) return a;
    return findGCD(b, a % b);
}

void mySimplify(int& a, int& b, int& c) {
    int gcd = findGCD(a, findGCD(b, c));
    a /= gcd;
    b /= gcd;
    c /= gcd;
}

int main() {
    int times = 0;
    int num1, num2, num3;

    do {
        cout << "Enter three positive integers: ";
        cin >> num1 >> num2 >> num3;
    } while (num1 <= 0 || num2 <= 0 || num3 <= 0);

    char choice;
    do {
        displayMenu(times);
        cout << "Enter your choice: ";
        cin >> choice;

        switch (choice) {
            case '1':
                mySort(num1, num2, num3);
                break;
            case '2':
                myPermutation(num1, num2, num3);
                break;
            case '3':
                mySimplify(num1, num2, num3);
                break;
            case '4':
                cout << "Median: " << median(num1, num2, num3) << endl;
                break;
            case '5':
                // Times called is already shown in the displayMenu function
                break;
            case 'Q':
            case 'q':
                cout << "Quitting..." << endl;
                break;
            default:
                cout << "Invalid choice. Please try again." << endl;
        }
    } while (choice != 'Q' && choice != 'q');

    return 0;
}
Editor is loading...