需求演進
- 輸出多行文本:
Paris\nin the\nSpring
- 加邊框:
+-------+\n|Paris |\n|in the |\n|Spring |\n+-------+
- 橫向拼接 (
|) 與縱向拼接 (&) 組合任意複雜圖形。
核心接口設計
class Picture {
// 友元操作符:自然語法
friend Picture frame(const Picture&); // 加框
friend Picture operator|(const Picture&, const Picture&); // 橫向拼接
friend Picture operator&(const Picture&, const Picture&); // 縱向拼接
friend std::ostream& operator<<(std::ostream&, const Picture&); // 輸出
// 內部表示
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 r, int c, const Picture& src);
void init(int h, int w); // 分配內存並填充空格
void clear(int r, int c, int h, int w); // 區域填充空格
public:
Picture() : height_(0), width_(0), data_(nullptr) {}
Picture(const char* const* lines, int n); // 從 C 字符串數組構造
Picture(const Picture&); // 深拷貝
~Picture() { delete[] data_; }
Picture& operator=(const Picture&); // 深拷貝賦值
static int max(int a, int b) { return a > b ? a : b; }
};
關鍵算法實現
構造函數:計算最大寬度、分配內存、填充空格
Picture::Picture(const char* const* lines, int n) {
int w = 0;
for (int i = 0; i < n; ++i) w = max(w, static_cast<int>(strlen(lines[i])));
init(n, w);
for (int i = 0; i < n; ++i) {
const char* src = lines[i];
int len = strlen(src);
int j = 0;
for (; j < len; ++j) position(i, j) = src[j];
for (; j < width_; ++j) position(i, j) = ' ';
}
}
橫向拼接 operator|
Picture operator|(const Picture& lhs, const Picture& rhs) {
Picture result;
result.init(max(lhs.height_, rhs.height_), lhs.width_ + rhs.width_);
// 左圖複製
for (int r = 0; r < lhs.height_; ++r)
for (int c = 0; c < lhs.width_; ++c)
result.position(r, c) = lhs.position(r, c);
// 右圖複製
for (int r = 0; r < rhs.height_; ++r)
for (int c = 0; c < rhs.width_; ++c)
result.position(r, lhs.width_ + c) = rhs.position(r, c);
return result;
}
縱向拼接 operator&
Picture operator&(const Picture& top, const Picture& bottom) {
Picture result;
result.init(top.height_ + bottom.height_, max(top.width_, bottom.width_));
// 上圖
for (int r = 0; r < top.height_; ++r)
for (int c = 0; c < top.width_; ++c)
result.position(r, c) = top.position(r, c);
// 下圖
for (int r = 0; r < bottom.height_; ++r)
for (int c = 0; c < bottom.width_; ++c)
result.position(top.height_ + r, c) = bottom.position(r, c);
return result;
}
加框 frame
Picture frame(const Picture& p) {
Picture result;
result.init(p.height_ + 2, p.width_ + 2);
// 四角
result.position(0, 0) = result.position(0, result.width_-1) = '+';
result.position(result.height_-1, 0) = result.position(result.height_-1, result.width_-1) = '+';
// 上下邊
for (int c = 1; c < result.width_-1; ++c)
result.position(0, c) = result.position(result.height_-1, c) = '-';
// 左右邊
for (int r = 1; r < result.height_-1; ++r)
result.position(r, 0) = result.position(r, result.width_-1) = '|';
// 內容區複製
for (int r = 0; r < p.height_; ++r)
for (int c = 0; c < p.width_; ++c)
result.position(r+1, c+1) = p.position(r, c);
return result;
}
使用展示
int main() {
const char* lines[] = { "Paris", "in the", "Spring" };
Picture p(lines, 3);
// 複雜組合:frame( frame(p) & (p | frame(p)) )
std::cout << frame(frame(p) & (p | frame(p))) << std::endl;
return 0;
}
設計啟示
- 運算符重載 讓領域邏輯表達如數學公式般直觀。
- 值語義 + 深拷貝 使組合操作無副作用,安全可組合。
- 封裝內部表示,對外僅暴露語義操作,便於未來優化存儲結構(如改用
std::vector<string>)。
- 「從使用例推導接口」比「憑空發明接口」更務實。