差集函数不输出怎么改

差集函数不输出怎么改


 
#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和自己的思路:在差集函数中,有一段代码:

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++;
    }
}

其中,s1为集合s2和当前集合的并集,然后再将当前集合s1中存在的元素从集合s1中删除。这里存在一个问题,即删除元素后s1的长度发生了改变,但在后续的代码中并没有更新该值,导致计算差集时出现错误,可以在删除元素后更新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++;
    }
}
s1.length = s1.length - s2.length;