[c언어] 합집합, 교집합, 차집합 (배열과 함수)

2020. 12. 21. 16:49Computer Science/C

 

중복을 허용하지 않는 집합 A와 집합 B를 입력받아 두 집합의 합집합과 교집합, 차집합을 구하는 프로그램을 작성한다.

 

#define MAX_SET_SIZE 10
#define HAVE_ELEMENT 1
#define DO_NOT_HAVE_ELEMENT 0
#include <stdio.h>

int hasElement(int set[], int size, int element)
{
    int i;
    for (i = 0;i < size;i++)
        if (set[i] == element)
            return HAVE_ELEMENT;
    return DO_NOT_HAVE_ELEMENT;
}

void printSet(int set[], int size)
{
    int i;
    printf("{ ");
    for (i = 0; i < size; i++) {
        if (i != 0 && i < size)
            printf(", ");
        printf("%d", set[i]);
    }
    printf(" }");
    printf("\n");
}

int isSetEqual(int set1[], int size1, int set2[], int size2)
{
    int i;
    for (i = 0;i < size2;i++)
        if (hasElement(set1, size1, set2[i]) == 0)
            return 0;
    return 1;
}

int addOneElement(int set[], int size, int element)
{
    if (hasElement(set, size, element) == 1) {
        printf("It is redundant. Please retry\n");
        return size;
    }

    else {
        set[size] = element;
        return size + 1;
    }
}

int setUnion(int set1[], int size1, int set2[], int size2, int setResult[])
{
    int i, size3 = size1;
    for (i = 0; i < size1; i++)
        setResult[i] = set1[i];

    for (i = 0; i < size2; i++) {
        if (hasElement(setResult, size3, set2[i]) == DO_NOT_HAVE_ELEMENT) {
            setResult[size3] = set2[i];
            size3++;
        }
    }
    return size3;
}

int setIntersection(int set1[], int size1, int set2[], int size2, int setResult[])
{
    int i, j, size3 = 0;

    for (i = 0; i < size1; i++)
        for(j = 0; j < size2; j++)
            if (set1[i] == set2[j]) {
                setResult[size3] = set1[i];
                size3++;
            }

    return size3;
}

int setComplements(int set1[], int size1, int set2[], int size2, int setResult[])
{
    int i, size3 = 0;
    for (i = 0; i < size1; i++) {
        if (hasElement(set2, size2, set1[i]) == DO_NOT_HAVE_ELEMENT) {
            setResult[size3] = set1[i];
            size3++;
        }
    }

    return size3;
}

int main(void)
{
    int i;

    int setA[MAX_SET_SIZE];
    int setB[MAX_SET_SIZE];
    int setC[MAX_SET_SIZE * 2];

    int sizeA, sizeB, sizeC;

    printf("Enter the size of Set A:");
    scanf("%d", &sizeA);
    i = 0;
    while (i < sizeA)
    {
        printf("Enter the number for Set A (%d/%d):", i + 1, sizeA);
        scanf("%d", &setA[i]);
        i = addOneElement(setA, i, setA[i]);
    }
    printf("Enter the size of Set B:");
    scanf("%d", &sizeB);
    i = 0;
    while (i < sizeB)
    {
        printf("Enter the number for Set B (%d/%d):", i + 1, sizeB);
        scanf("%d", &setB[i]);
        i = addOneElement(setB, i, setB[i]);
    }
    printf("Set A: ");
    printSet(setA, sizeA);
    printf("Set B: ");
    printSet(setB, sizeB);

    sizeC = setUnion(setA, sizeA, setB, sizeB, setC); // Union, setC is the result set
    printf("Union of setA and setB: ");
    printSet(setC, sizeC);

    sizeC = setIntersection(setA, sizeA, setB, sizeB, setC); //Intersection, setC is the result set
    printf("Intersection of setA and setB: ");
    printSet(setC, sizeC);

    sizeC = setComplements(setA, sizeA, setB, sizeB, setC); //Complements, setC is the result set
    printf("Set-theoretic difference of setA and setB (setA - setB): ");
    printSet(setC, sizeC);

    return 0;
}

 

결과