C++ project--the union and difference synthesis operation of sets

Title: Write a program to perform the operations of union, difference and intersection of sets, such as inputting the sets of integers {9, 5, 4, 3, 6, 7} and { 2, 4, 6, 9}, and calculate them The result of performing the union, difference, and intersection of sets.


Software used: vs2013

Note: vs2013 will end Enter and ctrl+Z as the standard input stream, while vs2015 cannot end Enter as the standard input stream, you can end ctrl + Z, but you can no longer input, even if you re-enable cin. So it is recommended to use vs2013 to write this program.

---- When the user uses ctrl + z to end the input, it means that the standard input file has been read and read, so the subsequent cin will be invalid (no input, just skip it). So it must be done

cin.clear ();

cin.sync();

to restart cin

Weaknesses: The set entered by the user may not satisfy the rules of the set. For example: have the same elements. In addition, this program does not consider the case of empty sets. Improve it after a while.

Documents used:

source.cpp:

#include <iostream>
using namespace std;
#define M 50
#include "class.h"

int main()
{
	cout << "输入集合s1\n";
	Set s1;
	cout << "输入集合s2\n";
	cin.clear(); //重新启用标准输入
	cin.sync();
	Set s2;
	Set s3(0);

	cout << "\n----------------";
	cout << "\n集合1\n";
	s1.show();
	cout << "\n集合2\n";
	s2.show();
	cout << "\n----------------\n";
	cout << "\n--集合1∪集合2\n";
	s3 = s1 + s2;
	s3.show();
	cout << "\n--集合1-集合2\n";
	s3 = s1 - s2;
	s3.show();
	cout << "\n--集合1∩集合2\n";
	s3 = s1*s2;
	s3.show();
	cout << "\n";
	system("pause");
	return 0;
}


class.h:

#pragma once

class Set
{
public:
	Set();//初始化集合	17行
	Set(int i);//不进行初始化,int i只用于区别set()		62
	void show();//输出集合		29
	Set operator+(Set &);//集合的并运算	36
	Set operator-(Set &);//集合的差运算	66
	Set operator*(Set &);//集合的交运算	100
private:
	int set[M];
	int num;//用于计数集合的数字个数
};

Set::Set()
{
	FILE* fp  = stdin;//定义一个文件指针指向标准输入
	num = 0;
	cout << "使用Ctrl + Z结束输入\n";
	for (int i = 0; i < M; i++)
	{
		if (fp->_cnt == 1)break;//如果当前位置为1,则说明用户输入的内容已经读入完成
		cin >> set[i];
		num++;
	}
}
void Set::show()
{
	for (int i = 0; i < num; i++)
	{
		cout << set[i] << ' ';
	}
}
Set Set::operator+(Set &s2)
{
	Set temp(0);//将计算结果添加到这个对象
	temp = s2;//将s2集合所有元素赋值给temp集合
	int sign;//作为标志
	int number = 0;
	for (int i = 0; i<num; i++)
	{
		sign = 1;//表示可以将set[i]添加到s2.set[]中
		for (int j = 0; j < s2.num; j++)
		{
			if (set[i] == s2.set[j])
			{
				sign = 0;//表示元素有重复,不能添加
				break;
			}
		}
		if (sign)
		{
			number++;
			temp.set[number + s2.num -1] = set[i];
		}
	}
	temp.num = number + s2.num;
	return temp;
}
inline Set::Set(int i)
{

}
Set Set::operator-(Set &S2)
{
	Set T(0);//将计算结果添加到这个对象
	int *p = new int[num];//用记录两个集合公有元素的下标
	int *ptemp = p;//用于移动,不改变原来p的指向,用于ptemp指针归位
	int number = 0;
	for (int i = 0; i < S2.num; i++)
	{
		for (int j = 0; j < num; j++)
		{
			if (S2.set[i] == set[j])
			{
				*(ptemp++) = j;//将下标记录
				break;
			}
		}
		*ptemp = -1;//将-1作为下标结束的标志,因为下标没有-1
	}
	for (int i = 0; i < num; i++)
	{
		ptemp = p;//将指针ptemp恢复原来的指向
		while (*(ptemp) != -1)
		{
			if (i == *(ptemp)) goto NEXT;//下标相同,则退出循环,跳过保存
			ptemp++;
		}
		T.set[number++] = set[i];
	NEXT:
		;
	}
	T.num = number;	  //T集合的元素个数
	delete []p;
	return T;
}
Set Set::operator*(Set &S2)
{
	Set T(0);//将计算结果添加到这个对象
	int *p = new int[num];//用记录两个集合公有元素的下标
	int *ptemp = p;//用于移动,不改变原来p的指向,用户ptemp归位
	int number = 0;
	for (int i = 0; i < S2.num; i++)
	{
		for (int j = 0; j < num; j++)
		{
			if (S2.set[i] == set[j])
			{
				*(ptemp++) = j;//将下标记录
				break;
			}
		}
		*ptemp = -1;//将-1作为下标结束的标志,因为下标没有-1
	}
	for (int i = 0; i < num; i++)
	{
		ptemp = p;//将指针ptemp恢复原来的指向
		while (*(ptemp) != -1)
		{
			if (i == *(ptemp))
			{
				T.set[number++] = set[i];
				break;
			}//下标相同,则保存并退出循环
			ptemp++;
		}
		
	
	}
	T.num = number;	  //T集合的元素个数
	delete[]p;
	return T;
}



operation result:




Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326218337&siteId=291194637