阅读背景:

c++学习笔记 -- 函数模板与类模板的综合运用

来源:互联网 

一维数组 接受 普通数据类型 以及 自定义 复数类 

#include<iostream>
#include<iomanip>
#include<math.h>
#include<cassert>

using namespace std;

class Complex;

ostream operator<<(ostream& out,const Complex& c);
Complex Add(const int a,const Complex &c1);
Complex Jian(const int a,const Complex &c1);
Complex Chen(const int a,const Complex &c1);
Complex Chu(const int a,const Complex &c1);
class Complex
{

	friend Complex Add(const int a,const Complex &c1);
	friend Complex Jian(const int a,const Complex &c1);
	friend Complex Chen(const int a,const Complex &c1);
	friend Complex Chu(const int a,const Complex &c1);
public:
	Complex(double r=0, double i=0) :real(r), imag(i){}
	~Complex(){}
	Complex operator+(const Complex& c1)const;
	Complex operator-(const Complex& c1)const;
	Complex operator*(const Complex& c1)const;
	Complex operator/(const Complex& c1)const;
	Complex operator-();
	Complex operator++();
	Complex operator++(int);
	bool operator==(const Complex& c1)const;
	bool operator!=(const Complex& c1)const;
	bool operator>(const Complex& c1)const;
	bool operator<(const Complex& c1)const;
	friend ostream operator<<(ostream& out, const Complex& c);
private:
	double real;
	double imag;
};

Complex Complex::operator+(const Complex& c1)const
{
	Complex temp;
	temp.real = real + c1.real;
	temp.imag = imag + c1.imag;
	return temp;

}
Complex Complex::operator-(const Complex& c1)const
{
	Complex temp;
	temp.real = real - c1.real;
	temp.imag = imag - c1.imag;
	return temp;
}
Complex Complex:: operator*(const Complex& c1)const{
	Complex temp;
	temp.real = real * c1.real;
	temp.imag = imag * c1.imag;
	return temp;
}
Complex Complex:: operator/(const Complex& c1)const
{
	Complex temp;
	temp.real = real / c1.real;
	temp.imag = imag / c1.imag;
	return temp;
}
Complex Add(const int a,const Complex &c1)
{
	Complex temp;
	temp.real = a + c1.real;
	temp.imag = c1.imag;
	return temp;
}
Complex Jian(const int a,const Complex &c1)
{
	Complex temp;
	temp.real = c1.real - a;
	temp.imag = c1.imag;
	return temp;
}
Complex Chen(const int a,const Complex &c1)
{
	Complex temp;
	temp.real = c1.real * a;
	temp.imag = c1.imag;
	return temp;
}
Complex Chu(const int a,const Complex &c1)
{
	Complex temp;
	temp.real = c1.real / a;
	temp.imag = c1.imag;
	return temp;
}

Complex Complex::operator-()
{
	return Complex(-real, -imag);
}
Complex Complex::operator++()
{
	++real;
	++imag;
	return Complex(real, imag);
}
Complex Complex::operator++(int)
{
	Complex temp(*this);
	real++;
	imag++;
	return temp;
}
bool Complex::operator==(const Complex& c1)const
{
	return real == c1.real&&imag == c1.imag;
}
bool Complex::operator!=(const Complex& c1)const
{
	return real != c1.real && imag != c1.imag;
}
bool Complex::operator>(const Complex& c1)const
{	
	if(sqrt(real*real+imag*imag)>(c1.real*c1.real+c1.imag*c1.imag))
	{
		return true;
	}
	else{
		return false;
	}

}
bool Complex:: operator<(const Complex& c1)const
{
	if(sqrt(real*real+imag*imag)<(c1.real*c1.real+c1.imag*c1.imag))
	{
		return true;
	}
	else{
		return false;
	}
}
ostream operator<<(ostream& out, const Complex& c)
{
	out <<"实部 "<<c.real<< " 虚部 "<<c.imag<<"i"<<endl;
	return out;
}

template<class T>
class Array{
public:
	Array(int sz = 50);
	Array(const T* li,int n);
	~Array();
	Array(const Array<T>&a);
	Array<T>& operator=(const Array<T>&rhs);
	T & operator[](int i);
	operator T*();
	void sort(T a[],int n);
	void sort();
	void swap(T &x,T &y);
	void show();
private:
	int size;
	T *list;
};
template<class T>
Array<T>::Array(int sz){
	assert(sz >= 0);
	size = sz;
	list = new T[size];
}
template<class T>
Array<T>::Array(const T* li,int n){

	assert(n >= 0);
	size = n;
	list = new T[size];
	for(int i =0;i<size;i++)
	{
		list[i] = li[i];
	}
}
template<class T>
Array<T>::~Array(){
	delete []list;
}
template<class T>
Array<T>::Array(const Array<T>&a)
{
	size = a.size;
	list = new T[size];
	for(int i=0;i<size;i++)
	{
		list[i] = a.list[i];
	}
}
template<class T>
Array<T>& Array<T>:: operator=(const Array<T>&rhs)
{
	if(&rhs!=this)
	{
		if(size!=rhs.size){
			delete []list;
			size = rhs.size;
			list = new T[size];
		}
		for(int i = 0;i<size;i++)
		{
			list[i] = rhs.list[i];
		}
	}
		return *this;
}
template<class T>
T & Array<T>::operator[](int i)
{
	assert(i>=0&&n<size);
	return list[n];
}
template<class T>
Array<T>::operator T*(){
	return list;
}


template<class T>
void Array<T>::swap(T &x,T &y)
{
	T temp = x;
	x = y;
	y = temp;
}
template<class T>
void Array<T>::sort(T a[],int n)
{
	for(int i = 0; i<n-1;i++)
	{
	int minIndex = i;
	for(int j = i+1;j<n;j++)
	{
		if(a[j]<a[minIndex])
			minIndex = j;

		swap(a[i],a[minIndex]);
	}
	}

}
template<class T>
void Array<T>::sort()
{
	for(int i = 0;i<size-1;i++)
	{
		int minIndex = i;
		for(int j= i+1;j<size;j++)
		{
			if(list[j]<list[minIndex])
				minIndex = j;
				T temp = list[i];
				list[i] = list[minIndex];
				list[minIndex] = temp;
		}
	}
}
template<class T>
void Array<T>::show()
{
	for(int i=0;i<size;i++)
	{
		cout<<list[i]<<" ";
	}
	cout<<endl;

}
int main()
{
	int a[] = {1,5,6,7,2};
	int num = sizeof(a)/sizeof(int);
	//cout<<num<<endl;
	Array<int> b(a,num);
	b.show();
	b.sort();
	b.show();
	Complex array[5];
	Complex c1(1, 2), c2(3, 4), c3(2,2),c4(3,3),c5(1,1);
	array[0] = c1;
	array[1] = c2;
	array[2] = c3;
	array[3] = c4;
	array[4] = c5;
	Array<Complex> c(array,5);
	c.show();
	c.sort();
	c.show();

	return 0;
}#include<iostre



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: