离散数学大作业——C++实现集合的基本运算

项目描述:可以实现从键盘输入集合,可以实现两个集合求解交集、差集、并集、补集、对称差等基本运算。运行后的界面如下所示:

程序源代码如下:

#include<iostream>
#include<vector>
#include<string>
using namespace std;

//全集
vector<int> UniversalSet()
{
	vector<int> universalSet;
	string setName1 = "全集";
	universalSet = { 0,1,2,3,4,5,6,7,8,9 };
	return universalSet;
}

//除去集合中相等的元素
vector<int> RemoveSameElement(vector<int> set)
{
	int length = set.size();
	vector<int>temp;
	for (int i = 0; i < length; i++)
	{
		for (int j = 0; j < length - 1; j++)
		{
			if (set[i] == set[j])
			{
				break;
			}
		}
		bool theSame = false;
		if (temp.size() != 0)
		{
			for (unsigned int k = 0; k < temp.size(); k++)
			{
				if (set[i] == temp[k])
				{
					theSame = true;
					break;
				}
			}
		}
		if (!theSame)
		{
			temp.push_back(set[i]);
		}
	}
	return temp;
}

// 从小到大排序:冒泡排序
vector<int> BubbleSort(vector<int> set)
{

	for (unsigned int i = 0; i < set.size() - 1; i++)//进行数组长度-1次比较
	{
		for (unsigned int j = 0; j < set.size() - i - 1; j++)//已排序的第len-i-1个后的数不用进行比较
		{
			if (set[j] > set[j + 1])
			{
				int temp = set[j];
				set[j] = set[j + 1];
				set[j + 1] = temp;
			}
		}
	}
	set = RemoveSameElement(set);
	return set;
}

//显示结果
void ShowResult(vector<int> set, string setName)
{
	if (set.size() == 0)
	{
		cout << setName << "为空集!" << endl;
	}
	else
	{

		set = BubbleSort(set);
		cout << setName << ":(";
		for (unsigned int k = 0; k < set.size(); k++)
		{
			if (k == set.size() - 1)
			{
				cout << set[k];
			}
			else
			{
				cout << set[k] << ",";
			}
		}
		cout << ")" << endl;
	}
}

//交集
void Intersection(vector<int>set_A, vector<int>set_B)
{
	string setName = "交集";
	vector<int> interSet;
	int length_1 = set_A.size();//集合A元素个数
	int length_2 = set_B.size();//集合B元素个数
	for (int i = 0; i < length_1; i++)
	{
		for (int j = 0; j < length_2; j++)
		{
			if (set_A[i] == set_B[j])
			{
				interSet.push_back(set_A[i]);
			}
		}
	}
	ShowResult(interSet, setName);
}

//差集
void Subtraction(vector<int> set_A, vector<int> set_B, string subName)
{
	vector<int> subSet;
	int length_1 = set_A.size();//集合A元素个数
	int length_2 = set_B.size();//集合B元素个数
	for (int i = 0; i < length_1; i++)
	{
		subSet.push_back(set_A[i]);
	}
	int elementPos = 0;
	for (int j = 0; j < length_2; j++)
	{
		for (unsigned int k = 0; k < subSet.size(); k++)
		{
			elementPos = k;
			if (set_B[j] == subSet[k])
			{
				break;
			}
		}
		if (subSet.size() != 0)
		{
			if (set_B[j] == subSet[elementPos])
			{
				subSet.erase(subSet.begin() + elementPos);
			}
		}

	}
	ShowResult(subSet, subName);
}

//并集
void Union(vector<int> set_A, vector<int> set_B)
{
	vector<int> unionSet;
	int length_1 = set_A.size();//集合A元素个数
	int length_2 = set_B.size();//集合B元素个数
	for (int i = 0; i < length_1; i++)
	{
		unionSet.push_back(set_A[i]);
	}
	for (int j = 0; j < length_2; j++)
	{
		bool difference = true;
		for (int i = 0; i < length_1; i++)
		{
			if (unionSet[i] == set_B[j])
			{
				difference = false;
				break;
			}
		}
		if (difference)
		{
			unionSet.push_back(set_B[j]);
		}
	}
	ShowResult(unionSet, "并集");
}

//补集
void ComplementOperation(vector<int> set, string setName)
{
	vector<int>complementarySet;
	vector<int>universalSet = UniversalSet();
	int length_1 = set.size();
	int length_2 = universalSet.size();
	for (int i = 0; i < length_2; i++)
	{
		bool difference = true;
		for (int j = 0; j < length_1; j++)
		{
			if (universalSet[i] == set[j])
			{
				difference = false;
				break;
			}
		}
		if (difference)
		{
			complementarySet.push_back(universalSet[i]);
		}
	}
	ShowResult(complementarySet, setName);
}

