这个C++程序哪出问题了呢?
以下是我写的一个集合类,集合元素为复数,复数元素用结构体表示的。以下程序通过编译,可运行,但是输出结果有问题,display方法可能有问题,集合的并集(+重载)似乎也有问题。。但是我一直找不到问题所在。。还有析构函数中的如何释放com[]的内存?直接用delete []com会出错。。大家帮我找找问题吧。。谢谢啦
#include<iostream>
#include<algorithm>
using namespace std;
struct comp
{
double r;
double a;
};
const max_size = 40;
class ComplexSet
{
public:
ComplexSet();//缺省构造函数
ComplexSet(const ComplexSet &); //拷贝函数
int find(struct comp c)const; //找到c元素返回1,否则返回0
int full(void)const; //集合满时返回1,否则返回0
int empty(void)const; //集合空时返回1,否则返回0
void create(void);//创建集合
void display(void) const;//输出集合
int getlen() const;//获取集合中元素个数
void increase() ;//扩大元素中的最大个数
// struct max() const; //获得元素中r最大值,并返回r值最大的复数
ComplexSet operator +(const ComplexSet &)const; //集合的并集
ComplexSet operator -(const ComplexSet &)const; //集合的差集
ComplexSet operator *(const ComplexSet &)const; //集合的交集
ComplexSet &operator <<(struct comp c); //增加一个元素
ComplexSet &operator >>(struct comp c); //删除一个元素
struct comp com[max_size]; //存放集合元素--复数
int count; //目前元素个数
int maxlen;//元素最大个数
~ComplexSet(void);
};
ComplexSet::ComplexSet()
{
com[max_size];
count=0;
maxlen=0;
}
ComplexSet::ComplexSet(const ComplexSet& s1)
{
maxlen=s1.getlen ()+10;
struct comp *com=new struct comp [maxlen];
int i(0);
for(;i<s1.count ;++i)
{
com[i].r=[i].r;
com[i].a=[i].a;
}
count=s1.count ;
}
ComplexSet::~ComplexSet()
{
//delete com;
count=0;
maxlen=0;
}
int ComplexSet::find(struct comp c)const
{
for(int i=0;i!=count;++i)
if(com[i].r==c.r && com[i].a==c.a)
return 1;
return 0;
}
int ComplexSet::full()const
{
return (maxlen==count);
}
int ComplexSet::empty ()const
{
return (0==count);
}
void ComplexSet::create()
{
cout<<"please enter the number of the ComplexSet."<<endl;
cin>>count;
maxlen=count+20;
struct comp com[40];
cout<<"please enter the value of the ComplexSet"<<endl;
for(int i=0;i!=count;++i)
{
cout<<"element"<<i+1<<":"<<"r=";
cin>>com[i].r;
cout<<"element"<<i+1<<":"<<"a=";
cin>>com[i].a;
}
}
void ComplexSet::display() const
{
for(int i=0;i<count;++i)
{
cout<<"("<< com[i].r << "," << com[i].a << ")" <<" ";
}
cout<<endl;
}
void ComplexSet::increase ()
{
struct comp *temp= new struct comp[count];
int i;
for(i=0;i<count;++i)
temp[i]=com[i];
//delete [] com;
maxlen+=10;
struct comp com[40];
for(i=0;i<count;++i)
com[i]=temp[i];
delete [] temp;
}
ComplexSet &ComplexSet::operator <<(struct comp c)
{
if(maxlen==count)
increase();
com[count].r=c.r;
com[count].a=c.a;
++count;
return *this;
}
ComplexSet &ComplexSet::operator >>(struct comp c)
{
if(!empty())
{
int loc(0);
for(;loc!=count;++loc)
if(com[loc].r==c.r && com[loc].a==c.a)
{
for(;loc!=count-1;++loc)
{
com[loc].r=com[loc+1].r;
com[loc].a=com[loc+1].a;
}
}
}
--count;
return *this;
}
int ComplexSet::getlen () const
{
return count;
}
ComplexSet ComplexSet::operator +(const ComplexSet &s1)const
{
ComplexSet s2(*this);
for(int i(0);i!=s1.count ;++i)
if(!find([i]))
s2<<[i];
return s2;
}
ComplexSet ComplexSet::operator -(const ComplexSet &s1)const
{
ComplexSet temp;
for(int i=0;i<count-1;++i)
if(!s1.find(com[i]))
temp<<com[i];
return temp;
}
ComplexSet ComplexSet::operator *(const ComplexSet &s1)const
{
ComplexSet s2;
for(int i=0;i<count;++i)
if(find([i]))
s2<<[i];
return s2;
}
int main()
{
ComplexSet com2;
com2.create();
//com2.display();
ComplexSet com3;
com3.create();
ComplexSet com4;
com4=com2+(com3);
com2.display();
com3.display();
com4.display();
return 0;
}