使用翻转编写递增/递减功能 [英] Writing a incrementing/decrementing function with roll-over

查看:63
本文介绍了使用翻转编写递增/递减功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我需要写一个简单的递增/递减函数,如下所示:


unsigned char

更改(unsigned char x,unsigned char min,unsigned char max,signed char d);


x是增加/减少的值

min是x可以假设的最小值

max是x可以假设的最大值

d是正数还是负数,表示x必须递增多少或

递减

当然,返回值是x的新值。


一些例子来澄清这个问题:

x = 10,min = 0,max = 20,d = 5 - 15

x = 10,min = 0,max = 20,d = -5 - 5

x = 18,min = 0,max = 20,d = 5 - 2

x = 18,min = 5,max = 20,d = 5 - 7

x = 3,min = 0,max = 20,d = -5 - 19

x = 10,min = 8,max = 20,d = -5 - 18


我想要一个只使用unsigned char的函数...


另一个类似的问题。我有一个变量x(无符号字符),我必须

增加它。增量存储在另一个变量d(unsigned char)中。

x不能假设值大于max,存储在另一个变量中(unsigned

char)。 br />
我通常写道:


if(x + d> max)

x = max;

否则

x + = d;


我认为它不正确,因为当x = 200,d = 100和max = 220时会发生什么?

如果我写:


if(x> max-d)

x = max;

else

x + = d;


当max = 10且d = 20时会发生什么?我需要一个大于unsigned的变量

char来做临时和x + d?如果我使用很长时间,我需要超长的

变量?

非常感谢您的帮助。

Hi all,

I need to write a simple incrementing/decrementing function like this:

unsigned char
change( unsigned char x, unsigned char min, unsigned char max, signed char d);

x is the value to increase/decrease
min is the minimum value that x can assume
max is the maximum value that x can assume
d is positive or negative and indicates how much x must be incremented or
decremented
Of course, the return value is the new value for x.

Some examples to clarify the question:
x=10,min=0,max=20,d=5 --15
x=10,min=0,max=20,d=-5 --5
x=18,min=0,max=20,d=5 --2
x=18,min=5,max=20,d=5 --7
x= 3,min=0,max=20,d=-5 --19
x=10,min=8,max=20,d=-5 --18

I''d like a function that use only unsigned char...

Another similar question. I have a variabile x (unsigned char) and I must
increment it. The increment is stored in another variable, d (unsigned char).
x can''t assume values greater than max, stored in another variable (unsigned
char).
I usually write:

if( x+d>max )
x = max;
else
x += d;

I think it''s not correct because what happens when x=200, d=100 and max=220??
If I write:

if( x>max-d )
x = max;
else
x += d;

what happens when max=10 and d=20?? I need a variable greater than unsigned
char to do the temporary sum x+d? And if I use long, I need extra-long
variables?
Thank you very much for the help.

推荐答案

如果你使用更宽的类型(使用unsigned char作为最终目标),它将更容易。


例如使用unsigned int而不是unsigned char。
If you use wider types (with unsigned char to be the final target), it will
be easier.

E.g. use unsigned int instead of unsigned char.




" pozz" < pN ************ @ libero.itwrote in message

新闻:是的****************** ** @ twister1.libero.it ...

"pozz" <pN************@libero.itwrote in message
news:Yp********************@twister1.libero.it...

大家好,


我需要写一个简单的递增/递减这样的函数:


unsigned char

change(unsigned char x,unsigned char min,unsigned char max,signed char

d);


x是增加/减少的值

min是x可以假设的最小值

max是x可以假设的最大值

d是正数还是负数,表示x必须递增多少



递减

当然,返回值是x的新值。


一些例子来澄清这个问题:

x = 10,min = 0,max = 20,d = 5 - 15

x = 10,min = 0,max = 20,d = -5 - 5

x = 18,min = 0, max = 20,d = 5 - 2

x = 18,min = 5,max = 20,d = 5 - 7

x = 3,min = 0, max = 20,d = -5 - 19

x = 10,mi n = 8,max = 20,d = -5 - 18

我想要一个只使用unsigned char的函数...


另一个类似的问题。我有一个变量x(无符号字符),我必须

增加它。增量存储在另一个变量d中(无符号

char)。

x不能假设值大于max,存储在另一个变量中

(未签名

char)。

我通常会写:


if(x + d> max)

x = max;

else

x + = d;


