您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
这篇文章将为大家详细讲解有关C++的运算符怎么用,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。
作用:用于执行代码的运算
运算符类型 | 作用 |
算术运算符 | 用于处理四则运算 |
赋值运算符 | 用于将表达式的值赋给变量 |
比较运算符 | 用于表达式的比较,并返回一个真值或假值 |
逻辑运算符 | 用于根据表达式的值返回真值或假值 |
运算符 | 术语 | 示例 | 结果 |
+ | 正号 | +3 | 3 |
- | 负号 | -3 | -3 |
+ | 加 | 10+5 | 15 |
- | 减 | 10-5 | 5 |
* | 乘 | 10*5 | 50 |
/ | 除 | 10/5 | 2 |
% | 取模/取余 | 10%3 | 1 |
++ | 前置递增 | a=2;b=++a; | a=3;b=3; |
++ | 后置递增 | a=2;b=a++; | a=3;b=2; |
-- | 前置递减 | a=2;b=--a; | a=1;b=1; |
-- | 后置递增 | a=2;b=a--; | a=1;b=2; |
#include<iostream> using namespace std; int main1() { //加减乘除 int a1 = 10; int b1 = 3; cout << a1 + b1 << endl; cout << a1 - b1 << endl; cout << a1 * b1 << endl; cout << a1 / b1 << endl; //两个整数相除,结果依然是整数,将小数部分去除 int a2 = 10; int b2 = 20; cout << a2 / b2 << endl; int a3 = 10; int b3 = 0; //cout << a3 / b3 << endl; //错误,除数不可以为0 double x1 = 0.5; double x2 = 0.22; cout << x1 / x2 << endl; //取模 int c1 = 10; int d1 = 3; cout << c1 % d1 << endl; int c2 = 10; int d2 = 20; cout << c2 % d2 << endl; int c3 = 10; int d3 = 0; //cout << c3 % d3 << endl; //错误,取模除数不可以为0 double x3 = 3.14; double x4 = 1.1; //cout << x3 % x4 << endl; //错误,不支持小数取模运算 //1.前置递增 int m = 10; ++m; cout << "m = " << m << endl; //2.后置递增 int n = 10; n++; cout << "n = " << n << endl; //3.前置和后置的区别 //前置递增:先让变量+1,后进行表达式运算 int m2 = 10; int n2 = ++m2 * 10; cout << "m2 = " << m2 << endl; cout << "n2 = " << n2 << endl; //后置递增:先进行表达式运算,后让变量+1 int m3 = 10; int n3 = m3++ * 10; cout << "m3 = " << m3 << endl; cout << "n3 = " << n3 << endl; system("pause"); return 0; }
运算符 | 术语 | 示例 | 结果 |
= | 赋值 | a=2;b=3; | a=2;b=3; |
+= | 加等于 | a=0;a+=2; | a=2; |
-= | 减等于 | a=5;a-=3; | a=2; |
*= | 乘等于 | a=2;a*=2; | a=4; |
/= | 除等于 | a=4;a/=2; | a=2; |
%= | 模等于 | a=3;a%=2; | a=1; |
#include<iostream> using namespace std; int main2() { //赋值运算符 int a = 10; cout << "a = " << a << endl; //10 // += a += 2; cout << "a = " << a << endl; //12 // -= a = 10; a -= 2; cout << "a = " << a << endl; //8 // *= a = 10; a *= 2; cout << "a = " << a << endl; //20 // /= a = 10; a /= 2; cout << "a = " << a << endl; //5 // %= a = 10; a %= 2; cout << "a = " << a << endl; //0 system("pause"); return 0; }
运算符术语示例结果==相等于4==30!=不等于4!=31<小于4<30>大于4>31<=小于等于4<=30>=大于等于4>=31
#include<iostream> using namespace std; int main3() { //比较运算符 // == 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; }
运算符术语示例结果!非!a如果a为假,则!a为真;如果a为真,则!a为假。&&与a&&b如果a和b都为真,则结果为真,否则为假。||或a||b如果a或b有一个为真,则结果为真;二者都为假时,结果为假。
#include<iostream> using namespace std; int main4() { //逻辑运算符 非 ! int a = 10; cout << !a << endl; //0 cout << !!a << endl; //1 //逻辑运算符 与 && int c = 10; int d = 10; cout << (c && d) << endl; // 1 c = 0; d = 10; cout << (c && d) << endl; // 0 c = 10; d = 0; cout << (c && d) << endl; // 0 c = 0; d = 0; cout << (c && d) << endl; // 0 //逻辑运算符 或 || int e = 10; int f = 10; cout << (e || f) << endl; // 1 e = 0; f = 10; cout << (e || f) << endl; // 1 e = 10; f = 0; cout << (e || f) << endl; // 1 e = 0; f = 0; cout << (e || f) << endl; // 0 system("pause"); return 0; }
关于“C++的运算符怎么用”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。