自顶向下,逐步求精

“自顶而下,逐步求精”(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;

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

以上就是一个自顶而下,逐步求精的一个例子。我们可以看出,这样的方法对我们的编程有很大的帮助。可以更好的帮助我们思考问题。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值