集合的运算

集合的运算

#include <stdio.h>
#include <stdlib.h>
void print(int size, char arr[])
{
	if (size == 0) {
		printf("null");
	}
	for (int i = 0; i < size; i++) {
		printf("%c", arr[i]);
	}
	printf("\n");
}
int main()
{
	char U[] = { 'a','b','c','d','e','f','g','h' };
	char A[] = { 'a','b','c','g' };
	char B[] = { 'd','e','f','g' };
	char C[] = { 'a','c','f' };
	char D[] = { 'f','h' };
	int M = sizeof(A) / sizeof(A[0]);//计算A集合的大小
	int N = sizeof(B) / sizeof(B[0]);//计算B集合的大小
	int P = sizeof(C) / sizeof(C[0]);//计算C集合的大小
	int Q = sizeof(D) / sizeof(D[0]);//计算D集合的大小
	int R = sizeof(U) / sizeof(U[0]);//计算U集合的大小
	char E[100] = { '0' };
	char F[100] = { '0' };
	char G[100] = { '0' };
	char H[100] = { '0' };
	char I[100] = { '0' };
	char J[100] = { '0' };
	char K[100] = { '0' };
	char L[100] = { '0' };
	int size = 0;//标记结果集合的大小
	for (int i = 0; i < M; i++) {
		for (int j = 0; j < N; j++) {
			if (A[i] == B[j]) {
				E[size] = A[i];
				size++;
				break;
			}
		}
	}
	printf("A∩B    :");

	print(size, E);
	int Size = 0;
	for (int i = 0; i < size; i++) {
		int find = 0;
		for (int j = 0; j < P; j++) {
			if (E[i] == C[j]) {
				find = 1;
				break;
			}
		}
		if (find == 0) {
			K[Size] = E[i];
			Size++;
		}

	}
	printf("(A∩B)-C:");

	print(Size, K);
	size = 0;
	for (int i = 0; i < N; i++) {
		for (int j = 0; j < P; j++) {
			for (int k = 0; k < Q; k++) {
				if ((B[i] == C[j]) && (C[j] == D[k]))
				{
					G[size] = B[i];
					size++;
				}
			}

		}
	}
	printf("B∩C∩D  :");
	print(size, G);
	size = 0;
	for (int i = 0; i < N; i++) {
		F[size] = B[i];
		size++;
	}
	for (int i = 0; i < P; i++) {
		int find = 0;
		for (int j = 0; j < N; j++) {
			if (C[i] == B[j]) {
				find = 1;
				break;
			}
		}
		if (find == 0) {
			F[size] = C[i];
			size++;
		}
	}
	printf("B∪C    :");
	print(size, F);
	Size = 0;
	for (int i = 0; i < M; i++) {
		for (int j = 0; j < size; j++) {
			if (A[i] == F[j]) {
				J[Size] = A[i];
				Size++;
			}
		}
	}
	printf("A∩(B∪C):");
	print(Size, J);
	Size = 0;
	for (int i = 0; i < size; i++) {
		L[Size] = F[i];
		Size++;
	}
	for (int i = 0; i < M; i++) {
		int find = 0;
		for (int j = 0; j < size; j++) {
			if (A[i] == F[j]) {
				find = 1;
				break;
			}
		}
		if (find == 0) {
			L[Size] = A[i];
			Size++;
		}
	}
	printf("A∪B∪C  :");
	print(Size, L);
	size = 0;
	for (int i = 0; i < R; i++) {
		int find = 1;
		for (int j = 0; j < M; j++) {
			if (U[i] == A[j]) {
				find = 0;
				break;
			}
		}
		if (find == 1) {
			H[size] = U[i];
			size++;
		}
	}
	printf("A的补集:");
	print(size, H);
	size = 0;
	for (int i = 0; i < P; i++) {
		int find = 0;
		for (int j = 0; j < Q; j++) {
			if (C[i] == D[j]) {
				find = 1;
				break;

			}
		}
		if (find == 0) {
			I[size] = C[i];
			size++;
		}

	}
	printf("C-D    :");
	print(size, I);
	Size = 0;

	return 0;
}

你可能感兴趣的:(离散数学,c语言)