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

2020-10-28 11:29:57 浏览数 (1)

内容:

利用高级语言实现集合交、差、并操作

实验数据文件:
代码语言:javascript复制
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。

代码实现:
4.1.交运算:
代码语言:javascript复制
#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;
    }
}

运行结果:

4.2 并运算:
代码语言:javascript复制
#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;
    }
}

运行结果

4.3差运算
代码语言:javascript复制
#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;
    }
}

运行结果

写在最后: Name:风骨散人,目前是一名双非在校大学生,预计考研,热爱编程,热爱技术,喜欢分享,知识无界,希望我的分享可以帮到你!名字的含义:我想有一天我能有能力随心所欲不逾矩,不总是向生活低头,有能力让家人拥有富足的生活而不是为了生计而到处奔波。“世人慌慌张张,不过是图碎银几两。偏偏这碎银几两,能解世间惆怅,可让父母安康,可护幼子成长 …”

0 人点赞