![]() |
#2
rjsp2011-08-30 13:19
随手写的,你再检查一下
![]() #include <iostream> class Picture { public: explicit Picture( size_t wight=0, size_t hight=0 ) : wight_(wight), hight_(hight) { if( wight_*hight_ == 0 ) data_ = 0; else { data_ = new char[wight_*hight_]; memset( data_, 0x20, wight_*hight_*sizeof(char) ); } } Picture( const char* buf[], size_t n ) { hight_ = n; wight_ = 0; for( size_t r=0; r<hight_; ++r ) wight_ = std::max( wight_, strlen(buf[r]) ); if( wight_*hight_ == 0 ) data_ = 0; else { data_ = new char[wight_*hight_]; memset( data_, 0x20, wight_*hight_*sizeof(char) ); for( size_t r=0; r<hight_; ++r ) memcpy( &data_[r*wight_], buf[r], strlen(buf[r]) ); } } Picture( const Picture& rhs ) { wight_ = rhs.wight_; hight_ = rhs.hight_; if( rhs.data_ == 0 ) data_ = 0; else { data_ = new char[wight_*hight_]; memcpy( data_, rhs.data_, wight_*hight_*sizeof(char) ); } } Picture& operator=( const Picture& rhs ) { if( &rhs != this ) { delete[] data_; wight_ = rhs.wight_; hight_ = rhs.hight_; if( rhs.data_ == 0 ) data_ = 0; else { data_ = new char[wight_*hight_]; memcpy( data_, 0, wight_*hight_*sizeof(char) ); } } return *this; } ~Picture() { delete[] data_; } size_t wight() const { return wight_; } size_t hight() const { return hight_; } char* position( size_t row, size_t col ) { return &data_[row*wight_+col]; } const char* position( size_t row, size_t col ) const { return &data_[row*wight_+col]; } void CopyFrom( size_t row_dst, size_t col_dst, const Picture& rhs ) { for( size_t r=row_dst; r<hight_ && r-row_dst<rhs.hight_; ++r ) memcpy( &data_[r*wight_+col_dst], &rhs.data_[(r-row_dst)*rhs.wight_], std::min(wight_-col_dst,rhs.wight_) ); } private: size_t wight_, hight_; char* data_; }; std::ostream& operator<<( std::ostream& os, const Picture& obj ) { for( size_t r=0; r<obj.hight(); ++r ) { os.write( obj.position(r,0), obj.wight() ); os<<'\n'; } return os; } Picture frame( const Picture& p ) { Picture s( p.wight()+2, p.hight()+2 ); s.CopyFrom( 1, 1, p ); for( size_t i=1; i<s.hight()-1; ++i ) { *s.position(i,0) = '|'; *s.position(i,s.wight()-1) = '|'; } for( size_t i=1; i<s.wight()-1; ++i ) { *s.position(0,i) = '-'; *s.position(s.hight()-1,i) = '-'; } *s.position(0,0) = '+'; *s.position(0,s.wight()-1) = '+'; *s.position(s.hight()-1,0) = '+'; *s.position(s.hight()-1,s.wight()-1) = '+'; return s; } using namespace std; int main() { const char* name[] = { "abc", "defg", "higkl" }; Picture p( name, sizeof(name)/sizeof(name[0]) ); cout << p << endl; cout << frame(p) << endl; return 0; } |
只有本站会员才能查看附件,请 登录
class Picture{
public:
Picture():wight(0),hight(0),data(0)
{
}//对象数组用
Picture(const char *const *,int );//构造实际需求对象
Picture &operator=(const Picture &);
Picture(const Picture &);
~Picture();
//int max1(int w,int h);
char &position(int w,int h);//可作为左值
char position(int w,int h)const;//静态成员函数与上面的position函数存储在不同的代码区
void init(int w,int h);
friend void fram(const Picture &);
friend Picture operator|(const Picture &,const Picture &);
friend Picture operator&(const Picture &,const Picture &);
friend ostream & operator<<(ostream &,const Picture &);//运算符重载
private:
//protected:
int wight,hight;//像素的宽度和高度
char *data;//储存字符涉及到指针。需要拷贝构造函数,析构函数
void copyPicture(int ,int ,const Picture &);
};
char &Picture::position(int w,int h)
{
return data[w*wight+h];//验证信息
}//可做为左值向data数组存入字符
char Picture::position(int w,int h)const
{
return data[w*wight+h];
}//只能const Picture对象调用返回原来字符数组的副本
void Picture::init(int w,int h)
{
wight=w;
hight=h;
data=new char[wight*hight+1];//?有必要扩充1字符串结束标志
}
Picture::Picture(const char *const *s,int n )
{
int m=0;
const char*p=NULL;
int i,j;
for( i=0;i<n;i++)
{
int len=strlen(s[i]);
m=max(m,len);
}//找到最大的值
init(m,n);
for(i=0;i<n;i++)
{
p=s[i];
for(j=0;j<strlen(p);j++)
{
position(i,j)=p[j];
}
while(j<wight)
{
Picture::position(i,j)=' ';//不足的用空格表示
++j;
}
}
}
void Picture::copyPicture(int w ,int h ,const Picture &p)//复制起点
{
int i,j;
for(i=0;i<p.hight;i++)
{
for(j=0;j<p.wight;j++)
{
position(i+h,j+w)=p.position(i,j);
}
}
}
Picture::Picture(const Picture &p):wight(p.wight),hight(p.hight),data(new char[p.wight*p.hight])
{
Picture::copyPicture(0,0,p);
}
Picture &Picture::operator=(const Picture &p)
{
if(this!=&p)
{
delete[]data;
Picture::init(p.wight,p.hight);
Picture::copyPicture(0,0,p);
}
return *this;
}
Picture::~Picture()
{
delete[]data;
}
ostream & operator<<(ostream &o,const Picture &p)
{
int i,j;
for(i=0;i<p.hight;i++)
{
for(j=0;j<p.wight;j++)
{
o<<p.position(i,j);
}
o<<endl;
}
return o;
}
void fram(const Picture &p)
{
Picture s;
int i,j;
s.init(p.wight+2,p.hight+2);
for(i=1;i<s.hight-1;i++)
{
s.position(i,0)='|';
s.position(i,s.wight-1)='|';
}
for(j=1;j<s.wight-1;j++)
{
s.position(0,j)='-';
s.position(s.hight-1,j)='-';
}
s.position(0,0)='+';
s.position(0,s.hight-1)='+';
s.position(s.wight-1,0)='+';
s.position(s.wight-1,s.hight-1)='+';
s.copyPicture(1,1,p);
cout<<s<<endl;
//return s;
}
//int &p(int n,int *num)
//{
// return num[n];
//
//}
int main()
{
char *name[]={"abc","defg","higkl"};
Picture p(name,3);
cout<<p<<endl;
fram(p);
return 0;
}//测试代码
为什么调用fram函数老出现问题