1.數的大小可以很大,因為我是用陣列編號去做,所以要設比較大
2.插入元素可以重複(陷阱),所以要判斷是否已經有了
3.判斷子集合,輸出為空集合,即指A本身是空集合,才輸出empty
例:
(1). A {3,4} B {2,3,5} 輸出 F
(2). A {} B {1,2,3} 輸出 empty
4.交集若為空集合,仍要輸出empty
註:num為集合中元素的個數
#include<iostream>
using namespace std;
class poly {
public:
poly() { initializeSet(); }
void initializeSet(void);
void insertElement(void);
void deleteElement(void);
void Intersection(poly a);
void Union(poly a);
void subSet(poly a);
void setPrint(void);
private:
int set[10001];
int num;
};
void poly::initializeSet()
{
for (int i = 0; i < 10001; i++)
set[i] = 0;
num = 0;
}
void poly::insertElement()
{
int x;
cin >> x;
if (set[x] == 0)
{
set[x] = 1;
num++;
}
}
void poly::deleteElement()
{
int y;
cin >> y;
if (set[y] != 0)
{
set[y] = 0;
num--;
}
}
void poly::Intersection(poly a)
{
int j = 0, isEmpty = 1;
int inset[10001] = { 0 };
for (int i = 0; i < 10001; i++)
{
if (set[i] != 0 && a.set[i] != 0)
{
inset[i] = 1;
isEmpty = 0;
}
}
if (isEmpty == 0)
{
for (int i = 0; i < 10001; i++)
{
if (inset[i] != 0)
{
if (j > 0)
cout << ",";
cout << i;
j++;
}
}
cout << endl;
}
else
cout << "empty" << endl;
}
void poly::Union(poly a)
{
int j = 0;
int unset[10001] = { 0 };
for (int i = 0; i < 10001; i++)
{
if (set[i] != 0 || a.set[i] != 0)
unset[i] = 1;
}
for (int i = 0; i < 10001; i++)
{
if (unset[i] != 0)
{
if (j > 0)
cout << ",";
cout << i;
j++;
}
}
cout << endl;
}
void poly::subSet(poly a)
{
int count = 0;
for (int i = 0; i < 10001; i++)
{
if (set[i] != 0 && a.set[i] != 0)
count++;
}
if (num == 0)
cout << "empty" << endl;
else if (count == num)
cout << "T" << endl;
else
cout << "F" << endl;
}
void poly::setPrint()
{
int j = 0;
for (int i = 0; i < 10001; i++)
{
if (set[i] != 0)
{
if (j > 0)
cout << ",";
cout << i;
j++;
}
}
cout << endl;
}
int main()
{
poly A, B;
A.insertElement();
A.insertElement();
A.insertElement();
A.deleteElement();
B.insertElement();
B.insertElement();
B.insertElement();
B.deleteElement();
A.setPrint();
B.setPrint();
A.subSet(B);
A.Intersection(B);
A.Union(B);
return 0;
}
沒有留言:
張貼留言