鱼C论坛

 找回密码
 立即注册
查看: 2103|回复: 12

[已解决]C++数组问题

[复制链接]
发表于 2020-4-24 10:12:17 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x

1.下面是一个整型数组类intArray的声明,请给出该类所有数据成员的类外定义。要求:

(1)能通过对“>>”、“<<”的重载实现数组元素的直接输入及其输出。输入时,第一次输入的值为数组长度,后面连续输入若干个数作为数组内容。输出时,首先提示该数组元素个数,然后依次输出该数组各元素;

(2)重载“=”、“+”、“-” 运算符使之能对两个数组类对象进行直接赋值、加减运算。

(3)写出主函数对该类进行测试。要求:

1)先构造包含10个数组元素的数组对象a,再调用流提取运算符重载函数实现a的元素初始化(元素值分别为1—10),输出a数组;

2)并利用a来构造数组b,输出数组b,并调用get函数获取b中下标为奇数的各元素值并输出,然后调用set函数将上述元素值乘以2,输出b数组;

3)构造一个包含5个元素的数组c,其值全为1,输出该数组,再调用ReSize函数将其大小重置为10,调用“=”重载函数将a的值复制给c,输出数组c;

4)分别将b+c,b-c的值送给数组d和数组e,并输出结果。
  1. class intArray
  2. {
  3.         public:
  4.         intArray(int size)//构造函数
  5.         intArray(const intArray &x)//复制构造函数
  6.         ~intArray()//析构函数
  7.         bool Set(int i, int elem);//设置第i个数组元素的值,设置成功返回true,失败返回false
  8.         bool Get(int i, int &elem);//获取第i个数组元素的值,获取成功返回true,失败返回false
  9.         int Length() const;//获取数组的长度
  10.         void ReSize(int size);//重置数组的长度
  11.         intArray operator=(const intArray &other);//赋值运算符“=”重载函数
  12.         intArray operator+(const intArray &other);//加运算符“+”重载函数
  13.         intArray operator-(const intArray &other);//减运算符“-”重载函数
  14.         friend ostream & operator>>(ostream &, intArray &);//数组的整体输入
  15.         friend ostream & operator<<(ostream &, intArray &);//数组的整体输出
  16.     private:
  17.         int *element;//指向动态数组的指针
  18.         int arraysize;//数组的当前长度
  19. };
