Logic Tests Series (3) - SUBT 逻辑测试系列之三 - SUBT

in #cn7 years ago (edited)

@justyy 's series of Logits Tests:

We have implemented the DECR function in last series using the only four keywords in this tiny programming language. This post, you will need to implement the SUBT function which takes two parameters and subtract one from the other i.e. X-=Y

In C++, you can probably implement this using a LOOP.

void subt(unsigned int &x, unsigned int y) {
  for (int i = 0; i < y; i ++) {
     x --;
  }
}

We take one from X variable Y times and the variable X is a reference variable in C++. If we translate this into this language, here it comes:

SUBT(X, Y) {
   LOOP(Y) {
       DECR(X)  // DECR has been implemented, so we can use it.
  }
}

As you see, complex functions are built upon simple instructions namely ASGN, LOOP, INCR and ZERO. We are going to explore more in the next coming series, please be patient!


Image Credit: Pixabay.com

@justyy 的逻辑测试系列:

上次添加了 DECR 函数来把 一个变量减一,我们这次来定义一个 SUBT 函数来实现 把 减法运算,也就是 X-=Y

如果我们用 C++ 来实现,大概是这样的:

void subt(unsigned int &x, unsigned int y) {
  for (int i = 0; i < y; i ++) {
     x --;
  }
}

这里 x 变量是引用,也就是直接在函数里能修改其值,退出函数后x能有变化。我们翻译成这门语言是:

SUBT(X, Y) {
   LOOP(Y) {
       DECR(X)  // DECR 在上期已经定义过了,这里拿来一用。
  }
}

复杂的函数是一点一点建立起来的,虽然这门语言只有 INCR, ZERO, ASGN, LOOP 这四个关键字,但是我们可以通过组合实现最基本的功能,进而完成更复杂的功能,让我们拭目以待。

CN 每日排行榜

// Later, it may be reposted to my blogs: justyy.com, helloacm.com and codingforspeed.com 稍后同步到我的中文博客和英文计算机博客

Originally published at https://steemit.com Thank you for reading my post, feel free to Follow, Upvote, Reply, ReSteem (repost) @justyy which motivates me to create more quality posts.

原文首发于 https://Steemit.com 首发。感谢阅读,如有可能,欢迎Follow, Upvote, Reply, ReSteem (repost) @justyy 激励我创作更多更好的内容。

@justyy 是CN 区的点赞机器人,对优质内容进行点赞,只要代理给 @justyy 每天收利息(100 SP 每天0.04 SBD)并且能获得一次相应至少2倍的点赞,可以认为是VP 200%+ ,详细请看:


欢迎你发表你的见解和看法,特别有意思的评论我可能会奖励你1 SBD哦。
Interesting Comments might be rewarded with 1 SBD.

Sort:  

拭目以待,加快速度。。。

慢慢来。大象需要一口一口吃。

好。。。。特别有意思,我都没有想过DECR可以这样写。

This post recieved an upvote from minnowpond. If you would like to recieve upvotes from minnowpond on all your posts, simply FOLLOW @minnowpond

Thank you so much for sharing all this information! that's great! All the best! Waiting for more posts! :)

Very appealing and informative post shared followed and upvoted. @justyy Wish you the best with all your future endeavors.

Coin Marketplace

STEEM 0.19
TRX 0.15
JST 0.029
BTC 63792.82
ETH 2563.50
USDT 1.00
SBD 2.66