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

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 using namespace std;
 5 struct Set {
 6     bool mark[128];
 7     Set() {
 8         memset(mark, 0, sizeof(mark));
 9     }
10     bool &operator [] (int x) {
11         return mark[x];
12     }
13     void inp(char ss[]) {
14         cout<< ss;
15         char str[102];
16         cin>> str;
17         for(int i = 0; str[i]; i++) {
18             if(str[i] == '$') break;
19             mark[str[i]] = 1;
20         }
21     }
22     void outp(char str[]) {
23         cout<< str;
24         for(char i = 0; i < 128 && i >= 0; i++) {
25             if(mark[i]) cout<< i<< " ";
26         }
27         cout<< endl;
28     }
29     void intersectionSet(Set A, Set B) {
30         for(int i = 0; i < 128; i++) {
31             (*this)[i] = A[i] && B[i];
32         }
33     }
34     void unionSet(Set A, Set B) {
35         for(int i = 0; i < 128; i++) {
36             (*this)[i] = A[i] || B[i];
37         }
38     }
39 };
40 int main()
41 {
42     while(1) {
43         Set A, B, C, D;
44         A.inp("A: ");
45         B.inp("B: ");
46         C.intersectionSet(A, B);
47         D.unionSet(A, B);
48         C.outp("A ^ B: ");
49         D.outp("A v B: ");
50     }
51     return 0;
52 }
View Code

 

posted @ 2014-11-25 10:01  jklongint  阅读(193)  评论(0编辑  收藏  举报