我认为这不正确,因为什么时候x = 200,d = 100和

max = 220 ??

如果我写:


if( x> max-d)

x = max;

else

x + = d;


当max = 10且d = 20时会发生什么?我需要一个大于

的变量unsigned

char来做临时和x + d?如果我使用很长时间,我需要超长的

变量?
Hi all,

I need to write a simple incrementing/decrementing function like this:

unsigned char
change( unsigned char x, unsigned char min, unsigned char max, signed char
d);

x is the value to increase/decrease
min is the minimum value that x can assume
max is the maximum value that x can assume
d is positive or negative and indicates how much x must be incremented
or
decremented
Of course, the return value is the new value for x.

Some examples to clarify the question:
x=10,min=0,max=20,d=5 --15
x=10,min=0,max=20,d=-5 --5
x=18,min=0,max=20,d=5 --2
x=18,min=5,max=20,d=5 --7
x= 3,min=0,max=20,d=-5 --19
x=10,min=8,max=20,d=-5 --18

I''d like a function that use only unsigned char...

Another similar question. I have a variabile x (unsigned char) and I must
increment it. The increment is stored in another variable, d (unsigned
char).
x can''t assume values greater than max, stored in another variable
(unsigned
char).
I usually write:

if( x+d>max )
x = max;
else
x += d;

I think it''s not correct because what happens when x=200, d=100 and
max=220??
If I write:

if( x>max-d )
x = max;
else
x += d;

what happens when max=10 and d=20?? I need a variable greater than
unsigned
char to do the temporary sum x+d? And if I use long, I need extra-long
variables?



x =((d max)||(x max-d))? max:x + d;

-

Fred L. Kleinschmidt

波音助理技术研究员

技术架构师,软件重用项目

x = ( (d max) || (x max-d) ) ? max : x+d;
--
Fred L. Kleinschmidt
Boeing Associate Technical Fellow
Technical Architect, Software Reuse Project




pozz写道:

pozz wrote:

大家好,


我需要编写一个简单的递增/递减函数,如下所示:


unsigned char

change(unsigned char) x,unsigned char min,unsigned char max,signed char d);


x是要增加/减少的值

min是x可以的最小值假设

max是x可以假设的最大值

d是正数还是负数,表示x必须递增多少或

递减

当然,返回值是x的新值。


一些例子来澄清这个问题:

x = 10,min = 0,max = 20,d = 5 - 15

x = 10,min = 0,max = 20,d = -5 - 5

x = 18, min = 0,max = 20,d = 5 - 2

x = 18,min = 5,max = 20,d = 5 - 7

x = 3,min = 0,max = 20,d = -5 - 19
x = 10,min = 8,max = 20,d = -5 - 18

我想要一个只使用unsigned char的函数...

Hi all,

I need to write a simple incrementing/decrementing function like this:

unsigned char
change( unsigned char x, unsigned char min, unsigned char max, signed char d);

x is the value to increase/decrease
min is the minimum value that x can assume
max is the maximum value that x can assume
d is positive or negative and indicates how much x must be incremented or
decremented
Of course, the return value is the new value for x.

Some examples to clarify the question:
x=10,min=0,max=20,d=5 --15
x=10,min=0,max=20,d=-5 --5
x=18,min=0,max=20,d=5 --2
x=18,min=5,max=20,d=5 --7
x= 3,min=0,max=20,d=-5 --19
x=10,min=8,max=20,d=-5 --18

I''d like a function that use only unsigned char...



我不会直接帮助这个,但请看下面的一两个提示。

I won''t directly help with this, but see below for a hint or two.


>

另一个类似的问题。我有一个变量x(无符号字符),我必须

增加它。增量存储在另一个变量d(unsigned char)中。

x不能假设值大于max,存储在另一个变量中(unsigned

char)。 br />
我通常写道:
>
Another similar question. I have a variabile x (unsigned char) and I must
increment it. The increment is stored in another variable, d (unsigned char).
x can''t assume values greater than max, stored in another variable (unsigned
char).
I usually write:



你通常可以这样做:

x =(x + d)%(最大值+1);


现在使用它,你应该能够找到解决上述问题的方法,对吧?


发布你的尝试,如果它不起作用


< snipped>

You can normally do this:
x = (x + d) % (max+1);

Now using that, you should be able to figure
out a way to solve your problem above, right?

Post your attempt if it won''t work

<snipped>


这篇关于使用翻转编写递增/递减功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