質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
C++/CLI

C++/CLIは、.NET Frameworkの共通言語基盤であるCLI向けにC++を拡張したプログラム言語です。前身のC++マネージ拡張と比較するとシンプルで分かりやすい構文になっており、高い可読性を持ちます。

コマンドライン

コマンドライン(別名:Command Line Interface)は、ユーザに命令の入力を促す(プロンプト)文字列の表示を行い、すべての操作をキーボードを用いて文字列を打ち込む事でプログラムを走らせるユーザインターフェースです。

C++

C++はC言語をもとにしてつくられた最もよく使われるマルチパラダイムプログラミング言語の1つです。オブジェクト指向、ジェネリック、命令型など広く対応しており、多目的に使用されています。

Q&A

解決済

2回答

1033閲覧

C++ コマンドラインの引数が増加した場合

alice331

総合スコア11

C++/CLI

C++/CLIは、.NET Frameworkの共通言語基盤であるCLI向けにC++を拡張したプログラム言語です。前身のC++マネージ拡張と比較するとシンプルで分かりやすい構文になっており、高い可読性を持ちます。

コマンドライン

コマンドライン(別名:Command Line Interface)は、ユーザに命令の入力を促す(プロンプト)文字列の表示を行い、すべての操作をキーボードを用いて文字列を打ち込む事でプログラムを走らせるユーザインターフェースです。

C++

C++はC言語をもとにしてつくられた最もよく使われるマルチパラダイムプログラミング言語の1つです。オブジェクト指向、ジェネリック、命令型など広く対応しており、多目的に使用されています。

0グッド

0クリップ

投稿2020/06/29 12:45

編集2020/06/29 12:57

C++

1 2C++の勉強中です。 3下記のようにコマンドラインから引数としてA,B,Cのいずれの組み合わせを入力 4すると、表示されるように作成しました。 5 6このコードを変更したいと思っており、現状A,B,Cの組み合わせは 7全部で15種類なのですが、組み合わせのパターンが増えることを想定した 8サンプルコードなど教えていただけますでしょうか? 9(例)組み合わせがA,B,C,D,E,Fなど 10 11#define などで文字を追加し組み合わせのパターンを増やしたり、減らしたり対応できる 12ようにしたいです。 13よろしくお願い致します。 14 15 16#include <iostream> 17#include <string> 18#include <unordered_set> 19using namespace std; 20 21int main(int argc, char* argv[]) 22{ 23 string str; 24 static const unordered_set<string> ptn 25 { 26 "A", 27 "A,B", 28 "A,C", 29 "A,B,C", 30 "A,C,B", 31 "B", 32 "B,A", 33 "B,C", 34 "B,A,C", 35 "B,C,A", 36 "C", 37 "C,A", 38 "C,B", 39 "C,A,B", 40 "C,B,A", 41 }; 42 bool found = false; 43 int cnt = 1; 44 45 if(ptn.count(argv[cnt])) 46 { 47 found = true; 48 } 49 if(found) 50 { 51 cout << "コマンドラインの引き数は : " << argv[cnt] << endl; 52 } 53 else 54 { 55 cout << "error: 入力誤りです"<< endl; 56 } 57}

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

takasima20

2020/06/29 13:01

ソートしたのと比較するならそんなに多くならないんじゃ?
guest

回答2

0

こんなのはいかがでしょうか?

C++

1#include <iostream> 2#include <sstream> // istringstream 3using namespace std; 4 5const int n = 3; // n <= 26. 3 for A,B,C. 5 for A,B,C,D,E. 6 7bool check(const char *s) 8{ 9 istringstream iss(s); 10 int cnt[26] = { 0 }; 11 char c; 12 while (iss >> c) 13 if (c < 'A' || c >= 'A' + n || cnt[c - 'A']++ || 14 iss >> c && c != ',') return false; 15 return true; 16} 17 18int main(int argc, char* argv[]) 19{ 20 bool found = false; 21 if (argc == 2) 22 found = check(argv[1]); 23 if (found) 24 cout << "コマンドラインの引き数は : " << argv[1] << endl; 25 else 26 cout << "error: 入力誤りです"<< endl; 27}

