标签:number opera 自身 int ring 算术 hellip 算术运算 逻辑运算
int a = 2, b = 4;
?
int c = a + b;
cout << "c = " << c << endl; // 6
?
int d = a - b;
cout << "d = " << d << endl; // -2
?
int e = a * b;
cout << "e = " << e << endl; // 8
?
int f = 5 / 2;
cout << "f = " << f << endl; // 0
// 0:int是整型 整数运算 结果还是整数,小数部分直接忽略
?
int g = b / a;
cout << "g = " << g << endl; // 2
?
float f0 = 2, f1 = 4;
float ff = f0 / f1;
cout << "ff = " << ff << endl; // 0.5
?
int num = 5 % 2; // 求5除以2的余数 %:求余 取模
cout << "num = " << num << endl;
?
int n = 5;
float m = 2.0;
float number = n / m;
cout << "number = " << number << endl; // 2.5
// 当参与运算的数据存在小数时,结果是小数
?
// % 两边都必须是整数
?
// 总结:运算结果以参与运算的数据中精度高的为准
大于 >
小于 <
等于等于 ==
大于等于 >=
小于等于 <=
不等于 !=
bool b = 3 > 7;
cout << "b = " << b << endl;
?
bool b = 3 != 3;
cout << "b = " << b << endl;
运算完成之后会给出一个bool值
与 && 前后两者都为真,表达式为真 否则为假
bool b = (2 > 3) && (3 < 4);
cout << "b = " << b << endl; // 0
?
bool b = (2 < 3) && (3 < 4);
cout << "b = " << b << endl; // 1
或 || 前者或后者为真,表达式为真 否则为假
bool b = (2 > 3) || (3 < 4);
cout << "b = " << b << endl; // 1
?
bool b = (2 < 3) || (3 < 4);
cout << "b = " << b << endl; // 1
?
bool b = (2 > 3) || (3 > 4);
cout << "b = " << b << endl; // 0
非 ! 反转真假
bool b = !(2 > 3);
cout << "b = " << b << endl; // 1
eg1:
int a = 1, b = 2;
bool bo = (a = a + b) && (b = a + b);
cout << "a = " << a << endl; // 3
cout << "b = " << b << endl; // 5
cout << "bo = " << bo << endl; // 1
?
eg2:
int a = 1, b = 2;
bool bo = (a = a + b) || (b = a + b);
cout << "a = " << a << endl; // 3
cout << "b = " << b << endl; // 2
cout << "bo = " << bo << endl; // 1
?
// 需要特别注意是否会继续向后运算
赋值 =
可以和算术运算符组合使用(复合赋值运算符)
+= -= *= /= %=
int a = 1, b = 2;
a -= 1; // 等价于 a = a - 1;
cout << "a = " << a << endl; // 0
a += 2; // 等价于 a = a + 2;
cout << "a = " << a << endl; // 2
a *= b; // 等价于 a = a * b;
cout << "a = " << a << endl; // 4
a /= b; // 等价于 a = a / b;
cout << "a = " << a << endl; // 2
a += 1; // 等价于 a = a + 1; 3
a %= 2; // 等价于 a = a % 2;
cout << "a = " << a << endl; // 1
由于自身参与运算并且最后会改变自身的值,所以必须是变量。
++:自身加一 --:自身减一
int a = 1;
a++; // 等价于 a = a + 1;
cout << "a = " << a << endl; // 2
++a; // 等价于 a = a + 1;
cout << "a = " << a << endl; // 3
?
?
int c0 = 0;
bool b0 = c0++; // 后置 先用c0给b0赋值 再c0++
cout << "c0 = " << c0 << endl; // 1
cout << "b0 = " << b0 << endl; // 0
?
int c1 = 0;
bool b1 = ++c1; // 前置 先c1++ 再用c0给b0赋值
cout << "c1 = " << c1 << endl; // 1
cout << "b1 = " << b1 << endl; // 1
// 后置:先用再加 前置:先加再用
//(表达式1) ? (表达式2) : (表达式3); //规则:先执行表达式1 为真执行表达式2 为假执行表达式3 int a = 1, b = 2, c = 3; (a > 0) ? (b++) : (c++); cout << "a = " << a << endl; // 1 cout << "b = " << b << endl; // 3 cout << "c = " << c << endl; // 3
逗号 ,
// 逗号运算符 , // 运算规则:从左至右依次运算 取最后一个表达式的值(表达式个数不受限制) int a = 1, b = 2, c = 3; int num = ((a++), (a = b + 1), c, (a + b + c)); cout << "a = " << a << endl; // 3 cout << "b = " << b << endl; // 2 cout << "c = " << c << endl; // 3 cout << "num = " << num << endl; // 8
()括号括起来的先算
箭头 ->
点 .
负号 -
取内容 *
取地址 &
求字节数 sizeof
按位非 ~
左移 <<
右移 >>
按位与 &
按位异或 ^
按位或 |
括号优先级最高 ()
算术运算符优先级高于关系运算符
赋值在逗号前面 = += -= ……
逗号优先级最低 ,
优先级 | 操作符 | |||
---|---|---|---|---|
1 | () | [] | -> | . |
:: | ||||
! | ~ | ++ | -- | |
2 | - (负号) | * (取内容) | ||
& (取地址) | sizeof | |||
3 | ->* | .* | ||
4 | * (乘) | / | % | |
5 | + | - | ||
6 | << | >> | ||
7 | < | <= | > | >= |
8 | == | != | ||
9 | & (按位与) | |||
10 | ^ | |||
11 | | | |||
12 | && | |||
13 | || | |||
14 | ? : | |||
15 | = | += | -= | etc. |
16 | , |
标签:number opera 自身 int ring 算术 hellip 算术运算 逻辑运算
原文地址:https://www.cnblogs.com/ZhenXin0101/p/13191316.html