define _CRT_SECURE_NO_WARNINGS 1
/*
1.求任意两个集合的交集、并集、差集
2.求任意一个集合的幂集
3.求任意一个集合的 m 元子集
*/
include
include
include
include
using namespace std;
//检验一个元素是否已经存在于集合中
bool existed(vector
{
bool exist = false;
for (auto i = s.begin(); i != s.end();i++)
{
if (*i == a)
{
exist = true;
}
}
return exist;
}
//输入两个集合,用字符串存储它们的元素
vector
{
vector
char in = '\0';
while (in != '\n')
{
scanf("%c", &in);
if (in != ' ' && in != '{' && in != '}' && in != ','
&& ((int)in >= 97 && (int)in <= 122 || (int)in >= 65 && (int)in <= 90))
{
if (existed(s1, in) == false)//集合中元素不能重复
{
s1.push_back(in);
}
}
}
return s1;
}
//交集
vector
{
vector
for (auto i = s1.begin(); i != s1.end(); i++)
{
for (auto j = s2.begin(); j != s2.end(); j++)
{
if (*i == *j)
{
jiao.push_back(*i);
}
}
}
return jiao;
}
//(去重),求并集
vector
{
vector
bool exist = false;
for (auto i = s1.begin(); i != s1.end(); i++)
{
if (existed(s2, *i) == false)
{
bing.push_back(*i);
}
}
return bing;
}
//差集
vector
{
vector
for (auto i = s2.begin(); i != s2.end(); i++)
{
if (existed(cha, *i) == true)
{
auto it = find(cha.begin(), cha.end(), *i);//找到s1中与s2相同的元素 *i 的位置
if (it != cha.end())
{
cha.erase(it);
}
}
}
return cha;
}
//幂集
vector<vector
{
vector<vector
int col = s.size();
int row = pow(2, col);
for (int i = 0; i < row; i++)
{
vector<char> submit;
for (int j = 0; j < col; j++)
{
if (i & (1 << j))//【最核心的代码】
{
submit.push_back(s[j]);
}
}
mi.push_back(submit);
}
return mi;
}
//打印普通集合
void show(vector
{
sort(s.begin(), s.end());//集合排序
cout << '{';
for (auto i = s.begin(); i != s.end(); i++)
{
cout << i;
if (i != s.end() - 1)
{
cout << ',';
}
}
cout << '}' << endl;
}
// 打印幂集
void show_mi(vector<vector
{
cout << "{ ";
for (auto i = mi.begin(); i != mi.end(); i++) {
cout << "{ ";
for (auto j = (
if (next(j) == (i).end())
{
cout << *j;//末尾不加“,”
}
else
{
cout << *j << ",";
}
}
if (next(i) == mi.end())
{
cout << " }";
}
else
{
cout << " }, ";
}
}
cout << " }" << std::endl;
}
//重载打印幂集的m元子集
void show_mi(vector<vector
{
vector<vector
bool success = false;
for (auto i = mi.begin(); i != mi.end(); i++)//把元素个数符合要求的都推进 mi_m 中
{
if ((*i).size() == n)
{
mi_m.push_back(*i);
success = true;
}
}
if (success == true)//用迭代器输出元素
{
cout << "{ ";
for (auto i = mi_m.begin(); i != mi_m.end(); i++) {
cout << "{";
for (auto j = (*i).begin(); j != (*i).end(); j++)
{
if (next(j) == (*i).end())//避免在最后一个位置打印 “,”
{
cout << *j;
}
else
{
cout << *j << ",";
}
}
if (next(i) == mi_m.end())
{
cout << "}";
}
else
{
cout << "},";
}
}
cout << " }" << std::endl;
}
else
{
cout << "该集合没有 " +to_string(n) + " 元子集";
}
}
//选择运算方式
void choice(char fuhao, vector
{
vector
vector
vector
vector
vector<vector
vector<vector
char xuanze = '\0';
bool done = true;
switch (fuhao)
{
case'&':
cout << "******************************" << endl;
cout << "交集= ";show(jiao);
cout << "******************************" << endl;
break;
case'|':
cout << "******************************" << endl;
cout << "并集= ";show(bing);
cout << "******************************" << endl;
break;
case'-':
while (done)
{
cout << "集合(1) - 集合(2)请输入 1,集合(2) - 集合(1)请输入 2" << endl;
cin >> xuanze;
if (xuanze == '1')
{
cout << "******************************" << endl;
cout << "集合(1) - 集合(2)= ";show(cha_1);
cout << "******************************" << endl;
done = false;
}
else if (xuanze == '2')
{
cout << "******************************" << endl;
cout << "集合(2) - 集合(1)= ";show(cha_2);
cout << "******************************" << endl;
done = false;
}
else
{
cout << "请重新输入:" << endl;
}
}
break;
case'm':
while (done)
{
cout << "求集合(1)的幂集请输入 1,求集合(2)的幂集请输入 2" << endl;
cin >> xuanze;
if (xuanze == '1')
{
cout << "******************************" << endl;
cout << "集合(1)的幂集= ";show_mi(mi_1);
cout << "******************************" << endl;
done = false;
}
else if (xuanze == '2')
{
cout << "******************************" << endl;
cout << "集合(2)的幂集= ";show_mi(mi_2);
cout << "******************************" << endl;
done = false;
}
else
{
cout << "请重新输入:" << endl;
}
}
break;
case'n':
while (done)
{
int m_yuan = 0;
int len_1 = input_1.size();
int len_2 = input_2.size();
bool shuru = true;
cout << "求集合(1)的幂集请输入 1,求集合(2)的幂集请输入 2" << endl;
cin >> xuanze;
if (xuanze == '1')
{
while (shuru)
{
cout << "您选择的是集合(1),请输入所求集合元素的个数:" << endl;
cout << "元素个数应该在 0 到 " + to_string(len_1) + " 的范围内。" << endl;
cin >> m_yuan;
if (m_yuan >= 0 && m_yuan <= len_1)
{
cout << "******************************" << endl;
cout << "集合(1)的幂集= ";show_mi(mi_1,m_yuan);
cout << "******************************" << endl;
shuru = false;
}
else
{
cout << "元素个数不在正确范围内,请重新输入\n" << endl;
}
}
done = false;
}
else if (xuanze == '2')
{
while (shuru)
{
cout << "您选择的是集合(2),请输入所求集合元素的个数:" << endl;
cout << "元素个数应该在 0 到 " + to_string(len_2) + " 的范围内。" << endl;
cin >> m_yuan;
if (m_yuan >= 0 && m_yuan <= len_2)
{
cout << "******************************" << endl;
cout << "集合(1)的幂集= ";show_mi(mi_2, m_yuan);
cout << "******************************" << endl;
shuru = false;
}
else
{
cout << "元素个数不在正确范围内,请重新输入" << endl;
}
}
done = false;
}
else
{
cout << "请重新输入:" << endl;
}
}
break;
case'A':
cout << "******************************" << endl;
cout << "交集= ";show(jiao);
cout << "并集= ";show(bing);
cout << "集合(1) - 集合(2)= ";show(cha_1);
cout << "集合(2) - 集合(1) = ";show(cha_2);
cout << "集合(1)的幂集= ";show_mi(mi_1);
cout << "集合(2)的幂集= ";show_mi(mi_2);
cout << "******************************" << endl;
break;
}
}
int main(void)
{
cout << "请输入集合 1 中的元素,以回车结束(仅支持单个字符):" << endl;
vector
cout << "请输入集合 2 中的元素,以回车结束(仅支持单个字符):" << endl;
vector
char fuhao = '\0';
while (fuhao != 'q')
{
cout << "******************************" << endl;
cout << "******** & 代表交运算*********" << endl;
cout << "******** | 代表并运算*********" << endl;
cout << "******** - 代表差运算*********" << endl;
cout << "******** m 代表求幂集*********" << endl;
cout << "**** n 代表求幂集的m元子集****" << endl;
cout << "******** A 代表输出所有*******" << endl;
cout << "******** q 代表退出运算*******" << endl;
cout << "******************************" << endl;
cout << "请输入运算符号(输入 q 退出):" << endl;
cin >> fuhao;
if (fuhao != 'q' && fuhao != '&' && fuhao != '|' && fuhao != '-' && fuhao != 'A' && fuhao != 'm' && fuhao != 'n')
{
cout << "\n请输入正确的符号" << endl;
}
else
{
choice(fuhao, input_1, input_2);
cout << endl;
}
system("pause");
}
return 0;
}
标签:容器,end,运算,mi,离散数学,vector,&&,input,cout From: https://www.cnblogs.com/Autumnker/p/18187820