首頁 > 軟體

C++的運運算元你真的瞭解嗎

2022-02-15 19:00:32

前言

運運算元的作用:用於執行程式碼的運算

主要有:

1 算術運運算元

用於處理四則運算

對於前置遞增:將遞增運算前置,使變數先加一,再進行表示式運算。

對於後置遞增:將遞增運算後置,使變數先進行表示式運算,再加一。

#include<iostream>
using namespace std;
int main()
{
	//1.前置遞增:先加一,再進行表示式運算
	int a = 10;
	int b = ++a * 10;
	cout << "a = " << a << endl;
	cout << "b = " << b << endl;
	//2.後置遞增:先進行表示式運算,再加一
	int c = 10;
	int d = c++ * 10;
	cout << "c = " << c << endl;
	cout << "d = " << d << endl;
	system("pause");
	return 0;
}

2 賦值運運算元

#include<iostream>
using namespace std;
int main1()
{
	//賦值運運算元
	int a = 10;
	int b = 2;
	cout << "a = " << a << endl;
	//+=
	a = 10;
	a += b;
	cout << "a = " << a << endl;
	//-=
	a = 10;
	a -= b;
	cout << "a = " << a << endl;
	//*=
	a = 10;
	a *= b;
	cout << "a = " << a << endl;
	// /=
	a = 10;
	a /= b;
	cout << "a = " << a << endl;
	// %=
	a = 10;
	a %= b;
	cout << "a = " << a << endl;
	system("pause");
	return 0;
}

3 比較運運算元

#include<iostream>
using namespace std;
int main()
{
	cout << (4 == 3) << endl;
	cout << (4 != 3) << endl;
	cout << (4 < 3) << endl;
	cout << (4 > 3) << endl;
	cout << (4 >= 3) << endl;
	cout << (4 <= 3) << endl;
	system("pause");
	return 0;
}

4 邏輯運運算元

#include<iostream>using namespace std;int main(){int a = 5;// 邏輯運運算元 非cout << !a << endl;cout << !!a << endl;// 邏輯運運算元 與int b = 0;int c = 3;cout << (a && b) << endl;cout << (a && c) << endl;//邏輯運運算元  或cout << (!a || b) << endl;cout << (a || c) << endl;system("pause");return 0;}#include<iostream>
using namespace std;
int main()
{
	int a = 5;
	// 邏輯運運算元 非
	cout << !a << endl;
	cout << !!a << endl;
	// 邏輯運運算元 與
	int b = 0;
	int c = 3;
	cout << (a && b) << endl;
	cout << (a && c) << endl;
	//邏輯運運算元  或
	cout << (!a || b) << endl;
	cout << (a || c) << endl;
	system("pause");
	return 0;
}

總結

本篇文章就到這裡了,希望能夠給你帶來幫助,也希望您能夠多多關注it145.com的更多內容!     


IT145.com E-mail:sddin#qq.com