C++ 学习(三)运算符 - 算术运算符、赋值运算符、比较运算符、逻辑运算符

26 篇文章 0 订阅

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 = 1
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
浮点数相除 c / d = 1.66667

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 =  1
a % b =  2
b % a =  3
a++ =  6
b-- =  2
浮点数相除 c / d =  1.6666666

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语言赋值运算符

上面与C++赋值运算符,且多出几个赋值运算符(加粗标注)

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语言比较运算符

与C++比较运算符相同

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;
}

 输出结果

一真一假(a && b) = 0
两个都真(a && c) = 1
两个都假(b && d) = 0
一真一假(a || b) = 1
两个都真(a || c) = 1
两个都假(b || d) = 0
非真(!a) = 0
非假(!b) = 1

Go语言逻辑运算符 

与C++逻辑运算符相同

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)
}

输出结果

一真一假(a && b = ) false
两个都真(a && c = ) true
两个都假(b && d = ) false
一真一假(a || b = ) true
两个都真(a || c = ) true
两个都假(b || d = ) false
非真(!a = ) false
非假(!b = ) true

  • 0
    点赞
  • 0
    收藏
  • 0
    评论

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

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:深蓝海洋 设计师:CSDN官方博客 返回首页
评论
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值