C++沉思录第九章的练习

2023-12-06 10:19
文章标签 c++ 练习 第九章 沉思

本文主要是介绍C++沉思录第九章的练习,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

先把代码贴着,明天再补充总结!
#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;
}


这篇关于C++沉思录第九章的练习的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/461471

相关文章

利用c++判断水仙花数并输出示例代码

《利用c++判断水仙花数并输出示例代码》水仙花数是指一个三位数,其各位数字的立方和恰好等于该数本身,:本文主要介绍利用c++判断水仙花数并输出的相关资料,文中通过代码介绍的非常详细,需要的朋友可以... 以下是使用C++实现的相同逻辑代码:#include <IOStream>#include <vec

基于C++的UDP网络通信系统设计与实现详解

《基于C++的UDP网络通信系统设计与实现详解》在网络编程领域,UDP作为一种无连接的传输层协议,以其高效、低延迟的特性在实时性要求高的应用场景中占据重要地位,下面我们就来看看如何从零开始构建一个完整... 目录前言一、UDP服务器UdpServer.hpp1.1 基本框架设计1.2 初始化函数Init详解

C++ 右值引用(rvalue references)与移动语义(move semantics)深度解析

《C++右值引用(rvaluereferences)与移动语义(movesemantics)深度解析》文章主要介绍了C++右值引用和移动语义的设计动机、基本概念、实现方式以及在实际编程中的应用,... 目录一、右值引用(rvalue references)与移动语义(move semantics)设计动机1

C++ move 的作用详解及陷阱最佳实践

《C++move的作用详解及陷阱最佳实践》文章详细介绍了C++中的`std::move`函数的作用,包括为什么需要它、它的本质、典型使用场景、以及一些常见陷阱和最佳实践,感兴趣的朋友跟随小编一起看... 目录C++ move 的作用详解一、一句话总结二、为什么需要 move?C++98/03 的痛点⚡C++

详解C++ 存储二进制数据容器的几种方法

《详解C++存储二进制数据容器的几种方法》本文主要介绍了详解C++存储二进制数据容器,包括std::vector、std::array、std::string、std::bitset和std::ve... 目录1.std::vector<uint8_t>(最常用)特点:适用场景:示例:2.std::arra

C++构造函数中explicit详解

《C++构造函数中explicit详解》explicit关键字用于修饰单参数构造函数或可以看作单参数的构造函数,阻止编译器进行隐式类型转换或拷贝初始化,本文就来介绍explicit的使用,感兴趣的可以... 目录1. 什么是explicit2. 隐式转换的问题3.explicit的使用示例基本用法多参数构造

C++,C#,Rust,Go,Java,Python,JavaScript的性能对比全面讲解

《C++,C#,Rust,Go,Java,Python,JavaScript的性能对比全面讲解》:本文主要介绍C++,C#,Rust,Go,Java,Python,JavaScript性能对比全面... 目录编程语言性能对比、核心优势与最佳使用场景性能对比表格C++C#RustGoJavapythonjav

C++打印 vector的几种方法小结

《C++打印vector的几种方法小结》本文介绍了C++中遍历vector的几种方法,包括使用迭代器、auto关键字、typedef、计数器以及C++11引入的范围基础循环,具有一定的参考价值,感兴... 目录1. 使用迭代器2. 使用 auto (C++11) / typedef / type alias

C++ scoped_ptr 和 unique_ptr对比分析

《C++scoped_ptr和unique_ptr对比分析》本文介绍了C++中的`scoped_ptr`和`unique_ptr`,详细比较了它们的特性、使用场景以及现代C++推荐的使用`uni... 目录1. scoped_ptr基本特性主要特点2. unique_ptr基本用法3. 主要区别对比4. u

C++11中的包装器实战案例

《C++11中的包装器实战案例》本文给大家介绍C++11中的包装器实战案例,本文结合实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录引言1.std::function1.1.什么是std::function1.2.核心用法1.2.1.包装普通函数1.2.