Last active
November 11, 2025 10:21
-
-
Save Klrfl/1c827d0594abcdeb0d38c2860dfd04d5 to your computer and use it in GitHub Desktop.
combination v1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <string> | |
| #include <iostream> | |
| #include <limits> | |
| #include <vector> | |
| using namespace std; | |
| void print_vector(const vector<string>& vector) | |
| { | |
| for (auto &v: vector) | |
| { | |
| std::cout << v << " "; | |
| } | |
| std::cout << std::endl; | |
| } | |
| vector<string> element(int x) { | |
| vector<string> labels; | |
| for(int i = 0; i < x; i++){ | |
| const string label = get_input<string>("Masukkan nama elemen: "); | |
| labels.push_back(label); | |
| } | |
| return labels; | |
| } | |
| unsigned long long factorial(const unsigned long long n) { | |
| if (n <= 1) return 1; | |
| unsigned long long answer = 1; | |
| for(unsigned long long i = 1; i <= n; i++) { | |
| answer = answer * i; | |
| //cout << answer << endl; | |
| } | |
| return answer; | |
| } | |
| void enumerate( | |
| const int n, | |
| const int r, | |
| const int start_at, | |
| vector<string>& labels, | |
| vector<string>& enumed | |
| ) | |
| { | |
| if (const int temp = static_cast<int>(enumed.size()); temp == r) | |
| { | |
| print_vector(enumed); | |
| return; | |
| } | |
| for (int i = start_at; i < n; i++) | |
| { | |
| auto current = labels.at(i); | |
| enumed.push_back(current); | |
| enumerate(n, r, i+1 ,labels, enumed); | |
| enumed.pop_back(); | |
| } | |
| } | |
| unsigned long long get_combination(const int n, const int r) | |
| { | |
| if (n == r) return 1; | |
| unsigned long long hasil = 0; | |
| // n/r! | |
| for (int i = n; i > r; i--) { | |
| hasil = hasil * i; | |
| } | |
| hasil = hasil / factorial(n - r); | |
| return hasil; | |
| } | |
| /** | |
| * prompts user with y/n question | |
| * returns true if user answers with y | |
| * */ | |
| bool confirm(const string &prompt) { | |
| bool is_valid = false; | |
| bool wants_to_repeat = false; | |
| char answer; | |
| do { | |
| cout << prompt; | |
| cin >> answer; | |
| if (answer != 'n' && answer != 'y') { | |
| cout << "invalid." << endl; | |
| continue; | |
| } | |
| wants_to_repeat = answer == 'y'; | |
| is_valid = true; | |
| } while (!is_valid); | |
| return wants_to_repeat; | |
| } | |
| template <typename T> | |
| T get_input(const string prompt, const string &invalid_message = "invalid input.") | |
| { | |
| bool is_valid = false; | |
| T input; | |
| while (!is_valid) | |
| { | |
| cout << prompt; | |
| cin >> input; | |
| if(!cin) { | |
| cin.clear(); | |
| cin.ignore(numeric_limits<streamsize>::max(), '\n'); | |
| cout << invalid_message << endl; | |
| continue; | |
| } | |
| is_valid = true; | |
| } | |
| return input; | |
| } | |
| int main () | |
| { | |
| bool wants_to_repeat = false; | |
| cout << "=====kalkulator kombinatorial tanpa pengulangan=====\n"; | |
| int n = 0, | |
| r = 0; | |
| do { | |
| bool repeat_input_numbers = true; | |
| do { | |
| n = get_input<int>("masukkan jumlah seluruh elemen :"); | |
| r = get_input<int>("masukkan elemen yang ingin dipilih :"); | |
| if (r > n) { | |
| cout << "Tidak valid. n harus lebih besar dari r" << endl; | |
| continue; | |
| } | |
| cout << "C(" << n << "," << r << ")" << endl; | |
| // if user answers no, repeat | |
| repeat_input_numbers = !confirm("apakah angka diatas benar (y/n) :"); | |
| if(repeat_input_numbers) { | |
| cout << "silahkan mengulang.\n\n"; | |
| } | |
| } while (repeat_input_numbers); | |
| cout << "\n======================================"; | |
| cout << "\n| Menu Utama |"; | |
| cout << "\n======================================\n"; | |
| cout << "1. hanya rumus.\n"; | |
| cout << "2. hanya enumerasi.\n"; | |
| cout << "3. dengan rumus dan enumerasi.\n"; | |
| int pilih_menu = get_input<int>("pilih menu: "); | |
| switch (pilih_menu) { | |
| case 1 : { | |
| unsigned long long hasil = get_combination(n, r); | |
| vector<string> enumed; | |
| cout << "=====hanya menggunakan rumus=====\n"; | |
| cout << n << "! / " << r << "!" << "(" << n << "-" << r << ")!" << endl; | |
| cout << hasil << endl; | |
| wants_to_repeat = confirm("apakah anda ingin kembali ke menu utama? (y/n): "); | |
| break; | |
| } | |
| case 2 : { | |
| vector<string> labels = element(n); | |
| vector<string> enumed; | |
| enumerate(n, r, 0, labels, enumed); | |
| wants_to_repeat = confirm("apakah anda ingin kembali ke menu utama? (y/n): "); | |
| labels.clear(); | |
| enumed.clear(); | |
| break; | |
| } | |
| case 3 : { | |
| unsigned long long hasil = get_combination(n, r); | |
| vector<string> labels = element(n); | |
| vector<string> enumed; | |
| cout << "===== Menggunakan rumus dan enumerasi =====\n"; | |
| cout << n << "! / " << r << "!" << "(" << n << "-" << r << ")!" << endl; | |
| cout << hasil << endl; | |
| enumerate(n, r, 0, labels, enumed); | |
| wants_to_repeat = confirm("apakah anda ingin kembali ke menu utama? (y/n): "); | |
| cout << endl; | |
| break; | |
| } | |
| default: { | |
| cout << "tolong masukkan input yang valid (1-3)." << endl; | |
| } | |
| } | |
| } while (wants_to_repeat); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
input elemen