2(*1)为下面各个表达式加上全部括号:
a=b+c*d<<2&8
a&077 !=3
a==b || a==c && c<5
c=x !=0
0<=i <7
f(1,2)+3
a=-1++b--- -5
a=b==c++
a=b=c=0
a[4][2]*=*b?c:*d*2
a-b,c=d
解析:
a=b+(((c*d)<<2)&8)
a&(077!=3)
(a==b)||(a==c)&&(c<5)
c=(x!=0)
(0<=i)<7
(f(1,2))+3
a=(-1)+(+(b--))-5
a=(b==(c++))
a=(b=(c=0))
a[4][2]*=(*b)?c:((*d)*2)
(a-b),(c=d)
写的时候发现真的很头疼啊,有好几个都拿不准,查了些资料。当然Benjarne建议我们不要写这种让人难以读懂的代码,在拿不准或是容易让人产生歧义的地方,加括号就是了~
附:
C++优先级列表
Precedence | Operator | Description | Example | Associativity |
1 | () | Grouping operator | (a + b) / 4; | left to right |
2 | ! | Logical negation | if( !done ) ... | right to left |
3 | ->* | Member pointer selector | ptr->*var = 24; | left to right |
4 | * | Multiplication | int i = 2 * 4; | left to right |
5 | + | Addition | int i = 2 + 3; | left to right |
6 | << | Bitwise shift left | int flags = 33 << 1; | left to right |
7 | < | Comparison less-than | if( i < 42 ) ... | left to right |
8 | == | Comparison equal-to | if( i == 42 ) ... | left to right |
9 | & | Bitwise AND | flags = flags & 42; | left to right |
10 | ^ | Bitwise exclusive OR | flags = flags ^ 42; | left to right |
11 | | | Bitwise inclusive (normal) OR | flags = flags | 42; | left to right |
12 | && | Logical AND | if( conditionA && conditionB ) ... | left to right |
13 | || | Logical OR | int i = (a > b) ? a : b | left to right |
14 | ? : | Ternary conditional (if-then-else) | if( conditionA || conditionB ) ... | right to left |
15 | = | Assignment operator | int a = b; | right to left |
16 | , | Sequential evaluation operator | for( i = 0, j = 0; i < 10; i++, j++ ) ... | left to right |