任务描述
编写程序实现两个集 合的并、交,相对补,对称差的运算并判断两个集合是否相等?
功能要求
①输入集合A与集合B的元素。
②输出集合A与B的并、交、相对补、差、对称差运算。
③输出集合A与B是否相等的断定结果。
④输出集合A与B的幂集。
//这个程序的幂集计算有错,做了修改,在另外一篇文章
http://blog.csdn.net/lu_1u/article/details/73864012
#include<stdio.h>
#include<stdlib.h>
int unionSets(int *A, int *B, int lenA, int lenB)//并集
{
int i, j;
int flag, index = lenB;
int *buffer = (int*)malloc(sizeof(int) * (lenA + lenB));
for (i = 0; i < lenB; i++) {
buffer[i] = B[i];
}
for (i = 0; i < lenA; i++) {
flag = 1;
for (j = 0; j < lenB; j++) {
if (A[i] == B[j]) {
flag = 0;
}
}
if (flag) {
buffer[index] = A[i];
index++;
}
}
printf("并集:{ ");
for (i = 0; i < index; i++) {
printf("%d ", buffer[i]);
}
printf("}\n");
}
int intersection(int *A, int *B, int lenA, int lenB)//交集
{
int i, j;
int flag, index = 0, len = lenA > lenB ? lenA : lenB;
int *buffer = (int*)malloc(sizeof(int) * len);
for (i = 0; i < lenA; i++) {
flag = 0;
for (j = 0; j < lenB; j++) {
if (A[i] == B[j]) {
flag = 1;
}
}
for (j = 0; j < index; j++) {
if (A[i] == buffer[j]) {
flag = 0;
}
}
if (flag) {
buffer[index] = A[i];
index++;
}
}
printf("交集:{ ");
for (i = 0; i < index; i++) {
printf("%d ", buffer[i]);
}
printf("}\n");
}
int complementary(int *A, int *B, int lenA, int lenB)//相对补
{
int i, j;
int flag, index = 0;
int *buffer = (int*)malloc(sizeof(int)*lenA);
for (i = 0; i < lenA; i++) {
flag = 1;
for (j = 0; j < lenB; j++) {
if (A[i] == B[j]) {
flag = 0;
}
}
if (flag) {
buffer[index] = A[i];
index++;
}
}
printf("相对补:{ ");
for (i = 0; i < index; i++) {
printf("%d ", buffer[i]);
}
printf("}\n");
}
int SymDifference(int *A, int *B, int lenA, int lenB)//对称差
{
int i, j;
int flag, index = 0;
int *buffer = (int*)malloc(sizeof(int)*lenA);
for (i = 0; i < lenA; i++) {
flag = 1;
for (j = 0; j < lenB; j++) {
if (A[i] == B[j]) {
flag = 0;
}
}
if (flag) {
buffer[index] = A[i];
index++;
}
}
for (i = 0; i < lenB; i++) {
flag = 1;
for (j = 0; j < lenA; j++) {
if (B[i] == A[j]) {
flag = 0;
}
}
if (flag) {
buffer[index] = B[i];
index++;
}
}
printf("对称差:{ ");
for (i = 0; i < index; i++) {
printf("%d ", buffer[i]);
}
printf("}\n");
}
int equalSets(int *A, int *B, int lenA, int lenB)//集合相等
{
int top = 0, i, j, temp;
int *buffer = (int*)malloc(sizeof(int) * lenA);
if (lenA != lenB) {
printf("\nA与B相等\n");
return 0;
}
for (i = 0; i < lenA; i++) {
for (j = i + 1; j < lenA; j++) {
if (A[i] < A[j]) {
temp = A[i];
A[i] = A[j];
A[j] = temp;
}
}
}
for (i = 0; i < lenB; i++) {
for (j = i + 1; j < lenB; j++) {
if (B[i] < B[j]) {
temp = B[i];
B[i] = B[j];
B[j] = temp;
}
}
}
for (i = 0; i < lenA; i++) {
if (A[i] != B[i]) {
printf("\nA与B不相等\n");
return 0;
}
}
printf("相等\n");
}
int powerSets(int *A, int lenA)//幂集
{
int i, j;
int tag, index;
printf("幂集:{ { 空集 }, ");
for (tag = 1; tag <= lenA; tag++) {
for (index = 0; index < lenA - tag + 1; index++) {
printf("{ ");
for (i = index,j=0; j < tag; i++,j++) {
printf("%d ", A[i]);
}
printf("},");
}
}
printf("}\n");
}
int main()
{
int *A, *B;
int lenA, lenB;
int i;
printf("输入A,B的元素个数: ");
scanf("%d%d", &lenA, &lenB);
A = (int*)malloc(sizeof(int) * lenA);
B = (int*)malloc(sizeof(int) * lenB);
printf("输入A的元素\n");
for (i = 0; i < lenA; i++) {
scanf("%d", &A[i]);
}
getchar();
printf("输入B的元素\n");
for (i = 0; i < lenB; i++) {
scanf("%d", &B[i]);
}
equalSets(A, B, lenA, lenB);
unionSets(A, B, lenA, lenB);
intersection(A, B, lenA, lenB);
printf("A对B的");
complementary(A, B, lenA, lenB);
printf("B对A的");
complementary(B, A, lenA, lenB);
SymDifference(A, B, lenA, lenB);
printf("A的");
powerSets(A, lenA);
printf("B的");
powerSets(B, lenB);
system("pause");
return 0;
}