鱼C论坛

 找回密码
 立即注册
查看: 512|回复: 4

[已解决][已修改]C++ 顺序表的加法实现,帮忙看看吧QAQ

[复制链接]
发表于 2020-3-14 17:32:14 | 显示全部楼层 |阅读模式

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

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

x
本帖最后由 Quenya 于 2020-3-15 13:28 编辑

要在C++顺序表中添加连接两个表的功能:就是定义了线性表中的顺序表,然后要实现顺序表c=a+b的功能,比如a是1,2,3 ;b是4,5,6,7,8,9,那c就应该是1,2,3,4,5,6,7,8,9嘛,但是我得到的c前两个元素不对,不管怎么改都是随机的两个数比如输出1549200,1510976,3,4,5,6,7,8,9
下面是我的错误代码,其他应该没问题,就是重载的加法、赋值和复制构造函数可能有错(seqList.h的118行起),能帮我看看吗
用的codeblocks

  1. #include "Seqlist.h"

  2. using namespace std;

  3. int main()
  4. {
  5.     seqList <int> a;
  6.     a.insert(0, 1);
  7.     a.insert(1, 2);
  8.     a.insert(2, 3);
  9.     cout << a.length();
  10.     a.traverse();//构建了一个顺序表a,表长为3,表内元素是1,2,3

  11.     seqList <int> b;
  12.     b.insert(0, 4);
  13.     b.insert(1, 5);
  14.     b.insert(2, 6);
  15.     b.insert(3, 7);
  16.     b.insert(4, 8);
  17.     b.insert(5, 9);

  18.     b.traverse();
  19.     //再建一个顺序表b:4,5,6,7,8,9,表长为6


  20.     seqList <int> c;
  21.     c = a + b;       //新增的加法功能,应得到新的顺序表c,表长为9,元素为1,2,3,4,5,6,7,8,9

  22.     c.traverse();                               //c表的第一第二个元素不正常,如果写c=a;  c.traverse(); 输出正常
  23.     cout << c.length();


  24.     return 0;
  25. }

复制代码




头文件:seqList.h

  1. #ifndef SEQLIST_H_INCLUDED
  2. #define SEQLIST_H_INCLUDED

  3. #include <iostream>
  4. #include <cstdlib>
  5. #include "list.h"

  6. using namespace std;

  7. //顺序表的定义
  8. template <class elemType>
  9. class seqList : public list <elemType>
  10. {
  11. private:
  12.     elemType *data;            //动态数组
  13.     int currentLength;         //表长
  14.     int maxSize;               //容量
  15.     void doubleSpace();        //扩容函数

  16. public:
  17.     seqList (int initSize = 10);                        //构造函数
  18.     ~seqList (){delete [] data;}                        //析构函数
  19.     void clear (){currentLength=0;}                     //清除线性表
  20.     int length()const {return currentLength;}           //求表长
  21.     void insert (int i, const elemType &x);             //插入
  22.     void remove (int i);                                //删除
  23.     int search (const elemType &x)const;                //查询
  24.     elemType visit(int i)const {return data[i];}        //访问
  25.     void traverse()const;                               //遍历输出

  26.     seqList <elemType> operator+ (const seqList <elemType> &b);  //加法重载
  27.     seqList(const seqList<elemType> &d);  //复制构造函数
  28.     seqList <elemType> &operator= (const elemType &c);  //赋值运算符重载函数


  29. };


  30. //扩容函数
  31. template <class elemType>
  32. void seqList <elemType>::doubleSpace()
  33. {
  34.     elemType *tmp = data;
  35.     maxSize *= 2;
  36.     data = new elemType [maxSize];

  37.     for (int i = 0; i < currentLength; ++i )
  38.         data[i] = tmp[i];

  39.     delete []tmp;
  40. }


  41. //构造函数
  42. template<class elemType>
  43. seqList<elemType> :: seqList(int initSize)
  44. {
  45.     data = new elemType [initSize];
  46.     maxSize = initSize;
  47.     currentLength = 0;
  48. }



  49. //插入
  50. template <class elemType>
  51. void seqList <elemType> ::insert(int i , const elemType &x)
  52. {
  53.     if (currentLength == maxSize) doubleSpace();

  54.     for (int j = currentLength; j > i; j--)
  55.         data[j] = data[j-1];

  56.     data[i] = x;
  57.     ++currentLength;
  58. }


  59. //删除
  60. template <class elemType>
  61. void seqList <elemType> :: remove(int i)
  62. {
  63.     if (i < 0 || i >= currentLength)  return;

  64.     for (int j = i; j < currentLength; ++j)
  65.         data[j] = data[j+1];

  66.     --currentLength;
  67. }


  68. //查询
  69. template <class elemType>
  70. int seqList <elemType> :: search(const elemType &x)const
  71. {
  72.     int i;
  73.     for (i = 0; i < currentLength; i++)
  74.         if (data[i] == x) break;

  75.     if (i == currentLength) return -1;
  76.      else return i;
  77. }


  78. //遍历输出
  79. template <class elemType>
  80. void seqList <elemType> :: traverse () const
  81. {
  82.     cout<<endl;

  83.     for (int i = 0; i < currentLength; ++i)
  84.         cout<< data[i] <<' ';

  85.     cout<<endl;
  86. }


  87. //加法重载
  88. template <class elemType>
  89. seqList <elemType> seqList <elemType> :: operator+ (const seqList <elemType> &b)
  90. {
  91.     seqList<elemType>c(currentLength + b.currentLength);    //新建表c存放加法结果,表长为a,b表长之和

  92.     for (int i = 0; i < currentLength; ++i)
  93.        c.data[i] = data[i];                                 //把表a的数据复制到c

  94.     for (int j = 0; j < b.currentLength; ++j)
  95.         c.data[j + currentLength ] = b.data[j];             //把表b的数据j接在表a后复制到c

  96.     c.currentLength = currentLength + b.currentLength;      //设置表长
  97.                                                                                    //如果在这里加代码:cout<<c.data[0]<<c.data[1];输出1 2

  98.     return c;
  99. }


  100. //复制构造函数
  101. template <class elemType>
  102. seqList <elemType> :: seqList(const seqList<elemType> &d)
  103. {
  104.     currentLength = d.currentLength;
  105.     maxSize = d.maxSize;
  106.     data = new elemType[maxSize];
  107.     for(int i = 0; i < currentLength; ++i)
  108.         data[i] = d.data[i];
  109. }


  110. //赋值运算符重载函数
  111. template <class elemType>
  112. seqList <elemType> &seqList <elemType>:: operator= (const elemType &c)
  113. {
  114.     if(this == &c)  return *this;     //防止自我赋值

  115.     delete[]data;                     //释放当前对象指向的空间

  116.     currentLength = c.currentLength;
  117.     maxSize = c.maxSize;
  118.     data = new elemType[maxSize];
  119.     for (int i = 0; i < currentLength; ++i)
  120.         data[i] = c.data[i];
  121.                                    //构造和等号右边一样的对象


  122.     return *this;

  123. }

  124. #endif // SEQLIST_H_INCLUDED
