代码整理如下:
Frame_Pic.cpp
p(pic), corner(c), sideborder(s), topborder(t) {}
return p.height() + 2;
}
return p.width() + 2;
}
if(row < 0 || row >= height()){
pad(os, 0, wd);
}else{
if(row == 0 || row == height() - 1){
os << corner;
int i = p.width();
while(--i >= 0)
os << topborder;
os << corner;
}else{
os << sideborder;
p.display(os, row - 1, p.width());
os << sideborder;
}
pad(os, width(), wd);
}
}
return new Frame_Pic(::reframe(p, c, s, t), c, s, t);
}
#include "P_Node.h"
#include "Picture.h"
class Frame_Pic : public P_Node{
friend Picture frame(const Picture&);
friend Picture reframe(const Picture&, char, char, char);
Frame_Pic(const Picture&, char='+', char='|', char ='-');
int height() const;
int width() const;
void display(ostream&, int, int) const;
Picture reframe(char, char, char);
Picture p;
char corner;
char sideborder;
char topborder;
};
Hcat_Pic.cpp
#include "Hcat_Pic.h"
Hcat_Pic::Hcat_Pic(const Picture& l, const Picture& r) : left(l), right(r) {}
int Hcat_Pic::height() const{
return max(left.height(), right.height());
}
int Hcat_Pic::width() const{
return left.width() + right.width();
}
void Hcat_Pic::display(ostream& os, int row, int wd) const{
left.display(os, row, left.width());
right.display(os, row, right.width());
pad(os, width(), wd);
}
Picture Hcat_Pic::reframe(char c, char s, char t){
return new Hcat_Pic(
::reframe(left, c, s, t),
::reframe(right, c, s, t)
);
}
Hcat_Pic.h
#include "P_Node.h"
#include "Picture.h"class Hcat_Pic : public P_Node{
friend Picture operator|(const Picture&, const Picture&);
friend Picture reframe(const Picture&, char, char, char);
Hcat_Pic(const Picture&, const Picture&);
int height() const;
int width() const;
void display(ostream&, int, int) const;
Picture reframe(char, char, char);
Picture left, right;
};
P_Node.cpp
#ifndef P_NODE_H
#define P_NODE_H
#include <iostream>
#include "Picture.h"
using namespace std;
class Picture;
class P_Node {
friend class Picture;
friend Picture reframe(const Picture&, char, char, char);
protected:
P_Node();
virtual ~P_Node();
virtual int height() const = 0;
virtual int width() const = 0;
virtual void display(ostream&, int, int) const = 0;
virtual Picture reframe(char, char, char) = 0;
static int max(int x, int y) { return x > y ? x : y; }
static void pad(ostream& os, int x, int y){
for(int i = x; i < y; i++)
os << " ";
}
int use;
};
#endif /*P_NODE_H*/
#include "Picture.h"
#include "String_Pic.h"
#include "Frame_Pic.h"
#include "Hcat_Pic.h"
#include "Vcat_Pic.h"
orig.p->use++;
}
if(--p->use == 0)
delete p;
}
orig.p->use++;
if(--p->use == 0)
delete p;
p = orig.p;
return *this;
}
return p->height();
}
return p->width();
}
p->display(o, x, y);
}
int ht = picture.height();
for(int i = 0; i < ht; i++){
picture.display(os, i, 0);
os << endl;
}
return os;
}
return new Frame_Pic(pic);
}
return pic.p->reframe(c, s, t);
}
return new Hcat_Pic(l, r);
}
return new Vcat_Pic(t, b);
}
#define PICTURE_H
#include <iostream>
#include "P_Node.h"
class P_Node;
class Picture{
friend ostream& operator <<(ostream& os,const Picture& p);
friend Picture frame(const Picture&);
friend Picture reframe(const Picture&, char, char, char);
friend Picture operator|(const Picture&, const Picture&);
friend Picture operator&(const Picture&, const Picture&);
friend class String_Pic;
friend class Frame_Pic;
friend class Hcat_Pic;
friend class Vcat_Pic;
public:
Picture() : p(0) {}
Picture(const char* const*, int);
Picture(const Picture&);
~Picture();
private:
Picture(P_Node*);
int height() const;
int width() const;
void display(ostream&, int, int) const;
P_Node* p;
};
#include <cstring>
#include <iostream>
using namespace std;
for(int i = 0; i < n; i++){
data[i] = new char[strlen(p[i])+1];
strcpy(data[i], p[i]);
}
}
for(int i = 0; i < size; i++)
delete[] data[i];
delete[] data;
}
return size;
}
int n = 0;
for(int i = 0; i < size; i++){
n = max(n, strlen(data[i]));
}
return n;
}
int start = 0;
if(row >= 0 && row < height()){
os << data[row];
start = strlen(data[row]);
}
pad(os, start, width);
}
use++;
return this;
}
friend class Picture;
~String_Pic();
int height() const;
int width() const;
void display(ostream&, int, int) const;
Picture reframe(char, char, char);
int size;
};
return top.height() + bottom.height();
}
return max(top.width(), bottom.width());
}
if( row >= 0 && row < top.height())
top.display(os, row, wd);
else if(row < top.height() + bottom.height())
bottom.display(os, row - top.height(), wd);
else
pad(os, 0, wd);
}
return new Vcat_Pic(
::reframe(top, c, s, t),
::reframe(bottom, c, s, t)
);
}
#include "Picture.h"
friend Picture operator&(const Picture&, const Picture&);
friend Picture reframe(const Picture&, char, char, char);
Vcat_Pic(const Picture&, const Picture&);
int height() const;
int width() const;
void display(ostream&, int, int) const;
Picture reframe(char, char, char);
Picture top, bottom;
};
#include "iostream"
using namespace std;
int main(int argc, char const *argv[])
{
Picture p(init, 3);
Picture q = frame(p);
cout << frame(q & (p | q)) << endl;
cout << reframe(q,'*','*','*') << endl;
return 0;
}