怎末改能让交集和差集输出

怎末改能让交集和差集输出


#include
#include
using namespace std;
class SET{
private:
    int allnum[100];
    int length;
public:
    SET(){
        this->length=0;
        memset(this->allnum,0,sizeof(allnum));
        cout<<"Default constructo"<SET(int n){
        cout<<"Ordinary constructor"<SET(SET &s){
        cout<<"Copy constructor"<SET(){
        cout<<"Destruct the constructor"<ch='y';
        cout<<"Enter the elements of the collection"<while(ch!='\n'){
            cin>>allnum[length];
            ch=getchar();
            length++;
        }
        cout<<"Input completed"<"Prints all the elements in the array"<for(int i=0;i" ";
        }
        cout<"Complete the output"<flag=0;
        for(int i=0;iif(allnum[i]==num){
                flag=1;
                break;
            }
        }
        if(flag==0){
            allnum[length]=num;
            length++;
        }
    }
    void delnum(int num){
        int n,flag=0;
        for(n=0;nif(allnum[n]==num){
                flag=1;
                break;
            }
        }
        if(flag==1){
            for(int i=n;ielse{
        cout<<"There is no element"<SET bingji(SET &s2){
        SET s;
        for(int i=0;ilength;i++){
            s.allnum[i]=this->allnum[i];
            s.length=this->length;
        }
        for(int i=0;iSET jiaoji(SET &s2){
        SET s;
        for(int i=0;ilength;i++){
            for(int j=0;jif(this->allnum[i]==s2.allnum[j]){
                    s.allnum[s.length]==s2.allnum[j];
                    s.length++;
                }
            }
        }
        return s;
    }
    SET chaji(SET &s2)
    {
        int flag=0;
        SET s,s1;
        s1=this->bingji(s2);
        for(int i=0;ilength;i++)
        {
            flag=0;
            for(int j=0;jif(this->allnum[i]==s1.allnum[j])
                {
                    flag=1;
                    break;
                }
            }
            if(flag==0){
                s.allnum[s.length]=this->allnum[i];
                s.length++;
            }
        }
        return s;
    }
};
int main()
{
    SET s1,s2,s3;
    SET s;
    int num;
    cout<<"1.创建集合及集合的初始化"<"4.显示set中的所有元素"<"2.向集合s1中添加一个元素"<"Enter an integer to add"<>num;
    s1.addnum(num);
    s1.shownum();
    cout<<"3.从集合s3中删除一个元素"<"Enter an integer to add"<>num;
    s3.delnum(num);
    s3.shownum();
    cout<<"5.计算s1和s2的交集"<s=s1.jiaoji(s2);
    s.shownum();
    cout<<"6.计算s1和s2的并集"<s=s1.bingji(s2);
    s.shownum();
    cout<<"7.计算s1和s2的差集"<s=s1.chaji(s2);
    s.shownum();
}

参考GPT和自己的思路:如果想要让交集和差集输出,只需要在计算交集和差集的函数中添加输出语句即可。

例如,在计算交集的函数jiaoji中,可以添加如下输出语句:

cout<<"The intersection of two sets is: ";

同理,在计算差集的函数chaji中,也可以添加输出语句:

cout<<"The difference of two sets is: ";

这样,在调用这些函数时就会在输出集合元素前有标识性的语句,让输出更加清晰明了。

完整的jiaoji和chaji函数分别如下:

SET jiaoji(SET &s2){
    SET s;
    for(int i=0;i<this->length;i++){
        for(int j=0;j<s2.length;j++){
            if(this->allnum[i]==s2.allnum[j]){
                s.allnum[s.length]==s2.allnum[j];
                s.length++;
            }
        }
    }
    cout<<"The intersection of two sets is: ";
    return s;
}

SET chaji(SET &s2)
{
    int flag=0;
    SET s,s1;
    s1=this->bingji(s2);
    for(int i=0;i<this->length;i++)
    {
        flag=0;
        for(int j=0;j<s1.length;j++)
        {
            if(this->allnum[i]==s1.allnum[j])
            {
                flag=1;
                break;
            }
        }
        if(flag==0){
            s.allnum[s.length]=this->allnum[i];
            s.length++;
        }
    }
    cout<<"The difference of two sets is: ";
    return s;
}