menu

mail@pastecode.io avatar
unknown
plain_text
a year ago
4.7 kB
2
Indexable
Never
/*
PIC 10A Homework 4, menu.cpp
Author: Joseph Lee
Date: 05/15/23
*/

#include <iostream>

using namespace std;


void displayMenu(int& times){
    cout << "1. Output the median\n";
    cout << "2. Get the next permutation\n";
    cout << "3. Sort in descending order\n";
    cout << "4. Simplify the numbers\n";
    cout << "Enter your choice (1-4), Q to quit: ";
    cin >> times;
    if (times == 'q' || times == 'Q'){
        cout << "Exit the menu";
    }
}


int median(int a, int b, int c){
    int value;
    //the following comments will show what the order of the value of the integers would be in order to calculate the median
    if (b < a && a < c){ //b a c
        value = a;
    }
    if (c < a && a < b){ // c a b
        value = a;
    }
    if (a < b && b < c){ //a b c
        value = b;
    }
    if (c < b && b < a){ // c b a
        value =b;
    }
    if (a < c && c < b){ // a c b
        value = c;
    }
    if (b < c && c < a){ // b c a
        value = c;
    }
    if (a == b || b == c) { // if there are duplictes, then the duplicate value will always be the median
        value = b;
    }
    if (a == c) { //if there are duplicates, then the duplicate value will always be the median
        value = c;
    }
    
    return value;
}


void myPermutation(int& a, int& b, int& c){
    int x, y, z; // create integers for the placeholders for the integer's values
    x = a; // x now holds a's value
    y = b; // y now holds b's value
    z = c; // z now holds c's value
//now we will "reorder" (basically re-value) the integers in the following way
    c = y;
    a = z;
    b = x;
    
    cout << "After one permutation: " << a << " " << b << " " << c << endl << endl;
}


void mySort(int& a, int& b, int& c){
    cout << "After sorting: ";
    if (b < a && a < c){ //b a c
        cout << c << " " << a << " " << b << endl;
    }
    if (c < a && a < b){ // c a b
        cout << b << " " << a << " " << c << endl;
    }
    if (a < b && b < c){ //a b c
        cout << c << " " << b << " " << a << endl;
    }
    if (c < b && b < a){ // c b a
        cout << a << " " << b << " " << c << endl;
    }
    if (a < c && c < b){ // a c b
        cout << b << " " << c << " " << a << endl;
    }
    if (b < c && c < a){ // b c a
        cout << a << " " << c << " " << b << endl;
    }
    if (b == a && a < c){ //  b a c (a and b are the same value)
        cout << c << " " << a << " " << b << endl;
    }
    if (b == a && c < a){ // b a c
        cout << a << " " << b << " " << c << endl;
    }
    if (a == c && c < b){ // a c b
        cout << b << " " << a << " " << c << endl;
    }
    if (a == c && b < c){ // b a c
        cout << c << " " << a << " " << b << endl;
    }
    if (b == c && c < a){ // b c a
        cout << a << " " << b << " " << c << endl;
    }
    if (b == c && a < c){ // a b c
        cout << c << " " << b << " " << a << endl;
    }
    
}

void mySimplify(int& a, int& b, int& c){
    for (int i = 2; a%i == 0 && b%i == 0 && c%i == 0; i++){
        a /= i;
        b /= i;
        c /= i;
        cout << "After one permutation: " << a << " " << b << " " << c << endl << endl;
    }
}



int main() {
    int integer1, integer2, integer3;
    bool validInput = false;
    do
    {
        cout << "Enter your three positive integers: ";
        cin >> integer1 >> integer2 >> integer3;
        if (cin.fail ()) {
            cin.clear(); // Clear the fail state of cin
            cin.ignore(9999, '\n');
            cout << "Error: non positive integer recieved." << endl << endl;
        }
        else if (integer1 < 0 || integer2 < 0 || integer3 < 0) {
            cout << "Error: non positive integer recieved." << endl << endl;
        }
        else {
            validInput = true;
        }
    }
    while (!validInput);
   
    int input, number;
    number = 0;
    while (validInput){
        number++;
        cout << "================== MENU " << number << " ==================\n";
        displayMenu(input);
        if (input == 1) {
            cout << "The median among the three is: " << median (integer1, integer2, integer3) << endl << endl;
        }
        if (input == 2) {
            myPermutation(integer1, integer2, integer3);
        }
        if (input == 3) {
            mySort(integer1, integer2, integer3);
        }
        if (input == 4){
            mySimplify(integer1, integer2, integer3);
        }
        if (input == 'q' || input == 'Q') { //CHECK ON THIS
            break;
        }
        if (cin.fail ()) {
            cin.clear(); // Clear the fail state of cin
            cin.ignore(9999, '\n');
        }
    }

    return 0;
}