程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> C++沉思錄第九章的練習

C++沉思錄第九章的練習

編輯:C++入門知識

C++沉思錄第九章的練習


先把代碼貼著,明天再補充總結!
#ifndef PICTURE_H
#define PICTURE_H
#include
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 ) = '-';
	}
	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;
}


 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved