MOOC清华《面向对象程序设计》第5章:OOP版填充数字旋转矩阵-产品级!-终极中文版!
来源:互联网 发布:中国赴欧洲旅游数据 编辑:程序博客网 时间:2024/05/16 11:24
最大的封装就是化于无形,最好的产品就是傻瓜产品!最终极的用户就是什么都不懂的用户!——连这种用户都能得心应手地使用我们的产品,我们的产品才算做到家了!代码如下:
//main.cpp#include <iostream>#include "Matrix.h"using namespace std;const int str_length = 7;//经测试,至少为7;印证了每个汉字占2个字节('\0'占一个字节) int main() {int size;char wise_or_anti[str_length];char first_point[str_length];cout << "请输入矩阵的边长 N: ";cin >> size;cout << "请输入“顺时针”或“逆时针”: ";cin >> wise_or_anti;cout << "请输入从哪一个角开始填充(左上角、左下角、右上角、右下角): ";cin >> first_point;Matrix obj(size);obj.fill_style(wise_or_anti, first_point);obj.fill();cout << obj;return 0;}
//Matrix.h#ifndef Matrix_h#define Matrix_h#include <iostream>using namespace std;class Matrix{int _size;//方阵的规模 int *_data;//数据空间int row, col;char dir;char* _wise_or_anti;char* _first_point;int findPosition_clockwise();//顺时针辅助函数int findPosition_anticlock();//逆时针辅助函数 public:Matrix(int size);~Matrix();void fill();int fill_style(char* str1, char* str2);friend ostream& operator<< (ostream& out, const Matrix& m); };#endif
//Matrix.cpp#include <iostream>#include <cstring>#include "Matrix.h"using namespace std;Matrix::Matrix(int size): _size(size) {//改变构造函数的初始化列表可以改变第一个数字的位置,注意不要把size-1写成了size _data = new int[size * size];memset(_data, 0, sizeof(int) * _size * _size);}Matrix::~Matrix(){delete _data;}ostream& operator<< (ostream& out, const Matrix& m){for(int r = 0; r < m._size; r++){ //rowfor(int c = 0; c < m._size; c++) //colcout << *(m._data + r * m._size + c) << '\t';cout << endl;}}int Matrix::fill_style(char* str1, char* str2){//strcpy(_wise_or_anti, str1);//fatal error!//strcpy(_first_point, str2);//fatal error!_wise_or_anti = str1;_first_point = str2;if(strcmp(str1, "顺时针") == 0){if(strcmp(str2, "左上角") == 0) return 1;if(strcmp(str2, "右上角") == 0) return 2;if(strcmp(str2, "右下角") == 0) return 3;if(strcmp(str2, "左下角") == 0) return 4;}if(strcmp(str1, "逆时针") == 0){if(strcmp(str2, "左上角") == 0) return 5;if(strcmp(str2, "左下角") == 0) return 6;if(strcmp(str2, "右下角") == 0) return 7;if(strcmp(str2, "右上角") == 0) return 8;}}void Matrix::fill(){int n = fill_style(_wise_or_anti, _first_point);if(n == 1){//顺时针、左上角 row = 0;col = -1;dir = 'R';for(int num = 1; num <= _size * _size; num++){int pos = findPosition_clockwise();_data[pos] = num;}}if(n == 2){//顺时针、右上角 row = -1;col = _size - 1;dir = 'D';for(int num = 1; num <= _size * _size; num++){int pos = findPosition_clockwise();_data[pos] = num;}}if(n == 3){//顺时针、右下角 row = _size - 1;col = _size;dir = 'L';for(int num = 1; num <= _size * _size; num++){int pos = findPosition_clockwise();_data[pos] = num;}}if(n == 4){//顺时针、左下角 row = _size;col = 0;dir = 'U';for(int num = 1; num <= _size * _size; num++){int pos = findPosition_clockwise();_data[pos] = num;}}if(n == 5){//逆时针、左上角 row = -1;col = 0;dir = 'D';for(int num = 1; num <= _size * _size; num++){int pos = findPosition_anticlock();_data[pos] = num;}}if(n == 6){//逆时针、左下角 row = _size - 1;col = -1;dir = 'R';for(int num = 1; num <= _size * _size; num++){int pos = findPosition_anticlock();_data[pos] = num;}}if(n == 7){//逆时针、右下角 row = _size;col = _size - 1;dir = 'U';for(int num = 1; num <= _size * _size; num++){int pos = findPosition_anticlock();_data[pos] = num;}}if(n == 8){//逆时针、右上角 row = 0;col = _size;dir = 'L';for(int num = 1; num <= _size * _size; num++){int pos = findPosition_anticlock();_data[pos] = num;}}}int Matrix::findPosition_clockwise(){ //clockwise为顺时针的意思 switch(dir){case 'D':if(row < _size - 1 && _data[(row + 1) * _size + col] == 0)row++;else{dir = 'L'; // next directioncol--;}break;case 'L':if(col > 0 && _data[row * _size + col - 1] == 0)col--;else{dir = 'U'; // next directionrow--;}break;case 'U':if(row > 0 && _data[(row - 1) * _size + col] == 0)row--;else{dir = 'R'; // next directioncol++;}break;case 'R':if(col < _size - 1 && _data[row * _size + col + 1] == 0)col++;else{dir = 'D'; // next directionrow++;}break;}return row * _size + col;}int Matrix::findPosition_anticlock(){ //anticlock为逆时针的意思 switch(dir){case 'D':if(row < _size - 1 && _data[(row + 1) * _size + col] == 0)row++;else{dir = 'R'; // next directioncol++;}break;case 'R':if(col < _size - 1 && _data[row * _size + col + 1] == 0)col++;else{dir = 'U'; // next directionrow--;}break;case 'U':if(row > 0 && _data[(row - 1) * _size + col] == 0)row--;else{dir = 'L'; // next directioncol--;}break;case 'L':if(col > 0 && _data[row * _size + col - 1] == 0)col--;else{dir = 'D'; // next directionrow++;}break;}return row * _size + col;}
全方位无死角测试结果:
阅读全文
0 0
- MOOC清华《面向对象程序设计》第5章:OOP版填充数字旋转矩阵-产品级!-终极中文版!
- MOOC清华《面向对象程序设计》第5章:OOP版填充数字矩阵-产品级终极中文版 v 2.0(增加按行、按列填充)
- MOOC清华《面向对象程序设计》第5章:OOP版填充数字旋转矩阵
- MOOC清华《面向对象程序设计》第5章:OOP版填充数字矩阵(采用模板方法)
- MOOC清华《面向对象程序设计》第5章:填充数字矩阵-终极中文版 v 3.0(采用模板方法,并增加集中控制功能)
- MOOC清华《面向对象程序设计》第5章:OOP版电子计价器
- MOOC清华《程序设计基础》第5章:数字旋转方阵的填充-算法一
- MOOC清华《程序设计基础》第5章:数字旋转方阵的填充-算法二
- MOOC清华《面向对象程序设计》第5章:OOP版谁做的好事
- MOOC清华《面向对象程序设计》第9章:听课感想
- MOOC清华《VC++面向对象与可视化程序设计》第5章:位图资源例程
- MOOC清华《VC++面向对象与可视化程序设计》第5章:图标资源例程
- MOOC清华《面向对象程序设计》第3章:对象组合实验
- MOOC清华《VC++面向对象与可视化程序设计》第5章:菜单资源例程(创建统计计算菜单项)
- MOOC清华《VC++面向对象与可视化程序设计》第5章:对话框资源例程(模态对话框样例)
- MOOC清华《VC++面向对象与可视化程序设计》第5章:对话框资源例程(非模态对话框样例)
- MOOC清华《面向对象程序设计》第2章:私有成员与公有成员不可同名实验
- MOOC清华《面向对象程序设计》第2章:auto变量实验
- ora-01033:oracle initialization or shutdown in progress(ORA-01589: 要打开数据库则必须使用 RESETLOGS 或 NORESETL)
- React Native学习网站收藏
- OCPP协议翻译之目录
- 快速幂( 分治 )
- 解决“java.lang.NoClassDefFoundError“的问题
- MOOC清华《面向对象程序设计》第5章:OOP版填充数字旋转矩阵-产品级!-终极中文版!
- Linux 网络栈剖析
- 除了封装,继承,多态 您还知道那些?-面向对象设计的金字塔
- 浅谈数据库及表设计的几个原则
- SSM框架——详细整合教程(Spring+SpringMVC+MyBatis)
- JS 设计模式--继承
- oracle存储过程、声明变量、for循环
- 死链是怎样产生的
- python-2 字符串