'๋ฐ๋ณต๋ฌธ ์์ฉ ํ๋ก๊ทธ๋จ' ํฌ์คํธ์์ ๊ตฌํํ๋ ํ๋ก๊ทธ๋จ์ ํจ์๋ฅผ ์ ๊ทน์ ์ผ๋ก ์ด์ฉํ์ฌ ๋์ผํ๊ฒ ๊ตฌํํด ๋ณด์์ต๋๋ค.
๊ธฐ์กด์ ํ๋ก๊ทธ๋จ์ ํจ์์ ํ์ฉ์ด ์์์ง๋ง, ์ด๋ฒ ํ๋ก๊ทธ๋จ์ ํจ์๋ก ๋ณ๊ฒฝํ๊ณ ๊ธฐ๋ฅ๋ ๋ช ๊ฐ ์ถ๊ฐํ์์ต๋๋ค.
ํจ์ ์์ด ๋ฐ๋ณต๋ฌธ๋ค๋ก๋ง ๊ตฌํ๋ ๊ธฐ์กด์ ํ๋ก๊ทธ๋จ์ ๋ค์ ํฌ์คํธ์์ ํ์ธ ๊ฐ๋ฅํฉ๋๋ค.
[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 ์ธ์์ ๋ํ ๊ฐ๋ ์ด ์๋ค๋ฉด ์ฝ๊ฒ ์ดํดํ ์ ์๋ ์ฝ๋์ ๋๋ค.
'C++ > ํ๋ก์ ํธ (Project)' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[C++] (Program) ์ฌ๊ท ํจ์ ์์ : ์ ์์ ์๋ฆฟ์ ๋ง์ (0) | 2023.12.18 |
---|---|
[C++] (Program) Word Pyramid/ ๋ฌธ์์ด ํผ๋ผ๋ฏธ๋ (2) | 2023.12.15 |
[C++] (Program) String ์์ฉ ์ํธํ ํ๋ก๊ทธ๋จ (string::npos) (1) | 2023.12.15 |
[C++] (Program) ๋ฐ๋ณต๋ฌธ ์์ฉ ํ๋ก๊ทธ๋จ (1) | 2023.12.13 |