算数运算符
加减乘除
#include <iostream> using namespace std; int main() { int a1 = 10; int a2 = 20; cout << a1 + a2 << endl; cout << a1 - a2 << endl; cout << a1 * a2 << endl; cout << a1 / a2 << endl; /*double a3 = 0.5; double a4 = 0.25; cout << a3 / a4 << endl;*/ double a3 = 0.5; double a4 = 0.22; cout << a3 / a4 << endl;//运算结果也可以是小数 system("pause"); return 0; }
取模
#include <iostream> using namespace std; int main() { int a1 = 10; int b1 = 3; cout << a1 % b1 << endl; /*两个小数之间不可以做取余运算 double d1 = 0.2; double d2 = 0.55; count << d1 % d2 << endl; */ system("pause"); return 0; }
自增自减
#include <iostream> using namespace std; int main() { //前置 int a2 = 10; int b2 = ++a2 * 10; cout << "a2 = " << a2 << endl; cout << "b2 = " << b2 << endl; //后置 int a3 = 10; int b3 = a3++ * 10; cout << "a3 =" << a3 << endl; cout << "b3 =" << b3 << endl; system("pause"); return 0; }
赋值运算符
#include <iostream> using namespace std; 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 %= 2; cout << "a=" << a << endl; system("pause"); return 0; }
比较运算符
#include <iostream> using namespace std; 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; system("pause"); return 0; }
逻辑运算符
逻辑非!
#include <iostream> using namespace std; int main() { int a = 10; cout << !a << endl; cout << !!a << endl; system("pause"); return 0; }
逻辑与&&
#include <iostream> using namespace std; int main() { int a = 10; int b = 10; cout << (a && b) << endl; a = 0; b = 10; cout << (a && b) << endl; a = 0; b = 0; cout << (a && b) << endl; system("pause"); return 0; }
逻辑或||
#include <iostream> using namespace std; int main() { int a = 10; int b = 10; cout << (a || b) << endl; a = 0; b = 10; cout << (a || b) << endl; a = 0; b = 0; cout << (a || b) << endl; system("pause"); return 0; }