集合编程作业: 给定两个集合A、B,验证集合的并、交、相对补和对称差运算。c语言
时间: 2024-06-08 07:05:13 浏览: 165
以下是使用C语言实现集合并、交、相对补和对称差运算的示例代码:
```c
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 100
void printSet(int set[], int size);
void unionSet(int setA[], int sizeA, int setB[], int sizeB);
void intersectionSet(int setA[], int sizeA, int setB[], int sizeB);
void complementSet(int setA[], int sizeA, int setB[], int sizeB);
void symmetricDifferenceSet(int setA[], int sizeA, int setB[], int sizeB);
int main() {
int setA[MAX_SIZE], setB[MAX_SIZE];
int sizeA, sizeB;
// 输入集合A
printf("Enter the size of set A: ");
scanf("%d", &sizeA);
printf("Enter the elements of set A: ");
for (int i = 0; i < sizeA; i++) {
scanf("%d", &setA[i]);
}
// 输入集合B
printf("Enter the size of set B: ");
scanf("%d", &sizeB);
printf("Enter the elements of set B: ");
for (int i = 0; i < sizeB; i++) {
scanf("%d", &setB[i]);
}
// 输出集合A和B
printf("Set A: ");
printSet(setA, sizeA);
printf("Set B: ");
printSet(setB, sizeB);
// 计算并集
printf("Union of set A and set B: ");
unionSet(setA, sizeA, setB, sizeB);
// 计算交集
printf("Intersection of set A and set B: ");
intersectionSet(setA, sizeA, setB, sizeB);
// 计算集合A相对于集合B的补集
printf("Complement of set A relative to set B: ");
complementSet(setA, sizeA, setB, sizeB);
// 计算对称差
printf("Symmetric difference of set A and set B: ");
symmetricDifferenceSet(setA, sizeA, setB, sizeB);
return 0;
}
// 打印集合
void printSet(int set[], int size) {
printf("{ ");
for (int i = 0; i < size; i++) {
printf("%d ", set[i]);
}
printf("}\n");
}
// 计算并集
void unionSet(int setA[], int sizeA, int setB[], int sizeB) {
int unionSet[MAX_SIZE];
int sizeUnion = 0;
for (int i = 0; i < sizeA; i++) {
unionSet[sizeUnion++] = setA[i];
}
for (int i = 0; i < sizeB; i++) {
int found = 0;
for (int j = 0; j < sizeA; j++) {
if (setB[i] == setA[j]) {
found = 1;
break;
}
}
if (!found) {
unionSet[sizeUnion++] = setB[i];
}
}
printSet(unionSet, sizeUnion);
}
// 计算交集
void intersectionSet(int setA[], int sizeA, int setB[], int sizeB) {
int intersectionSet[MAX_SIZE];
int sizeIntersection = 0;
for (int i = 0; i < sizeA; i++) {
for (int j = 0; j < sizeB; j++) {
if (setA[i] == setB[j]) {
intersectionSet[sizeIntersection++] = setA[i];
}
}
}
printSet(intersectionSet, sizeIntersection);
}
// 计算集合A相对于集合B的补集
void complementSet(int setA[], int sizeA, int setB[], int sizeB) {
int complementSet[MAX_SIZE];
int sizeComplement = 0;
for (int i = 0; i < sizeA; i++) {
int found = 0;
for (int j = 0; j < sizeB; j++) {
if (setA[i] == setB[j]) {
found = 1;
break;
}
}
if (!found) {
complementSet[sizeComplement++] = setA[i];
}
}
printSet(complementSet, sizeComplement);
}
// 计算对称差
void symmetricDifferenceSet(int setA[], int sizeA, int setB[], int sizeB) {
int symmetricDifferenceSet[MAX_SIZE];
int sizeSymmetricDifference = 0;
for (int i = 0; i < sizeA; i++) {
int found = 0;
for (int j = 0; j < sizeB; j++) {
if (setA[i] == setB[j]) {
found = 1;
break;
}
}
if (!found) {
symmetricDifferenceSet[sizeSymmetricDifference++] = setA[i];
}
}
for (int i = 0; i < sizeB; i++) {
int found = 0;
for (int j = 0; j < sizeA; j++) {
if (setB[i] == setA[j]) {
found = 1;
break;
}
}
if (!found) {
symmetricDifferenceSet[sizeSymmetricDifference++] = setB[i];
}
}
printSet(symmetricDifferenceSet, sizeSymmetricDifference);
}
```
使用该代码,您可以输入两个集合A和B,并计算它们的并、交、相对补和对称差。
阅读全文