夫为剑者

示之以虚 开之以利 后之以发 先之以至

常用链接

统计

积分与排名

最新评论

2006-08-14

简单的数组类
#ifndef ARRAY_H_
#define ARRAY_H_

#include <iostream.h>;
#include <stdlib.h>;

enum ErrorType { invalidArraySize,memoryAllocationError,indexOutOfRange};
char * errorMsg[] = { "Invalid array size","Memory allocation error","invalid index"};

template <class T>;
class Array
{
private:
T* alist;
int size;

void Error(ErrorType errorCommitted,int badIndex=0) const;
public:
Array(int sz=50);
Array(const Array<T>;& X);
~Array();

Array<T>;& operator =(const Array<T>;& rhs);
T& operator [](int i);
operator T* () const;
int ListSize() const;
void Resize(int sz);
};

template <class T>;
Array<T>;::Array(int sz/* =50 */)
{
if (sz <= 0)
Error(invalidArraySize);
size = sz;
alist = new T[size];
if (alist == NULL)
Error(memoryAllocationError);
}

template <class T>;
Array<T>;::Array(const Array<T>;& X)
{
int n = X.size;
size = n;
alist = new T[n];
if (alist == NULL)
Error(memoryAllocationError);
T* srcptr = X.alist;
T* destptr = alist;
while(n--)
*destptr++ = *srcptr++;
}

template <class T>;
Array<T>;::~Array()
{
delete [] alist;
}

template <class T>;
T& Array<T>;::operator [](int n)
{
if (n<0 || n>;size - 1)
Error(indexOutOfRange,n);
return alist[n];
}

template <class T>;
Array<T>;::operator T* () const
{
return alist;
}

template <class T>;
int Array<T>;::ListSize() const
{
return size;
}

template <class T>;
void Array<T>;::Resize(int sz)
{
if (sz <= 0)
Error(invalidArraySize);
if (sz == size)
return;
T* newlist = new T[sz];
if (newlist == NULL)
Error(memoryAllocationError);
int n = (sz <= size ? sz : size);
T* srcptr = alist;
T* destptr = newlist;
while (n--)
*destptr++ = *srcptr++;
delete [] alist;
alist = newlist;
size = sz;
}

template <class T>;
void Array<T>;::Error(ErrorType errorCommitted,int badIndex/* =0 */) const
{
if (errorCommitted == invalidArraySize)
cerr<<errorMsg[0]<<endl;
else if (errorCommitted == memoryAllocationError)
cerr<<errorMsg[1]<<endl;
else if (errorCommitted == indexOutOfRange)
cerr<<errorMsg[2]<<" "<<badIndex<<endl;
exit(1);
}

template <class T>;
Array<T>;& Array<T>;::operator =(const Array<T>;& X)
{
T* newlist = new T[X.size];
if (newlist == NULL)
Error(memoryAllocationError);T* srcptr = alist;
T* destptr = newlist;
while (n--)
*destptr++ = *srcptr++;
delete [] alist;
alist = newlist;
size = sz;
return this;
}


#endif

下面是测试代码,求质数
#include "Array.h"
#include <iomanip.h>;

int main()
{
Array <int>; A(10);
int upperlimit,primecount = 0,i,j;
cout<<"enter value >;= 2 as upper limit for prime numbers:";
cin>;>;upperlimit;
A[primecount++] = 2;
for (i=3;i<upperlimit;i++)
{
if (primecount == A.ListSize())
A.Resize(primecount+10);
if (i % 2 == 0)
continue;
j = 3;
while (j <= i/2 && i % j != 0)
j+=2;
if (j>;i/2)
A[primecount++] = i;
}
for (i=0;i<primecount;i++)
{
cout<<setw(5)<<A[i];
if ((i+1)%10 == 0)
cout<<endl;
}
cout <<endl;
return 0;
}

posted on 2006-08-14 15:49 vivip 阅读(62) 评论(0)  编辑 收藏 引用 所属分类: 自我监督


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理