# C++ 学习（三）运算符 - 算术运算符、赋值运算符、比较运算符、逻辑运算符

25 篇文章 0 订阅
21 篇文章 8 订阅

## 1、算术运算符

#### C++ 算术运算符

+

+5 （正号）

5 + 3

-

-5 （负号）

5 - 3

*乘法5 * 3
/除法5 / 3（整数类型相除得整数，浮点数类型相除得小数）
%取模（求余数）5 % 3 （整数类型运算，浮点数不能取模运算）
++

a = 1; b = ++a; // a = 2; b = 2;

a = 1; b = a++; // a = 2; b = 1;

--

a = 1; b = --a; // a = 0; b = 0;

a = 1; b = a--; // a = 0; b = 1;

#include <iostream>
using namespace std;

int main()
{
//算术运算符
int a = 5;
int b = 3;

cout << "a + b = " << a + b << endl;
cout << "a - b = " << a - b << endl;
cout << "a * b = " << a * b << endl;
cout << "整数相除 a / b = " << a / b << endl;
cout << "a % b = " << a % b << endl;
cout << "b % a = " << b % a << endl;
//前置递增：先让变量+1，再进行表达式运算
b = ++a;
cout << "b = ++a;结果a = " << a << ", b = " << b << endl;
//后置递增，先进行表达式运算， 再让变量+1
b = a++;
cout << "b = a++;结果a = " << a << ", b = " << b << endl;
b = --a;
cout << "b = --a;结果a = " << a << ", b = " << b << endl;
b = a--;
cout << "b = a--;结果a = " << a << ", b = " << b << endl;

double c = 5;
double d = 3;
cout << "浮点数相除 c / d = " << c / d << endl;

system("pause");

return 0;
}

a + b = 8
a - b = 2
a * b = 15

a % b = 2

b % a = 3
b = ++a;结果a = 6, b = 6
b = a++;结果a = 7, b = 6
b = --a;结果a = 6, b = 6
b = a--;结果a = 5, b = 6

#### Go语言算术运算符

package main

import "fmt"

func main() {
a := 5
b := 3
fmt.Println("a + b = ", a + b)
fmt.Println("a - b = ", a - b)
fmt.Println("a * b = ", a * b)
fmt.Println("整数相除 a / b = ", a / b)
fmt.Println("a % b = ", a % b)
fmt.Println("b % a = ", b % a)
//b = a++  //错误写法，报错：',', ';', new line or '}' expected, got '++'
a++
fmt.Println("a++ = ", a)
b--
fmt.Println("b-- = ", b)

var c float32 = 5
var d float32 = 3
fmt.Println("浮点数相除 c / d = ", c / d)
}

a + b =  8
a - b =  2
a * b =  15

a % b =  2
b % a =  3
a++ =  6
b-- =  2

### 2、赋值运算符

#### C++赋值运算符

#include <iostream>
using namespace std;

int main()
{
//赋值运算符
int a = 5;
cout << "（a = 5） = " << a << endl;

a += 2;
cout << "（a += 2） = " << a << endl;

a = 5;
a -= 2;
cout << "（a -= 2） = " << a << endl;

a = 5;
a *= 2;
cout << "（a *= 2） = " << a << endl;

a = 5;
a /= 2;
cout << "（a /= 2） = " << a << endl;

a = 5;
a %= 2;
cout << "（a %= 2） = " << a << endl;

system("pause");

return 0;
}

（a = 5） = 5
（a += 2） = 7
（a -= 2） = 3
（a *= 2） = 10
（a /= 2） = 2
（a %= 2） = 1

#### Go语言赋值运算符

package main

import "fmt"

func main() {
var a int = 5
fmt.Println("(a = 5) = ", a)

a += 2
fmt.Println("(a += 2) = ", a)

a = 5
a -= 2
fmt.Println("(a -= 2) = ", a)

a = 5
a *= 2
fmt.Println("(a *= 2) = ", a)

a = 5
a /= 2
fmt.Println("(a /= 2) = ", a)

a = 5
a %= 2
fmt.Println("(a %= 2) = ", a)

a = 5
a >>= 2
fmt.Println("(a >>= 2) = ", a)

a = 5
a <<= 2
fmt.Println("(a <<= 2) = ", a)

a = 5
a &= 2
fmt.Println("(a &= 2) = ", a)

a = 5
a ^= 2
fmt.Println("(a ^= 2) = ", a)

a = 5
a |= 2
fmt.Println("(a |= 2) = ", a)
}

(a = 5) =  5
(a += 2) =  7
(a -= 2) =  3
(a *= 2) =  10
(a /= 2) =  2
(a %= 2) =  1
(a >>= 2) =  1
(a <<= 2) =  20
(a &= 2) =  0
(a ^= 2) =  7
(a |= 2) =  7

### 3、比较运算符（关系运算符）

#### C++比较运算符

#include <iostream>
using namespace std;

int main()
{
//比较运算符
int a = 5;
int b = 3;
cout << "（a == b） = " << (a == b) << endl;
cout << "（a != b） = " << (a != b) << endl;
cout << "（a > b） = " << (a > b) << endl;
cout << "（a < b） = " << (a < b) << endl;
cout << "（a >= b） = " << (a >= b) << endl;
cout << "（a <= b） = " << (a <= b) << endl;

system("pause");

return 0;
}

（a == b） = 0
（a != b） = 1
（a > b） = 1
（a < b） = 0
（a >= b） = 1
（a <= b） = 0

#### Go语言比较运算符

package main

import "fmt"

func main() {
a := 5
b := 3
fmt.Println("(a == b)", a == b)
fmt.Println("(a != b)", a != b)
fmt.Println("(a > b)", a > b)
fmt.Println("(a < b)", a < b)
fmt.Println("(a >= b)", a >= b)
fmt.Println("(a <= b)", a <= b)
}

(a == b) false
(a != b) true
(a > b) true
(a < b) false
(a >= b) true
(a <= b) false

### 4、逻辑运算符

#### C++逻辑运算符

#include <iostream>
using namespace std;

int main()
{
//逻辑运算符
int a = 5;
int b = 0;
bool c = true;
bool d = false;
cout << "一真一假（a && b） = " << (a && b) << endl;
cout << "两个都真（a && c） = " << (a && c) << endl;
cout << "两个都假（b && d） = " << (b && d) << endl;
cout << "一真一假（a || b） = " << (a || b) << endl;
cout << "两个都真（a || c） = " << (a || c) << endl;
cout << "两个都假（b || d） = " << (b || d) << endl;
cout << "非真（!a） = " << (!a) << endl;
cout << "非假（!b） = " << (!b) << endl;

system("pause");

return 0;
}

输出结果

#### Go语言逻辑运算符

package main

import "fmt"

func main() {
a := true
b := false
c := true
d := false
fmt.Println("一真一假(a && b = )", a && b)
fmt.Println("两个都真(a && c = )", a && c)
fmt.Println("两个都假(b && d = )", b && d)
fmt.Println("一真一假(a || b = )", a || b)
fmt.Println("两个都真(a || c = )", a || c)
fmt.Println("两个都假(b || d = )", b || d)
fmt.Println("非真(!a = )", !a)
fmt.Println("非假(!b = )", !b)
}

• 0
点赞
• 0
收藏
觉得还不错? 一键收藏
• 0
评论
10-21 2905

### “相关推荐”对你有帮助么？

• 非常没帮助
• 没帮助
• 一般
• 有帮助
• 非常有帮助

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、付费专栏及课程。