C++ C++;子集数组查找器在结尾使用逗号循环?
这是我的密码C++ C++;子集数组查找器在结尾使用逗号循环?,c++,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;
#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}
{1,2}
{3}
{1,3}
{2,3}
{1,2,3}
有谁能帮我编辑我的代码吗?我不能把“,”放在循环数组上,因为它会产生{1,2,3,}
提前谢谢 <代码> CUT考虑在第一次输出之前打印逗号。任何人能帮助我编辑我的代码吗?作业练习的要点是让你找出如何构造你的代码以便它做你想做的事情。这不仅仅是“编辑代码”。更喜欢使用
std::string
而不是字符数组。如果必须使用字符数组,请找到一种更安全的方法,因为cin>>子集
不计算它输入的字符数。尝试使用std::istream::read
或std::getline
的派生词。注意:subset[x+1]
将在x==99
时访问未定义的内存,因为subset[100]
不存在。
cout << "{";
bool first = true;
for(int j=0; j<n; j++) {
if(i & (1 <<j)) {
if (!first) cout << ","; // <<< try this
first = false;
cout << arr[j];
}
}
cout <<"}"<<endl;
cout << "{";
bool first = true;
for(int j=0; j<n; j++) {
if(i & (1 <<j)) {
if (!first) cout << ","; // <<< try this
first = false;
cout << arr[j];
}
}
cout <<"}"<<endl;