使用高级程序设计语言实现集合的交并差运算

时间:2023-02-08 21:06:09
R: 
a1 b1 c1
a1 b2 c2
a2 b2 c1
S:
a1 b2 c2
a1 b3 c2
a2 b2 c1

实际输入数据为:
3 3 3
a1 b1 c1
a1 b2 c2
a2 b2 c1
a1 b2 c2
a1 b3 c2
a2 b2 c1

其中R的行数为rank1=3,S的行数为rank3=3,RS的列数为col=3。

交运算:
#include <bits/stdc++.h>
using namespace std;
vector<string> R[200];
vector<string> S[200];
vector<string> ans[200];
int Jiao(int rank1, int rank2, int col)
{
int tot = 0;
for (int i = 0; i < rank1; i++)
{
for (int j = 0; j < rank2; j++)
{
for (int k = 0; k < col; k++)
{

if (R[i][k] == S[j][k])
ans[tot].push_back(R[i][k]);
else
{
ans[tot].clear();
tot--;
break;
}
}
tot++;
}
}
return tot;
}
int main()
{
int rank1, rank2, col;
cin >> rank1 >> rank2 >> col;
for (int i = 0; i < rank1; i++)
{
for (int j = 0; j < col; j++)
{
string c;
cin >> c;
R[i].push_back(c);
}
}
for (int i = 0; i < rank2; i++)
{
for (int j = 0; j < col; j++)
{
string c;
cin >> c;
S[i].push_back(c);
}
}
int rank3 = Jiao(rank1, rank2, col);
for (int i = 0; i < rank3; i++)
{
for (auto p : ans[i])
{
cout << p << " ";
}
cout << endl;
}
}
并运算:
#include <bits/stdc++.h>
using namespace std;
vector<string> R[200];
vector<string> S[200];
vector<string> ans[200];
int bing(int rank1, int rank2, int col)
{
int tot = 0;
int flag = 0;
for (int j = 0; j < rank2; j++)
{
for (int k = 0; k < col; k++)
{
ans[tot].push_back(S[j][k]);
}
tot++;
}
for (int i = 0; i < rank1; i++)
{
int flag = 0;
for (int j = 0; j < rank2; j++)
{
int flag2=1;
for (int k = 0; k < col; k++)
{
if (R[i][k] != S[j][k])
{
flag2 = 0;
break;
}
}
if(flag2==1)
{
flag=1;
break;
}
}
if (flag)
continue;
else
{
for (int k = 0; k < R[i].size(); k++)
{
ans[tot].push_back(R[i][k]);
}
tot++;
}
}

return tot;
}
int main()
{
int rank1, rank2,rank3, col;
cin >> rank1 >> rank2 >> col;
for (int i = 0; i < rank1; i++)
{
for (int j = 0; j < col; j++)
{
string c;
cin >> c;
R[i].push_back(c);
}
}
for (int i = 0; i < rank2; i++)
{
for (int j = 0; j < col; j++)
{
string c;
cin >> c;
S[i].push_back(c);
}
}
cout << "-------Bing------------\n";
rank3 = bing(rank1, rank2, col);
for (int i = 0; i < rank3; i++)
{
for (auto p : ans[i])
{
cout << p << " ";
}
cout << endl;
}
}
差运算:
#include <bits/stdc++.h>
using namespace std;
vector<string> R[200];
vector<string> S[200];
vector<string> ans[200];
int cha(int rank1, int rank2,int col)
{
int tot = 0;
int flag = 0;
for (int i = 0; i < rank1; i++)
{
int flag = 0;
for (int j = 0; j < rank2; j++)
{
int flag2=0;
for (int k = 0; k <col; k++)
{
if (R[i][k] == S[j][k])
;
else
flag2 = 1;
}
if(flag2==0)
{
flag=1;
break;
}
}
if (flag){
for (int k = 0; k <col; k++)
{
ans[tot].push_back(R[i][k]);
}
tot++;
}
}
return tot;
}
int main()
{
int rank1, rank2, rank3, col;
cin >> rank1 >> rank2 >> col;
for (int i = 0; i < rank1; i++)
{
for (int j = 0; j < col; j++)
{
string c;
cin >> c;
R[i].push_back(c);
}
}
for (int i = 0; i < rank2; i++)
{
for (int j = 0; j < col; j++)
{
string c;
cin >> c;
S[i].push_back(c);
}
}
cout << "-------cha------------\n";

rank3 = cha(rank1, rank2,col);
for (int i = 0; i < rank3; i++)
{
for (auto p : ans[i])
{
cout << p << " ";
}
cout << endl;
}
}