목표 :은 "캐릭터 이미지"를 조작하는 클래스와 함수의 시리즈를 작성합니다. "문자 이미지"소위, 문자 인쇄의 직사각형 배열입니다.
코드 전에 9.6 :
Picture.h
사용법 #include <iostream>
#INCLUDE <CString을>
네임 스페이스를 사용하여 표준;
클래스 그림 {
친구 ostream에 << & 연산자 (& ostream에, 영상 및 CONST);
공공의:
화상 ()의 높이 (1), 폭 (1), 데이터 (0) {}
INT 최대 (INT의 m, n을 INT) {복귀 m> N? m : N; }
보이드 INIT (INT H를 INT w) {
높이 = H;
w = 폭;
새로운 문자 데이터 = [* 높이 폭];
}
그림 (CONST 숯 *의 * CONST 배열 INT 않음) {
INT w = 0;
I 값 int;
대 (I = 0; I <N은, 내가 ++)
w = 최대 (나 strlen, W (배열 [I]));
초기화 (N, w);
대 (I = 0; I <N은, 내가 ++) {
CONST 숯 *의 SRC = 배열 [I];
LEN = INT 나 strlen (SRC);
INT의 J = 0;
반면 (j <LEN) {
위치 (I, J) = SRC [J]
++ J;
}
반면 (j <폭) {
위치 (I, J) = "";
++ J;
}
}
}
그림 (CONST 그림 & P)의 높이 (p.height), 폭 (p.width), 데이터 (새로운 문자 [p.height p.width *]) {copyblock (0, 0, P); }
~ 그림 () {[] 데이터를 삭제 }
영상 및 연산자 = (CONST 그림 & P);
사유의:
INT 높이, 폭;
숯 * 데이터;
문자 및 위치 (INT 행, INT의 COL) {
데이터를 반환 [행 * 너비 + COL];
}
문자 위치 (INT 행, INT의 COL) {CONST
데이터를 반환 [행 * 너비 + COL];
}
보이드 copyblock (INT 행, INT의 COL, CONST 그림 & P) {
(0 = 1을 나타내는 int i가 p.height를 <; I ++) {
대 (INT의 J = 0; J <p.width; ++ j)의
위치 p.position = (I, J) (ⅰ 행, J + COL을 +);
}
}
};
<< ostream에 & 연산자 (& ostream에, 영상 및 CONST);
픽쳐 프레임 (CONST 그림 & P);
Picture operator&(const Picture&, const Picture&);
Picture operator|(const Picture&, const Picture&);
picturecontrol.cpp:
#include <iostream>
#include "Picture.h"
using namespace std;
const char* init[] = {"Paris", "in the", "Spring"};
ostream& operator<<(ostream& o, const Picture& p){
for(int i=0; i<p.height; ++i){
for(int j=0; j<p.width; ++j){
o << p.position(i, j);
}
o << endl;
}
return o;
}
Picture frame(const Picture& p)
{}
Picture operator&(const Picture&, const Picture&)
{}
Picture operator|(const Picture&, const Picture&)
{}
int main(int argc, char const *argv[])
{
Picture p(init, 3);
cout << p << endl;
Picture q = p;
cout << q << endl;
//Picture r = p | q;
//cout << r << endl;
//Picture s = q & r;
//cout << s << endl;
//cout << frame(s) << endl;
return 0;
}
#include <iostream>
#include <cstring>
using namespace std;
class Picture{
friend ostream& operator<<(ostream&, const Picture&);
friend Picture frame(const Picture& p);
friend Picture operator&(const Picture&, const Picture&);
friend Picture operator|(const Picture&, const Picture&);
public:
Picture(): height(0), width(0), data(0) {}
Picture(const char* const* array, int n){
int w = 0;
int i;
for(i=0; i<n; i++)
w = max(w, strlen(array[i]));
init(n, w);
for(i=0; i<n; i++){
const char* src = array[i];
int len = strlen(src);
int j = 0;
while(j < len){
position(i, j) = src[j];
++j;
}
while(j < width){
position(i, j) = ' ';
++j;
}
}
}
Picture(const Picture& p): height(p.height), width(p.width), data(new char[p.height * p.width]){ copyblock(0, 0, p); }
~Picture() { delete[] data; }
Picture& operator=(const Picture& p);
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];
}
void copyblock(int row, int col, const Picture& p){
for(int i=0; i<p.height; ++i){
for(int j=0; j<p.width; ++j)
position(i+row, j+col) = p.position(i, j);
}
}
void clear(int r1, int c1, int r2, int c2){
for(int r=r1; r<r2; ++r)
for(int c=c1; c<c2; ++c)
position(r, c) = ' ';
}
static int max(int m, int n) { return m > n ? m : n; }
void init(int h, int w){
height = h;
width = w;
data = new char[height * width];
}
};
ostream& operator<<(ostream&, const Picture&);
Picture frame(const Picture& p);
Picture operator&(const Picture&, const Picture&);
Picture operator|(const Picture&, const Picture&);
picturecontrol.cpp
#include <iostream>
#include "Picture.h"
using namespace std;
const char* init[] = {"Paris", "in the", "Spring"};
ostream& operator<<(ostream& o, const Picture& p){
for(int i=0; i<p.height; ++i){
for(int j=0; j<p.width; ++j){
o << p.position(i, j);
}
o << endl;
}
return o;
}
Picture frame(const Picture& p){
Picture r;
r.init(p.height + 2, p.width + 2);
for(int i=1; i < r.height-1; ++i){
r.position(i, 0) = '|';
r.position(i, r.width-1) = '|';
}
for(int j=1; j<r.width-1; ++j){
r.position(0, j) = '-';
r.position(r.height-1, j) = '-';
}
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.copyblock(1, 1, p);
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.copyblock(0, 0, p);
r.copyblock(p.height, 0, q);
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.copyblock(0, 0, p);
r.copyblock(0, p.width, q);
return r;
}
int main(int argc, char const *argv[])
{
Picture p(init, 3);
Picture q = frame(p);
cout << frame(q & (p | q)) << endl;
return 0;
}