简单的aryth问题C#。 [英] Simple arythmetic question C#.

查看:57
本文介绍了简单的aryth问题C#。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

int a = 3;

int b = 5;

int c = 40;

int d = c- - - b * a;

Console.WriteLine($a = {a},b = {b} c = {c} d = {d});



订购c-- - b * a表达处理预期如下:



39 15

int d =(c - ) - (b * a); == 24?不是cource! d = 25



因为实际执行顺序如下:



int d = c-(b * a);和c--是后递减操作并以某种方式丢失



所以这些表达式相等



int d = c-(b * a)和

int d =(c - ) - (b * a)



可能有人解释了怎么样?



而且!



如果你试着用QuickWatch on(c-- - b * a),它在后台完成程序并且更改c 所以当你再次尝试使用它时,结果将是24!因为c已被更改。

我认为它不正确

因为QuickWatch应该执行表达式,但是在某个地方的不同位置不应该更改变量。



我尝试过:



简单的arythmetic问题C#。

int a = 3;
int b = 5;
int c = 40;
int d = c-- - b * a;
Console.WriteLine($"a={a}, b={b} c={c} d={d}");

an order of "c-- - b * a" expression handling expected like this :

39 15
int d = (c--)-(b*a); == 24? of cource not! d=25!

because in fact an execution order is following:

int d = c-(b*a); and c-- is postdecrement operation and lost somehow

so thiese expressons are equal

int d = c-(b*a) and
int d = (c--)-(b*a)

Could somebody explain how?

and moreover!

if you'll try to use QuickWatch on (c-- - b * a), it fulfilles the procedure in the background and changes the c so when you try to use it again, the result will be 24! because the c has being changed.
I believe it is not correct
because QuickWatch should execute the expression, but in the different place somewhere and shouldn't change the variable.

What I have tried:

simple arythmetic question C#.

推荐答案

a = {a},b = {b} c = {c} d = {d});



订购c-- - b * a表达处理预期如下:



39 15

int d =(c - ) - (b * a); == 24?不是cource! d = 25



因为实际执行顺序如下:



int d = c-(b * a);和c--是后递减操作并以某种方式丢失



所以这些表达式相等



int d = c-(b * a)和

int d =(c - ) - (b * a)



可能有人解释了怎么样?



而且!



如果你试着用QuickWatch on(c-- - b * a),它在后台完成程序并且更改c 所以当你再次尝试使用它时,结果将是24!因为c已被更改。

我认为它不正确

因为QuickWatch应该执行表达式,但是在某个地方的不同位置不应该更改变量。



我尝试过:



简单的arythmetic问题C#。
"a={a}, b={b} c={c} d={d}");

an order of "c-- - b * a" expression handling expected like this :

39 15
int d = (c--)-(b*a); == 24? of cource not! d=25!

because in fact an execution order is following:

int d = c-(b*a); and c-- is postdecrement operation and lost somehow

so thiese expressons are equal

int d = c-(b*a) and
int d = (c--)-(b*a)

Could somebody explain how?

and moreover!

if you'll try to use QuickWatch on (c-- - b * a), it fulfilles the procedure in the background and changes the c so when you try to use it again, the result will be 24! because the c has being changed.
I believe it is not correct
because QuickWatch should execute the expression, but in the different place somewhere and shouldn't change the variable.

What I have tried:

simple arythmetic question C#.


它们不一样。 c - 是一个后缀运算符,在使用后会更改 c

你使用这些时需要非常小心,因为你得到的结果可能不是你所期望的。请参阅此处:为什么x = ++ x + x ++给我错误的答案? [ ^ ]



They aren't the same. c-- is a postfix operator which changes c after it's been used.
You need to be very careful when using these, as the results you get may not be what you expected. See here: Why does x = ++x + x++ give me the wrong answer?[^]

Quote:

非常感谢你!我读了一篇文章,发现MSDN的优先级和评价顺序。

目前运营商的优先级没有回答为什么这个问题

例如

int a = 5;

int b = 3;

int i = 10;

int x =(i ++)+ a * b;

逻辑上(如果我们同意优先权)我应该是11(!)和x = 26,而不是25,因为i ++放在括号中。

但是++运算符最终仍然被省略,无论评估方向如何

Thank you so much! I've read an article and found the "Precedence and Order of Evaluation" from MSDN.
For the moment the precedence of operators doesn't answer the question "why"
for example
int a = 5;
int b = 3;
int i = 10;
int x = (i++) + a * b;
logically (if we agree with precedence) i should be 11 (!) and x = 26, not 25, because i++ is placed in brackets.
but the ++ operator is still omit eventually, no matter on evaluation direction





不,你错了。



No, you are wrong.

int x = i++ + a * b;

使用修补后增量,即使用当前价值然后加一个到

如果长手写的话,你得到这个:

Is using a post fix increment, which says, "use the current value of i and then add one to i"
If you write it long hand, you get this:

int i2 = i;
i += 1;
x = i2 + a * b;

这是:

25 == 10 + 3 * 5;

为什么 在同一个表达式中混合自动递增和递减运算符是如此充满:没有通用规则可以准确说明何时应该进行增量。

想想这个,假设我是10

That's why mixing auto increment and decrement operators in the same expression is so fraught: there are no universal rules to say exactly when the increment should be done.
Think of this, assuming i is 10

x = i++ + i;

x的值是多少?

20?

21?

22?



正确答案是21,我最终为11.

What is the value of x?
20?
21?
22?

The "right" answer is 21, and i ends up as 11.

int i2 = i;
i += 1;
x = i2 + i;

但是......如果从右到左严格评估,它很容易就是20:

But ... it could easily be 20 if strictly evaluated from right to left:

x = i + i;
i += 1;


正如你所说,你正在使用一个发布减少 - 所以c的值会在之后改变在表达式中使用。



所以
As you are stating yourself, you are using a postdecrement - so the value of c will change after it is used in your expression.

So
d=(c--)-(b*a);

与:

is identical to:

d=c-b*a; c=c-1;

就像

d=(--c)-(b*a);

c=c-1;d=c-b*a;

当你有多个操作在一个语句中的一个变量上时,它才会变得非常有趣。有趣的玩,但从未在生产代码中使用,所以我个人忘了它是如何工作的。

It only gets really fun when you have multiple of them operating on one variable in one statement. Fun to play with, but never used in production code so personally I just forgot how that works.


这篇关于简单的aryth问题C#。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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