//---------------------------------------------------------------------------
// Step03结果
//---------------------------------------------------------------------------
#ifndef CharPictureH
#define CharPictureH
#include <stdlib.h>
class CharPicture;
class Pic {
public:
static Pic Frame(Pic& pic) { return Pic(new FramePic(pic)); }
static Pic HCat(Pic& pic1,Pic& pic2) { return Pic(new HCatPic(pic1,pic2)); }
static Pic VCat(Pic& pic1,Pic& pic2) { return Pic(new VCatPic(pic1,pic2)); }
public:
Pic(CharPicture * pp): p(pp) { }
~Pic( ) { ReleaseRef( ); }
Pic(const Pic& pic) { p = pic.p; AddRef( ); }
Pic& operator=(const Pic& rhs); {
if( this != &rhs ) {
ReleaseRef( );
p = rhs.p;
AddRef( );
}
return *this;
}
void Dispaly( ) const { p->Display( ); }
int Width( ) const { return p->Width(); }
int Height( ) const { return p->Height();}
protected:
void AddRef( ) {
++p->use;
}
void ReleaseRef( ) {
if (--p->use == 0)
delete p;
}
protected:
CharPicture * p;
};
class CharPicture
{
friend class Pic; //指定友元类
protected:
static int bigger(int a,int b) { return (a>b?a:b); }
public:
CharPicture( ):use(1) { }
virtual ~CharPicture(){ } //增加虚的析构函数
CharPicture(const CharPicture& pic); //只声明,不定义
CharPicture& operator=(const CharPicture& rhs); //只声明,不定义
void Dispaly() const; //待实现
int Width( ) const { return width;}
int Height( ) const {return height;}
protected:
int use;
int width;
int height;
};
class DataPic:public CharPicture
{
public:
DataPic(const char * pstr) {
int len = strlen(pstr);
str = new char[len+1];
strcpy(str,pstr);
width = len;
height = 1;
}
virtual ~DataPic( ) { delete[] str;}
protected:
char * str;
};
class FramePic:public CharPicture
{
public:
FramePic(Pic & pic1) :kernel(pic1) { width = kernel.Width()+2; height = kernel.Height( )+2; }
protected:
Pic kernel;
};
class HCatPic:public CharPicture
{
public:
HCatPic(Pic& pic1,Pic& pic2) : left(pic1),right(pic2) { width = left.Width() + right.Width();
height = bigger(left.Height(),right.Height( ));
}
protected:
Pic left;
Pic right;
};
class VCatPic:public CharPicture
{
public:
VCatPic(Pic& pic1,Pic& pic2) : top(pic1),bottom(pic2) { width = bigger(top.Width(), bottom.Width());
height = top.Height() + bottom.Height( );
}
protected:
Pic top;
Pic bottom;
};
#endif
//---------------------------------------------------------------------------
// 实现Display. (为方便阅读,已经写过的不写了)
// 1)将Pic的Display委托给CharPicture
// 2)实现CharPicture中Display(也可成虚的)
// 3)实现虚的DisplayLine。
// 4)添加辅助函数PadBlank(int from,int to);
//---------------------------------------------------------------------------
#ifndef CharPictureH
#define CharPictureH
#include <stdlib.h>
class CharPicture;
class Pic {
public:
static Pic Frame(Pic& pic);
static Pic HCat(Pic& pic1,Pic& pic2);
static Pic VCat(Pic& pic1,Pic& pic2);
public:
Pic(CharPicture * pp): p(pp) { }
~Pic( );
Pic(const Pic& pic);
Pic& operator=(const Pic& rhs);
void Dispaly( ) const { p->Display( ); }
void DisplayLine(int row,int wd) const { p->DisplayLine(row,wd); }
int Width( ) const { return p->Width(); }
int Height( ) const { return p->Height();}
protected:
void AddRef( ) {
++p->use;
}
void ReleaseRef( ) {
if (--p->use == 0)
delete p;
}
protected:
CharPicture * p;
};
class CharPicture
{
friend class Pic; //指定友元类
protected:
static int bigger(int a,int b) { return (a>b?a:b); }
public:
CharPicture( ):use(1) { }
virtual ~CharPicture(){ } //增加虚的析构函数
CharPicture(const CharPicture& pic); //只声明,不定义
CharPicture& operator=(const CharPicture& rhs); //只声明,不定义
void Dispaly() const {
for(int i=1;i<=height;i++)
DisplayLine(i,width);
cout<<endl;
}
int Width( ) const { return width;}
int Height( ) const {return height;}
protected:
virtual void DisplayLine(int lineno,int wd) const = 0;
void PadBlank(int from,int to) const{
for(int i= from;i<=to;i++)
cout<<" ";
}
protected:
int use;
int width;
int height;
};
class DataPic:public CharPicture
{
public:
DataPic(const char * pstr) {
int len = strlen(pstr);
str = new char[len+1];
strcpy(str,pstr);
width = len;
height = 1;
}
virtual ~DataPic( ) { delete[] str;}
protected:
virtual void DisplayLine(int lineno,int wd) const {
if (lineno == 1) {
for(int i=0;i<width;i++)
cout<<str[i];
PadBlank(width,wd);
} else
PadBlank(0,wd);
}
protected:
char * str;
};
class FramePic:public CharPicture
{
public:
FramePic(Pic & pic1) :kernel(pic1) { width = kernel.Width()+2; height = kernel.Height( )+2; }
virtual void DisplayLine(int lineno,int wd) const {
if (lineno>=1 && lineno<=height) {
if ((lineno==1) ||(lineno==height)) { //顶或底
cout<<"+";
int len=kernel.Width();
for (int i=0;i<len;i++) cout<<"-";
cout<<"+";
} else { //内部
cout<<"|";
kernel.DisplayLine(lineno,kernel.Width());
cout<<"|";
}
PadBlank(width,wd);
}else { //越界
PadBlank(0,wd);
}
}
protected:
Pic kernel;
};
class HCatPic:public CharPicture
{
public:
HCatPic(Pic& pic1,Pic& pic2) : left(pic1),right(pic2) { width = left.Width() + right.Width();
height = bigger(left.Height(),right.Height( ));
}
virtual void DisplayLine(int lineno,int wd) const {
left.DisplayLine(lineno,left.Width());
right.DisplayLine(lineno,right.Width());
PadBlank(width,wd);
}
protected:
Pic lef