C++離散数学の集合と関係演算、性質判断と閉包演算を実現する

4579 ワード

私はプログラミングを試みて教科書の上で集合と関係の関連内容を実現して、例えば集合の逆演算、複合演算、集合の上で関係の性質の判断と閉包演算など、基本的な判断方法はすべて定義法です.
コードは次のとおりです.
#include
#include
#include
using namespace std;
typedef vector> v_v;

void PrintMatrix(const v_v& v)   //    
{
	for (auto &i : v)
	{
		for (auto j : i)
			cout << j << " ";
		cout << endl;
	}
}

void Get_Matrix(v_v& v,int num)    //     
{
	vector A, B;
	int a, b, n;
	if (num == 1)
	{
		cout << "              " << endl;
		cin >> n;
		v.resize(n);     //   v   
		while (n--)
		{
			cin >> a;
			A.push_back(a);
		}
		cout << "              " << endl;
		cin >> n;
		for (int i = 0; i != v.size(); ++i)
			v[i].resize(n);
		while (n--)
		{
			cin >> b;
			B.push_back(b);
		}
	}
	else if (num == 2)
	{
		cout << "              " << endl;
		cin >> n;
		v.resize(n);
		for (int i = 0; i != v.size(); ++i)
			v[i].resize(n);
		while (n--)
		{
			cin >> a;
			A.push_back(a);
		}
		B = A;
	}
	cout << "               " << endl;
	cin >> n;
	while (n--)
	{
		cin >> a >> b;
		auto a_loc = find(A.begin(), A.end(), a);
		auto b_loc = find(B.begin(), B.end(), b);
		int a_index = distance(A.begin(), a_loc);      // a,b   
		int b_index = distance(B.begin(), b_loc);
		v[a_index][b_index] = 1;
	}
	cout << "     :" << endl;
	PrintMatrix(v);
}

v_v MatrixInver(const v_v& v)     //     
{
	int row = v.size();
	int col = v[0].size();
	v_v v_inv(col);
	for (int i = 0; i != col; ++i)
		v_inv[i].resize(row);
	for (int i = 0; i != row; ++i)
		for (int j = 0; j != col; ++j)
			v_inv[j][i] = v[i][j];
	return v_inv;
}

v_v MatrixMul(const v_v& v1, const v_v& v2)   //      
{
	v_v v_mul;
	int m = v1.size(), n = v2.size(), p = v2[0].size();
	v_mul.resize(m);
	for (int i = 0; i != m; ++i)
		v_mul[i].resize(p);
	for (int i = 0; i != m; ++i)
		for (int j = 0; j != p; ++j)
			for (int k = 0; k != n; ++k)
				v_mul[i][j] |= v1[i][k] * v2[k][j];
	return v_mul;
}

void Reflex(const v_v& v)
{
	int count = 0;
	v_v v1(v);
	for (int i = 0; i != v.size(); ++i)
		if (v[i][i] == 1)
			++count;
	if (count == v.size())
		cout << "        " << endl;
	if (count == 0)
		cout << "         " << endl;
	cout << "           " << endl;
	for (int i = 0; i != v.size(); ++i)
		v1[i][i] = 1;
	PrintMatrix(v1);
}

void Symmetry(const v_v& v)
{
	v_v v1(v);
	bool flag1 = true, flag2 = true;
	for (int i = 0; i != v.size(); ++i)
	{
		for (int j = 0; j != v.size(); ++j)
		{
			if (i != j)
			{
				if (v[i][j] != v[j][i])
					flag1 = false;
				else
					flag2 = false;
			}
		}
	}
	if (flag1)
		cout << "        " << endl;
	if(flag2)
		cout << "         " << endl;
	cout << "           " << endl;
	auto inver = MatrixInver(v);
	for (int i = 0; i != v.size(); ++i)
	{
		for (int j = 0; j != v.size(); ++j)
		{
			v1[i][j] |= inver[i][j];
		}
	}
	PrintMatrix(v1);
}

void Trans(const v_v& v)
{
	v_v v1(v);
	bool flag = true;
	for (int i = 0; i != v.size(); ++i)              //Warshall       
	{
		for (int j = 0; j != v.size(); ++j)
		{
			if (v[i][j])
			{
				for (int k = 0; k != v.size(); ++k)
				{
					if (v[j][k])
						if (v[i][k] == 0)
							flag = false;
				}
			}
		}
	}
	if (flag)
		cout << "        " << endl;
	cout << "           " << endl;
	for (int i = 0; i != v.size(); ++i)
	{
		for (int j = 0; j != v.size(); ++j)
		{
			if (v[j][i])
				for (int k = 0; k != v.size(); ++k)
					v1[j][k] |= v[i][k];
		}
	}
	PrintMatrix(v1);
}

int main()
{
	int next;
	v_v v1, v2, v_mul;
	cout << "1、        \t2、               " << endl;
	cin >> next;
	if (next == 1)
	{
		Get_Matrix(v1,1);
		cout << "    :1、   \t2、    " << endl;
		cin >> next;
		if (next == 1)
		{
			cout << "    " << endl;
			PrintMatrix(MatrixInver(v1));
		}
		else if (next == 2)
		{
			cout << "            " << endl;
			Get_Matrix(v2,1);
			cout << "    " << endl;
			PrintMatrix(MatrixMul(v1, v2));
		}
	}
	else if (next == 2)
	{
		Get_Matrix(v1,2);
		cout << endl;
		Reflex(v1);
		cout << endl;
		Symmetry(v1);
		cout << endl;
		Trans(v1);
		cout << endl;
	}
	system("pause");
}