c++攻 小弟我不是伸手党 但是小弟我真的看不懂
c++求助攻 我不是伸手党 但是我真的看不懂啊
以下程序段已定义了一个整数集合的类jihe:
class jihe
{
protected:
int nCount;
int * A;
public:
jihe()
{
nCount =0;
A=NULL;
}
jihe(int n, int *p)
{
int i;
A =new int[n];
nCount =n;
for(i=0;i<n;i++)
A[i]=*(p+i);
}
jihe(jihe & j)
{
int i;
A =new int[j.nCount];
nCount =j.nCount;
for(i=0;i<j.nCount;i++)
A[i]=*(j.A+i);
}
~jihe()
{
if(nCount >0)
delete [] A;
}
jihe operator=(jihe& j)
{
int i;
if(nCount >0)
delete [] A;
A =new int[j.nCount];
nCount =j.nCount;
for(i=0;i<j.nCount;i++)
A[i]=*(j.A+i);
return *this;
}
void Show()
{
int i,j=0;
cout << "JIHE("<<nCount<<")=";
for(i=0;i<nCount;i++)
cout << A[i]<< '\t' ;
cout << endl;
}
jihe operator*(jihe& jh);//集合的并运算
jihe operator/(jihe& jh); //集合的交运算
jihe operator%(jihe& jh); //集合的差运算
void Add(int x) ; //加入函数
void Del(int x) ; //删除函数
};
现需将完成其中的三个运算符重载函数和加入删除函数,注意加入时应检查原集合中没有该数字时才加入。
用以下程序调试
void main()
{
jihe t1,t2,t3;
int d,n,i;
int *p;
cin >>n;
while (n>0)
{
cin >>d;
t1.Add(d);
n--;
}
cout <<"l1 : ";
t1.Show();
p=new int[5];
for(i=0;i<5;i++)
t2.Add(2*i+1);
cout <<"l2 : ";
t2.Show();
t3=t1*t2;
cout <<"l1 ∪l2: ";
t3.Show();
t3=t1%t2;
cout <<"l1 - l2: ";
t3.Show();
t1.Del(3);
t3=t1/t2;
cout <<"l1 ∩l2: ";
t3.Show();
}
------解决思路----------------------
这样可好:
------解决思路----------------------
看到3处bug, 其他问题自己解决吧
以下程序段已定义了一个整数集合的类jihe:
class jihe
{
protected:
int nCount;
int * A;
public:
jihe()
{
nCount =0;
A=NULL;
}
jihe(int n, int *p)
{
int i;
A =new int[n];
nCount =n;
for(i=0;i<n;i++)
A[i]=*(p+i);
}
jihe(jihe & j)
{
int i;
A =new int[j.nCount];
nCount =j.nCount;
for(i=0;i<j.nCount;i++)
A[i]=*(j.A+i);
}
~jihe()
{
if(nCount >0)
delete [] A;
}
jihe operator=(jihe& j)
{
int i;
if(nCount >0)
delete [] A;
A =new int[j.nCount];
nCount =j.nCount;
for(i=0;i<j.nCount;i++)
A[i]=*(j.A+i);
return *this;
}
void Show()
{
int i,j=0;
cout << "JIHE("<<nCount<<")=";
for(i=0;i<nCount;i++)
cout << A[i]<< '\t' ;
cout << endl;
}
jihe operator*(jihe& jh);//集合的并运算
jihe operator/(jihe& jh); //集合的交运算
jihe operator%(jihe& jh); //集合的差运算
void Add(int x) ; //加入函数
void Del(int x) ; //删除函数
};
现需将完成其中的三个运算符重载函数和加入删除函数,注意加入时应检查原集合中没有该数字时才加入。
用以下程序调试
void main()
{
jihe t1,t2,t3;
int d,n,i;
int *p;
cin >>n;
while (n>0)
{
cin >>d;
t1.Add(d);
n--;
}
cout <<"l1 : ";
t1.Show();
p=new int[5];
for(i=0;i<5;i++)
t2.Add(2*i+1);
cout <<"l2 : ";
t2.Show();
t3=t1*t2;
cout <<"l1 ∪l2: ";
t3.Show();
t3=t1%t2;
cout <<"l1 - l2: ";
t3.Show();
t1.Del(3);
t3=t1/t2;
cout <<"l1 ∩l2: ";
t3.Show();
}
------解决思路----------------------
这样可好:
class jihe
{
protected:
int nCount;
int *A;
public:
jihe()
{
nCount = 0;
A = NULL;
}
jihe(int n, int *p)
{
int i;
A = new int[n];
nCount = n;
for (i = 0; i<n; i++)
A[i] = *(p + i);
}
jihe(jihe &j)
{
int i;
A = new int[j.nCount];
nCount = j.nCount;
for (i = 0; i<j.nCount; i++)
A[i] = *(j.A + i);
}
~jihe()
{
if (nCount >0)
delete[] A;
}
jihe operator=(jihe &j)
{
int i;
if (nCount > 0)
delete[] A;
A = new int[j.nCount];
nCount = j.nCount;
for (i = 0; i<j.nCount; i++)
A[i] = *(j.A + i);
return *this;
}
void Show()
{
int i, j = 0;
cout << "JIHE(" << nCount << ")=";
for (i = 0; i < nCount; i++)
cout << A[i] << '\t';
cout << endl;
}
jihe operator*(jihe& jh); //集合的并运算
jihe operator/(jihe& jh); //集合的交运算
jihe operator%(jihe& jh); //集合的差运算
void Add(int x); //加入函数
void Del(int x); //删除函数
int find(int x); //查找函数,成功,返回下标;否则,返回-1
};
jihe jihe::operator*(jihe &jh) //集合的并运算
{
if (0 == this->nCount) return jh;
int i, j, n;
n = this->nCount + jh.nCount;
int *p = new int[this->nCount + jh.nCount];
for (i = 0; i < this->nCount; i++) p[i] = this->A[i];
j = -1;
while (1)
{
begin:
j++;
if (j == jh.nCount) break;
for (int r = 0; r < i; r++)
if (jh.A[j] == p[r])
goto begin;
p[i++] = jh.A[j];
}
jihe res(i, p);
delete[]p;
return res;
}
jihe jihe::operator/(jihe &jh) //集合的交运算
{
int i, j, n;
n = this->nCount >= jh.nCount ? this->nCount : jh.nCount;
int *p = new int[n];
j = 0;
for (i = 0; i < this->nCount; i++)
if (-1 != jh.find(this->A[i]))
p[j++] = this->A[i];
jihe res(j, p);
delete[]p;
return res;
}
jihe jihe::operator%(jihe &jh) //集合的差运算
{
int i, j;
int *p = new int[this->nCount];
j = 0;
for (i = 0; i < this->nCount; i++)
if (-1 == jh.find(this->A[i]))
p[j++] = this->A[i];
jihe res(j, p);
delete[]p;
return res;
}
void jihe::Add(int x) //加入函数
{
if (0 == this->nCount)
{
A = new int[1]{x};
this->nCount = 1;
}
else
{
int pos = find(x);
if (-1 == pos)
{
nCount++;
A = (int*)realloc(A, sizeof(int)*(nCount + 1));
A[nCount - 1] = x;
}
}
}
void jihe::Del(int x) //删除函数
{
int pos;
if (-1 != (pos = find(x)))
{
if (1 == nCount)
{
delete[]A;
nCount = 0;
}
nCount--;
for (int i = pos; i < nCount; i++) A[i] = A[i + 1];
}
}
int jihe::find(int x) //查找函数
{
for (int i = 0; i < nCount; i++)
if (A[i] == x)
return i;
return -1;
}
/*
现需将完成其中的三个运算符重载函数和加入删除函数,注意加入时应检查原集合中没有该数字时才加入。
用以下程序调试*/
int main()
{
jihe t1, t2, t3;
int d, n, i;
int *p;
cin >> n;
while (n>0)
{
cin >> d;
t1.Add(d);
n--;
}
cout << "l1 : ";
t1.Show();
p = new int[5];
for (i = 0; i < 5; i++)
t2.Add(2 * i + 1);
cout << "l2 : ";
t2.Show();
t3 = t1*t2;
cout << "l1 ∪l2: ";
t3.Show();
t3 = t1%t2;
cout << "l1 - l2: ";
t3.Show();
t1.Del(3);
t3 = t1 / t2;
cout << "l1 ∩l2: ";
t3.Show();
delete[]p;
return 0;
}
//4
//1 3 5 7
//l1 : JIHE(4)=1 3 5 7
//l2 : JIHE(5)=1 3 5 7 9
//l1 ∪l2: JIHE(5)=1 3 5 7 9
//l1 - l2: JIHE(0)=
//l1 ∩l2: JIHE(3)=1 5 7
------解决思路----------------------
jihe(const jihe &j)
jihe& operator=(const jihe &j)
A = new int[1];
A[0] = x;
看到3处bug, 其他问题自己解决吧