算数运算符
加减乘除
#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; }
文章来源:https://www.toymoban.com/news/detail-682020.html
逻辑或||
#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; }
文章来源地址https://www.toymoban.com/news/detail-682020.html
到了这里,关于运算符(个人学习笔记黑马学习)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!