#include <iostream>
using namespace std;
int main() {
/*
* ---------------------------------------------------------------------------------
*算数运算符
* 加减乘除 (需要注意除法 除数不能为0 两个整数相除,会自动去掉小数位)
*/
int a = 1;
int b = 2;
int c = 2;
int d = (b + c - a) * b / c;
cout << "d = " << d << endl;
/*
* 取模运算 (取余数)
* 除数不能为0
* 小数也不允许进行取模运算
*/
int i = 10;
int y = i % 3;
cout << "y = " << y << endl;
/*
* ---------------------------------------------------------------------------------
* 递增递减
*/
//前置递增 后置递增
int a1 = 10;
int a2 = 10;
++a1;
a2++;
cout << "a1与a2的值:" << a1 << "与" << a2 << endl;
//前后递增的区别在于 ,前置递增在表达式之前先递增在执行表达式 后置递增是先执行表达式在进行递增
int a3 = ++a1 * 10;
int a4 = a2++ * 10;
cout << "a1 - a3 -a2 - a4:" << a1 << "-" << a3 << "-" << a2 << "-" << a4 << endl;
/*
* ---------------------------------------------------------------------------------
* 赋值运算符
* 用于将表达式的值赋给变量
* = += -= *= /= %=
*/
//例如
int aa = 10;
aa += 2;
cout << "aa = " << aa << endl;
/*
* ---------------------------------------------------------------------------------
* 比较运算符
* 用于表达式的比较,并返回一个真值或者假值
* == != < > <= >=
*/
//例如
int aaa = 1;
int aaa2 = 4;
bool flag = false;
flag = (aaa < aaa2);
cout << "flag = " << flag << endl;
/*
* ---------------------------------------------------------------------------------
* 逻辑运算符
* 用于表达式的比较,并返回一个真值或者假值
* !(非) && (与) || (或)
*/
//例如
flag = !(aaa < aaa2);
cout << "flag = " << flag << endl;
system("pause");
return 0;
}
网友评论