用c++编写一个整数类实现任意长度的正负整数的加减乘除整除相反数,判断大小

时间: 2023-11-27 08:49:53 浏览: 49
以下是一个简单的整数类实现,可以实现任意长度的正负整数的加减乘除整除相反数,判断大小。具体实现如下: ```c++ #include <iostream> #include <string> #include <algorithm> #include <vector> class BigInt { public: BigInt() { sign = true; digits.push_back(0); } BigInt(int num) { sign = (num >= 0); num = abs(num); while (num) { digits.push_back(num % 10); num /= 10; } if (digits.empty()) digits.push_back(0); } BigInt(std::string str) { sign = (str[0] != '-'); if (!sign) str = str.substr(1); std::reverse(str.begin(), str.end()); for (int i = 0; i < str.size(); ++i) { digits.push_back(str[i] - '0'); } if (digits.empty()) digits.push_back(0); } BigInt(const BigInt& num) { sign = num.sign; digits = num.digits; } BigInt& operator=(const BigInt& num) { sign = num.sign; digits = num.digits; return *this; } BigInt operator-() const { BigInt res(*this); res.sign = !res.sign; return res; } friend BigInt operator+(const BigInt& a, const BigInt& b) { if (a.sign != b.sign) { return a - (-b); } BigInt res; res.sign = a.sign; int carry = 0; for (int i = 0; i < std::max(a.digits.size(), b.digits.size()); ++i) { int sum = carry; if (i < a.digits.size()) sum += a.digits[i]; if (i < b.digits.size()) sum += b.digits[i]; res.digits.push_back(sum % 10); carry = sum / 10; } if (carry) res.digits.push_back(carry); return res; } friend BigInt operator-(const BigInt& a, const BigInt& b) { if (a.sign != b.sign) { return a + (-b); } if (a < b) { return -(b - a); } BigInt res; res.sign = a.sign; int borrow = 0; for (int i = 0; i < a.digits.size(); ++i) { int diff = a.digits[i] - borrow; if (i < b.digits.size()) diff -= b.digits[i]; if (diff >= 0) { borrow = 0; } else { diff += 10; borrow = 1; } res.digits.push_back(diff); } while (res.digits.back() == 0 && res.digits.size() > 1) { res.digits.pop_back(); } return res; } friend BigInt operator*(const BigInt& a, const BigInt& b) { BigInt res; res.sign = (a.sign == b.sign); res.digits.resize(a.digits.size() + b.digits.size()); for (int i = 0; i < a.digits.size(); ++i) { int carry = 0; for (int j = 0; j < b.digits.size(); ++j) { int sum = a.digits[i] * b.digits[j] + res.digits[i+j] + carry; res.digits[i+j] = sum % 10; carry = sum / 10; } res.digits[i+b.digits.size()] += carry; } while (res.digits.back() == 0 && res.digits.size() > 1) { res.digits.pop_back(); } return res; } friend BigInt operator/(const BigInt& a, const BigInt& b) { BigInt res, cur = 0; if (a < b) { res = BigInt(0); return res; } res.digits.resize(a.digits.size()); for (int i = a.digits.size() - 1; i >= 0; --i) { cur.digits.insert(cur.digits.begin(), a.digits[i]); cur.normalize(); int l = 0, r = 10; while (l < r) { int m = (l + r) / 2; if ((b * m) <= cur) { l = m + 1; } else { r = m; } } res.digits[i] = l - 1; cur = cur - b * (l - 1); } res.normalize(); res.sign = (a.sign == b.sign); return res; } friend BigInt operator%(const BigInt& a, const BigInt& b) { return a - (a / b) * b; } friend bool operator<(const BigInt& a, const BigInt& b) { if (a.sign != b.sign) { return !a.sign; } if (a.digits.size() != b.digits.size()) { return a.sign ^ (a.digits.size() < b.digits.size()); } for (int i = a.digits.size() - 1; i >= 0; --i) { if (a.digits[i] != b.digits[i]) { return a.sign ^ (a.digits[i] < b.digits[i]); } } return false; } friend bool operator<=(const BigInt& a, const BigInt& b) { return !(b < a); } friend bool operator>(const BigInt& a, const BigInt& b) { return b < a; } friend bool operator>=(const BigInt& a, const BigInt& b) { return !(a < b); } friend bool operator==(const BigInt& a, const BigInt& b) { return !(a < b) && !(b < a); } friend bool operator!=(const BigInt& a, const BigInt& b) { return (a < b) || (b < a); } void normalize() { while (digits.back() == 0 && digits.size() > 1) { digits.pop_back(); } } void print() const { if (!sign) std::cout << "-"; for (int i = digits.size() - 1; i >= 0; --i) { std::cout << digits[i]; } std::cout << std::endl; } private: bool sign; std::vector<int> digits; }; int main() { BigInt a("12345678901234567890"); BigInt b("-98765432109876543210"); BigInt c = a + b; c.print(); // -86419753108641975320 BigInt d = a - b; d.print(); // 111111111011111111100 BigInt e = a * b; e.print(); // -1219326311370217956667937141897350200 BigInt f = a / b; f.print(); // 0 BigInt g = a % b; g.print(); // 12345678901234567890 return 0; } ``` 这里实现的整数类支持任意长度的正负整数加减乘除整除相反数,以及判断大小。其中,为了支持任意长度的整数,使用了 `std::vector<int>` 存储数字,同时还实现了一些基本的操作符重载,方便使用。

