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

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

Chan Lee 2023. 12. 18. 23:37

 

'๋ฐ˜๋ณต๋ฌธ ์‘์šฉ ํ”„๋กœ๊ทธ๋žจ' ํฌ์ŠคํŠธ์—์„œ ๊ตฌํ˜„ํ–ˆ๋˜ ํ”„๋กœ๊ทธ๋žจ์„ ํ•จ์ˆ˜๋ฅผ ์ ๊ทน์ ์œผ๋กœ ์ด์šฉํ•˜์—ฌ ๋™์ผํ•˜๊ฒŒ ๊ตฌํ˜„ํ•ด ๋ณด์•˜์Šต๋‹ˆ๋‹ค.

๊ธฐ์กด์˜ ํ”„๋กœ๊ทธ๋žจ์€ ํ•จ์ˆ˜์˜ ํ™œ์šฉ์ด ์—†์—ˆ์ง€๋งŒ, ์ด๋ฒˆ ํ”„๋กœ๊ทธ๋žจ์€ ํ•จ์ˆ˜๋กœ ๋ณ€๊ฒฝํ•˜๊ณ  ๊ธฐ๋Šฅ๋„ ๋ช‡ ๊ฐœ ์ถ”๊ฐ€ํ•˜์˜€์Šต๋‹ˆ๋‹ค.

 

ํ•จ์ˆ˜ ์—†์ด ๋ฐ˜๋ณต๋ฌธ๋“ค๋กœ๋งŒ ๊ตฌํ˜„๋œ ๊ธฐ์กด์˜ ํ”„๋กœ๊ทธ๋žจ์€ ๋‹ค์Œ ํฌ์ŠคํŠธ์—์„œ ํ™•์ธ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

 

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

์ฝ”๋“œ ์„ค๋ช… switch๋ฌธ, for๋ฌธ, do-while๋ฌธ์„ ๋ชจ๋‘ ํ™œ์šฉํ•œ ๊ฐ„๋‹จํ•œ ํ”„๋กœ๊ทธ๋žจ์ž…๋‹ˆ๋‹ค. ์—ฌ๋Ÿฌ๊ฐ€์ง€ ๊ธฐ๋Šฅ์„ ์ˆ˜ํ–‰ํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ์ž๋กœ๋ถ€ํ„ฐ ์ž…๋ ฅ์„ ๋ฐ›๊ณ , ๊ทธ ์ž…๋ ฅ์— ํ•ด๋‹นํ•˜๋Š” ๊ธฐ๋Šฅ์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ์ฝ”๋“œ #include using na

code-studies.tistory.com

 

๊ฐ„๋žตํ•˜๊ฒŒ ์„ค๋ช…ํ•˜์ž๋ฉด,

์‚ฌ์šฉ์ž๋กœ๋ถ€ํ„ฐ ๋ฌธ์ž๋ฅผ ํ†ตํ•ด ์˜ต์…˜์„ ์„ ํƒ ๋ฐ›๊ณ  ๊ทธ์— ๋”ฐ๋ฅธ ๋™์ž‘์„ ํ–‰ํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์ž…๋‹ˆ๋‹ค.

๋ชจ๋“  ๋™์ž‘๋“ค์€ ๋ฒกํ„ฐ๋ฅผ ๋‹ค๋ฃจ๊ณ  ์žˆ์œผ๋ฉฐ, ํ”„๋กœ๊ทธ๋žจ ์ƒ์œผ๋กœ๋Š” ๋ฆฌ์ŠคํŠธ๋กœ ์†Œ๊ฐœ๋ฉ๋‹ˆ๋‹ค.

 


์ฝ”๋“œ

#include <iostream>
#include <vector>

using namespace std;

// Prototype
void display_menu();
void print_nums(const vector<int> &vec);
void add_num(vector<int> &vec);
void del_num(vector<int> &vec);
void dp_mean(const vector<int> &vec);
void dp_min(const vector<int> &vec);
void dp_max(const vector<int> &vec);
void sort_vec(vector<int> &vec);
void clear_vec(vector<int> &vec);
void exit();

