您的位置:首页 > 其它

自顶向下,逐步求精

2017-11-29 23:05 141 查看
“自顶而下,逐步求精”(top-down design)

当我们面对一个复杂的问题的时候,我们会觉得手足无措,不知道该从何做起,这时,我们不妨使用“自顶而下,逐步求精”的方法来实现这个复杂的问题。那什么是“自顶而下,逐步求精”呢?

下面是百度百科给出的解释 :将复杂的大问题分解为相对简单的小问题,找出每个问题的关键、重点所在,然后用精确的思维定性、定量地去描述问题。其核心本质是”分解”。

Top-down parsing is a parsing strategy where one first looks at the highest level of the parse tree and works down the parse tree by using the rewriting rules of a formal grammar. LL parsers are a type of parser that uses a top-down parsing strategy.



说得简单点,也就是讲一个大问题,分解成一个个小的问题,再对这一个个小问题继续分解,直到这些小问题已经可以很简单地解决了。这样,大问题也不再是大问题了,就可以比较容易地解决了。

下面以洗衣机为例,来解释一下“自顶而下,逐步求精”的想法。

洗衣机的大致过程是

浸泡 -> 洗涤 -> 脱水 -> 停机

对于浸泡的伪代码如下`

input waterlevel , time1; //输入水位和浸泡时间
waterinswitch(open) ;
if(waterlevel >= getwatervolumn) //进水
waterinswitch(close);
if(clothes is more than water)
waterinswitch(open) ;
until (getwatervolumn is suitable)
waterinswitch(close);
then timecounter() //开始计时
if(timecounter() == time1)
finish ;//完成浸泡


接着是洗涤的伪代码;

input  washingtimes,washtime;
timecounter()
while(washingtimes >= times){
do{

{motorrun(left) for 4 seconds;
motorrun(right) for 4 seconds;}
}until(washtime == timecounter())
motorrun(stop);
wateroutswitch (open) ;
if(watervolumn == 0)
wateroutswitch(close);
then
waterinswitch(open) ;
until(watervolum == waterlevel)
waterinswitch(close);
add 1 to times;
}


然后就是脱水了

while(water is not less){
motorrun(left) for 2 seconds;
motorrun(right) for 2 seconds;
if(watervolumn == 0) break;
}
motor (stop);


最后就是洗衣机的提示

halt(sussess);
ring belt for several times;


当然,在不同的过程中也许会有同样的操作,这样,我们就可以写出一些类似函数的东西,让我们调用。简化我们的程序,也就是逐步求精的过程了。

以上就是一个自顶而下,逐步求精的一个例子。我们可以看出,这样的方法对我们的编程有很大的帮助。可以更好的帮助我们思考问题。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: