欢迎来到天天文库
浏览记录
ID:38422030
大小:83.50 KB
页数:6页
时间:2019-06-12
《程序:第04章:数组和字符串》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、第4章数组和字符串程序4.1一维数组类#includetemplateclassArray1D{public:Array1D(intsz=0);//缺省时长度为0~Array1D(){delete[]elements;}T&operator[](inti)const;//取元素值Array1D&operator=(constArray1D&r);//整体赋值friendistream&operator>>(istream&in,Array1D&r);friendostream&operat
2、or<<(ostream&out,constArray1D&r);private:intsize;T*elements;//指向T类型数组的指针};templateArray1D::Array1D(intsz){assert(sz>=0);//越界检查size=sz;elements=newT[sz];}templateT&Array1D::operator[](inti)const{assert(i>=0&&i3、classT>Array1D&Array1D::operator=(constArray1D&r)//数组r整体赋值给this{if(this!=&r){//防止自我赋值6size=r.size;delete[]elements;//释放原空间elements=newT[size];//重新分配空间for(inti=0;iistream&operator>>(istream&in,A4、rray1D&r){cout<<"Intputarray";for(inti=0;i>r.elements[i];returnin;}templateostream&operator<<(ostream&out,constArray1D&r){cout<<"Array=";for(inti=0;i5、dmain(){Array1Da(5),b(8);Array1Dc;//采用缺省长度0cin>>a;cout<<"a"<>b;cout<<"b"<classSparseMatrix {public:SparseMatrix(intmaxRowSize6、,intmaxColSize){};~SparseMatrix(){};virtualvoidAdd(constSparseMatrix&B,SparseMatrix&C)const;virtualvoidMul(constSparseMatrix&B,SparseMatrix&C)const;virtualvoidTranspose(SparseMatrix&B)const;private: intmaxRows,maxCols;};程序4.4Term结构template structTerms7、{introw,col; Tvalue; };程序4.5行三元组表示的稀疏矩阵的C++类templateclassSeqTriple{public:SeqTriple(intmSize);~SeqTriple(){delete[]trip;};voidAdd(constSeqTriple&B,SeqTriple&C)const;voidMul(constSeqTriple&B,SeqTriple&C)const;voidTranspose(SeqTriple&B)const;friendistre8、am&operator>>(istream&input,constSeqTriple&);friendostream&operator<<(ostream&o
3、classT>Array1D&Array1D::operator=(constArray1D&r)//数组r整体赋值给this{if(this!=&r){//防止自我赋值6size=r.size;delete[]elements;//释放原空间elements=newT[size];//重新分配空间for(inti=0;iistream&operator>>(istream&in,A
4、rray1D&r){cout<<"Intputarray";for(inti=0;i>r.elements[i];returnin;}templateostream&operator<<(ostream&out,constArray1D&r){cout<<"Array=";for(inti=0;i5、dmain(){Array1Da(5),b(8);Array1Dc;//采用缺省长度0cin>>a;cout<<"a"<>b;cout<<"b"<classSparseMatrix {public:SparseMatrix(intmaxRowSize6、,intmaxColSize){};~SparseMatrix(){};virtualvoidAdd(constSparseMatrix&B,SparseMatrix&C)const;virtualvoidMul(constSparseMatrix&B,SparseMatrix&C)const;virtualvoidTranspose(SparseMatrix&B)const;private: intmaxRows,maxCols;};程序4.4Term结构template structTerms7、{introw,col; Tvalue; };程序4.5行三元组表示的稀疏矩阵的C++类templateclassSeqTriple{public:SeqTriple(intmSize);~SeqTriple(){delete[]trip;};voidAdd(constSeqTriple&B,SeqTriple&C)const;voidMul(constSeqTriple&B,SeqTriple&C)const;voidTranspose(SeqTriple&B)const;friendistre8、am&operator>>(istream&input,constSeqTriple&);friendostream&operator<<(ostream&o
5、dmain(){Array1Da(5),b(8);Array1Dc;//采用缺省长度0cin>>a;cout<<"a"<>b;cout<<"b"<classSparseMatrix {public:SparseMatrix(intmaxRowSize
6、,intmaxColSize){};~SparseMatrix(){};virtualvoidAdd(constSparseMatrix&B,SparseMatrix&C)const;virtualvoidMul(constSparseMatrix&B,SparseMatrix&C)const;virtualvoidTranspose(SparseMatrix&B)const;private: intmaxRows,maxCols;};程序4.4Term结构template structTerms
7、{introw,col; Tvalue; };程序4.5行三元组表示的稀疏矩阵的C++类templateclassSeqTriple{public:SeqTriple(intmSize);~SeqTriple(){delete[]trip;};voidAdd(constSeqTriple&B,SeqTriple&C)const;voidMul(constSeqTriple&B,SeqTriple&C)const;voidTranspose(SeqTriple&B)const;friendistre
8、am&operator>>(istream&input,constSeqTriple&);friendostream&operator<<(ostream&o
此文档下载收益归作者所有