bool run{true};

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

  do {
    display_menu();
    cin >> selection;

    switch (selection) {
    case 'p':
    case 'P':
      print_nums(numbers);
      break;

    case 'a':
    case 'A':
      add_num(numbers);
      break;

    case 'd':
    case 'D':
      del_num(numbers);
      break;

    case 'm':
    case 'M':
      dp_mean(numbers);
      break;

    case 's':
    case 'S':
      dp_min(numbers);
      break;

    case 'l':
    case 'L':
      dp_max(numbers);
      break;

    case 'r':
    case 'R':
      sort_vec(numbers);
      break;

    case 'c':
    case 'C':
      clear_vec(numbers);
      break;

    case 'q':
    case 'Q':
      exit();
      break;

    default:
      cout << "โ—๏ธโ—๏ธโ—๏ธ INVALID INPUT. PLEASE RE-ENTER YOUR CHOICE โ—๏ธโ—๏ธโ—๏ธ "
           << endl;
    }

    cout << endl;
  } while (run);
}

// Function Declare
void display_menu() {
  // Display menu
  cout << "\nP - Print numbers" << endl;
  cout << "A - Add a number" << endl;
  cout << "D - Delete a number" << endl;
  cout << "M - Display mean of the numbers" << endl;
  cout << "S - Display the smallest number" << endl;
  cout << "L - Display the largest number" << endl;
  cout << "R - Sort the list of numbers" << endl;
  cout << "C - Clear the list of numbers" << endl;
  cout << "Q - Quit" << endl;
  cout << "\nEnter your choice: ";
}

void print_nums(const vector<int> &vec) {
  if (vec.size() == 0) {
    cout << "Can't print numbers since there is no number in the list." << endl;
    return;
  }

  cout << "Printing numbers..." << endl;
  for (int num : vec) {
    cout << num << " ";
  }

  return;
}

void add_num(vector<int> &vec) {
  int adding_num;
  cout << "Enter a number you want to add: ";
  cin >> adding_num;

  cout << "Adding a number..." << endl;
  cout << "Succesfully added a number into the list." << endl;
  vec.push_back(adding_num);
}

void del_num(vector<int> &vec) {
  cout << "Displaying the numbers in the list with its index number:" << endl;
  for (size_t i{0}; i < vec.size(); i++) {
    cout << "[" << (i + 1) << "]"
         << " : " << vec.at(i) << endl;
  }

  int choice{};
  cout << "\nPlease enter the corresponding index of a number you want to "
          "delete from the list:";
  cin >> choice;

  vec.erase(vec.begin() + choice - 1);
  cout << "The number has been successfully deleted from the list." << endl;
}

void dp_mean(const vector<int> &vec) {
  if (vec.size() == 0) {
    cout << "Calculation failed since there is no number in the list." << endl;
    return;
  }

  cout << "Calculating mean of the numbers..." << endl;

  double total_num{};
  for (int num : vec) {
    total_num += num;
  }

  cout << "The mean of the numbers is " << (total_num / vec.size()) << "."
       << endl;
}

void dp_min(const vector<int> &vec) {
  if (vec.size() == 0) {
    cout << "Calculation failed since there is no number in the list." << endl;
    return;
  }

  int min_val = vec.at(0);

  for (int i{1}; i < vec.size(); i++) {
    if (vec.at(i) < min_val) {
      min_val = vec.at(i);
    }
  }
  cout << "The smallest number in the list is " << min_val << "." << endl;
}

void dp_max(const vector<int> &vec) {
  if (vec.size() == 0) {
    cout << "Calculation failed since there is no number in the list." << endl;
    return;
  }

  int max_val = vec.at(0);

  for (int i{1}; i < vec.size(); i++) {
    if (vec.at(i) > max_val) {
      max_val = vec.at(i);
    }
  }
  cout << "The largest number in the list is " << max_val << "." << endl;
}

void sort_vec(vector<int> &vec) {
  cout << "Sorting the list of numbers..." << endl;
  sort(vec.begin(), vec.end());
  cout << "The list has been successfully sorted." << endl;
}

void clear_vec(vector<int> &vec) {
  if (vec.size() == 0) {
    cout << "Can't clear the list because there is no element in the list."
         << endl;
    return;
  }

  cout << "๐Ÿงน Clearing the list of numbers... ๐Ÿงน" << endl;
  vec.clear();
  cout << "The list has been successfully cleared out." << endl;
}

void exit() {
  cout << "๐Ÿ‘‹ Turning off the program... ๐Ÿ‘‹" << endl;
  run = false;
}

 

ํ”„๋กœ๊ทธ๋žจ ์ถœ๋ ฅ ์˜ˆ์‹œ

 

pass by reference์™€ const ์ธ์ž์— ๋Œ€ํ•œ ๊ฐœ๋…์ด ์žˆ๋‹ค๋ฉด ์‰ฝ๊ฒŒ ์ดํ•ดํ•  ์ˆ˜ ์žˆ๋Š” ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค.