用c++实现正整数集合的交、并、差

1—输入集合A和B
2—求集合A交B
3—求集合A并B
4—求集合A-B

代码实现

（编译环境：vs2017）

#include "pch.h"
#include  <stdio.h>
#include <iostream>
#include <stdlib.h>
using namespace std;
int array01[50];
int array02[50];
//检查重复的方法
bool CheckRepeat(int array[], int newNumber)
{

int length = 50;
for (int i = 0; i < length; i++)
{
if (newNumber == array[i])
{

return false;
}
}
return true;
}
//交集
void IN(int array1[], int array2[],int length1,int length2)
{
int newLength = length1 > length2 ? length1 : length2;
int newArray[50];
int a = 0;
for (int i = 0; i < length1; i++)
{

if (!CheckRepeat(array2, array1[i]))
{
newArray[a++] = array1[i];
}
}
cout << "交集的结果为" << endl;
for (int  i = 0; i  <  length1; i++)
{
if (newArray[i] <= 0)break;
cout << newArray[i] <<"  ";

}
}
//并集
void Union(int array1[], int array2[],int length1,int length2)
{
int newLength = length1 + length2;
int newArray[100];
for (int i = 0; i < length1; i++)
{
newArray[i] = array1[i];
}
for (int i = 0,j=0; i < length2; i++)
{
if (CheckRepeat(array1, array2[i]))
{
newArray[length1  + j] = array2[i];
j++;
}
}
cout << "并集的结果为" << endl;
for (int i = 0; i < newLength; i++)
{
if (newArray[i] <= 0)break;
cout << newArray[i] << "  ";

}

}
//差
void Difference(int array1[], int array2[],int length1,int length2)
{
int number = 0;
int array01[50];
for (int i = 0; i <length1; i++)
{
array01[i] = array1[i];
}
//以下是获取交集数组的过程
int newArray[50];
int a = 0;
for (int i = 0; i < length1; i++)
{

if (!CheckRepeat(array2, array1[i]))
{
newArray[a++] = array1[i];
}
}
//获取newArray为交集数组
for (int i = 0; i < length1; i++)
{
if (!CheckRepeat(newArray,array01[i]))
{
array01[i] = 0;
continue;
}
}
cout << "差的结果为" << endl;
for (int i = 0; i < length1; i++)
{
if (array01[i] > 0)

cout << array01[i] << "  ";

}

}
void MAIN(int array1[],int array2[],int length1,int length2)
{
int inputNum=1;
system("cls");
while (inputNum != 0)
{
cout << "2---求集合A交B" << endl;
cout << "3---求集合A并B" << endl;
cout << "4---求集合A-B" << endl;
cout << "输入0---退出" << endl;

cin >> inputNum;
switch (inputNum)
{
case 2:
IN(array1, array2, length1, length2); break;
case 3:
Union(array1, array2, length1, length2); break;
case 4:
Difference(array1, array2, length1, length2); break;
default:
break;
}
cout << "输入数字回车跳转进入下一步" << endl;
cin >> inputNum;
system("cls");
}
}

int main()
{int nums1=0;
int nums2=0;
//第一个集合输入
while (true)
{
cout << "请输入第一个集合的元素个数" << endl;

cin >> nums1;
if (nums1 < 49 && nums1 > 0)
{
break;

}
else
cout << "我只支持0~50的，这样不行哦" << endl;

}
int i = 0;
while (true)
{
int num = 0;
cout << "请输入元素" << endl;
cin >> num;
if (i ==nums1-1)
{
cout << "已经够多了，容不下了" << endl;
cout << "程序输入结束！" << endl;
array01[i] = num;
break;
}
if(num<0)
{
cout << "程序输入结束！" << endl;
break;
}
array01[i] = num;
i++;
}
cout << "第一个集合如下" << endl;
for (int i = 0; i < nums1; i++)
{
cout << array01[i]<<"  ";
}
//第二个集合输入
while (true)
{
cout << "请输入第二个集合的元素个数" << endl;

cin >> nums2;
if (nums2 < 49 && nums2 >= 0)
{
break;

}
else
cout << "我只支持0~50的，这样不行哦" << endl;

}
i = 0;
while (true)
{
int num = 0;
cout << "请输入元素" << endl;
cin >> num;
if (i == nums2-1)
{
cout << "已经够多了，容不下了" << endl;
cout << "程序输入结束！" << endl;
array02[i] = num;
break;
}
if (num < 0)
{
cout << "程序输入结束！" << endl;
break;
}
array02[i] = num;
i++;
}
cout << "第二个集合如下" << endl;
for (int i = 0; i < nums2; i++)
{
cout << array02[i] << "  ";
}
MAIN( array01,array02, nums1,  nums2);

}


运行页面部分截图

• 点赞 1
• 评论 1
• 分享
x

海报分享

扫一扫，分享海报

• 收藏 2
• 手机看

分享到微信朋友圈

x

扫一扫，手机阅读

• 打赏

打赏

尊酹江月

你的鼓励将是我创作的最大动力

C币 余额
2C币 4C币 6C币 10C币 20C币 50C币
• 一键三连

点赞Mark关注该博主, 随时了解TA的最新博文
10-24

10-06 4314
10-01 7226
03-29 1万+
10-22 4687
02-07 2129
01-04 5788
10-24 4961
07-18 1207