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