什么是運算符
運算符是讓程序執(zhí)行特定的數(shù)學(xué)或邏輯操作的符號,用來表示針對數(shù)據(jù)的特定操作,也稱之為操作符。C++運算符分別有算術(shù)運算符、關(guān)系運算符、邏輯運算符、賦值運算符、位運算符、移位運算符、sizeof運算符、三目運算符、逗號運算符和域解析運算符。
算術(shù)運算符
C++中的算術(shù)運算符用于進(jìn)行數(shù)學(xué)運算,包括加法、減法、乘法、除法和取模等。以下是C++算術(shù)運算符的介紹和使用:文章來源地址http://www.zghlxwxcb.cn/news/detail-837865.html
- 加法運算符(+) :用于將兩個數(shù)值相加,或者將一個數(shù)值與一個字符串連接起來。例如:
點擊查看代碼
#include <iostream>
int main() {
int a = 5;
int b = 3;
int sum = a + b; // 8
std::cout << sum;
}
點擊查看代碼
#include <iostream>
int main()
{
int a = 10;
int b = 2;
int d = a - b; // 8
std::cout << d;
}
點擊查看代碼
#include <iostream>
int main()
{
int a = 5;
int b = 3;
int product = a * b; // 15
std::cout << product;
}
點擊查看代碼
#include <iostream>
int main()
{
int a = 10;
int b = 3;
int quotient = a / b; // 3
std::cout << quotient;
}
點擊查看代碼
#include <iostream>
int main()
{
int a = 10;
int b = 3;
int c = a % b; // 1
std::cout<< c;
}
點擊查看代碼
#include <iostream>
int main()
{
int a = 5;
a++; // a的值變?yōu)?
std::cout << a << std::endl ;
/*++a; // 7
std::cout << a ;*/
}
點擊查看代碼
#include <iostream>
int main()
{
int a = 5;
a--; // 先自減再賦值,a變?yōu)?
std::cout << a << std::endl;
/*--a; // 先賦值再自減,a的值變?yōu)?
std::cout << a;*/
}
點擊查看代碼
int main()
{
int a = 5;
int b = 5;
if(a == b)
{
// 如果a等于b,則執(zhí)行這里的代碼
std::cout << "相等";
}
}
點擊查看代碼
#include <iostream>
int main()
{
int a = 5;
int b = 3;
if(a != b)
{
// 如果a不等于b,則執(zhí)行這里的代碼
std::cout << "不相等";
}
}
點擊查看代碼
#include <iostream>
int main()
{
int a = 5;
int b = 3;
if(a > b)
{
// 如果a大于b,則執(zhí)行這里的代碼
std::cout << "a大于b";
}
}
點擊查看代碼
#include <iostream>
int main()
{
int a = 3;
int b = 5;
if(a < b)
{
// 如果a小于b,則執(zhí)行這里的代碼
std::cout << "a小于b";
}
}
點擊查看代碼
#include <iostream>
int main()
{
int a = 5;
int b = 3;
if(a >= b)
{
// 如果a大于或等于b,則執(zhí)行這里的代碼
std::cout << "a大于或等于b";
}
}
點擊查看代碼
#include <iostream>
int main()
{
int a = 3;
int b = 5;
if(a < b)
{
// 如果a 小于或等于b,則執(zhí)行這里的代碼
std::cout << "a小于或等于b";
}
}
點擊查看代碼
#include <iostream>
int main()
{
bool a = true;
bool b = true;
if(a && b)
{
// 如果a和b都為真,則執(zhí)行這里的代碼
std::cout << "a和b都為真";
}
}
點擊查看代碼
#include <iostream>
int main()
{
bool a = true;
bool b = false;
if(a || b)
{
// 如果a或b有一個為真,則執(zhí)行這里的代碼
std::cout << "a或b有一個為真";
}
}
點擊查看代碼
#include <iostream>
int main()
{
bool a = false;
if (!a)
{
// 如果a為真,則執(zhí)行這里的代碼
std::cout << "將a取反等于真";
}
}
點擊查看代碼
#include <iostream>
int main()
{
int a = 5;
int b = 3;
int c = 2;
// 使用括號明確運算順序,先計算加法再計算關(guān)系運算,最后進(jìn)行邏輯與運算
int sum = (a + b) && (b > c); // 1
if(sum)
{
// 如果sum為真,則執(zhí)行這里的代碼
std:: cout << sum;
}
}
點擊查看代碼
#include <iostream>
int main()
{
int a = 5; // 將5賦值給變量a
std::cout << a;
}
點擊查看代碼
#include <iostream>
int main()
{
int a = 5;
a += 3; // 將3加到a上,并將結(jié)果賦值給a 5 + 3 = 8
std::cout << a;
}
點擊查看代碼
#include <iostream>
int main()
{
int a = 10;
a -= 3; // 從a中減去3,并將結(jié)果7賦值給a;
std::cout << a;
}
點擊查看代碼
#include <iostream>
int main()
{
int a = 5;
a *= 3; // 將a的值乘以3,并將結(jié)果15賦值給a
std::cout << a;
}
點擊查看代碼
#include <iostream>
int main()
{
int a = 10;
a /= 3; // 將a的值除以3,并將結(jié)果3賦值給a
std::cout << a;
}
點擊查看代碼
#include <iostream>
int main()
{
int a = 10;
a %= 3; // 將a的值除以3的余數(shù)1賦值給a
std::cout << a;
}
點擊查看代碼
#include <iostream>
int main()
{
int a = 60; // 二進(jìn)制表示為 0010 1100
int b = 13; // 二進(jìn)制表示為 0000 1101
int c = a & b; // 結(jié)果為0000 1100,即十進(jìn)制中的12
std::cout << c;
}
點擊查看代碼
#include <iostream>
int main()
{
int a = 60; // 二進(jìn)制表示為0010 1100
int b = 13; // 二進(jìn)制表示為0000 1101
int c = a | b; // 結(jié)果為0010 1101,即十進(jìn)制中的61
std::cout << c;
}
點擊查看代碼
#include <iostream>
int main()
{
int a = 60; // 二進(jìn)制表示為0010 1100
int b = 13; // 二進(jìn)制表示為0000 1101
int c = a ^ b; // 結(jié)果為0010 0001,即十進(jìn)制中的49
std::cout << c;
}
點擊查看代碼
#include <iostream>
int main()
{
int a = 60; // 二進(jìn)制表示為0010 1100
int b = ~a; // 二進(jìn)制表示為1101 0011,即十進(jìn)制中的-61
std::cout << b;
}
點擊查看代碼
#include <iostream>
int main()
{
int a = 5; // 二進(jìn)制表示為0000 0101
// 向左移動2位
int b = a << 2; // 結(jié)果為0001 0100,即十進(jìn)制中的20
std::cout << b;
}
點擊查看代碼
#include <iostream>
int main()
{
int a = 20; // 二進(jìn)制數(shù)表示為0010 0100
// 向右移動2位
int b = a >> 2; // 結(jié)果為0000 0101 // 即十進(jìn)制中的5
std::cout << b;
}
點擊查看代碼
#include <iostream>
int main()
{
int a = 5;
int b[10];
int *p = &a;
std::cout << "Size of int:"<< sizeof(int) <<"bytes\n" ; // 通常是4字節(jié),具體取決于平臺
std::cout << "Size of array:"<< sizeof(b) <<"bytes\n" ; // 返回整個數(shù)組的大小,通常是40字節(jié)。(假設(shè)int是4字節(jié))
std::cout << "Size of pointer:"<< sizeof(p) <<"bytes\n" ; // 返回指針的大小,通常是8字節(jié) (假設(shè)64位系統(tǒng))
std::cout << "Size of object:"<< sizeof(a) <<"bytes\n" ; //返回對象的大小, 通常是4字節(jié)(假設(shè)int是4字節(jié))
}
點擊查看代碼
#include <iostream>
int main()
{
int x = 5;
std::cout << (x > 0 ? "x是正數(shù)" : "x不是正數(shù)") << std::endl;
}
點擊查看代碼
#include <iostream>
int main()
{
int a = 10;
int b = 12;
int c = (a > b ? a : b);
// 輸出結(jié)果為12
std::cout<< c;
}
點擊查看代碼
#include <iostream>
int main()
{
// for循環(huán)中初始化多個變量
for(int i = 0, j = 0; i < 10; i++,j--)
{
// do something
}
// 執(zhí)行多個操作
int a = 5;
int b = 10;
int c = (a+=2,b--,a + b); // c的值為16
std::cout << c;
}
文章來源:http://www.zghlxwxcb.cn/news/detail-837865.html
到了這里,關(guān)于C++入門編程----C++運算符(8)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!