네이버나 다음이나 구글이나 똑바로 된 소스가 없냐...
이거 푸는데 좀 고생고생했다.
이건 소스공개를 하지 않겠다.
나중에 해피캠퍼스에 올려야지 ㅡㅡ;;;
#include <iostream>
using namespace std;
const int MaxSize = 100;
class Set {
private:
int len; // 집합의 원소개수
char members[MaxSize]; // 집합을 저장하는 배열
int find(char ch); // 원소를 찾는다
public:
Set() { len = 0; } // 공집합 생성자
int getLength() { return len; } // 집합에 있는 원소의 개수를 반환
void showset(); // 집합 출력
bool isMember(char ch); // 집합의 원소인지 검사
Set operator +(char ch); // 원소 추가
Set operator -(char ch); // 원소 제거
Set operator +(Set ob2); // 합집합
Set operator -(Set ob2); // 차집합
bool operator <(Set ob2); // 부분집합 확인
bool operator >(Set ob2); // 부분집합 확인
Set operator &(Set ob2); // 교집합
Set operator |(Set ob2); // 대칭차집합
};
int Set::find(char ch) // ch에 명시된 원소의 색인을 찾아서 반환, 없으면 -1를 반환
{
for (int i=0; i<len; i++) { if(members[i] == ch) return i; }
return -1;
}
void Set::showset() // 집합의 원소들 출력
{
cout << "{ ";
for(int i=0; i<len; i++) { cout << members[i] << " "; }
cout << "}\n";
}
bool Set::isMember(char ch) // ch가 집합이면 트루, 아니면 폴스
{
if(find(ch) != -1) return true;
return false;
}
Set Set::operator +(char ch) // 집합에 유일한 원소 추가
{
Set newset; // 객체생성
if(len == MaxSize)
{
cout << "Set is Full.\n";
return *this; // 현재 집합을 반환
}
newset = *this; // 현재 집합을 복사
if(find(ch) == -1) // 추가하려는 원소가 이미 존재하는지 검사하고, 존재하지않으면 추가
{
newset.members[newset.len] = ch;
newset.len++;
}
return newset; // 갱신된 집합을 반환
}
Set Set::operator -(char ch) // 집합으로부터 원소 하나 제거
{
Set newset;
int i = find(ch); // 원소가 없다면 i는 -1
for(int j=0; j<len; j++) // 남아있는 원소들을 복사하고 제거된 원소의 빈자리를 채움
{
if(j != i) newset = newset+members[j];
}
return newset;
}
Set Set::operator +(Set ob2) // 합집합
{
Set newset = *this; // 첫번째 집합 복사
for(int i=0; i<ob2.len; i++) { newset = newset + ob2.members[i]; } // 유일한 원소들 추가
return newset; // 갱신된 집합 반환
}
Set Set::operator -(Set ob2) // 차집합
{
Set newset = *this; // 첫번째 집합 복사
for(int i=0; i<ob2.len; i++) newset = newset-ob2.members[i]; // 두번째 집합의 원소들 제거
return newset; // 갱신된 집합 반환
}
bool Set::operator <(Set ob2) // 좌변의 집합이 우변의 집합의 부분집합인가? 트루 or 폴스
{
if(len > ob2.len) return false; // ob1의 원소의 개수가 더 많으면 폴스
for(int i=0; i<len; i++) { if(ob2.find(members[i] == -1)) return false; }
return true;
}
bool Set::operator >(Set ob2) // 우변의 집합이 좌변의 집합의 부분집합인가? 트루 or 폴스
{
if(len < ob2.len) return false; // ob1의 원소의 개수가 더 적으면 폴스
for(int i=0; i<ob2.len; i++) { if(ob2.find(members[i] == -1)) return false; }
return true;
}
Set Set::operator &(Set ob2) // 교집합
{
Set newset;
for(int i=0; i<len; i++)
if(ob2.find(members[i]) != -1) // 두 집합에 모두 포함되어있는 원소 추가
newset = newset + members[i];
return newset;
}
Set Set::operator |(Set ob2) // 대칭차집합
{
Set newset = *this;
for(int i=0; i<ob2.len; i++)
{
newset = newset + ob2.members[i]; // 일단 합집합을 하고
}
Set newset2;
for(int j=0; j<len; j++)
{
if(ob2.find(members[j]) != -1)
{
newset2 = newset2 + members[j]; // 합집합 한거에서 교집합 뺀다
newset = newset - newset2;
}
}
return newset;
}
int main()
{
Set s1;
Set s2;
Set s3; // 공집합 3개 생성
s1 = s1 + 'A';
s1 = s1 + 'B';
s1 = s1 + 'C';
cout << "s1 after adding A B C: ";
s1.showset();
cout << "\n";
cout << "Testing for membership using isMember().\n";
if(s1.isMember('B'))
cout << "B is a member of s1.\n";
else
cout << "B is not a member of s1.\n";
if(s1.isMember('T'))
cout << "T is a member of s1.\n";
else
cout << "T is not a member of s1.\n";
cout << "\n";
s1 = s1 - 'B';
cout << "s1 after s1 = s1 - 'B' : ";
s1.showset();
s1 = s1 - 'A';
cout << "s1 after s1 = s1 - 'A' : ";
s1.showset();
s1 = s1 - 'C';
cout << "s1 after s1 = s1 - 'C' : ";
s1.showset();
cout << "\n";
s1 = s1 + 'A';
s1 = s1 + 'B';
s1 = s1 + 'C';
cout << "s1 after adding A B C: ";
s1.showset(); // A B C
cout << "\n- - - - - - - - - - - - - - - - - - - -\n\n";
s2 = s2 + 'A';
s2 = s2 + 'X';
s2 = s2 + 'W';
cout << "s2 after adding A X W: ";
s2.showset(); // A X W
cout << "\n- - - - - - - - - - - - - - - - - - - -\n\n";
s3 = s1 + s2;
cout << "s3 after s3 = s1 + s2: ";
s3.showset(); // A B C W X
cout << "\n";
s3 = s3 - s1;
cout << "s3 after s3 = s3 - s1: ";
s3.showset(); // X W
cout << "\n";
cout << "s2 after s2 = s2 - s2: ";
s2 = s2 - s2;
s2.showset(); // s2에서 s2빼니까 아무것도 안남는다
cout << "\n";
s2 = s2 + 'C';
s2 = s2 + 'B';
s2 = s2 + 'A'; // ABC를 역순으로 추가
cout << "s2 after adding C B A: ";
s2.showset(); // C B A
cout << "\n\n━━━━━━━━━━━━━━━━━━━━━━\n\n\n";
Set s4;
Set s5;
Set s6;
Set s7;
s4 = s4 + 'A';
s4 = s4 + 'B';
s4 = s4 + 'C';
s4 = s4 + 'D';
cout << "s4: ";
s4.showset(); // A B C D
s5 = s5 + 'B';
s5 = s5 + 'C';
s5 = s5 + 'S';
s5 = s5 + 'P';
cout << "s5: ";
s5.showset(); // B C S P
cout << "\n";
s6 = s4 & s5;
cout << "s4와 s5의 교집합: ";
s6.showset(); // B C
cout << "\n";
s7 = s4 | s5;
cout << "s4와 s5의 대칭차집합 ";
s7.showset(); // A D S P
return 0;
}