先把代码贴着,明天再补充总结!
#ifndef PICTURE_H #define PICTURE_H #include<iostream> using namespace std; class Picture { private: int height, width; char * data; char & position(int row, int col) { return data[row*width + col]; } char position(int row, int col)const { return data[row*width + col]; } static int max(int m, int n){ return m > n ? m : n; } void init(int h, int w); void copypic(int n, int m, const Picture & p); void clear(int a, int b, int c, int d); public: Picture() :height(0), width(0), data(0){} Picture(const char * const *, int); Picture(const Picture &); ~Picture(){ delete[] data; } Picture & operator =(const Picture &); friend ostream & operator <<(ostream & os, const Picture & p); friend Picture frame(const Picture & p); friend Picture operator |(const Picture & p, const Picture & q);//横向连接 friend Picture operator &(const Picture & p, const Picture & q);//纵向连接 }; void Picture::init(int h, int w) { height = h; width = w; data = new char[w*h]; } Picture::Picture(const char * const * arr, int m) { int w = 0; int i; for (i = 0; i < m; ++i) w = Picture::max(m, strlen(arr[i])); init(m, w); for (i = 0; i < m; ++i) { const char * c = arr[i]; int len = strlen(arr[i]); int j = 0; while (j < len) { position(i, j) = c[j]; ++j; } while (j < width) { position(i, j) = ' '; ++j; } } } void Picture::copypic(int n, int m, const Picture & p) { for (int i = 0; i < p.height; ++i) { for (int j = 0; j < p.width; ++j) position(n + i, m + j) = p.position(i, j); } } Picture::Picture(const Picture & p) :height(p.height), width(p.width), data(new char[p.height*p.width]) { copypic(0, 0, p); } void Picture::clear(int a, int b, int c, int d) { for (int i = a; i < c;++i) for (int j = b; j < d; ++j) position(i, j) = ' '; } Picture & Picture::operator=(const Picture & p) { if (this != &p) { delete[]data; init(p.height, p.width); copypic(0, 0, p); } return *this; } ostream & operator <<(ostream & os, const Picture & p) { for (int i = 0; i < p.height; ++i) { for (int j = 0; j < p.width; ++j) os << p.position(i, j); os << endl; } return os; } Picture frame(const Picture & p) { Picture r; r.init(p.height + 2, p.width + 2); for (int i = 1; i <= r.width-1 ; ++i) { r.position(0,i) = '-'; r.position(r.height - 1,i ) = '-'; <pre> } for (int j = 1; j <=r.height-1; ++j) { r.position(j,0) = '|'; r.position(j,r.width-1) = '|'; } r.position(0, 0) = '+'; r.position(0, r.width-1) = '+'; r.position(r.height - 1, 0) = '+'; r.position(r.height - 1, r.width - 1) = '+'; r.copypic(1, 1, p); return r; } Picture operator |(const Picture & p, const Picture & q)//横向连接 { Picture r; r.init(Picture::max(p.height, q.height), p.width + q.width); r.clear(p.height, 0, r.height, p.width);
r.clear(q.height,p.width, r.height, r.width);r.copypic(0, 0, p);r.copypic(0, p.width, q);return r;}Picture operator &(const Picture & p, const Picture & q)//纵向连接{Picture r;r.init(p.height+ q.height, Picture::max(p.width, q.width));r.clear(0,
p.width, p.height, r.width);r.clear(p.height, q.width, r.height, r.width);r.copypic(0, 0, p);r.copypic(p.height,0 ,q);return r;}#endif
#include"picture.h" int main() { char * init[] = { "Summer", "love", "Xieweizhong" }; Picture p(init,3); cout << p << endl; cout << frame((p | frame(p)) | (p | frame(p))) << endl; cout << frame((p & frame(p)) | (p | frame(p))) << endl; cout << frame((p | frame(p)) | (p & frame(p))) << endl; cout << frame((p & frame(p)) | (p & frame(p))) << endl; cout << frame((p | frame(p)) & (p | frame(p))) << endl; cout << frame((p & frame(p)) & (p | frame(p))) << endl; cout << frame((p | frame(p)) & (p & frame(p))) << endl; cout << frame((p & frame(p)) & (p & frame(p))) << endl; return 0; }
时间: 2024-10-12 04:50:53