12 个解决方案
#1
1+2*3//*的优先级比较高,所以先算2*3,再算1+。。。
(1+2)*3//这难道就是传说中的结合律。。。。呵呵
(1+2)*3//这难道就是传说中的结合律。。。。呵呵
#2
顶楼上,不懂,可以百度嘛。。
#3
优先级请参考下面:
Operator Precedence and Associativity
The table below lists the C and C++ operators and their precedence and associativity values. The highest precedence level is at the top of the table.
Symbol Name or Meaning Associativity
Highest Precedence
++ Post-increment Left to right
-- Post-decrement
( ) Function call
[ ] Array element
-> Pointer to structure member
. Structure or union member
++ Pre-increment Right to left
-- Pre-decrement
! Logical NOT
~ Bitwise NOT
- Unary minus
+ Unary plus
& Address
* Indirection
sizeof Size in bytes
new Allocate program memory
delete Deallocate program memory
(type) Type cast [for example, (float) i]
.* Pointer to member (objects) Left to right
->* Pointer to member (pointers)
* Multiply Left to right
/ Divide
% Remainder
+ Add Left to right
- Subtract
<< Left shift Left to right
>> Right shift
< Less than Left to right
<= Less than or equal to
> Greater than
>= Greater than or equal to
== Equal Left to right
!= Not equal
& Bitwise AND Left to right
^ Bitwise exclusive OR Left to right
| Bitwise OR Left to right
&& Logical AND Left to right
|| Logical OR Left to right
? : Conditional Right to left
= Assignment Right to left
*=, /=, %=, +=, -=, <<=, >>=, &=, ^=, |= Compound assignment
, Comma Left to right
Lowest Precedence
Operator Precedence and Associativity
The table below lists the C and C++ operators and their precedence and associativity values. The highest precedence level is at the top of the table.
Symbol Name or Meaning Associativity
Highest Precedence
++ Post-increment Left to right
-- Post-decrement
( ) Function call
[ ] Array element
-> Pointer to structure member
. Structure or union member
++ Pre-increment Right to left
-- Pre-decrement
! Logical NOT
~ Bitwise NOT
- Unary minus
+ Unary plus
& Address
* Indirection
sizeof Size in bytes
new Allocate program memory
delete Deallocate program memory
(type) Type cast [for example, (float) i]
.* Pointer to member (objects) Left to right
->* Pointer to member (pointers)
* Multiply Left to right
/ Divide
% Remainder
+ Add Left to right
- Subtract
<< Left shift Left to right
>> Right shift
< Less than Left to right
<= Less than or equal to
> Greater than
>= Greater than or equal to
== Equal Left to right
!= Not equal
& Bitwise AND Left to right
^ Bitwise exclusive OR Left to right
| Bitwise OR Left to right
&& Logical AND Left to right
|| Logical OR Left to right
? : Conditional Right to left
= Assignment Right to left
*=, /=, %=, +=, -=, <<=, >>=, &=, ^=, |= Compound assignment
, Comma Left to right
Lowest Precedence
#4
3楼这位哥哥是只负责提供检索资料的吗?个性十足
#5
其实就是只负责指路,不负责领路。(^_^)
#6
结合律?
难道是向左结合还是向右结合吗?
难道是向左结合还是向右结合吗?
#7
google
#8
优先级:1楼的例子很好。
再例如:
结合律:
当优先级相同时,复合运算中操作数的结合方式。通常都是自左向右。
例如:3+2+1,两个+优先级相同,就是3+2得到5,再5+1得到6
但C语言中也有自右向左结合的运算符,如赋值运算符、单目运算符、三目运算符。
例如:x=y=z=3*4;这些赋值运算就是自右向左结合的,先算3*4,再把12赋给z,然后z=12这个表达式的值是12,把这个值再赋给y,y=12这个表达式的值是12,再赋给x。
再例如:
if((x>y)&&(y>2)||(y>x)&&(x>0))
//由于&&的优先级比||要高一级,所以不必再加更多括号,也是先计算两个&&,最后再计算||
{
//do something...
}
结合律:
当优先级相同时,复合运算中操作数的结合方式。通常都是自左向右。
例如:3+2+1,两个+优先级相同,就是3+2得到5,再5+1得到6
但C语言中也有自右向左结合的运算符,如赋值运算符、单目运算符、三目运算符。
例如:x=y=z=3*4;这些赋值运算就是自右向左结合的,先算3*4,再把12赋给z,然后z=12这个表达式的值是12,把这个值再赋给y,y=12这个表达式的值是12,再赋给x。
#9
1 + 1 + 2 means (1 + 1) + 2
1 + 1 * 2 means 1 + (1 * 2)
1 + 1 * 2 means 1 + (1 * 2)
#10
x = 1+7*5&7-1*|2运算过程是什么样的? 都是十进制的数。
#11
x = 1+7*5&7-1*4|2 掉了一个数
#12
优先级的判断,首先语句是一个完整的语句,就C语言而言,是以分号分隔的。就跟小学学的先乘除后加减一样。要判断这些优先级,首先应该该百年我们的习惯性思维,用计算机的思维去看程序。完全按照语言定义去看,就连“=”也当一般的运算符看待。记住,一定要摆脱自己的习惯性思维,总是认为所有的运算都是先乘除后加减,这些都是认为定义的,当然你也可以定义先加减后乘除,一样的道理,在什么地方,就要按照当地的规矩去办事。加入你去外星球,就要看找他们的法则去生存!!!
#1
1+2*3//*的优先级比较高,所以先算2*3,再算1+。。。
(1+2)*3//这难道就是传说中的结合律。。。。呵呵
(1+2)*3//这难道就是传说中的结合律。。。。呵呵
#2
顶楼上,不懂,可以百度嘛。。
#3
优先级请参考下面:
Operator Precedence and Associativity
The table below lists the C and C++ operators and their precedence and associativity values. The highest precedence level is at the top of the table.
Symbol Name or Meaning Associativity
Highest Precedence
++ Post-increment Left to right
-- Post-decrement
( ) Function call
[ ] Array element
-> Pointer to structure member
. Structure or union member
++ Pre-increment Right to left
-- Pre-decrement
! Logical NOT
~ Bitwise NOT
- Unary minus
+ Unary plus
& Address
* Indirection
sizeof Size in bytes
new Allocate program memory
delete Deallocate program memory
(type) Type cast [for example, (float) i]
.* Pointer to member (objects) Left to right
->* Pointer to member (pointers)
* Multiply Left to right
/ Divide
% Remainder
+ Add Left to right
- Subtract
<< Left shift Left to right
>> Right shift
< Less than Left to right
<= Less than or equal to
> Greater than
>= Greater than or equal to
== Equal Left to right
!= Not equal
& Bitwise AND Left to right
^ Bitwise exclusive OR Left to right
| Bitwise OR Left to right
&& Logical AND Left to right
|| Logical OR Left to right
? : Conditional Right to left
= Assignment Right to left
*=, /=, %=, +=, -=, <<=, >>=, &=, ^=, |= Compound assignment
, Comma Left to right
Lowest Precedence
Operator Precedence and Associativity
The table below lists the C and C++ operators and their precedence and associativity values. The highest precedence level is at the top of the table.
Symbol Name or Meaning Associativity
Highest Precedence
++ Post-increment Left to right
-- Post-decrement
( ) Function call
[ ] Array element
-> Pointer to structure member
. Structure or union member
++ Pre-increment Right to left
-- Pre-decrement
! Logical NOT
~ Bitwise NOT
- Unary minus
+ Unary plus
& Address
* Indirection
sizeof Size in bytes
new Allocate program memory
delete Deallocate program memory
(type) Type cast [for example, (float) i]
.* Pointer to member (objects) Left to right
->* Pointer to member (pointers)
* Multiply Left to right
/ Divide
% Remainder
+ Add Left to right
- Subtract
<< Left shift Left to right
>> Right shift
< Less than Left to right
<= Less than or equal to
> Greater than
>= Greater than or equal to
== Equal Left to right
!= Not equal
& Bitwise AND Left to right
^ Bitwise exclusive OR Left to right
| Bitwise OR Left to right
&& Logical AND Left to right
|| Logical OR Left to right
? : Conditional Right to left
= Assignment Right to left
*=, /=, %=, +=, -=, <<=, >>=, &=, ^=, |= Compound assignment
, Comma Left to right
Lowest Precedence
#4
3楼这位哥哥是只负责提供检索资料的吗?个性十足
#5
其实就是只负责指路,不负责领路。(^_^)
#6
结合律?
难道是向左结合还是向右结合吗?
难道是向左结合还是向右结合吗?
#7
google
#8
优先级:1楼的例子很好。
再例如:
结合律:
当优先级相同时,复合运算中操作数的结合方式。通常都是自左向右。
例如:3+2+1,两个+优先级相同,就是3+2得到5,再5+1得到6
但C语言中也有自右向左结合的运算符,如赋值运算符、单目运算符、三目运算符。
例如:x=y=z=3*4;这些赋值运算就是自右向左结合的,先算3*4,再把12赋给z,然后z=12这个表达式的值是12,把这个值再赋给y,y=12这个表达式的值是12,再赋给x。
再例如:
if((x>y)&&(y>2)||(y>x)&&(x>0))
//由于&&的优先级比||要高一级,所以不必再加更多括号,也是先计算两个&&,最后再计算||
{
//do something...
}
结合律:
当优先级相同时,复合运算中操作数的结合方式。通常都是自左向右。
例如:3+2+1,两个+优先级相同,就是3+2得到5,再5+1得到6
但C语言中也有自右向左结合的运算符,如赋值运算符、单目运算符、三目运算符。
例如:x=y=z=3*4;这些赋值运算就是自右向左结合的,先算3*4,再把12赋给z,然后z=12这个表达式的值是12,把这个值再赋给y,y=12这个表达式的值是12,再赋给x。
#9
1 + 1 + 2 means (1 + 1) + 2
1 + 1 * 2 means 1 + (1 * 2)
1 + 1 * 2 means 1 + (1 * 2)
#10
x = 1+7*5&7-1*|2运算过程是什么样的? 都是十进制的数。
#11
x = 1+7*5&7-1*4|2 掉了一个数
#12
优先级的判断,首先语句是一个完整的语句,就C语言而言,是以分号分隔的。就跟小学学的先乘除后加减一样。要判断这些优先级,首先应该该百年我们的习惯性思维,用计算机的思维去看程序。完全按照语言定义去看,就连“=”也当一般的运算符看待。记住,一定要摆脱自己的习惯性思维,总是认为所有的运算都是先乘除后加减,这些都是认为定义的,当然你也可以定义先加减后乘除,一样的道理,在什么地方,就要按照当地的规矩去办事。加入你去外星球,就要看找他们的法则去生存!!!