您的位置:首页 > 理论基础 > 数据结构算法

[数据结构实验]集合交并

2014-11-25 10:01 197 查看
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
struct Set {
bool mark[128];
Set() {
memset(mark, 0, sizeof(mark));
}
bool &operator [] (int x) {
return mark[x];
}
void inp(char ss[]) {
cout<< ss;
char str[102];
cin>> str;
for(int i = 0; str[i]; i++) {
if(str[i] == '$') break;
mark[str[i]] = 1;
}
}
void outp(char str[]) {
cout<< str;
for(char i = 0; i < 128 && i >= 0; i++) {
if(mark[i]) cout<< i<< " ";
}
cout<< endl;
}
void intersectionSet(Set A, Set B) {
for(int i = 0; i < 128; i++) {
(*this)[i] = A[i] && B[i];
}
}
void unionSet(Set A, Set B) {
for(int i = 0; i < 128; i++) {
(*this)[i] = A[i] || B[i];
}
}
};
int main()
{
while(1) {
Set A, B, C, D;
A.inp("A: ");
B.inp("B: ");
C.intersectionSet(A, B);
D.unionSet(A, B);
C.outp("A ^ B: ");
D.outp("A v B: ");
}
return 0;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: