3 运算符

144 阅读3分钟

3 运算符

作用:用于执行代码的运算

本章我们主要讲解以下几类运算符:

image-20231225232633782

3.1 算术运算符

作用:用于处理四则运算

算术运算符包括以下符号:

image-20231226233319668

加减乘除

#include <iostream>
using namespace std;


int main() {

	//加减乘除
	int a1 = 10;
	int b1 = 3;

	cout << a1 + b1 << endl;
	cout << a1 - b1 << endl;
	cout << a1 * b1 << endl;
	cout << a1 / b1 << endl; //两个整数相除 结果依然是整数,将小数部分去除

	cout << endl;

	int a2 = 10;
	int b2 = 20;

	cout << a2 / b2 << endl;

	cout << "\n";

	//两个小数可以相除
	double d1 = 0.5;
	double d2 = 0.25;
	double d3 = 0.22;
	cout << d1 / d2 << endl;
	cout << d1 / d3 << endl; //运算的结果也可以是小数


	system("pause");

	return 0;
}
13
7
30
3

0

2
2.27273
请按任意键继续. . .

取模(取余)

Snipaste_2023-12-27_00-08-25

#include <iostream>
using namespace std;


int main() {

	//取模运算本质 就是求余数
	int a1 = 10;
	int b1 = 3;
	
	cout << a1 % b1 << endl;

	int a2 = 10;
	int b2 = 20;
	
	cout << a2 % b2 << endl;

	//两个数相除,除数不可以为0,所以也做不了取模运算
	int a3 = 10;
	int b3 = 0;

	//cout << a3 % b3 << endl;


	//两个小数不能做取模运算
	double d1 = 3.14;
	double d2 = 1.1;

	//cout << d1 % d2 << endl;



	system("pause");

	return 0;
}

Snipaste_2023-12-27_00-11-55

Snipaste_2023-12-27_00-16-01

1
10
请按任意键继续. . .

总结:只有整型变量可以进行取模运算。

前置和后置 递增递减

#include <iostream>
using namespace std;


int main() {

	//1、前置递增
	int a = 10;
	++a; // 让变量+1
	cout << "a = " << a << endl;

	//2、后置递增
	int b = 10;
	b++; // 让变量+1
	cout << "b = " << b << endl;

	//3、前置和后置的区别 (先赋值后运算,先运算后赋值)
	//前置递增   先让变量+1,然后进行表达式运算
	int a2 = 10;
	int b2 = ++a2 * 10;
	cout << "a2 = " << a2 << endl;
	cout << "b2 = " << b2 << endl;

	//后置递增   先进行表达式运算,然后让变量+1
	int a3 = 10;
	int b3 = a3++ * 10;
	cout << "a3 = " << a3 << endl;
	cout << "b3 = " << b3 << endl;


	system("pause");

	return 0;
}
a = 11
b = 11
a2 = 11
b2 = 110
a3 = 11
b3 = 100
请按任意键继续. . .

3.2 赋值运算符

作用:用于将表达式的值赋给变量

赋值运算符包括以下几个符号:

image-20231227003840413

#include <iostream>
using namespace std;


int main() {

	// =
	int a = 10;
	a = 100;
	cout << "a = " << a << endl;

	// +=
	a = 10;
	a += 2; // a = a + 2
	cout << "a = " << a << endl;
    
    // -=
    // *=
    // /=

	// %=
	a = 10;
	a %= 2; // a = a % 2
	cout << "a = " << a << endl;

	system("pause");

	return 0;
}
a = 100
a = 12
a = 0
请按任意键继续. . .

3.3 比较运算符

作用:用于表达式的比较,并返回一个真值或假值

比较运算符有以下符号:

image-20231227004635881

#include <iostream>
using namespace std;


int main() {

	//比较运算符
	// ==
	int a = 10;
	int b = 20;
	cout << (a == b) << endl;

	// !=
	cout << (a != b) << endl;

	// <
	// >
	// <=
	// >=

	system("pause");

	return 0;
}
0
1
请按任意键继续. . .

3.4 逻辑运算符

作用:用于根据表达式的值返回真值或假值

逻辑运算符有以下符号:

image-20231227005700414

示例1:逻辑非

#include <iostream>
using namespace std;


int main() {

	// 非 !
	int a = 10;
	//在C++中,除了 0 都为真
	cout << !a << endl;
	cout << !!a << endl;

	system("pause");

	return 0;
}
0
1
请按任意键继续. . .

总结:真变假,假变真

示例2:逻辑与

#include <iostream>
using namespace std;


int main() {

	// 与 &&
	int a = 10;
	int b = 20;
	int c = 0;

	cout << (a && b) << endl;
	cout << (a && c) << endl;

	system("pause");

	return 0;
}
1
0
请按任意键继续. . .

同真为真,其余为假

示例3:逻辑或

#include <iostream>
using namespace std;


int main() {

	// 或 ||
	int a = 10;
	int b = 20;
	int c = 0;
	int d = 0;

	cout << (a || b) << endl;
	cout << (a || c) << endl;
	cout << (c || d) << endl;

	system("pause");

	return 0;
}
1
1
0
请按任意键继续. . .

同假为假,其余为真