64,654
社区成员
发帖
与我相关
我的任务
分享
#ifndef SQLIST_H_
#define SQLIST_H_
#define LIST_MAX_SIZE 100
#define LISTINCREMENT 10
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <iomanip>
template <class ElemType>
class Sqlist
{
private:
ElemType *elem; //顺序表动态存储空间首地址
int listSize; //当前存储空间大小
int n; //当前元素个数
public:
Sqlist();
Sqlist(const Sqlist<ElemType> & otherL );
virtual ~Sqlist();
Sqlist<ElemType> operator = (Sqlist<ElemType> rightL);
friend Sqlist<ElemType> RandCreat(); //随机生成一个顺序表
friend void Display ( Sqlist<ElemType> PG );//输出顺序表
};
template <class ElemType> //构造函数
Sqlist<ElemType>::Sqlist()
{
elem = new ElemType[LIST_MAX_SIZE];
// assert(elem!=0); //有啥用?
listSize=LIST_MAX_SIZE;
n=0;
}
template <class ElemType> //拷贝初始化构造函数
Sqlist<ElemType>::Sqlist( const Sqlist<ElemType>& otherL)
{
listSize=otherL.listSize;
elem = new ElemType[listSize];
n=otherL.n;
for( int i=0 ; i!=n; i++ )
*(elem+i)=*(otherL.elem+i);
}
template <class ElemType> //析构函数
Sqlist<ElemType>::~Sqlist()
{
delete [] elem;
n=0;
listSize=0;
}
template<class ElemType> //重载=运算符
Sqlist<ElemType> Sqlist<ElemType>:: operator = ( Sqlist<ElemType>rightL )
{
listSize=rightL.listSize;
elem= new ElemType[ listSize ];
n= rightL.n;
for (int i=0; i != n; i++ )
{
elem[i]=rightL.elem[i];
}
return *this;
}
template<class ElemType> //随机生产一个顺序表
Sqlist<ElemType> RandCreat()
{
Sqlist<ElemType> pg;
pg.listSize= rand() % 10 +1;
pg.elem = new ElemType[ pg. listSize ];
n= rand()% (pg.listSize-1) ;
srand( (unsigned) time (NULL) );
for( int i=0; i!=n; i++ )
{
pg.elem[i] = rand() % 99 + 1;
}
return pg;
}
template <class ElemType>
void Display( Sqlist<ElemType> PG ) //输出顺序表
{
using std::cout;
using std::endl;
for ( int i=0; i<PG.n; i++ )
cout<<i+1<<setw(5);
cout<<endl;
for( int j=0; j<PG.n; j++ )
cout<<PG.elem[j]<<setw(5);
cout<<endl;
}
#endif
#include <iostream>
#include "Sqlist.h"
int main()
{
using std::cout;
using std::endl;
Sqlist<int> A;
A=RandCreat();
cout<<"随机生成的顺序表为:"<<endl;
Display(A);
system("pause");
return 0;
}
template<class T>
friend Sqlist<T> RandCreat(); //随机生成一个顺序表
template<class T>
friend void Display ( Sqlist<T> PG );//输出顺序表
能const reference就尽量用, display函数不如直接提供 operator<<
#define LIST_MAX_SIZE 100
#define LISTINCREMENT 10
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <iomanip>
using namespace std;
template <class ElemType>
class Sqlist
{
public:
ElemType *elem; //顺序表动态存储空间首地址
int listSize; //当前存储空间大小
int n; //当前元素个数
public:
Sqlist();
Sqlist(const Sqlist<ElemType> & otherL );
virtual ~Sqlist();
Sqlist<ElemType> operator = (Sqlist<ElemType> rightL);
template <class ElemType>
friend Sqlist<ElemType> RandCreat(); //随机生成一个顺序表
template <class ElemType>
friend void Display ( Sqlist<ElemType> PG );//输出顺序表
};
template <class ElemType> //构造函数
Sqlist<ElemType>::Sqlist()
{
elem = new ElemType[LIST_MAX_SIZE];
// assert(elem!=0); //有啥用?
listSize=LIST_MAX_SIZE;
n=0;
}
template <class ElemType> //拷贝初始化构造函数
Sqlist<ElemType>::Sqlist( const Sqlist<ElemType>& otherL)
{
listSize=otherL.listSize;
elem = new ElemType[listSize];
n=otherL.n;
for( int i=0 ; i!=n; i++ )
*(elem+i)=*(otherL.elem+i);
}
template <class ElemType> //析构函数
Sqlist<ElemType>::~Sqlist()
{
delete [] elem;
n=0;
listSize=0;
}
template<class ElemType> //重载=运算符
Sqlist<ElemType> Sqlist<ElemType>:: operator = ( Sqlist<ElemType>rightL )
{
listSize=rightL.listSize;
elem= new ElemType[ listSize ];
n= rightL.n;
for (int i=0; i != n; i++ )
{
elem[i]=rightL.elem[i];
}
return *this;
}
template<class ElemType> //随机生产一个顺序表
Sqlist<ElemType> RandCreat()
{
Sqlist<ElemType> pg;
pg.listSize= rand() % 10 +1;
pg.elem = new ElemType[ pg. listSize ];
int n= rand()% (pg.listSize-1) ;
srand( (unsigned) time (NULL) );
for( int i=0; i!=n; i++ )
{
pg.elem[i] = rand() % 99 + 1;
}
return pg;
}
template <class ElemType>
void Display( Sqlist<ElemType> PG ) //输出顺序表
{
using std::cout;
using std::endl;
for ( int i=0; i<PG.n; i++ )
cout<<i+1<<setw(5);
cout<<endl;
for( int j=0; j<PG.n; j++ )
cout<<PG.elem[j]<<setw(5);
cout<<endl;
}
int main()
{
Sqlist<int> A;
A=RandCreat<int>();
cout<<"随机生成的顺序表为:"<<endl;
Display(A);
system("pause");
return 0;
}
问题一:
A=RandCreat();
仅凭返回值无法推导出模板,改法:
A=RandCreat<int>();
问题二:
友元函数并不是类的一部分,友元函数其实就是个普通的全局函数,其声明成函数模板时要这样来
template <class ElemType>
friend Sqlist<ElemType> RandCreat();
template <class ElemType>
friend void Display ( Sqlist<ElemType> PG );
并且友元函数会破坏程序的封装性,这里的RandCreat实现为类的静态成员函数,Display实现为类的成员函数会更好些。
问题三:
RandCreat中的变量n没有定义
问题四:
代码逻辑上可能存在问题