在相关环境中编译运行,能够成功实现集合的运算操作,包括交集、并集、差集、幂集、任意m元子集。用C语言实现
时间: 2023-07-10 19:09:48 浏览: 114
下面是用C语言实现集合运算的代码,其中包括交集、并集、差集、幂集、任意m元子集。
```c
#include <stdio.h>
#include <math.h>
#define MAX_SIZE 100
void print_set(int set[], int size);
void input_set(int set[], int *size);
void union_set(int set1[], int size1, int set2[], int size2, int result[], int *result_size);
void intersection_set(int set1[], int size1, int set2[], int size2, int result[], int *result_size);
void difference_set(int set1[], int size1, int set2[], int size2, int result[], int *result_size);
void power_set(int set[], int size);
int main() {
int set1[MAX_SIZE], set2[MAX_SIZE], result[MAX_SIZE];
int size1, size2, result_size;
printf("Input set1:\n");
input_set(set1, &size1);
printf("Input set2:\n");
input_set(set2, &size2);
printf("Set1: ");
print_set(set1, size1);
printf("Set2: ");
print_set(set2, size2);
printf("Union of Set1 and Set2: ");
union_set(set1, size1, set2, size2, result, &result_size);
print_set(result, result_size);
printf("Intersection of Set1 and Set2: ");
intersection_set(set1, size1, set2, size2, result, &result_size);
print_set(result, result_size);
printf("Difference of Set1 and Set2: ");
difference_set(set1, size1, set2, size2, result, &result_size);
print_set(result, result_size);
printf("Power set of Set1: \n");
power_set(set1, size1);
return 0;
}
void print_set(int set[], int size) {
printf("{ ");
for (int i = 0; i < size; i++) {
printf("%d ", set[i]);
}
printf("}\n");
}
void input_set(int set[], int *size) {
printf("Enter the size of the set: ");
scanf("%d", size);
printf("Enter the elements of the set: ");
for (int i = 0; i < *size; i++) {
scanf("%d", &set[i]);
}
}
void union_set(int set1[], int size1, int set2[], int size2, int result[], int *result_size) {
int i, j;
for (i = 0; i < size1; i++) {
result[i] = set1[i];
}
*result_size = size1;
for (i = 0; i < size2; i++) {
for (j = 0; j < size1; j++) {
if (set2[i] == set1[j]) {
break;
}
}
if (j == size1) {
result[*result_size] = set2[i];
(*result_size)++;
}
}
}
void intersection_set(int set1[], int size1, int set2[], int size2, int result[], int *result_size) {
int i, j;
*result_size = 0;
for (i = 0; i < size1; i++) {
for (j = 0; j < size2; j++) {
if (set1[i] == set2[j]) {
result[*result_size] = set1[i];
(*result_size)++;
break;
}
}
}
}
void difference_set(int set1[], int size1, int set2[], int size2, int result[], int *result_size) {
int i, j;
*result_size = 0;
for (i = 0; i < size1; i++) {
for (j = 0; j < size2; j++) {
if (set1[i] == set2[j]) {
break;
}
}
if (j == size2) {
result[*result_size] = set1[i];
(*result_size)++;
}
}
}
void power_set(int set[], int size) {
int i, j, k;
printf("{ }\n");
for (i = 1; i <= size; i++) {
for (j = 0; j < pow(2, size); j++) {
int subset[MAX_SIZE], subset_size = 0;
for (k = 0; k < size; k++) {
if (j & (1 << k)) {
subset[subset_size] = set[k];
subset_size++;
}
}
if (subset_size == i) {
print_set(subset, subset_size);
}
}
}
}
```
这个程序包含了输入集合、输出集合、求并集、交集、差集、幂集、任意m元子集等操作。你可以根据需要调用这些函数来实现所需的集合运算。
阅读全文