📄 intvect.cpp
字号:
//the realization of vector template class
//#include "matlib.h"
//#include "materr.hpp"
#include "IntVect.hpp"
extern char **Mwarningstring;
extern char **Merrorstring;
IntVector::IntVector()
{
p=new vrep;
p->length=0;
p->f=NULL;
p->refcnt=1;
}
IntVector::IntVector(int size,int init)
{
if(size<=0)
{
cout<< Merrorstring[EMAT_INVALIDSIZE];
throw Merrorstring[EMAT_INVALIDSIZE];
}
if(size>= UINT_MAX /sizeof(int))
{
cout<< Merrorstring[EMAT_EXCEEDCAPACITY]<<endl;
throw Merrorstring[EMAT_EXCEEDCAPACITY];
}
p=new vrep;
p->f=new int[p->length=size];
for(int i=0; i<size; i++)
p->f[i]=init;
p->refcnt=1;
}
IntVector::initial(int size)
{
int retn=1;
if(size<=0)
{
cout<< Merrorstring[EMAT_INVALIDSIZE];
throw Merrorstring[EMAT_INVALIDSIZE];
}
if(size>= UINT_MAX /sizeof(int))
{
cout<< Merrorstring[EMAT_EXCEEDCAPACITY]<<endl;
throw Merrorstring[EMAT_EXCEEDCAPACITY];
}
if(p->refcnt==1)
{
if(p->length==size)
return 1;
if(p->f !=NULL)
delete p->f;
}
else
{
p->refcnt--;
p=new vrep;
p->refcnt=1;
}
if(p->length=size)
{
p->f=new int[p->length];
}
return retn;
}
IntVector::IntVector(int* a,int length)
{
if(length<=0)
{
cout<< Merrorstring[EMAT_INVALIDSIZE];
throw Merrorstring[EMAT_INVALIDSIZE];
}
if(length>= UINT_MAX /sizeof(int))
{
cout<< Merrorstring[EMAT_EXCEEDCAPACITY]<<endl;
throw Merrorstring[EMAT_EXCEEDCAPACITY];
}
p=new vrep;
p->f=new int[p->length=length];
for(int i=0; i<length; i++,a++)
p->f[i]=*a;
p->refcnt=1;
}
IntVector::IntVector(IntVector& a1, IntVector& a2)
{
int len1=a1.p->length;
int len2=a2.p->length;
int size=len1+len2;
if(size>= UINT_MAX /sizeof(int))
{
cout<< Merrorstring[EMAT_EXCEEDCAPACITY]<<endl;
throw Merrorstring[EMAT_EXCEEDCAPACITY];
}
p=new vrep;
p->length=size;
p->f=new int[size];
for(int i=0; i<len1; i++)
p->f[i]=a1[i];
for(int j=0; j<len2; j++)
p->f[len1+j]=a2[j];
p->refcnt=1;
}
//constructs an sub-IntVector using IntVector a. the element number=last-fist and
//index from first to last-1.
IntVector:: IntVector(IntVector& a,int first,int last)
{
int size=last-first;
if(size<=0)
{
cout<<Merrorstring[EMAT_ASSIGNDATAERR];
throw Merrorstring[EMAT_ASSIGNDATAERR];
}
if((first<0)||(last>a.vlen()))
{
cout<< Merrorstring[EMAT_OUTOFRANGE];
throw Merrorstring[EMAT_OUTOFRANGE];
}
p=new vrep;
p->length=size;
p->f=new int[p->length];
for(int i=0; i<size; i++)
p->f[i]=a[first+i];
p->refcnt=1;
}
IntVector::IntVector(IntVector& x)
{
x.p->refcnt++;
p=x.p;
}
IntVector::~IntVector()
{
if(--p->refcnt==0)
{
if(p->f !=NULL)
delete p->f;
delete p;
}
}
IntVector copy(IntVector& m)
{
int len=m.vlen();
IntVector x(len);
int *f1=x.p->f;
int *f2=m.p->f;
for(int i=0; i<len; i++, f1++, f2++)
*f1=*f2;
return x;
}
IntVector IntVector::copy()
{
int len=vlen();
IntVector x(len);
int *f1=x.p->f;
int *f2=p->f;
for(int i=0; i<len; i++, f1++, f2++)
*f1=*f2;
return x;
}
//v2=v1
IntVector& IntVector::operator=(IntVector& vec)
{
if( this==&vec)
return *this;
vec.p->refcnt++;
if(--p->refcnt==0)
{
delete p->f;
delete p;
}
p=vec.p;
/*
if( p->length!=vec() ){
initial( vec() );
}
if(p->length){
int i;
for(i=0;i<p->length;i++){
p->f[i]=vec[i];
}
}
*/
return *this;
}
//v=x
IntVector& IntVector::operator=(int x)
{
int len=p->length;
if(p->refcnt>1)
{
p->refcnt--;
p=new vrep;
p->length=len;
p->refcnt=1;
p->f=new int[len];
}
int *f=p->f;
for(int i=0; i<len; i++, f++)
*f=x;
return *this;
}
//v=v1+v2
IntVector operator+(IntVector& v1,IntVector& v2)
{
//allocate the new IntVector
if(v1()!=v2())
{
cout<<"IntVector's dim are not equal";
}
int len=MIN(v1.p->length,v2.p->length);
IntVector v(len);
//do the operations
int *f=v.p->f;
int *f1=v1.p->f;
int *f2=v2.p->f;
for(int i=0; i<len; i++, f++, f1++, f2++)
*f=*f1 + *f2;
return v;
}
//v=v1-v2
IntVector operator-(IntVector& v1,IntVector& v2)
{
//allocate the new IntVector
if(v1()!=v2())
{
cout<<"IntVector's dim are not equal";
}
int len=MIN(v1.p->length,v2.p->length);
IntVector v(len);
//do the operations
int *f=v.p->f;
int *f1=v1.p->f;
int *f2=v2.p->f;
for(int i=0; i<len; i++, f++, f1++, f2++)
*f=*f1 - *f2;
return v;
}
//v=v1*v2
IntVector operator*(IntVector& v1,IntVector& v2)
{
//allocate the new IntVector
if(v1()!=v2())
{
cout<<"IntVector's dim are not equal";
}
int len=MIN(v1.p->length,v2.p->length);
IntVector v(len);
//do the operations
int *f=v.p->f;
int *f1=v1.p->f;
int *f2=v2.p->f;
for(int i=0; i<len; i++, f++, f1++, f2++)
*f=*f1 * *f2;
return v;
}
//v=v1/v2
IntVector operator/(IntVector& v1,IntVector& v2)
{
//allocate the new IntVector
int len=MIN(v1.p->length,v2.p->length);
IntVector v(len);
//do the operations
int *f=v.p->f;
int *f1=v1.p->f;
int *f2=v2.p->f;
for(int i=0; i<len; i++, f++, f1++, f2++)
*f=*f1 / *f2;
return v;
}
//v=v1+x
IntVector operator+(IntVector& v1,int x)
{
//allocate the new IntVector
int len=v1.p->length;
IntVector v(len);
//do the operations
int *f=v.p->f;
int *f1=v1.p->f;
for(int i=0; i<len; i++, f++, f1++)
*f=*f1 + x;
return v;
}
//v=v1-x
IntVector operator-(IntVector& v1,int x)
{
//allocate the new IntVector
int len=v1.p->length;
IntVector v(len);
//do the operations
int *f=v.p->f;
int *f1=v1.p->f;
for(int i=0; i<len; i++, f++, f1++)
*f=*f1 - x;
return v;
}
//v=v1*x
IntVector operator*(IntVector& v1,int x)
{
//allocate the new IntVector
int len=v1.p->length;
IntVector v(len);
//do the operations
int *f=v.p->f;
int *f1=v1.p->f;
for(int i=0; i<len; i++, f++, f1++)
*f=*f1 * x;
return v;
}
//v=v1/x
IntVector operator/(IntVector& v1,int x)
{
//allocate the new IntVector
int len=v1.p->length;
IntVector v(len);
//do the operations
int *f=v.p->f;
int *f1=v1.p->f;
for(int i=0; i<len; i++, f++, f1++)
*f=*f1 / x;
return v;
}
//v=x+v1
IntVector operator+(int x,IntVector& v1)
{
//allocate the new IntVector
int len=v1.p->length;
IntVector v(len);
//do the operations
int *f=v.p->f;
int *f1=v1.p->f;
for(int i=0; i<len; i++, f++, f1++)
*f=x+ *f1;
return v;
}
//v=x-v1
IntVector operator-(int x,IntVector& v1)
{
//allocate the new IntVector
int len=v1.p->length;
IntVector v(len);
//do the operations
int *f=v.p->f;
int *f1=v1.p->f;
for(int i=0; i<len; i++, f++, f1++)
*f=x- *f1;
return v;
}
//v=x*v1
IntVector operator*(int x,IntVector& v1)
{
//allocate the new IntVector
int len=v1.p->length;
IntVector v(len);
//do the operations
int *f=v.p->f;
int *f1=v1.p->f;
for(int i=0; i<len; i++, f++, f1++)
*f=x * *f1;
return v;
}
//v=x/v1
IntVector operator/(int x,IntVector& v1)
{
//allocate the new IntVector
int len=v1.p->length;
IntVector v(len);
//do the operations
int *f=v.p->f;
int *f1=v1.p->f;
for(int i=0; i<len; i++, f++, f1++)
*f=x/ *f1;
return v;
}
//v=+v1
IntVector operator+(IntVector& v1)
{
return v1;
}
//v=-v1
IntVector operator-(IntVector& v1)
{
//allocate the new IntVector
int len=v1.p->length;
IntVector v(len);
//do the operations
int *f=v.p->f;
int *f1=v1.p->f;
for(int i=0; i<len; i++, f++, f1++)
*f=- *f1;
return v;
}
//v+=v1
IntVector& IntVector::operator+=(IntVector& v1)
{
//allocate the new IntVector
if(v1()!=vlen())
{
cout<<"IntVector's dim are not equal";
}
int len=MIN(v1.p->length, p->length);
//do the operations
int *f=p->f;
int *f1=v1.p->f;
for(int i=0; i<len; i++, f++, f1++)
*f +=*f1;
return *this;
}
//v-=v1
IntVector& IntVector::operator-=(IntVector& v1)
{
//allocate the new IntVector
if(v1()!=vlen())
{
cout<<"IntVector's dim are not equal";
}
int len=MIN(v1.p->length, p->length);
//do the operations
int *f=p->f;
int *f1=v1.p->f;
for(int i=0; i<len; i++, f++, f1++)
*f -=*f1;
return *this;
}
//v*=v1
IntVector& IntVector::operator*=(IntVector& v1)
{
//allocate the new IntVector
if(v1()!=vlen())
{
cout<<"IntVector's dim are not equal";
}
int len=MIN(v1.p->length, p->length);
//do the operations
int *f=p->f;
int *f1=v1.p->f;
for(int i=0; i<len; i++, f++, f1++)
*f *=*f1;
return *this;
}
//v/=v1
IntVector& IntVector::operator/=(IntVector& v1)
{
//allocate the new IntVector
if(v1()!=vlen())
{
cout<<"IntVector's dim are not equal";
}
int len=MIN(v1.p->length, p->length);
//do the operations
int *f=p->f;
int *f1=v1.p->f;
for(int i=0; i<len; i++, f++, f1++)
*f /=*f1;
return *this;
}
//v+=x
IntVector& IntVector::operator+=(int x)
{
//allocate the new IntVector
int len=p->length;
//do the operations
int *f=p->f;
for(int i=0; i<len; i++, f++)
*f +=x;
return *this;
}
//v-=x
IntVector& IntVector::operator-=(int x)
{
//allocate the new IntVector
int len=p->length;
//do the operations
int *f=p->f;
for(int i=0; i<len; i++, f++)
*f -=x;
return *this;
}
//v*=x
IntVector& IntVector::operator*=(int x)
{
//allocate the new IntVector
int len=p->length;
//do the operations
int *f=p->f;
for(int i=0; i<len; i++, f++)
*f *=x;
return *this;
}
//v/=x
IntVector& IntVector::operator/=(int x)
{
//allocate the new IntVector
int len=p->length;
//do the operations
int *f=p->f;
for(int i=0; i<len; i++, f++)
*f /=x;
return *this;
}
//f=v[i]
int& IntVector::operator[](int i)
{
if((i>=0)&&(i<p->length))
return p->f[i];
else{
{
cout<< Merrorstring[EMAT_OUTOFRANGE];
throw Merrorstring[EMAT_OUTOFRANGE];
}
//next statement has no effect. the only function is avoid warnning message.
return p->f[0];
}
}
//x=(v1==v2): returns 1 when v1==v2. otherwise, returns 0.
int operator==(IntVector& v1, IntVector& v2)
{
if(v1.p->length !=v2.p->length)
return 0;
int len=v1.p->length;
//do the operations
int *f1=v1.p->f;
int *f2=v2.p->f;
for(int i=0; i<len; i++, f1++, f2++)
if(*f1 != *f2)
return 0;
return 1;
}
//x=(v1 !=v2): returns 1 when v1 !=v2. otherwise, returns 0.
int operator!=(IntVector& v1, IntVector& v2)
{
if(v1.p->length !=v2.p->length)
return 1;
int len=v1.p->length;
//do the operations
int *f1=v1.p->f;
int *f2=v2.p->f;
for(int i=0; i<len; i++, f1++, f2++)
if(*f1 != *f2)
return 1;
return 0;
}
//cin>>v1
istream& operator>>(istream& in, IntVector& v1)
{
int n=v1.vlen();
for(int i=0; i<n; i++){
in>>v1[i];
}
return in;
}
//cout<<v1
ostream& operator<<(ostream& out, IntVector& v1)
{
int *f=v1.p->f;
int len=v1.p->length;
for(int i=0; i<len; i++, f++)
out<<*f<<" ";
out<<"\n";
return out;
}
//sum(v): returns the sum of every elements.
int IntVector::sum()
{
int n=vlen();
int re=0;
int *f=p->f;
for(int i=0; i<n; i++, f++)
re +=*f;
return re;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -