blv转换mp4:高手,,,vc++问题呀?

来源:百度文库 编辑:高校问答 时间:2024/04/28 17:23:05
int j=5
j=(++j)+(++j)+(++j);等于几
运算原理
谢了
我算等不对呀
结果为22
不是21我算也是21
但运行结果为22

这个问题不是太清楚,我用程序论证了,可是不是太明白
程序如下:
#include<iostream.h>
int sun(int j)
{
++j;
cout<<j<<endl;
return j;
}
void main()
{
int j=5,a=0;
j=(j=sun(j))+(j=sun(j))+(j=sun(j));
//j=(++j)+(++j)+(++j);
//a+=++j;
//a+=++j;
//a+=++j;
cout<<j<<endl;
}
结果:
6
7
8
22

我想可能是计算的优先级问题吧~!
我曾经遇到过此问题,但不是这样式的,
我只知道C++和C语言的运算时的优先级和方向是有些是相反的.

#include<iostream.h>
#include<stdlib.h>
int main()
{
int j=5,a=0;
j=5;
cout<<j<<endl;
j=5;
cout<<j+(++j)<<endl;
j=5;
cout<<(++j)+(++j)<<endl;
j=5;
cout<<j+(++j)+(++j)<<endl;
j=5;
cout<<(++j)+(++j)+(++j)<<endl;
j=5;
cout<<(++j)+(++j)+(++j)+(++j)<<endl;
j=5;
cout<<(++j)+(++j)+(++j)+(++j)+(++j)<<endl;
system("pause");
return 0;
}

结果:
5
12
14
19
22
31
41

发现规律没有
注意第二个

有时候CB下和VC下编译同样的程序结果可能也不一样,顺序问题,所以不要写成这种有歧义的程序,否则可能出现莫名其妙的错误!

++j,是先自增1,在参加运算。

结果是21没错

6+7+8 ?