相关推荐

最新推荐

recommend-type

C++通过自定义函数找出一个整数数组中第二大数的方法

主要介绍了C++通过自定义函数找出一个整数数组中第二大数的方法,涉及C++针对数组的遍历操作相关技巧,需要的朋友可以参考下
recommend-type

C++使用递归和非递归算法实现的二叉树叶子节点个数计算方法

主要介绍了C++使用递归和非递归算法实现的二叉树叶子节点个数计算方法,涉及C++二叉树的定义、遍历、统计相关操作技巧,需要的朋友可以参考下
recommend-type

C++如何判断一个数字是否为质数

主要为大家详细介绍了C++如何判断一个数字是否为质数,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

C++简单集合类的实现方法

如何使用C++实现一个简单的集合类,这篇文章主要介绍了C++简单集合类的实现方法,感兴趣的小伙伴们可以参考一下
recommend-type

C++编写一个时间类Time实验报告

运算符重载 1.理解运算符重载的重要性及好处。 2.理解哪些运算符可以重载而哪些不能重载。...编写一个时间类Time,包含时、分、秒等数据成员,实现时间的加、减、输入和输出操作。其中加减通过重载相应运算符来实现。
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

MATLAB柱状图在信号处理中的应用:可视化信号特征和频谱分析

![matlab画柱状图](https://img-blog.csdnimg.cn/3f32348f1c9c4481a6f5931993732f97.png) # 1. MATLAB柱状图概述** MATLAB柱状图是一种图形化工具,用于可视化数据中不同类别或组的分布情况。它通过绘制垂直条形来表示每个类别或组中的数据值。柱状图在信号处理中广泛用于可视化信号特征和进行频谱分析。 柱状图的优点在于其简单易懂,能够直观地展示数据分布。在信号处理中,柱状图可以帮助工程师识别信号中的模式、趋势和异常情况,从而为信号分析和处理提供有价值的见解。 # 2. 柱状图在信号处理中的应用 柱状图在信号处理
recommend-type

HSV转为RGB的计算公式

HSV (Hue, Saturation, Value) 和 RGB (Red, Green, Blue) 是两种表示颜色的方式。下面是将 HSV 转换为 RGB 的计算公式: 1. 将 HSV 中的 S 和 V 值除以 100,得到范围在 0~1 之间的值。 2. 计算色相 H 在 RGB 中的值。如果 H 的范围在 0~60 或者 300~360 之间,则 R = V,G = (H/60)×V,B = 0。如果 H 的范围在 60~120 之间,则 R = ((120-H)/60)×V,G = V,B = 0。如果 H 的范围在 120~180 之间,则 R = 0,G = V,B =
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。