运算符
作用:用于执行代码的运算 本章我们主要讲解以下几类运算符:
运算符类型 | 作用 |
---|
算术运算符 | 处理四则运算 | 赋值运算符 | 将表达式的值赋给变量 | 比较运算符 | 用于表达式的比较,返回真值或假值 | 逻辑运算符 | 根据表达式的值返回真值或假值 |
1 算术运算符 作用:处理四则运算 算术运算符包含以下符号:
运算符 | 术语 | 示例 | 结果 |
---|
+ | 正号 | +3 | 3 | - | 负号 | -3 | -3 | + | 加号 | 10+3 | 13 | - | 减号 | 10-3 | 7 | * | 乘 | 10*3 | 30 | / | 除 | 9*3 | 3 | % | 取模(取余) | 10%3 | 1 | ++ | 前置递增 | a=2;b=++a | a=3,b=3 | ++ | 后置递增 | a=2;b=a++ | a=3,b=2 | – | 前置递减 | a=2;b=–a | a=1,b=1 | – | 后置递减 | a=2;b=a– | a=1,b=2 |
1.1 加减乘除 代码:
int main()
{
int a1 = 10;
int b1 = 3;
cout << a1 + b1 << endl;
cout << a1 - b1 << endl;
cout << a1 * b1 << endl;
cout << a1 / b1 << endl;
double d1 = 0.5;
double d2 = 0.22;
cout << d1 / d2 << endl;
return 0;
}
结果: 1.2 取模/取余 注意:两个小数不能做该运算,只有整型数据才可以做! 代码:
int main()
{
cout << a1 % b1 << endl;
return 0;
return 0;
}
结果: 1.3 递增递减运算符 前置:++a,先做加分,再做表达式运算 后置:a++,先做表达式,再对a加1
int main()
{
int a = 10;
++a;
cout << "a="<<a << endl;
int b = 10;
b++;
cout <<"b="<< b << endl;
int c1 = 10;
int c2 = ++c1 * 10;
cout << "c1=" << c1 << endl;
cout << "c2=" << c2 << endl;
int c3 = 10;
int c4 = c3++ * 10;
cout << "c4=" << c4 << endl;
cout << "c3=" << c3 << endl;
return 0;
}
结果:
2 赋值运算符 作用:将表达式的值赋给变量 包含以下几个符号:
运算符 | 术语 | 示例 | 结果 |
---|
= | 赋值 | a=2;b=3 | a=2;b=3 | += | 加等于 | a=0;a+=2 | a=2 | -= | 减等于 | a=5;a=-3 | a=2 | *= | 乘等于 | a=2;a*=2 | a=4 | /= | 除等于 | a=4;a/2 | a=2 | %= | 模等于 | a=3;a%2 | a=1 |
代码示例:
int main()
{
int a = 10;
a = 100;
cout << "a=" << a << endl;
a = 10;
a += 2;
cout << "a=" << a << endl;
a = 10;
a -= 2;
cout << "a=" << a << endl;
a = 10;
a *= 2;
cout << "a=" << a << endl;
a = 10;
a /= 2;
cout << "a=" << a << endl;
a = 10;
a %= 3;
cout << "a=" << a << endl;
return 0;
}
结果:
3 比较运算符 作用:表达式的比较,返回一个真值或假值
比较运算符有以下符号:
运算符 | 术语 | 示例 | 结果 |
---|
== | 相等于 | 4==3 | 0 | != | 不等于 | 4!=3 | 1 | < | 小于 | 4<3 | 0 | > | 大于 | 4>3 | 1 | <= | 小于等于 | 4<=3 | 0 | >= | 大于等于 | 4>=1 | 1 |
代码:
int main()
{
int a = 10;
int b = 20;
cout << (a == b) << endl;
cout << (a != b) << endl;
cout << (a > b) << endl;
cout << (a < b) << endl;
cout << (a >=b) << endl;
cout << (a <=b) << endl;
}
结果; 4 逻辑运算符 作用:根据表达式的值返回真值或假值
运算符 | 术语 | 示例 | 结果 |
---|
! | 非 | !a | 如果a为假,则!a为真;反之亦然 | && | 与 | a&&b | 如果a和b都为真,则为真,否则为假 | II | 或 | aIIb | 如果a和b有一个为真,则为真,二者都为假时才为假 |
代码:
int main()
{
int a = 10;
cout << !a << endl;
cout << !!a << endl;
int b = 0;
cout <<(a&&b) << endl;
cout << (a || b) << endl;
}
结果: 本章节所有源码:
#include<iostream>
#include<string>
using namespace std;
void base_op()
{
int a1 = 10;
int b1 = 3;
cout << a1 + b1 << endl;
cout << a1 - b1 << endl;
cout << a1 * b1 << endl;
cout << a1 / b1 << endl;
double d1 = 0.5;
double d2 = 0.22;
cout << d1 / d2 << endl;
cout << a1 % b1 << endl;
int a = 10;
++a;
cout << "a=" << a << endl;
int b = 10;
b++;
cout << "b=" << b << endl;
int c1 = 10;
int c2 = ++c1 * 10;
cout << "c1=" << c1 << endl;
cout << "c2=" << c2 << endl;
int c3 = 10;
int c4 = c3++ * 10;
cout << "c4=" << c4 << endl;
cout << "c3=" << c3 << endl;
}
void value_op()
{
int a = 10;
a = 100;
cout << "a=" << a << endl;
a = 10;
a += 2;
cout << "a=" << a << endl;
a = 10;
a -= 2;
cout << "a=" << a << endl;
a = 10;
a *= 2;
cout << "a=" << a << endl;
a = 10;
a /= 2;
cout << "a=" << a << endl;
a = 10;
a %= 3;
cout << "a=" << a << endl;
}
void compare_op()
{
int a = 10;
int b = 20;
cout << (a == b) << endl;
cout << (a != b) << endl;
cout << (a > b) << endl;
cout << (a < b) << endl;
cout << (a >=b) << endl;
cout << (a <=b) << endl;
}
void logic_op()
{
int a = 10;
cout << !a << endl;
cout << !!a << endl;
int b = 0;
cout <<(a&&b) << endl;
cout << (a || b) << endl;
}
int main()
{
logic_op();
return 0;
}
|