_Phoebe__ 2022-04-11 20:24 采纳率: 96.9%
浏览 37
已结题

这个代码编译不通过想知道怎么改

要求如下


//(1)定义Set类,编写Set类构造函数和析构函数,使它们完成打开文件读入元素到数组中和将数组中元素写入指定的磁盘文件中;
//(2)编写集合增加一个元素的成员函数insert(int x);(注:如果集合中已有相同的元素,则不再加入)
//(3)编写删除一个元素的成员函数erase(int x);
//(4)编写判别元素是否属于集合的成员函数isInSet(int x);
//(5)编写求两个集合交的函数成员intersection(Set &s);
//(6)编写求两个集合差的成员函数intersetion(Set &s);
//(7)重载+运算符,用于求两个集合的并;
//(8)编写集合排序的成员函数sort();(从大到小排序)
//(9)重载<<运算符,用于在屏幕上输出集合元素。
#include<bits/stdc++.h>
using namespace std; 
class AbsCollection{
protected:
    int *iPtr;        //指向数组的指针
    int last;        //数组最后一个元素的位置
    int maxSize;    //数组总体长度
public:
    AbsCollection(int size = 100){
        maxSize = size;
        iPtr = new int[maxSize];
        last = -1;
    };
    virtual ~AbsCollection();
    void OutputSet();  //输出集合中元素
};

void AbsCollection::OutputSet()
{
    cout<<"The number of elements:"<<last+1<<endl;
    for(int i=0;i<=last;i++)
    {
        cout<<iPtr[i]<<" ";
    }
    cout<<endl;
}
class Set:public AbsCollection
{
private:
    char *fileName; //存放数据的文件
public:
    Set(int size = 100, const char *szFileName="");//从文件读取数据存入数组(要判断是否有重复元素)
    virtual ~Set();        //析构函数,释放内存并将数据写入文件
    void insert(int x);    
    void erase(int x);
    int isInSet(int x);     //判断x是否已经存在于集合中
    friend ostream& operator<<(ostream& os, Set& s);
    void intersetion(Set &s);//求集合的交(*this与s的交,结果放在*this中)
    void operator+(Set &s);     //求集合的并(*this与s的并,结果放在*this中)
    void diff(Set &s);         //求集合的差(*this与s的差,结果放在*this中)
    set& operator=(Set &s);  //赋值拷贝函数
    void sort();             //将集合中元素从大到小排序
};