复制代码


  1. //头文件list.h,线性表的抽象类
  2. #ifndef LIST_H_INCLUDED
  3. #define LIST_H_INCLUDED

  4. #include <iostream>

  5. using namespace std;

  6. template <class elemType>
  7. class list
  8. {
  9. public:
  10.     virtual void clear ()=0;
  11.     virtual int  length()const =0;
  12.     virtual void insert (int i, const elemType &x)=0;
  13.     virtual void remove (int i)=0;
  14.     virtual int search (const elemType &x)const=0;
  15.     virtual elemType visit (int i)const=0;
  16.     virtual void traverse()const=0;
  17.     virtual ~list(){};
  18. };

  19. #endif // LIST_H_INCLUDED
复制代码


————————————头文件中其他对顺序表的定义和实现没有问题——



之前不知道江湖规矩。。。。:P 直接把代码贴上来了hhh抱歉~

先谢谢大佬们~[鞠躬]
最佳答案
2020-3-14 22:54:37
本帖最后由 bin554385863 于 2020-3-14 22:56 编辑

注释注释注释!@!!!!!!!!!!!!!!!!!!!!!!!
几百行代码一行注释都没!!!!!!!!
错误代码也不发!!!!!!!!!!!!
说实话,几百行代码,一句注释都没?谁能知道你想些什么
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2020-3-15 13:54:29 | 显示全部楼层
hhhhh我知道了!
赋值运算符重载的参数写错了:seqList <elemType> &seqList <elemType>:: operator= (const elemType &c)
应该是seqList <elemType> &seqList <elemType>:: operator= (constseqList <elemType> &c)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-3-14 22:54:37 | 显示全部楼层    本楼为最佳答案   
本帖最后由 bin554385863 于 2020-3-14 22:56 编辑

注释注释注释!@!!!!!!!!!!!!!!!!!!!!!!!
几百行代码一行注释都没!!!!!!!!
错误代码也不发!!!!!!!!!!!!
说实话,几百行代码,一句注释都没?谁能知道你想些什么
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-3-15 12:03:32 | 显示全部楼层
本帖最后由 Quenya 于 2020-3-15 12:10 编辑
bin554385863 发表于 2020-3-14 22:54
注释注释注释!@!!!!!!!!!!!!!!!!!!!!!!!
几百行代码一行注释都没!!!!!!! ...


对不起对不起我错了,我现在加注释了QwQ
因为等级太低没有办法传图片,错误的结果如下:
3                                                           a的表长
1 2 3                                                     a的数据

4 5 6 7 8 9                                             b的数据

1549200 1510976 3 4 5 6 7 8 9               c的数据,但是前两个是错的
9                                                          c的表长,正确
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-3-15 13:51:36 | 显示全部楼层
本帖最后由 Quenya 于 2020-3-15 13:54 编辑

不用麻烦了!我知道哪里错了!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-23 23:00

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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