C++ 基本算术运算符

目录及简要概括

本节内容较简单,主要讲述C++中的基本算数运算符。

  1. 加 +
  2. 减 -
  3. 乘 *
  4. 除 /
  5. 括号 ()

如何输出计算值?

在C++中,您可以使用cout语句直接输出几个数的计算值。

#include<iostream>
using namespace std;
int main() {
	cout << 1+1;
	return 0;
}

计算

加、减、乘、除和括号大家在数学中见到的应该不少,在C++中,这5种计算与数学中的计算顺序、规则一致。只是其对应符号有些不同。其中乘法用*(星号),除法用/(斜杠)。简单看一下描述。

运算符描述
+把两个操作数相加
-从第一个操作数中减去第二个操作数 A - B 将得到 -10
*把两个操作数相乘 A * B 将得到 200
/分子除以分母

小括号的优先级最高,且小括号支持嵌套,例如

cout << (2+9*(4+1))+85;

在C++中,称号不可以像数学中一样省略,例如2*a不可以写成2a

唯一要注意的是,在C++中,除法比较特殊。你只需记住,整数除以整数,结果仍是整数,如果要得到小数,则必须有一个变量时小数

例如:

#include<iostream>
using namespace std;
int main()
{
    cout << 100 / 3;
    return 0;
}

其输出结果为33。
如果要输出小数,您只需要更改其中任意一个变量为小数,例如

cout << 100.0 / 3;

cout << 100 / 3.0;

这样也可以

cout << 100.0 / 3.0;

输出结果均为33.3333。

好了,自己练习一下计算吧,下节我们将学习变量。

  • 5
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是一个基本的高精度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; } ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值