• c++:array


    #ifndef ARRAY_CLASS
    #define ARRAY_CLASS
    #include<iostream>
    #include<cstdlib>
    using namespace std;
    #ifndef NULL
    #define NULL 0
    #endif
    const enum ErrorType{invalidArraySize, memoryAllocationError, indexOutOfRange};
    
    template <class T>
    class Array{
    private:
    	T * alist;
    	int size;
    	void Error(ErrorType error, int badIndex = 0) const;
    public:
    	Array(int sz = 50);
    	Array(const Array<T>&A);
    	~Array(void);
    	Array<T> &operator=(const Array <T> &rhs);
    	T & operator[] (int i);
    	operator T * (void) const;
    	int ListSize(void) const;
    	void Resize(int sz);
    };
    template<class T>
    void Array<T>::Error(ErrorType error, int badIndex) const{
    	char *errorMsgs[] = { "invalid array size", "memory allocation error", "invalid index:" };
    	cout << errorMsgs[error];
    	if (error == indexOutOfRange)
    		cout << badIndex;
    	cout << endl;
    	exit(1);
    }
    template<class T>
    Array<T>::Array(int sz){
    	if (sz <= 0)
    		Error(invalidArraySize);
    	size = sz;
    	alist = new T[size];
    	if (alist == NULL)
    		Error(memoryAllocationError);
    }
    template<class T>
    Array<T>::~Array(void){
    	delete[] alist;
    }
    template<class T>
    Array<T>::Array(const Array<T> & X){
    	int n = X.size;
    	size = n;
    	alist = new T[size];
    	if (alist == NULL)
    		Error(memoryAllocationError);
    	T *srcptr = X.alist;
    	T *destptr = alist;
    	while (n--){
    		*destptr++ = *srcptr++;
    	}
    }
    template<class T>
    Array<T>& Array<T>::operator= (const Array<T> &rhs){
    	int n = rhs.size;
    	if (size != n){
    		delete[] alist;
    		alist = new T[n];
    		if (alist == NULL)
    			Error(memoryAllocationError);
    		size = n;
    	}
    	T *destptr = alist;
    	T *srcptr = rhs.alist;
    	while (n--){
    		*destptr++ = *srcptr++;
    	}
    	return this;
    }
    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 *  (void) const
    {
    	return alist;
    }
    template<class T>
    int Array<T>::ListSize(void) 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;
    };
    #endif
    <pre class="cpp" name="code">#include<iostream>
    #include<iomanip>
    #include"Array.h"
    int main()
    {
    	Array <int> A(10);
    	int n;
    	int primecount = 0, i, j;
    	cout << "enter a value>=2 as upper limit for prime number:";
    	cin >> n;
    	A[primecount++] = 2;
    	for (i = 3; i < n; 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;
    }

    
    
  • 相关阅读:
    博客园的自定义皮肤
    为自己的审美观感到惭愧
    关于GitHub的Hello Word
    使用Windows Live Writer撰写的第一篇博文
    正式入驻博客园了
    一个使用 Web Components 的音乐播放器: MelodyPlayer
    一个(伪)MaterialDesign风格的博客园皮肤
    从零开始,做一个NodeJS博客(一):Heroku上的最简NodeJS服务器
    从零开始,做一个NodeJS博客(零):整体规(chui)划(niu)
    在 Xamarin.Android 中使用 Notification.Builder 构建通知
  • 原文地址:https://www.cnblogs.com/javafly/p/6037240.html
Copyright © 2020-2023  润新知