//对称差
void SysmmetricDifference(vector<int>set_A, vector<int>set_B)
{
	vector<int> set;//对称差
	vector<int> sumSet;//合集
	vector<int> interSet;//交集
	int length_1 = set_A.size();//集合A元素个数
	int length_2 = set_B.size();//集合B元素个数
	for (int i = 0; i < length_1; i++)
	{
		sumSet.push_back(set_A[i]);
	}
	for (int i = 0; i < length_2; i++)
	{
		sumSet.push_back(set_B[i]);
	}
	sumSet = BubbleSort(sumSet);//排序合集
	for (unsigned int i = 0; i < sumSet.size() - 1; i++)
	{
		if (sumSet[i] == sumSet[i + 1])
		{
			interSet.push_back(sumSet[i]);
		}
	}
	for (unsigned int i = 0; i < sumSet.size(); i++)
	{
		bool isDifference = true;
		for (unsigned int j = 0; j < interSet.size(); j++)
		{
			if (sumSet[i] == interSet[j])
			{
				isDifference = false;
				break;
			}
		}
		if (isDifference)
		{
			set.push_back(sumSet[i]);
		}
	}
	ShowResult(set, "对称差");
}

//判断集合是否相等
void IsEqual(vector<int>set_A, vector<int>set_B)
{
	int length_1 = set_A.size();//集合1元素个数
	int length_2 = set_B.size();//集合2元素个数
	if (length_1 == length_2)
	{
		bool difference = true;
		for (int i = 0; i < length_1; i++)
		{
			if (set_A[i] == set_B[i])
			{
				difference = false;
			}
			else
			{
				difference = true;
				cout << "集合不相等!" << endl;
				break;
			}
		}
		if (!difference)
		{
			cout << "集合相等!" << endl;
		}
	}
	else
	{
		cout << "集合A、B不相等!" << endl;
	}
}

vector<int> InputSet(vector<int> set, string setName)
{
	cout << "请输入集合" << setName << endl;
	int length;
	int temp;
	cout << "请输入集合" << setName << "的元素个数:" << endl;
	cin >> length;
	cout << "请分别输入集合" << setName << "的元素" << endl;
	for (int i = 0; i < length; i++)
	{
		cin >> temp;
		if (set.size() != 0)
		{
			bool difference = false;
			while (!difference)
			{
				for (unsigned int j = 0; j < set.size(); j++)
				{
					if (temp != set[j])
					{
						difference = true;
					}
					else
					{
						cout << "该数已存在集合中!请重新输入!" << endl;
						difference = false;
						cin >> temp;
						break;
					}
				}
			}
			if (difference)
			{
				set.push_back(temp);
			}
		}
		else
		{
			set.push_back(temp);
		}
	}
	set = BubbleSort(set);
	return set;
}

void Menu()
{
	cout << "***********************************************" << endl;
	cout << "输入相应数字进行操作:" << endl;
	cout << "1、求交集 " << endl
		<< "2、求差集 " << endl
		<< "3、求并集" << endl
		<< "4、求补集 " << endl
		<< "5、求对称差" << endl
		<< "6、判断两集合相等" << endl
		<< "7、重新输入集合" << endl
		<< "8、退出程序" << endl;
	cout << "***********************************************" << endl;
}

int main()
{
	vector<int>set_A;//定义两个集合数组
	vector<int>set_B;
	int operateNum;
	bool reInput = true;
	while (reInput)
	{
		//开始时就从键盘获取用户输入两个集合
		set_A = InputSet(set_A, "A");
		set_B = InputSet(set_B, "B");
		ShowResult(set_A, "集合A:");
		ShowResult(set_B, "集合B:");
		Menu();
		bool isContinue = true;

		//用户想要进行的操作
		while (isContinue)
		{
			cin >> operateNum;
			switch (operateNum)
			{
			case 1:	Intersection(set_A, set_B); break;//交集
			case 2:	Subtraction(set_A, set_B, "差集A-B");//差集 
				    Subtraction(set_B, set_A, "差集B-A"); break;
			case 3: Union(set_A, set_B); break;//并集
			case 4: ComplementOperation(set_A, "A的补集");//补集
				    ComplementOperation(set_B, "B的补集"); break;
			case 5: SysmmetricDifference(set_A, set_B); break;//对称差
			case 6: IsEqual(set_A, set_B); break;//判断相等
			case 7: isContinue = false; //重新输入集合
				    set_A.clear(); 
					set_B.clear(); break;
			case 8: isContinue = false; reInput = false; break;//退出
			default:cout << "请重新输入" << endl;
				break;
			}
		}
	}
}

 喜欢的话就点个赞呀~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值