在VS2010中的amusung计算 [英] amusung calculation in VS2010

查看:109
本文介绍了在VS2010中的amusung计算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我测试了VS2010中操作符的优先级,所以使用这个小代码

I tested priority of operators in VS2010 so with this small code

#include <iostream>
using std::cout;
void main(){
	int i=5;
	cout <<"\n i++="<<i++;
	i=5;
	cout <<"\n i++ + i="<<i++ + i;
        i=5;
	cout <<"\n i++ +i++ + ++i="<<i++ +i++ + ++i;
        i=5;
	cout <<"\n i++ + i++ +i + ++i="<<i++ + i++ +i + ++i;
	i=5;
	int b=(i++ +i++ +i);
	cout <<"\n i++ +i++ +i="<<b;
	cout << "\ni++ +i++ +i + ++i="<<b+ ++i;
	i=5;
	cout <<"\n i++ + i++ + i++ + ++i="<<i++ + i++ + i++ + ++i;
	cout<<"\n";
	system ("pause");
}



所以答案对我来说真的很有趣

i ++ = 5

i ++ + i = 10

i ++ + i ++ + ++ i = 18

i ++ + i ++ + i + ++ i = 24

i ++ + i ++ + i = 15

i ++ + i ++ + i + ++ i = 23

i ++ + i ++ + i ++ + ++ i = 24



我不明白这个结果是怎么做的?

任何人都可以帮助我?


so answers are really amusing for me
i++=5
i++ + i=10
i++ +i++ + ++i=18
i++ + i++ +i + ++i=24
i++ +i++ +i=15
i++ +i++ +i + ++i=23
i++ + i++ + i++ + ++i=24

and i don't understand how this results are made?
any one can help me?

推荐答案

这是低至C ++规范,该规范基于较旧的C规范,该规范未准确说明必须执行前后增量指令的时间 - 或者实际上指令的执行顺序必须保持与您所写的相同:如果愿意,编译器可以自由地重新排序它们以适应它的执行管道。



这意味着当您尝试使用前后增量来玩游戏时所获得的内容并未由规范定义,并且因编译器而异。



在C#中并非如此,其中执行顺序是非常明确定义的,并且还指定了前后增量的确切时间。



It's down to the C++ specification, which is based on the older C specification, which does not state exactly when pre and post increment instructions must be performed - or indeed that the order of execution of instructions must be maintained to be the same as you wrote: the compiler is free to reorder them to suit it's execution pipeline if it wishes.

This means that what you get when you try to play games with pre-and post increment is not defined by the specification and varies from compiler to compiler.

This is not the case in C#, where the execution order is very specifically defined, and the exact timing of pre and post increments are also specified.

i = 10;
x = ++i + 5;



可以理解为:


Can be read as:

i = 10;
i = i + 1;
x = i + 5;



是吗?这有点......简单......

是的,确实如此。或者,也许不是。



这很简单,如果你以简单的方式使用它 - 作为数组索引器,例如:


Is that it? It's a bit...simple...
Yes, it is. Or, perhaps not.

It is simple, if you use it in simple ways - as an array indexer for example:

x = myArray[index++];

或作为循环增量:



Or as a loop increment:

for (i = 0; i < 10; i++)
   {
   WriteLine(myArray[i]);
   }

但在那之后,你进入一个充满困惑和痛苦的世界!



例如,这会留下什么值i:

But after that, you are into a world of confusion and pain!

For example, what does this leave as a value of i:

int i,j;

i = 10;
for (j = 0; j < 5; j++)
    {
    i = i++;
    }

答案是:不变。我仍然在10岁。为什么?想象一下:如果我们扩展它会是什么样的?

int i = 10;

i = i ++;

如果我们用C#写这个,然后IL看起来像这样:

The answer is: unchanged. i remains at 10. Why? Think of it like this: what does this look like if we expand it?
int i = 10;
i = i++;
If we write this in C#, then the IL looks like this:

.line 14,14 : 13,24 ''
IL_0001:  ldc.i4.s   10                    Push a constant value '10' to the stack, 4 byte integer,
IL_0003:  stloc.0                          Pop the top of the stack into local number 0
.line 15,15 : 13,21 ''
IL_0004:  ldloc.0                          Push local number 0 to the stack
IL_0005:  dup                              Duplicate the top of the stack
IL_0006:  ldc.i4.1                         Push a constant value '1', 4 byte integer
IL_0007:  add                              Pop the top two stack items, add them, and push the result
IL_0008:  stloc.0                          Store the top of the stack in local number 0
IL_0009:  stloc.0                          Store the top of the stack in local number 0

什么?在扩展的C#代码中,它返回:

What? In expanded C# code, that comes back as:

int i = 10;
int j = i;
int k = j;
k = k + 1;
i = k;
i = j;

这很奇怪......因为你可以扔掉中间的三条线而不影响结果。



不应该那样做,不是吗?



是的。是的,它应该:i ++是一个后缀操作:它说,记住i的值,然后递增1,然后返回你记住的值。



所以你告诉编译器做的是通过用你开始时的值覆盖它来忽略增量的结果。



有趣的是,如果你试试它在Visual Studio C ++编译器中...它没有...因为它以不同的方式处理它!



所以,现在我们有你的第一个原因当你开始使用递增和递减运算符时要小心:它实际上是一个副作用,一行代码插入到你的行中,如果你不仔细思考,它就不会按照你的想法行事。

Which is rather strange...because you could throw away the three lines in the middle without affecting the results.

It shouldn't do that, should it?

Yes. Yes, it should: i++ is a postfix operation: It says, "remember the value of i, then increment i by one, and then return the value you remembered".

So what you have told the compiler to do is ignore the result of the increment by overwriting it with the value you started off with.

Interestingly, if you try it in the Visual Studio C++ compiler...it doesn't... because it handles it differently!

So, now we have the first reason why you have to be careful when you start using increment and decrement operators for real: it's effectively a side effect, a whole line of code inserted into your line, and if you don't think very carefully, it won't do what you think.


OK
这是一个程序员认为在上述情况下编码行为的不合适的事情,因为将算法转换为代码可能很困难。
$ b例如,如果我们定义一个堆栈并订阅t,则为$ b更高的元素下面的视觉工作室代码是正确的

OK This is a not a suitable thing that a programmer think to code behavior in cases like above because converting an algorithm to code can be difficult enough .
for example if we define a stack and to subscribe two higher elements the below code in visual studio is correct
a=pop()-pop()



但是这段代码不是你想要的


but this code doesn't what do you want

{
a=sub (pop(),pop())
}
int sub(int a,int b)
{
return a-b;
}



因为首先将右侧pop()(堆栈中的顶部元素)作为b发送,然后将下一个顶部元素(函数中的左侧元素)作为a发送。

但是在上面的代码中(由OriginalGriff写的)


because at first the right pop() (top element in stack) sent as b then next top element (left one in function) sent as a.
but in the above code (wrote by OriginalGriff)

int i,j;
 
i = 10;
for (j = 0; j < 5; j++)
    {
    i = i++;
    }



我认为它等于


I think it is equal to

i = 10;
for (j = 0; j < 5; j++)
    {
    i = i;
    i=i+1;
    }



,最后i = 15

我希望看到一些关于此的标准。

因此,最好的方法是编写非常清晰的代码,就像我们想要学习7岁的孩子一样。


and finally i=15
I hope to see some defined standard about this .
So it seems the best way is to write codes very clear as when we want to learn to 7 years old child.


这篇关于在VS2010中的amusung计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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