追記
全パターンを生成したいのなら、

C++

1#include <iostream> 2using namespace std; 3 4const int n = 3; // n <= 26. 3 for A,B,C. 5 for A,B,C,D,E. 5char a[n], used[n]; 6 7void show(int k) 8{ 9 for (int i = 0; i < k; i++) cout << a[i] << ','; 10 cout << a[k] << '\n'; 11} 12 13void generate(int i) 14{ 15 if (i == n) return; 16 for (int j = 0; j < n; j++) 17 if (!used[j]) 18 used[j] = a[i] = 'A'+j, show(i), generate(i+1), used[j] = 0; 19} 20 21int main() { generate(0); }

追記2
最初のコードは、A,B,C, のように最後が , の場合を
入力謝りにしていなかったので、次のように訂正します。

C++

1#include <iostream> 2#include <sstream> // istringstream 3using namespace std; 4 5const int n = 3; // n <= 26. 3 for A,B,C. 5 for A,B,C,D,E. 6 7bool check(const char *s) 8{ 9 istringstream iss(s); 10 int cnt[26] = { 0 }; 11 char c; 12 while (iss >> c && c >= 'A' && c < 'A'+n && !cnt[c-'A']++) { 13 if (!(iss >> c)) return true; 14 if (c != ',') break; 15 } 16 return false; 17} 18 19int main(int argc, char* argv[]) 20{ 21 bool found = false; 22 if (argc == 2) 23 found = check(argv[1]); 24 if (found) 25 cout << "コマンドラインの引き数は : " << argv[1] << endl; 26 else 27 cout << "error: 入力誤りです"<< endl; 28}

投稿2020/06/30 02:06

編集2020/06/30 09:40
kazuma-s

総合スコア8224

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

episteme

2020/06/30 03:23

- 指定された文字以外出現せず - 文字の重複がない を満足すればOKならこれでいいのか。 # あたしゃ愚直に満足するパターンを列挙したけど
episteme

2020/06/30 04:01

> 追記 まいりましたー orz
guest

0

ベストアンサー

これでいいんじゃないかなー...

C++

1#include <iostream> 2#include <string> 3#include <algorithm> 4#include <numeric> 5#include <array> 6#include <vector> 7 8int main() { 9 using namespace std; 10 11 const int N = 5; 12 13 // base = "ABCDE..." の反転 14 string base(N,' '); 15 iota(base.begin(), base.end(), 'A'); 16 reverse(base.begin(), base.end()); 17 18 vector<string> combination; 19 for ( int M = 1; M <= 3; ++M ) { 20 21 // bits : N-M個のfalse と M個の true 22 array<bool,N> bits; 23 fill(bits.begin(), bits.end(), false); 24 fill(bits.begin()+N-M, bits.end(), true); 25 26 // bitsに基づいたpatternを生成し、combinationに挿入 27 std::string pattern; 28 do { 29 pattern.clear(); 30 for ( int i = 0; i < N; ++i ) { 31 if ( bits[i] ) pattern += base[i]; 32 } 33 reverse(pattern.begin(),pattern.end()); 34 combination.emplace_back(pattern); 35 } while ( next_permutation(bits.begin(), bits.end()) ); 36 } 37 38 // combination内のpatternそれぞれに対し順列を生成 39 for ( string pattern : combination ) { 40 do { 41 string result; 42 for ( char ch : pattern ) { 43 if ( !result.empty() ) result += ','; 44 result += ch; 45 } 46 cout << result << endl; 47 } while ( next_permutation(pattern.begin(),pattern.end()) ); 48 } 49}

投稿2020/06/29 23:03

編集2020/06/29 23:45
episteme

総合スコア16614

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問