|
以下程序段已定义了一个整数集合的类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(); } |
|
|
求助攻 谢谢
|
|
|
看不懂啊啊啊啊啊啊。
|
|
|
怎么看不懂了…其实我也看不懂 |
|
|
排版太丑了←_←
|
|
|
重载构造函数,重载运算符,就这些
|
|
|
看不懂问题就大了,怎么回了?
|
|
|
不要在意细节 |
|
|
额….能写下来吗 谢谢 真的不会啊… |
|
|
理解和讨论之前请先学会如何观察!
代码功能归根结底不是别人帮自己看或讲解或注释出来的;而是被自己静下心来花足够长的时间和精力亲自动手单步或设断点或对执行到某步获得的中间结果显示或写到日志文件中一步一步分析出来的。 计算机组成原理→DOS命令→汇编语言→C语言(不包括C++)、代码书写规范→数据结构、编译原理、操作系统→计算机网络、数据库原理、正则表达式→其它语言(包括C++)、架构…… 对学习编程者的忠告: 单步类的实例“构造”或“复制”或“作为函数参数”或“作为函数返回值返回”或“参加各种运算”或“退出作用域”的语句对应的汇编代码几步后,就会来到该类的“构造函数”或“复制构造函数”或“运算符重载”或“析构函数”对应的C/C++源代码处。 VC调试时按Alt+8、Alt+7、Alt+6和Alt+5,打开汇编窗口、堆栈窗口、内存窗口和寄存器窗口看每句C对应的汇编、单步执行并观察相应堆栈、内存和寄存器变化,这样过一遍不就啥都明白了吗。 |
|
| 80分 |
这样可好:
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
|
|
这个我试了 提示加入函数的{x}有问题,删了{x}可以运行,但是会有乱码的,仍谢谢你 |
|
|
如果Add有问题,下面的代码如何正确
for (i = 0; i < 5; i++) t2.Add(2 * i + 1); //这里使用了Add cout << "l2 : "; |
|
|
排版丑算了,变量名还这么蛋疼
|
|
| 20分 |
jihe(const jihe &j) jihe& operator=(const jihe &j) A = new int[1]; A[0] = x; 看到3处bug, 其他问题自己解决吧 |
|
还没结贴?》
|
|