当前位置:网站首页>C language uses arrays to realize the intersection, union, difference and complement of sets
C language uses arrays to realize the intersection, union, difference and complement of sets
2022-07-05 07:40:00 【LCH Nan'an】
Start with the result chart :
Complete code :
#include <stdio.h>
// Initialize collection
int a[1024] = {0}, b[1024] = {0}, c[1024] = {0}, d[1024] = {0}, e[1024] = {0};
int com[1024] = {0}, temp[1024] = {0}, temp1[1024] = {0};
// display
void show()
{
printf("\t************** Set operation interface **************\n");
printf("\t\t**** 1-** Please enter the set first A and B Element is performing the following operations ******\n");
printf("\t\t**** 2-** Please assemble A∩B******\n");
printf("\t\t**** 3-** Please assemble AUB******\n");
printf("\t\t**** 4-** Please assemble A-B******\n");
printf("\t\t**** 5-** Please assemble CeA******\n");
printf("\t\t**** 0-** Exit the operation page ******\n");
printf("\t******************************************\n");
}
// aggregate AB Element filling
void input(int x, int y)
{
int i, j;
printf(" Please enter A Medium element :\n");
for (i = 0; i < x; i++)
{
scanf("%d", &a[i]);
getchar();
for (j = 0; j < i; j++)
{
if (a[i] == a[j])
{
printf(" Duplicate element occurred ");
a[i] = 0;
}
}
}
printf(" Please enter B Medium element :\n");
for (i = 0; i < y; i++)
{
scanf("%d", &b[i]);
getchar();
for (j = 0; j < i; j++)
{
if (b[i] == b[j])
{
printf(" Duplicate element occurred ");
b[i] = 0;
}
}
}
}
// aggregate A,B Intersection
int Mixed(int x, int y)
{
int i, j, k = 0;
for (i = 0; i < x; i++)
{
for (j = 0; j < y; j++)
{
if (a[i] == b[j])
{ // testing AB The same elements between sets
c[k] = a[i]; // Save the same element to the empty set
k++; // Statistics c The total number in the set
}
}
}
printf("A∩B={");
for (i = 0; i < k - 1; i++)
{ //k-1 effect : Reserve a place
printf(" %d,", c[i]);
}
printf(" %d}\n", c[k - 1]); // Output the last element and attach }
getchar();
}
// aggregate A,B Union
int Union(int x, int y)
{
int i, j, k = 0, m = 0, n;
for (i = 0; i < x; i++)
{
for (j = 0; j < y; j++)
{
if (a[i] == b[j])
{ // testing AB The same elements between sets
c[k] = a[i]; // Save the same element to the empty set
k++; // Statistics c The total number in the set
}
}
}
for (i = 0; i < x; i++)
{ // take A Set elements in com Collection
com[m] = a[i];
m++;
}
for (i = 0; i < y; i++)
{ // take B Set elements in com Collection
com[m] = b[i];
m++;
}
for (i = 0; i < x + y - k; i++)
{ //x+y-k by A,B Set the total number of all elements minus the same elements
for (j = i + 1; j < m; j++)
{ //
if (com[j] == com[i])
{
for (n = j; n < m; n++)
{
com[n] = com[n + 1];
}
break;
}
}
}
printf("AUB={");
for (i = 0; i < x + y - k - 1; i++)
{
printf(" %d,", com[i]);
}
printf(" %d}\n", com[x + y - k - 1]);
getchar();
}
// aggregate A,B The difference operation of
int Difference(int x, int y)
{
int i, j, k = 0;
int m;
for (i = 0; i < x; i++)
{
for (j = 0; j < y; j++)
{
if (a[i] == b[j])
{
c[k] = a[i];
k++;
}
}
} // The above is the intersection part , obtain A,B The total number of identical elements in the set
for (i = 0; i < x; i++)
{ // take A Set elements in D Collection
d[i] = a[i];
}
for (i = 0; i < x - k; i++)
{ // Remove the number of the same elements , Cycle through
for (j = 0; j < k; j++)
{
if (d[i] == c[j]) // use D The elements in the set are related to A,B Intersection set of C Compare elements
{
for (m = i; m < x; m++)
{ // If D In the set i Elements and sets C identical , be i The element after moves forward one bit
d[m] = d[m + 1];
}
i--;
break;
}
}
}
printf("A-B={");
for (i = 0; i < x - k - 1; i++)
{
printf(" %d,", d[i]); // Output difference set
}
printf(" %d}\n", d[x - k - 1]); // Enclosed }
getchar();
}
// aggregate E Fill of
void inputE(int elngth)
{
int i, j;
printf(" Please enter E Medium element :\n");
for (i = 0; i < elngth; i++)
{
scanf("%d", &e[i]);
getchar();
for (j = 0; j < i; j++)
{
if (e[i] == e[j])
{
printf(" Duplicate element occurred ");
e[i] = 0;
}
}
}
}
// aggregate A The complement of E
int Left(int x, int elength)
{ // Same as difference set
if (elength < x)
{
printf("E The number of elements should be greater than A Number of elements in \n");
return 0;
}
int i, j, k = 0, m = 0;
for (i = 0; i < elength; i++)
{
for (j = 0; j < x; j++)
{
if (e[i] == a[j])
{
temp1[k] = e[i];
k++;
}
}
}
for (i = 0; i < elength; i++)
{
temp[i] = e[i];
}
for (i = 0; i < elength - k; i++)// Remove the number of the same elements , Cycle through
{
for (j = 0; j < k; j++)
{
if (temp[i] == temp1[j]) // use E The elements in the set are related to A,E The intersection of compares elements
{
for (m = i; m < elength; m++)
{
temp[m] = temp[m + 1];
}
i--;
break;
}
}
}
printf("CeA={");
for (i = 0; i < elength - k - 1; i++)
{
printf(" %d,", temp[i]); // Output complement
}
printf(" %d}\n", temp[elength - k - 1]); // Enclosed }
//getchar();
}
// The main function
int main()
{
int x, y, elngth, choice;
while (1)
{
show();
printf(" Please enter the operation number :\n");
scanf("%d", &choice);
if (choice == 0)
{
printf(" Exit the operation page \n");
return 0;
}
else
{
switch (choice)
{
case 1:
printf(" Please enter A,B The number of set elements \n");
scanf("%d %d", &x, &y);
input(x, y);
printf(" Please press enter to continue !\n");
getchar();
break;
case 2:
Mixed(x, y);
printf(" Please press enter to continue !\n");
getchar();
break;
case 3:
Union(x, y);
printf(" Please press enter to continue !\n");
getchar();
break;
case 4:
Difference(x, y);
printf(" Please press enter to continue !\n");
getchar();
break;
case 5:
printf("A The length of the set :%d\n", x);
printf(" Please enter the set E Number of elements :\n");
scanf("%d", &elngth);
inputE(elngth);
Left(x, elngth);
printf(" Please press enter to continue !\n");
getchar();
break;
default:
break;
}
}
}
}
边栏推荐
- A series of problems in offline installation of automated test environment (ride)
- How to delete the virus of inserting USB flash disk copy of shortcut to
- The folder directly enters CMD mode, with the same folder location
- Don't confuse the use difference between series / and / *
- Microservice registry Nacos introduction
- Idea common settings
- Leetcode solution - number of islands
- Deepin get file (folder) list
- Play with grpc - go deep into concepts and principles
- CADD课程学习(6)-- 获得已有的虚拟化合物库(Drugbank、ZINC)
猜你喜欢
And play the little chestnut of dynamic agent
Line test -- data analysis -- FB -- teacher Gao Zhao
Numpy——1.数组的创建
剑指 Offer 56 数组中数字出现的次数(异或)
Rough notes of C language (2) -- constants
With the help of Navicat for MySQL software, the data of a database table in different or the same database link is copied to another database table
Differences between pycharm and idle and process -- join() in vs Code
Latex notes
I 用c l 栈与队列的相互实现
Idea common settings
随机推荐
Day06 class variables instance variables local variables constant variables naming conventions
Could NOT find XXX (missing: XXX_LIBRARY XXX_DIR)
How to modify the file path of Jupiter notebook under miniconda
With the help of Navicat for MySQL software, the data of a database table in different or the same database link is copied to another database table
Detailed explanation of miracast Technology (I): Wi Fi display
Use stm32cubemx tool to write the demo program of FreeRTOS
Don't confuse the use difference between series / and / *
I 用c I 实现队列
II Simple NSIS installation package
Ugnx12.0 initialization crash, initialization error (-15)
GBK error in web page Chinese display (print, etc.), solution
TCP and UDP
NSIS finds out whether the file exists and sets the installation path
Leetcode solution - number of islands
CADD课程学习(6)-- 获得已有的虚拟化合物库(Drugbank、ZINC)
Opendrive arc drawing script
Linked list (establishment, deletion, insertion and printing of one-way linked list)
Microservice registry Nacos introduction
"Source code interpretation" famous programmer TJ's only library
Day09 how to create packages import package naming conventions Alibaba Development Manual