最近软导课学到了自顶向下,逐步求精,top-down方法,有点小小感触。
我们先来看一下百度百科的描述:
将复杂的大问题分解为相对简单的小问题,找出每个问题的关键、重点所在,然后用精确的思维定性、定量地去描述问题。其核心本质是”分解”。
自顶向下,就是先写一个大框架,先把轮廓写好,然后再慢慢地填充细节。
对于C++程序来说,可以说基本模块就是函数,先想好需要那些函数,而且函数不一定要实现出来,可以先写个函数名,确定函数用途,函数参数个数和类型,然后再把每个函数之间关系连接好,再一个个填充函数。
下面,我想举一个大整数的例子,由于C++不像java一样支持大整数,所以需要自己写高精度。
我们把每个高精度数当作一个结构体,我们就要想我们需要那些功能,由于结构体并没有定义等于,小于,加号这些符号,我们需要重载运算符。
- 构造函数
- 赋值
- 加法
- 减法
- 乘法
- 除法
- 求余
- 小于
- 大于
- 大于等于
- 小于等于
等于
细看下来发现,需求真的很多,而且如果每想到一个就写一个,思路就会很凌乱,所以我们就需要Top-down方法。
struct bign{
bign(const bign& b)
bign(int num) {}
bign operator = (const char* num){}
bign operator = (int num){}
bign operator + (const bign& b){}
bign operator - (const bign& b){}
bign operator * (const bign& b)const{}
bign operator / (const bign& b){}
bign operator % (const bign& b){}
bool operator <(const bign& b) const{}
bool operator >(const bign& b) const{}
bool operator<=(const bign& b) const{}
bool operator>=(const bign& b) const{}
bool operator!=(const bign& b) const{}
bool operator==(const bign& b) const{}
}
写完这个之后,思路就很清晰了(并没有),就可以慢慢填坑了。