C++/ํ”„๋กœ์ ํŠธ (Project)

[C++] (Program) ๋ฐ˜๋ณต๋ฌธ ์‘์šฉ ํ”„๋กœ๊ทธ๋žจ

Chan Lee 2023. 12. 13. 02:20

์ฝ”๋“œ ์„ค๋ช…

switch๋ฌธ, for๋ฌธ, do-while๋ฌธ์„ ๋ชจ๋‘ ํ™œ์šฉํ•œ ๊ฐ„๋‹จํ•œ ํ”„๋กœ๊ทธ๋žจ์ž…๋‹ˆ๋‹ค.

 

์—ฌ๋Ÿฌ๊ฐ€์ง€ ๊ธฐ๋Šฅ์„ ์ˆ˜ํ–‰ํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ์ž๋กœ๋ถ€ํ„ฐ ์ž…๋ ฅ์„ ๋ฐ›๊ณ , 

๊ทธ ์ž…๋ ฅ์— ํ•ด๋‹นํ•˜๋Š” ๊ธฐ๋Šฅ์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค.

 


์ฝ”๋“œ

#include <iostream>

using namespace std;

int main() {
  vector<int> vec{};
  char selection{};

  do {
    cout << "\n-------------------------" << endl;
    cout << "P - Print numbers\nA - Add a number\nM - Display mean of the "
            "numbers\nS - Display the smallest number\nL - Display the largest "
            "number\nF - Display the count of a number\nC - Clear the list\nQ "
            "- Quit"
         << endl;
    cout << "Enter your choice: ";
    cin >> selection;
    cout << endl;

    int adding_num{};
    int total_added{};
    int largest_num{};
    int smallest_num{};
    int counting_val{};
    int count{};

    switch (selection) {
      case 'p':
      case 'P':
        cout << "Selected: P - Print numbers." << endl;
        cout << "Printing all the numbers in the list." << endl;
        if (vec.size() == 0) {
          cout << "[] - the list is empty." << endl;
        } else {
          cout << "[ ";
          for (auto val : vec) {
            cout << val << " ";
          }
          cout << "]" << endl;
        }

        break;

      case 'a':
      case 'A':
        cout << "Selected: A - Add a number." << endl;
        cout << "Enter a number you want to add to the list: ";
        cin >> adding_num;
        vec.push_back(adding_num);
        cout << "Number successfully added." << endl;
        break;

      case 'm':
      case 'M':
        cout << "Selected: M - Display mean of the numbers." << endl;
        for (auto val : vec) {
          total_added += val;
        }
        if (vec.size() == 0) {
          cout << "There is no value since there is no data in the list."
               << endl;
        } else {
          cout << "The mean of the number is: " << (total_added / vec.size())
               << endl;
        }
        break;

      case 's':
      case 'S':
        cout << "Selected: S - Display the smallest number." << endl;
        if (vec.size() == 0) {
          cout << "There is no value since there is no data in the list."
               << endl;
        } else {
          smallest_num = vec.at(0);
          for (auto val : vec) {
            if (val < smallest_num) {
              smallest_num = val;
            }
          }
          cout << "The smallest number is: " << smallest_num << endl;
        }
        break;

      case 'l':
      case 'L':
        cout << "Selected: L - Display the largest number." << endl;
        for (auto val : vec) {
          if (val > largest_num) {
            largest_num = val;
          }
        }
        if (vec.size() == 0) {
          cout << "There is no value since there is no data in the list."
               << endl;
        } else {
          cout << "The largest number is: " << largest_num << endl;
        }
        break;

      case 'c':
      case 'C':
        cout << "Selected: C - Clear the list." << endl;
        vec.clear();
        cout << "Your list has been succesfully cleared." << endl;
        break;

      case 'f':
      case 'F':
        cout << "Selected: F - Display the count of a number." << endl;
        cout << "Enter a number you want to get the count of it: ";
        cin >> counting_val;
        for (auto val : vec) {
          if (val == counting_val) {
            count++;
          }
        }
        cout << "Total count of " << counting_val
             << " in the list is: " << count << "." << endl;
        count = 0;
        break;

      case 'q':
      case 'Q':
        cout << "Selected: Q - Quit." << endl;
        cout << "Turning off programm..." << endl;
        break;

      default:
        cout << "Invalid Input. Please re-enter your choice." << endl;
    }
  } while (!(selection == 'q' || selection == 'Q'));

  cout << endl;
  return 0;
}

 

์ถœ๋ ฅ ๊ฒฐ๊ณผ