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

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 = 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
    评论
以下是一个基本的高精度C++代码,支持小数,负数,算术运算符、关系运算符逻辑运算符、位运算符赋值运算符: ```c++ #include <bits/stdc++.h> using namespace std; const int MAXN = 1005; const int INF = 0x3f3f3f3f; struct BigInteger { int len, s[MAXN]; BigInteger() { memset(s, 0, sizeof(s)); len = 1; } BigInteger(int num) { *this = num; } BigInteger(const char* num) { *this = num; } BigInteger operator = (int num) { char s[MAXN]; sprintf(s, "%d", num); *this = s; return *this; } BigInteger operator = (const char* num) { memset(s, 0, sizeof(s)); len = strlen(num); for (int i = 0; i < len; i++) { s[i] = num[len - i - 1] - '0'; } return *this; } BigInteger operator + (const BigInteger& b) const { BigInteger c; c.len = 0; for (int i = 0, g = 0; g || i < max(len, b.len); i++) { int x = g; if (i < len) x += s[i]; if (i < b.len) x += b.s[i]; c.s[c.len++] = x % 10; g = x / 10; } return c; } BigInteger operator - (const BigInteger& b) const { assert(b < *this); // 确保减数小于被减数 BigInteger c; c.len = 0; for (int i = 0, g = 0; i < len; i++) { int x = s[i] - g; if (i < b.len) x -= b.s[i]; if (x >= 0) { g = 0; } else { g = 1; x += 10; } c.s[c.len++] = x; } c.trim(); return c; } BigInteger operator * (const BigInteger& b) const { BigInteger c; c.len = len + b.len; for (int i = 0; i < len; i++) { for (int j = 0; j < b.len; j++) { c.s[i + j] += s[i] * b.s[j]; } } for (int i = 0; i < c.len - 1; i++) { c.s[i + 1] += c.s[i] / 10; c.s[i] %= 10; } c.trim(); return c; } BigInteger operator / (const BigInteger& b) const { assert(b > 0); // 确保除数不为0 BigInteger c = *this; BigInteger m; for (int i = len - 1; i >= 0; i--) { m = m * 10 + s[i]; c.s[i] = 0; while (m >= b) { m = m - b; c.s[i]++; } } c.trim(); return c; } BigInteger operator % (const BigInteger& b) const { BigInteger c = *this; BigInteger m; for (int i = len - 1; i >= 0; i--) { m = m * 10 + s[i]; c.s[i] = 0; while (m >= b) { m = m - b; c.s[i]++; } } return m; } BigInteger operator += (const BigInteger& b) { *this = *this + b; return *this; } BigInteger operator -= (const BigInteger& b) { *this = *this - b; return *this; } BigInteger operator *= (const BigInteger& b) { *this = *this * b; return *this; } BigInteger operator /= (const BigInteger& b) { *this = *this / b; return *this; } BigInteger operator %= (const BigInteger& b) { *this = *this % b; return *this; } bool operator < (const BigInteger& b) const { if (len != b.len) return len < b.len; for (int i = len - 1; i >= 0; i--) { if (s[i] != b.s[i]) return s[i] < b.s[i]; } return false; } bool operator > (const BigInteger& b) const { return b < *this; } bool operator <= (const BigInteger& b) const { return !(b < *this); } bool operator >= (const BigInteger& b) const { return !(*this < b); } bool operator != (const BigInteger& b) const { return b < *this || *this < b; } bool operator == (const BigInteger& b) const { return !(b < *this) && !(*this < b); } void trim() { while (len > 1 && !s[len - 1]) { len--; } } BigInteger operator - () const { BigInteger c = *this; c.s[0] = -c.s[0]; return c; } BigInteger abs() const { BigInteger c = *this; c.s[0] = ::abs(c.s[0]); return c; } int intValue() const { int res = 0; for (int i = len - 1; i >= 0; i--) { res = res * 10 + s[i]; } return s[0] < 0 ? -res : res; } friend istream& operator >> (istream& in, BigInteger& x) { string s; if (!(in >> s)) return in; x = s.c_str(); return in; } friend ostream& operator << (ostream& out, const BigInteger& x) { if (x.s[0] < 0) out << '-'; out << x.s[x.len - 1]; for (int i = x.len - 2; i >= 0; i--) { char buf[10]; sprintf(buf, "%d", x.s[i]); for (int j = 0; j < strlen(buf); j++) { out << buf[j]; } } return out; } string toString() const { string res = ""; if (s[0] < 0) res += '-'; res += to_string(s[len - 1]); for (int i = len - 2; i >= 0; i--) { res += to_string(abs(s[i])); } return res; } }; int main() { BigInteger a = 123456789012345678901234567890; BigInteger b = "987654321098765432109876543210"; cout << a + b << endl; cout << a - b << endl; cout << a * b << endl; cout << a / b << endl; cout << a % b << endl; return 0; } ```

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

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值