int Set::isInSet(int x)
{
    int flag = 0;
    for(int i=0; i<last+1; i++)
    {
        if(x==iPtr[i])
            flag = 0;
        else
            flag = 1;
    } 
    return flag; 
}
void Set::insert(int x)
{
    if(!isInSet(x)){
        if(last+1<maxSize){
        iPtr[++last]  =  x; 
    }
    else{
        Set tempSet(maxSize+1);
        for(int i = 0;i<= last;i++){
            tempSet.iPtr[i] = iPtr[i];
            tempSet.last++;
        }
        delete []iPtr;
        last = -1;
        iPtr = new int[++maxSize];
        for(int i = 0;i<= tempSet.last;i++){
            iPtr[i] = tempSet.iPtr[i];
            last++;
        }
        last++;
        iPtr[last] = x;
    }
}
Set::Set(int size, const char *szFileName):AbsCollection(size)
{   
    if(szFileName =="")
    {
        fileName=Null;
        return;
    }
    fileName = new char[strlen(szFileName)];
    fstream fs;
    fs.open(szFileName,ios::in);
    if(!fs.isopen())
    {
        cout<<"open error!"<<endl;
        abort();
    } 
    int x;
    while(fs>>x)
    {
        if(!isInSet(x))
        {
            insert(x);
        }
    }
    fs.close();
}
Set::~Set()
{    
    if(fileName){
    fstream fs(fileName,ios::out|ios::trunc)
    for(int *ip=iPtr;ip<=iPtr+last;)
    {
        fs<<*ip++<<" ";
    }
    fs.close();
    delete fileName;
}
    if(iPtr)
    {
        delete []iPtr;
    }
}
void Set::erase(int x)
{
    
    for(int i = 0;i<=last;i++){
        if(iPtr[i]==x){
            break;
        }
        if(i<=last){
            while(i!=last){
                iPtr[i]=iPtr[i++];
            }
        }
    }
    last--;
}
ostream&operator<<(ostream& os, Set& s)
{
    
    for(int i = 0;i<=last;i++)
    {
        os<<iPtr[i]<<" ";
    }
    return os;
}
void Set::intersetion(Set &s)
{
    Set tempSet(maxSize);
    for(int i = 0;i<=last;i++)
    {
        if(s.isInSet(iPtr[i]))
        {
            tempSet.insert(iPtr[i]);
        }
    }
    *this = tempSet;
    return *this;
}
void Set::operator+(Set &s){
    for(int i=0;i<=s.last;i++){
        if(!isInSet(s.iPtr[i])){
            insert(s.iPtr[i]); 
        } 
    }
}
void Set::diff(Set &s)
{
    Set tempSet(maxSize);
    tempSet = *this;
    for(int i=0;i<=s.last;i++)
    {
        if(isInSet(s.iPtr[i]))
        {
            tempSet.erase(s.iPtr[i]);
        }
    }
    *this = tempSet;
    
}
Set& Set::operator=(Set &s)
{
    if(iPtr) delete []iPtr;
    maxSize = s.maxSize;
    iPtr = new int[maxSize];
    last = s.last;
    for(int i=0;i<=last;i++)
    {
        iPtr[i]=s.iPtr[i];
    }
    return *this;
} 
void Set::sort()
{
    std::sort(iPtr,iPtr+last+1,great<int>());
}
int main()
{
    Set set1(0,"f1.txt");
    set1.insert(30);
    set1.OutputSet();
}

img


谢谢大家!

  • 写回答

3条回答 默认 最新

  • CSDN专家-link 2022-04-11 20:34
    关注

    void Set::insert(int x)函数少了一个右大括号,其它问题不少,修改如下,编译通过

    #include<bits/stdc++.h>
    #include <fstream>
    #include <algorithm>    // std::sort
    using namespace std; 
    class AbsCollection{
    protected:
        int *iPtr;        //指向数组的指针
        int last;        //数组最后一个元素的位置
        int maxSize;    //数组总体长度
    public:
        AbsCollection(int size = 100){
            maxSize = size;
            iPtr = new int[maxSize];
            last = -1;
        };
        virtual ~AbsCollection() {};
        void OutputSet();  //输出集合中元素
    };
    
    void AbsCollection::OutputSet()
    {
        cout<<"The number of elements:"<<last+1<<endl;
        for(int i=0;i<=last;i++)
        {
            cout<<iPtr[i]<<" ";
        }
        cout<<endl;
    }
    class Set:public AbsCollection
    {
    private:
        char *fileName; //存放数据的文件
    public:
        Set(int size = 100, const char *szFileName="");//从文件读取数据存入数组(要判断是否有重复元素)
        virtual ~Set();        //析构函数,释放内存并将数据写入文件
        void insert(int x);    
        void erase(int x);
        int isInSet(int x);     //判断x是否已经存在于集合中
        friend ostream& operator<<(ostream& os, Set& s);
        void intersetion(Set &s);//求集合的交(*this与s的交,结果放在*this中)
        void operator+(Set &s);     //求集合的并(*this与s的并,结果放在*this中)
        void diff(Set &s);         //求集合的差(*this与s的差,结果放在*this中)
        Set& operator=(Set &s);  //赋值拷贝函数
        void sort();             //将集合中元素从大到小排序
    };
    
    int Set::isInSet(int x)
    {
        int flag = 0;
        for(int i=0; i<last+1; i++)
        {
            if(x==iPtr[i])
                flag = 0;
            else
                flag = 1;
        } 
        return flag; 
    }
    void Set::insert(int x)
    {
        if(!isInSet(x)){
            if(last+1<maxSize){
                iPtr[++last]  =  x; 
            }
            else{
                Set tempSet(maxSize+1);
                for(int i = 0;i<= last;i++){
                    tempSet.iPtr[i] = iPtr[i];
                    tempSet.last++;
                }
                delete []iPtr;
                last = -1;
                iPtr = new int[++maxSize];
                for(int i = 0;i<= tempSet.last;i++){
                    iPtr[i] = tempSet.iPtr[i];
                    last++;
                }
                last++;
                iPtr[last] = x;
            }
        }
    }
    Set::Set(int size, const char *szFileName):AbsCollection(size)
    {   
        if(szFileName =="")
        {
            fileName=NULL;
            return;
        }
        fileName = new char[strlen(szFileName)];
        fstream fs(szFileName,ios::in);
        if(!fs.is_open())
        {
            cout<<"open error!"<<endl;
            abort();
        } 
        int x;
        while(fs>>x)
        {
            if(!isInSet(x))
            {
                insert(x);
            }
        }
        fs.close();
    }
    Set::~Set()
    {    
        if(fileName)
        {
            fstream fs(fileName,ios::out|ios::trunc);
            for(int *ip=iPtr;ip<=iPtr+last;)
            {
                fs<<*ip++<<" ";
            }
            fs.close();
            delete fileName;
        }
        if(iPtr)
        {
            delete []iPtr;
        }
    }
    void Set::erase(int x)
    {
    
        for(int i = 0;i<=last;i++){
            if(iPtr[i]==x){
                break;
            }
            if(i<=last){
                while(i!=last){
                    iPtr[i]=iPtr[i++];
                }
            }
        }
        last--;
    }
    ostream& operator<<(ostream& os, Set& s)
    {
    
        for(int i = 0;i<=s.last;i++)
        {
            os<<s.iPtr[i]<<" ";
        }
        return os;
    }
    void Set::intersetion(Set &s)
    {
        Set tempSet(maxSize);
        for(int i = 0;i<=last;i++)
        {
            if(s.isInSet(iPtr[i]))
            {
                tempSet.insert(iPtr[i]);
            }
        }
        *this = tempSet;
    }
    void Set::operator+(Set &s){
        for(int i=0;i<=s.last;i++){
            if(!isInSet(s.iPtr[i])){
                insert(s.iPtr[i]); 
            } 
        }
    }
    void Set::diff(Set &s)
    {
        Set tempSet(maxSize);
        tempSet = *this;
        for(int i=0;i<=s.last;i++)
        {
            if(isInSet(s.iPtr[i]))
            {
                tempSet.erase(s.iPtr[i]);
            }
        }
        *this = tempSet;
    
    }
    Set& Set::operator=(Set &s)
    {
        if(iPtr) delete []iPtr;
        maxSize = s.maxSize;
        iPtr = new int[maxSize];
        last = s.last;
        for(int i=0;i<=last;i++)
        {
            iPtr[i]=s.iPtr[i];
        }
        return *this;
    } 
    void Set::sort()
    {
        std::sort(iPtr,iPtr+last+1,greater<int>());
    }
    int main()
    {
        Set set1(0,"f1.txt");
        set1.insert(30);
        set1.OutputSet();
    }
    
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 4月19日
  • 已采纳回答 4月11日
  • 创建了问题 4月11日

悬赏问题

  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan