马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
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
#include "Seqlist.h"
using namespace std;
int main()
{
seqList <int> a;
a.insert(0, 1);
a.insert(1, 2);
a.insert(2, 3);
cout << a.length();
a.traverse();//构建了一个顺序表a,表长为3,表内元素是1,2,3
seqList <int> b;
b.insert(0, 4);
b.insert(1, 5);
b.insert(2, 6);
b.insert(3, 7);
b.insert(4, 8);
b.insert(5, 9);
b.traverse();
//再建一个顺序表b:4,5,6,7,8,9,表长为6
seqList <int> c;
c = a + b; //新增的加法功能,应得到新的顺序表c,表长为9,元素为1,2,3,4,5,6,7,8,9
c.traverse(); //c表的第一第二个元素不正常,如果写c=a; c.traverse(); 输出正常
cout << c.length();
return 0;
}
头文件:seqList.h
#ifndef SEQLIST_H_INCLUDED
#define SEQLIST_H_INCLUDED
#include <iostream>
#include <cstdlib>
#include "list.h"
using namespace std;
//顺序表的定义
template <class elemType>
class seqList : public list <elemType>
{
private:
elemType *data; //动态数组
int currentLength; //表长
int maxSize; //容量
void doubleSpace(); //扩容函数
public:
seqList (int initSize = 10); //构造函数
~seqList (){delete [] data;} //析构函数
void clear (){currentLength=0;} //清除线性表
int length()const {return currentLength;} //求表长
void insert (int i, const elemType &x); //插入
void remove (int i); //删除
int search (const elemType &x)const; //查询
elemType visit(int i)const {return data[i];} //访问
void traverse()const; //遍历输出
seqList <elemType> operator+ (const seqList <elemType> &b); //加法重载
seqList(const seqList<elemType> &d); //复制构造函数
seqList <elemType> &operator= (const elemType &c); //赋值运算符重载函数
};
//扩容函数
template <class elemType>
void seqList <elemType>::doubleSpace()
{
elemType *tmp = data;
maxSize *= 2;
data = new elemType [maxSize];
for (int i = 0; i < currentLength; ++i )
data[i] = tmp[i];
delete []tmp;
}
//构造函数
template<class elemType>
seqList<elemType> :: seqList(int initSize)
{
data = new elemType [initSize];
maxSize = initSize;
currentLength = 0;
}
//插入
template <class elemType>
void seqList <elemType> ::insert(int i , const elemType &x)
{
if (currentLength == maxSize) doubleSpace();
for (int j = currentLength; j > i; j--)
data[j] = data[j-1];
data[i] = x;
++currentLength;
}
//删除
template <class elemType>
void seqList <elemType> :: remove(int i)
{
if (i < 0 || i >= currentLength) return;
for (int j = i; j < currentLength; ++j)
data[j] = data[j+1];
--currentLength;
}
//查询
template <class elemType>
int seqList <elemType> :: search(const elemType &x)const
{
int i;
for (i = 0; i < currentLength; i++)
if (data[i] == x) break;
if (i == currentLength) return -1;
else return i;
}
//遍历输出
template <class elemType>
void seqList <elemType> :: traverse () const
{
cout<<endl;
for (int i = 0; i < currentLength; ++i)
cout<< data[i] <<' ';
cout<<endl;
}
//加法重载
template <class elemType>
seqList <elemType> seqList <elemType> :: operator+ (const seqList <elemType> &b)
{
seqList<elemType>c(currentLength + b.currentLength); //新建表c存放加法结果,表长为a,b表长之和
for (int i = 0; i < currentLength; ++i)
c.data[i] = data[i]; //把表a的数据复制到c
for (int j = 0; j < b.currentLength; ++j)
c.data[j + currentLength ] = b.data[j]; //把表b的数据j接在表a后复制到c
c.currentLength = currentLength + b.currentLength; //设置表长
//如果在这里加代码:cout<<c.data[0]<<c.data[1];输出1 2
return c;
}
//复制构造函数
template <class elemType>
seqList <elemType> :: seqList(const seqList<elemType> &d)
{
currentLength = d.currentLength;
maxSize = d.maxSize;
data = new elemType[maxSize];
for(int i = 0; i < currentLength; ++i)
data[i] = d.data[i];
}
//赋值运算符重载函数
template <class elemType>
seqList <elemType> &seqList <elemType>:: operator= (const elemType &c)
{
if(this == &c) return *this; //防止自我赋值
delete[]data; //释放当前对象指向的空间
currentLength = c.currentLength;
maxSize = c.maxSize;
data = new elemType[maxSize];
for (int i = 0; i < currentLength; ++i)
data[i] = c.data[i];
//构造和等号右边一样的对象
return *this;
}
#endif // SEQLIST_H_INCLUDED
//头文件list.h,线性表的抽象类
#ifndef LIST_H_INCLUDED
#define LIST_H_INCLUDED
#include <iostream>
using namespace std;
template <class elemType>
class list
{
public:
virtual void clear ()=0;
virtual int length()const =0;
virtual void insert (int i, const elemType &x)=0;
virtual void remove (int i)=0;
virtual int search (const elemType &x)const=0;
virtual elemType visit (int i)const=0;
virtual void traverse()const=0;
virtual ~list(){};
};
#endif // LIST_H_INCLUDED
————————————头文件中其他对顺序表的定义和实现没有问题——
之前不知道江湖规矩。。。。:P 直接把代码贴上来了hhh抱歉~
先谢谢大佬们~[鞠躬]
本帖最后由 bin554385863 于 2020-3-14 22:56 编辑
注释注释注释!@!!!!!!!!!!!!!!!!!!!!!!!
几百行代码一行注释都没!!!!!!!!
错误代码也不发!!!!!!!!!!!!
说实话,几百行代码,一句注释都没?谁能知道你想些什么
|