集合的并交差对称差运算

Posted 可能自洽

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了集合的并交差对称差运算相关的知识,希望对你有一定的参考价值。

#include<cstdio>
#include<iostream>

using namespace std;

typedef int ElemType;  //假设线性表中的元素均为整型
typedef struct {
    ElemType *elem;   //存储空间基地址
    int length;       //表中元素的个数
} SqList;   //顺序表类型定义

int ListCreate_Sq(SqList &L, int n) {
    L.elem = new ElemType[n];
    for (int i = 0; i < n; i++)
        cin >> L.elem[i];
    L.length = n;
    return 1;
}

void ListPrintf_Sq(SqList &L) {
    ElemType *p;
    for (p = L.elem; p < L.elem + L.length; ++p)
        printf(" %d", *p);
    cout << endl;
}

SqList the_intersection_of_a_and_b(SqList &a, SqList &b) {
    SqList c;
    c.elem = new ElemType[200];
    int p, i, j;
    for (i = 0; i < a.length; i++)
        c.elem[i] = a.elem[i];
    c.length = a.length;
    for (i = 0, j = a.length; i < b.length; i++) {
        for (p = 0; p < a.length; p++) {
            if (a.elem[p] == b.elem[i])
                break;
        }
        if (p == a.length) {
            c.elem[j++] = b.elem[i];
            c.length++;
        }
    }
    return c;
}

SqList the_union_of_a_and_b(SqList &a, SqList &b) {
    SqList c;
    c.elem = new ElemType[100];
    c.length = 0;
    int i, j;
    for (i = 0; i < a.length; i++) {
        for (j = 0; j < b.length; j++) {
            if (b.elem[j] == a.elem[i])
                c.elem[c.length++] = a.elem[i];
        }
    }
    return c;
}

SqList the_relative_complement_of_a_and_b(SqList &a, SqList &b) {
    SqList c;
    c.elem = new ElemType[100];
    c.length = 0;
    int i, j;
    for (i = 0; i < a.length; i++) {
        for (j = 0; j < b.length; j++) {
            if (b.elem[j] == a.elem[i])
                break;
        }
        if (j == b.length)
            c.elem[c.length++] = a.elem[i];
    }
    return c;
}

SqList the_symmetric_difference_of_a_and_b(SqList &a, SqList &b) {
    SqList temp1, temp2;
    temp1 = the_relative_complement_of_a_and_b(a, b);
    temp2 = the_relative_complement_of_a_and_b(b, a);
    return the_intersection_of_a_and_b(temp1, temp2);
}


// test_data
// 6 6
// 1 2 3 4 5 6
// 2 3 5 7 8 9
int main() {
    int NumA, NamB;
    cin >> NumA >> NamB;
    SqList La, Lb;
    ListCreate_Sq(La, NumA);
    ListCreate_Sq(Lb, NamB);

    SqList L_intersection = the_intersection_of_a_and_b(La, Lb);
    SqList L_union = the_union_of_a_and_b(La, Lb);
    SqList L_relative_complement = the_relative_complement_of_a_and_b(La, Lb);
    SqList L_symmetric_difference = the_symmetric_difference_of_a_and_b(La, Lb);

    cout << "the intersection of a and b:";
    ListPrintf_Sq(L_intersection);
    cout << "the union of a and b:";
    ListPrintf_Sq(L_union);
    cout << "the relative complement of a and b:";
    ListPrintf_Sq(L_relative_complement);
    cout << "the symmetric difference of a and b:";
    ListPrintf_Sq(L_symmetric_difference);
    return 0;
}

以上是关于集合的并交差对称差运算的主要内容,如果未能解决你的问题,请参考以下文章

我需要一个用Java编写的程序:集合的并、交和差运算。不知道你能不能帮我,谢谢!!!

两个集合求对称差集

正则语言的 并 交 差

C++拾取——stl标准库中集合交集并集差集对称差方法

C++拾取——stl标准库中集合交集并集差集对称差方法

使用Maps与Sets处理集合的交差运算