-2
這裏是我的代碼C + +子集數組查找器循環與逗號結束?
#include "iostream"
using namespace std;
void choice();
void Start(){
system("cls");
char subset[100];
int y,x;
cout << "Enter a Set"<<endl;
int pass = 0;
while (pass < 1){
y = 0;
x = 0;
cin >> subset;
while(y < 100){
if (subset[x+1] == '}') { cout<<"Invalid Set"<<endl<<"Enter a Set"<<endl; break; }
if (subset[x] == '}'&& subset[0] == '{') { cout << "Set Accepted"<<endl; pass = 1; break;}
x = x+2;
if (y == 99) {cout <<"Invalid Set"<<endl<<"Enter a Set"<<endl; }
y++;
}
}
int nofsubset = x/2;
int b = 1;
char arr[99];
int n = nofsubset;
while(nofsubset!= 0) {
for(int l=0; l<n; l++){
//cin >> arr[l];
arr[l] = subset[b];
b = b + 2;
}
for (int i=0; i<(1<<n); i++){
cout << "{";
for(int j=0; j<n; j++) {
if(i & (1 <<j)) {
cout << arr[j];
}
}
cout <<"}"<<endl;
}
nofsubset = 0;
}
system("pause");
choice();
}
void choice(){
system("cls");
cout <<"**************************"<<endl;
cout <<"* *"<<endl;
cout <<"* 1 - Generate Subsets *"<<endl;
cout <<"* 2 - Exit *"<<endl;
cout <<"* *"<<endl;
cout <<"**************************"<<endl;
int choice;
cin >> choice;
if(choice == 1){ Start();}
else{ return ;}
}
int main() {
choice();
}
這段代碼將使找到一套子集,例如我進入{1,2,3},它會產生
{}
{1}
{2}
{12}
{3}
{13}
{23}
{123}
我的教授想要的是出來把它放在格式逗號這樣
{}
{1}
{2}
{1,2}
{3}
{1,3}
{2,3}
{1,2,3}
任何人都可以請幫我修改我的代碼,我不能似乎只是把「」循環陣列上,因爲ITL農產品{1,2,3,}
在此先感謝!
考慮在第一個輸出之後的任何輸出之前打印逗號。 –
*任何人都可以請幫我編輯我的代碼* - 作業練習的重點是讓你弄清楚如何構造你的代碼,以便它能做到你想做的事情。這不僅僅是「編輯代碼」。 – PaulMcKenzie
傾向於使用'std :: string'而不是字符數組。如果您必須使用字符數組,請找到一個更安全的方法,因爲'cin >> subset'不會計算它輸入的字符數。嘗試使用'std :: istream :: read'或者'std :: getline'的衍生物。 –