复制代码
最佳答案
2020-4-24 14:14:08
  1. #include <iostream>
  2. #include<string>
  3. using namespace std;
  4. class intArray
  5. {
  6.     friend istream& operator>>(istream& cin, intArray&);//数组的整体输入
  7.     friend ostream& operator<<(ostream& cout, intArray&);//数组的整体输出
  8. public:
  9.     intArray(){}
  10.     intArray(int size);//构造函数
  11.     intArray(const intArray& x);//复制构造函数
  12.     ~intArray();//析构函数
  13.     bool Set(int i, int elem)//设置第i个数组元素的值,设置成功返回true,失败返回false
  14.     {
  15.         if (i>=0&&i<this->arraysize)
  16.         {
  17.             this->element[i] = elem;
  18.             return true;
  19.         }
  20.         else
  21.         {
  22.             return false;
  23.         }
  24.     }
  25.     int Get(int i)//获取第i个数组元素的值
  26.     {
  27.         if (i > 0 && i < this->arraysize)
  28.         {
  29.             return this->element[i];
  30.         }
  31.     }
  32.     int Length() {//获取数组长度
  33.         return arraysize;
  34.     }
  35.    
  36.     void ReSize(int size);//重置数组的长度
  37.     intArray& operator=(const intArray& other);//赋值运算符“=”重载函数
  38.     intArray operator+(const intArray& other);//加运算符“+”重载函数
  39.     intArray operator-(const intArray& other);//减运算符“-”重载函数
  40.    
  41. private:
  42.     int* element;//指向动态数组的指针
  43.     int arraysize;//数组的当前长度
  44. };

  45. intArray::~intArray() {
  46.     delete element;
  47.     element = NULL;
  48. }
  49. intArray::intArray(const intArray& x) {
  50.     this->arraysize = x.arraysize;
  51.     this->element = new int[arraysize];
  52.     for (int i = 0; i < arraysize; i++)
  53.     {
  54.         this->element[i] = x.element[i];
  55.     }
  56. }
  57. intArray::intArray(int size) {
  58.     this->arraysize = size;
  59.     this->element = new int[size];
  60. }

  61. /*
  62.     对数组对象进行输入
  63. */
  64. istream& operator>>(istream& cin, intArray& arr) {
  65.     int temp = 0;
  66.     for (int i = 0; i <arr.arraysize; i++)
  67.     {
  68.         cin >> temp;
  69.         arr.element[i] = temp;
  70.     }
  71.     return cin;
  72. }
  73. /*
  74.     对数组对象进行输出
  75. */
  76. ostream& operator<<(ostream& cout, intArray& arr) {
  77.     for (int i = 0; i < arr.arraysize; i++)
  78.     {
  79.         cout << arr.element[i] << endl;
  80.     }
  81.     return cout;
  82. }
  83. intArray& intArray::operator=(const intArray& other)//赋值运算符“=”重载函数
  84. {
  85.     if (this->element != NULL)
  86.     {
  87.         delete this->element;
  88.         this->element = NULL;
  89.     }
  90.     this->arraysize = other.arraysize;
  91.     this->element = new int[arraysize];
  92.     for (int i = 0; i < arraysize; i++)
  93.     {
  94.         this->element[i] = other.element[i];
  95.     }
  96.     return *this;
  97. }
  98. intArray intArray:: operator+(const intArray& other)//加运算符“+”重载函数
  99. {
  100.     intArray temp(this->arraysize);
  101.     for (int i = 0; i < this->arraysize; i++)
  102.     {
  103.         temp.element[i] = this->element[i] + other.element[i];
  104.     }
  105.     return temp;
  106. }
  107. intArray intArray:: operator-(const intArray& other)//减运算符“-”重载函数
  108. {
  109.     intArray temp(this->arraysize);
  110.     for (int i = 0; i < this->arraysize; i++)
  111.     {
  112.         temp.element[i] = this->element[i] - other.element[i];
  113.     }
  114.     return temp;
  115. }
  116. void intArray::ReSize(int size) {
  117.     this->arraysize = size;
  118.     if (this->element!=NULL)
  119.     {
  120.         delete this->element;
  121.     }
  122.     this->element = new int[size];
  123. }
  124. intArray& testA() {
  125.     int size = 0;
  126.     cout << "请输入要构造的数组长度:" << endl;
  127.     cin >> size;
  128.     intArray* a=new intArray(size);
  129.     cout << "请输入" << size << "个数字来为数组对象a初始化:" << endl;
  130.     cin >> *a;
  131.     cout << "=================打印数组对象a===============" << endl;
  132.     cout << *a;
  133.     return *a;
  134. }
  135. intArray& testB(intArray& a) {
  136.     //拷贝构造数组b
  137.     intArray* b = new intArray(a);
  138.     int size = a.Length();//获取数组长度
  139.     cout << "===============打印数组对象b=================" << endl;
  140.     cout << *b << endl;
  141.     cout << "==========打印数组对象b下标为奇数的值==========" << endl;
  142.     for (int i = 1; i < size; i += 2)
  143.     {
  144.         cout << (*b).Get(i) << endl;
  145.     }
  146.     cout << "=======将奇数下标的值翻倍,再打印数组对象b======" << endl;
  147.     for (int i = 1; i < size; i += 2)
  148.     {
  149.         (*b).Set(i, (*b).Get(i) * 2);
  150.     }
  151.     cout << *b << endl;
  152.     return *b;
  153. }
  154. intArray& testC(intArray& a) {
  155.     //构造数组对象c
  156.     intArray* c=new intArray(5);
  157.     for (int i = 0; i < 5; i++)
  158.     {
  159.         (*c).Set(i, 1);
  160.     }
  161.     cout << "===============打印数组对象c=================" << endl;
  162.     cout << *c << endl;
  163.     (*c).ReSize(10);
  164.     (*c) = a;
  165.     cout << "===========打印赋值操作后的数组对象c===========" << endl;
  166.     cout << *c << endl;
  167.     return *c;
  168. }
  169. void testD(intArray& b, intArray& c) {
  170.     intArray d = b + c;
  171.     intArray e = b - c;
  172.     cout << "================打印数组对象d=================" << endl;
  173.     cout << d << endl;
  174.     cout << "================打印数组对象e================" << endl;
  175.     cout << e << endl;
  176. }
  177. void test() {
  178.     cout << "*************问题1解答*************" << endl << endl;
  179.     intArray a=testA();
  180.     cout << "*************问题2解答*************" << endl << endl;
  181.     intArray b = testB(a);
  182.     cout << "*************问题3解答*************" << endl << endl;
  183.     intArray c= testC(a);
  184.     cout << "*************问题4解答*************" << endl << endl;
  185.     testD(b, c);
  186. }
  187. int main()
  188. {
  189.     test();
  190.     system("pause");
  191.     return 0;
  192. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-4-24 14:14:08 | 显示全部楼层    本楼为最佳答案   
  1. #include <iostream>
  2. #include<string>
  3. using namespace std;
  4. class intArray
  5. {
  6.     friend istream& operator>>(istream& cin, intArray&);//数组的整体输入
  7.     friend ostream& operator<<(ostream& cout, intArray&);//数组的整体输出
  8. public:
  9.     intArray(){}
  10.     intArray(int size);//构造函数
  11.     intArray(const intArray& x);//复制构造函数
  12.     ~intArray();//析构函数
  13.     bool Set(int i, int elem)//设置第i个数组元素的值,设置成功返回true,失败返回false
  14.     {
  15.         if (i>=0&&i<this->arraysize)
  16.         {
  17.             this->element[i] = elem;
  18.             return true;
  19.         }
  20.         else
  21.         {
  22.             return false;
  23.         }
  24.     }
  25.     int Get(int i)//获取第i个数组元素的值
  26.     {
  27.         if (i > 0 && i < this->arraysize)
  28.         {
  29.             return this->element[i];
  30.         }
  31.     }
  32.     int Length() {//获取数组长度
  33.         return arraysize;
  34.     }
  35.    
  36.     void ReSize(int size);//重置数组的长度
  37.     intArray& operator=(const intArray& other);//赋值运算符“=”重载函数
  38.     intArray operator+(const intArray& other);//加运算符“+”重载函数
  39.     intArray operator-(const intArray& other);//减运算符“-”重载函数
  40.    
  41. private:
  42.     int* element;//指向动态数组的指针
  43.     int arraysize;//数组的当前长度
  44. };

  45. intArray::~intArray() {
  46.     delete element;
  47.     element = NULL;
  48. }
  49. intArray::intArray(const intArray& x) {
  50.     this->arraysize = x.arraysize;
  51.     this->element = new int[arraysize];
  52.     for (int i = 0; i < arraysize; i++)
  53.     {
  54.         this->element[i] = x.element[i];
  55.     }
  56. }
  57. intArray::intArray(int size) {
  58.     this->arraysize = size;
  59.     this->element = new int[size];
  60. }

  61. /*
  62.     对数组对象进行输入
  63. */
  64. istream& operator>>(istream& cin, intArray& arr) {
  65.     int temp = 0;
  66.     for (int i = 0; i <arr.arraysize; i++)
  67.     {
  68.         cin >> temp;
  69.         arr.element[i] = temp;
  70.     }
  71.     return cin;
  72. }
  73. /*
  74.     对数组对象进行输出
  75. */
  76. ostream& operator<<(ostream& cout, intArray& arr) {
  77.     for (int i = 0; i < arr.arraysize; i++)
  78.     {
  79.         cout << arr.element[i] << endl;
  80.     }
  81.     return cout;
  82. }
  83. intArray& intArray::operator=(const intArray& other)//赋值运算符“=”重载函数
  84. {
  85.     if (this->element != NULL)
  86.     {
  87.         delete this->element;
  88.         this->element = NULL;
  89.     }
  90.     this->arraysize = other.arraysize;
  91.     this->element = new int[arraysize];
  92.     for (int i = 0; i < arraysize; i++)
  93.     {
  94.         this->element[i] = other.element[i];
  95.     }
  96.     return *this;
  97. }
  98. intArray intArray:: operator+(const intArray& other)//加运算符“+”重载函数
  99. {
  100.     intArray temp(this->arraysize);
  101.     for (int i = 0; i < this->arraysize; i++)
  102.     {
  103.         temp.element[i] = this->element[i] + other.element[i];
  104.     }
  105.     return temp;
  106. }
  107. intArray intArray:: operator-(const intArray& other)//减运算符“-”重载函数
  108. {
  109.     intArray temp(this->arraysize);
  110.     for (int i = 0; i < this->arraysize; i++)
  111.     {
  112.         temp.element[i] = this->element[i] - other.element[i];
  113.     }
  114.     return temp;
  115. }
  116. void intArray::ReSize(int size) {
  117.     this->arraysize = size;
  118.     if (this->element!=NULL)
  119.     {
  120.         delete this->element;
  121.     }
  122.     this->element = new int[size];
  123. }
  124. intArray& testA() {
  125.     int size = 0;
  126.     cout << "请输入要构造的数组长度:" << endl;
  127.     cin >> size;
  128.     intArray* a=new intArray(size);
  129.     cout << "请输入" << size << "个数字来为数组对象a初始化:" << endl;
  130.     cin >> *a;
  131.     cout << "=================打印数组对象a===============" << endl;
  132.     cout << *a;
  133.     return *a;
  134. }
  135. intArray& testB(intArray& a) {
  136.     //拷贝构造数组b
  137.     intArray* b = new intArray(a);
  138.     int size = a.Length();//获取数组长度
  139.     cout << "===============打印数组对象b=================" << endl;
  140.     cout << *b << endl;
  141.     cout << "==========打印数组对象b下标为奇数的值==========" << endl;
  142.     for (int i = 1; i < size; i += 2)
  143.     {
  144.         cout << (*b).Get(i) << endl;
  145.     }
  146.     cout << "=======将奇数下标的值翻倍,再打印数组对象b======" << endl;
  147.     for (int i = 1; i < size; i += 2)
  148.     {
  149.         (*b).Set(i, (*b).Get(i) * 2);
  150.     }
  151.     cout << *b << endl;
  152.     return *b;
  153. }
  154. intArray& testC(intArray& a) {
  155.     //构造数组对象c
  156.     intArray* c=new intArray(5);
  157.     for (int i = 0; i < 5; i++)
  158.     {
  159.         (*c).Set(i, 1);
  160.     }
  161.     cout << "===============打印数组对象c=================" << endl;
  162.     cout << *c << endl;
  163.     (*c).ReSize(10);
  164.     (*c) = a;
  165.     cout << "===========打印赋值操作后的数组对象c===========" << endl;
  166.     cout << *c << endl;
  167.     return *c;
  168. }
  169. void testD(intArray& b, intArray& c) {
  170.     intArray d = b + c;
  171.     intArray e = b - c;
  172.     cout << "================打印数组对象d=================" << endl;
  173.     cout << d << endl;
  174.     cout << "================打印数组对象e================" << endl;
  175.     cout << e << endl;
  176. }
  177. void test() {
  178.     cout << "*************问题1解答*************" << endl << endl;
  179.     intArray a=testA();
  180.     cout << "*************问题2解答*************" << endl << endl;
  181.     intArray b = testB(a);
  182.     cout << "*************问题3解答*************" << endl << endl;
  183.     intArray c= testC(a);
  184.     cout << "*************问题4解答*************" << endl << endl;
  185.     testD(b, c);
  186. }
  187. int main()
  188. {
  189.     test();
  190.     system("pause");
  191.     return 0;
  192. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

 楼主| 发表于 2020-4-24 17:58:41 | 显示全部楼层

谢谢,我已经把你的答案设为最佳答案了,真的感谢/花/花/
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-24 18:02:20 | 显示全部楼层
chichengsun 发表于 2020-4-24 17:58
谢谢,我已经把你的答案设为最佳答案了,真的感谢/花/花/

话说这个该不会是什么选修课老师布置的要计算学分结业作业吧?
不要回头你老师告诉我,这个要承担法律责任
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-4-24 18:14:36 | 显示全部楼层
倒戈卸甲 发表于 2020-4-24 18:02
话说这个该不会是什么选修课老师布置的要计算学分结业作业吧?
不要回头你老师告诉我,这个要 ...

这个是我们这周的C++作业,不会的,你放心
这个也是你的作业吗
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-24 18:19:29 From FishC Mobile | 显示全部楼层
chichengsun 发表于 2020-4-24 18:14
这个是我们这周的C++作业,不会的,你放心
这个也是你的作业吗

我毕业已经好多年
以后周作业还是自己写吧,这个里面也没啥难点,水一样的流程一大段而已。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-4-24 18:25:06 | 显示全部楼层
倒戈卸甲 发表于 2020-4-24 18:19
我毕业已经好多年
以后周作业还是自己写吧,这个里面也没啥难点,水一样的流程一大段而已。

我也想自己写啊,但是对于我来说,这个题太难了
我说出来,你可能不信,这个题我做了一上午
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-24 18:55:19 From FishC Mobile | 显示全部楼层
chichengsun 发表于 2020-4-24 18:25
我也想自己写啊,但是对于我来说,这个题太难了
我说出来,你可能不信,这个题我做了一上午

做不出来的时候,抄也是个办法。
不过要确实的抄一遍,Ctrl+C那就没意义了
代码这东西多抄是能够进步的
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-4-24 19:02:55 | 显示全部楼层
倒戈卸甲 发表于 2020-4-24 18:55
做不出来的时候,抄也是个办法。
不过要确实的抄一遍,Ctrl+C那就没意义了
代码这东西多抄是 ...

我现在就在看你的代码,争取看懂,谢谢你的提醒
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-4-24 19:05:46 | 显示全部楼层
倒戈卸甲 发表于 2020-4-24 18:55
做不出来的时候,抄也是个办法。
不过要确实的抄一遍,Ctrl+C那就没意义了
代码这东西多抄是 ...

我还把你的代码保存下来了,供我以后借鉴参考
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-4-24 19:06:34 | 显示全部楼层
倒戈卸甲 发表于 2020-4-24 18:55
做不出来的时候,抄也是个办法。
不过要确实的抄一遍,Ctrl+C那就没意义了
代码这东西多抄是 ...

真的感谢你,我也不知道说什么好,反正就是挺感激的!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-24 19:27:51 From FishC Mobile | 显示全部楼层
本帖最后由 倒戈卸甲 于 2020-4-24 19:37 编辑
chichengsun 发表于 2020-4-24 19:06
真的感谢你,我也不知道说什么好,反正就是挺感激的!


这些要求看着挺多的,其实实际操作的时候,实现一个,再实现一个,很快就全部解决了。在问题1时,我先写了含参构造函数,这是创建对象所必须的。然后重载了>>运算符去初始化。为了输出又重载了<<运算符。问题2时,重写了拷贝构造函数,顺便把赋值运算符重载了,因为二者的代码重复度很高,赋值运算符又对拷贝构造函数有依赖性。这时我又在想能不能各个问题,封装为不同的函数,不然一个函数代码太长不符合编程规范。但问题在于,后面的问题还要用到前面问题生成的对象。于是我回过头去把对象a放到了堆上,这样就可以返回对象a的引用,使得a能在整个程序运行期间存活且能被访问到。之后的对象也都放在堆上。然后实现了Get和Set函数,并用一个循环去提取奇数下标元素。这里的循环需要数组长度,于是实现了Length函数来获取私有属性arraysize。问题3需要更改数组长度时才去实现了Resize函数。问题4时最后实现了加减运算符。每完成一个问题我都会运行调试一次,基本也没碰到太大问题。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-4-24 20:31:22 | 显示全部楼层
倒戈卸甲 发表于 2020-4-24 19:27
这些要求看着挺多的,其实实际操作的时候,实现一个,再实现一个,很快就全部解决了。在问题1时,我先 ...

我以后也学着用这种方式去写代码,提高我的效率。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-7-16 05:12

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表