一维数组 接受 普通数据类型 以及 自定义 复数类
#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<iostream>
#include<iomanip>
#include<cassert>
using namespace std;
template<class T>
class Set
{
public:
Set(int n);
Set(T a[],int n);
void show();
void add(T a);
void sort();
void remove(T a);
Set<T> jiao(Set<T>a);
Set<T> bing(Set<T>a);
Set<T> cha(Set<T>a);
bool xiangdeng(Set<T>a);
bool baohan(Set<T>a);
private:
int size;
T *list;
};
template<class T>
Set<T>::Set(int n)
{
size = n;
}
template<class T>
Set<T>::Set(T a[],int n)
{
size = n;
list = new T[size];
for(int i = 0;i<size;i++)
{
list[i] = a[i];
}
}
template<class T>
void Set<T>::show()
{
for(int i=0;i<size;i++)
{
cout<<list[i]<<" ";
}
cout<<endl;
}
template<class T>
void Set<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 Set<T>::add(T a)
{
bool flag = true;
for(int i = 0;i<size;i++)
{
if(list[i] == a)
{
flag = false;
}
}
if(flag == true)
{
T* temp = new T[size+1];
for(int i=0;i<size;i++)
{
temp[i] = list[i];
}
temp[size] = a;
delete []list;
size++;
list = temp;
this->sort();
}
}
template<class T>
void Set<T>::remove(T a)
{
for(int i = 0;i<size;i++)
{
if(list[i] == a)
{
list[i] = 0;
}
}
this->sort();
T* temp = new T[size-1];
for( i=1;i<size;i++)
{
temp[i-1] = list[i];
}
delete []list;
size--;
list = temp;
}
template<class T>
Set<T> Set<T>::jiao(Set<T>a)
{
int num = 0;
for(int i=0;i<size;i++)
{
for(int j = 0;j<a.size;j++)
{
if(list[i] == a.list[j])
{
num++;
}
}
}
T *temp;
temp = new T[num];
int k = 0;
for(i=0;i<size;i++)
{
for(int n = 0;n<a.size;n++)
{
if(list[i] == a.list[n])
{
temp[k] = list[i];
k++;
}
}
}
Set<T>t(temp,num);
t.sort();
return t;
}
template<class T>
Set<T> Set<T>::bing(Set<T>a)
{
int num = size+a.size;
T *temp;
temp = new T[num];
for(int i = 0;i<size;i++)
{
temp[i] = list[i];
}
for(int j = i,int k=0;j<num;j++,k++)
{
temp[j] = a.list[k];
}
Set<T>t(temp,num);
return t;
}
template<class T>
bool Set<T>::xiangdeng(Set<T>a)
{
if(size != a.size)
{
return false;
}
int t =0;
for(int i = 0;i<size;i++)
{
for(int j = 0;j<a.size;j++)
{
if(list[i] == a.list[j])
{
t++;
}
}
}
if(t == size)
{
return true;
}
return false;
}
template<class T>
bool Set<T>::baohan(Set<T>a)
{
if(size == a.size)
{
if(this->xiangdeng(a))
return true;
}
if(size > a.size)
{
Set<T>temp = this->jiao(a);
if(a.xiangdeng(temp))
{
return true;
}
}
if(size < a.size)
{
Set<T>temp = this->jiao(a);
if(this->xiangdeng(temp))
{
return true;
}
}
return false;
}
template<class T>
Set<T> Set<T>::cha(Set<T>a)
{
int num = 0;
if(!(this->baohan(a)))
{
cout<<"参数应该输入一个全集"<<endl;
}
if(this->xiangdeng(a))
{
cout<<"补集是空集"<<endl;
}
num = a.size - size;
bool flag = true;
T *temp = new T[num];
int k = 0;
for(int i = 0;i<a.size;i++)
{
for(int j = 0;j<size;j++)
{
if(a.list[i] == list[j])
{
flag = false;
break;
}
}
if(flag == true)
{
temp[k] = a.list[i];
k++;
}
flag = true;
}
Set<T> t(temp,num);
return t;
}
int main()
{
int a[] = {1,2,4,5,6};
int v[] = {1,2};
int num = sizeof(a)/sizeof(int);
int num2 = sizeof(v)/sizeof(int);
Set<int> s(a,num);
Set<int> s2(v,num2);
s2.show();
s.show();
bool w = s.baohan(s2);
cout<<"s 与 s2是否有包含关系 "<<w<<endl;
cout<<"s 与 s2的补集为:";
s2.cha(s).show();
Set<int> b(a,num);
b.show();
bool p = s.xiangdeng(b);
cout<<"b 与 s 是否相等"<<" "<<p<<endl;
bool g = s.baohan(b);
cout<<"b 与 s 是否有包含关系"<<" "<<g<<endl;
s.add(3);
s.show();
s.add(3);
s.show();
s.remove(4);
s.show();
Set<int>c = s.jiao(b);
c.show();
bool q = s.xiangdeng(c);
cout<<"c 与 s 是否相等"<<" "<<q<<endl;
Set<int>d = s.bing(b);
d.show();
return 0;
}