// Picture.cpp: implementation of the Picture class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "Picture.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
ostream operator<<(ostream& o,Picture& p)
{
p.Display(o);
return o;
}
Picture operator+(Picture& p1,Picture& p2)
{
Picture p(p1);
p.RightAdd(p2);
return p;
}
//////////////////////////////////////////////////////////////////////////
Picture::Picture():m_width(0),m_height(0),m_pdata(NULL)//默认构造函数
{
}
Picture::Picture(const char*const*const arr ,int len)
{
int maxlen=0;
for (int i=0;i<len;i++)
{
maxlen = Picture::max(maxlen,strlen(arr[i]));
}
m_width = maxlen;//字符串长度,不包括 '\0'
m_height = len;
m_pdata = new char[m_width*m_height];
memset(m_pdata,0,m_width*m_height);
for (i=0;i<m_height;i++)
{
for (int j=0;arr[i][j]!='\0';j++)
{
m_pdata[i*m_width+j]=arr[i][j];
}
}
}
Picture::~Picture()
{
delete m_pdata;
}
Picture::Picture(const Picture& p)
{
m_width = p.m_width;
m_height= p.m_height;
m_pdata = new char[m_width*m_height];
memcpy(m_pdata,p.m_pdata,m_width*m_height);
}
Picture& Picture::operator =(const Picture& p)
{
if (this==&p)//自我赋值
{return *this;
}
delete m_pdata;
this->Picture::Picture(p);//把复制构造函数当普通函数使用
return *this;
}
//////////////////////////////////////////////////////////////////////////
void Picture::Display(ostream& o)
{
for (int i=0;i<m_height;i++)
{
for (int j=0;j<m_width;j++)
{
o<<position(i,j);
}
o<<endl;
}
}
//////////////////////////////////////////////////////////////////////////
Picture& Picture::ShowFrame(char jiao/* = */,char topbottom/* = */,char leftright/* = */)
{
m_jiao = jiao;
m_topbottom = topbottom;
m_leftright = leftright;
return frame();
}
Picture& Picture::frame()
{
int w = m_width + 2;
int h = m_height + 2;
char* pdata=new char[w*h];
memset(pdata,0,w*h);
for (int i=0;i<m_height;i++)
{
for (int j=0;j<m_width;j++)
{
pdata[(i+1)*w+j+1]=position(i,j);
}
}
delete m_pdata;
m_pdata = pdata;
m_width = w;
m_height = h;
kuang(0,0,w,h);
return *this;
}
Picture& Picture::LeftAdd(const Picture& p)
{
int w = m_width + p.m_width;
int h = Picture::max(m_height,p.m_height);
char* pdata = new char[w*h];
memset(pdata,0,w*h);
for (int i=0;i<m_height;i++)
{
for (int j=0;j<m_width;j++)
{
pdata[i*w+j+p.m_width] = position(i,j);
}
}
for (i=0;i<p.m_height;i++)
{
for (int j=0;j<p.m_width;j++)
{
pdata[i*w+j]=p.position(i,j);
}
}
m_width = w;
m_height =h;
delete m_pdata;
m_pdata = pdata;
return *this;
}
Picture& Picture::RightAdd(const Picture& p)
{
int w = m_width + p.m_width;
int h = Picture::max(m_height,p.m_height);
char* pdata = new char[w*h];
memset(pdata,0,w*h);
for (int i=0;i<m_height;i++)
{
for (int j=0;j<m_width;j++)
{
pdata[i*w+j] = position(i,j);
}
}
for (i=0;i<p.m_height;i++)
{
for (int j=0;j<p.m_width;j++)
{
pdata[i*w+j+m_width]=p.position(i,j);
}
}
m_width = w;
m_height =h;
delete m_pdata;
m_pdata = pdata;
return *this;
}
Picture& Picture::TopAdd(const Picture& p)
{
int w = Picture::max(m_width,p.m_width);
int h = m_height + p.m_height;
char* pdata = new char[w*h];
memset(pdata,0,w*h);
for (int i=0;i<m_height;i++)
{
for (int j=0;j<m_width;j++)
{
pdata[(i+p.m_height)*w+j] = position(i,j);
}
}
for (i=0;i<p.m_height;i++)
{
for (int j=0;j<p.m_width;j++)
{
pdata[i*w+j] = p.position(i,j);
}
}
m_height = h;
m_width = w;
delete m_pdata;
m_pdata = pdata;
return *this;
}
Picture& Picture::BottomAdd(const Picture& p)
{
int w = Picture::max(m_width,p.m_width);
int h = m_height + p.m_height;
char* pdata = new char[w*h];
memset(pdata,0,w*h);
for (int i=0;i<m_height;i++)
{
for (int j=0;j<m_width;j++)
{
pdata[i*w+j] = position(i,j);
}
}
for (i=0;i<p.m_height;i++)
{
for (int j=0;j<p.m_width;j++)
{
pdata[(i+m_height)*w+j] = p.position(i,j);
}
}
m_height = h;
m_width = w;
delete m_pdata;
m_pdata = pdata;
return *this;
}