- A+B A-B A*B A/B A%B Problem
【求助】高精度板子
- 2025-2-8 23:46:49 @
请求各位OIer大佬帮忙检查并改改下面的代码,
我改来改去总是报两种错误:
[Error] no matching function for call to 'operator-(const bigint&, const bigint&)'
[Error] no matching function for call to 'operator-(const bigint&, bigint&)'
#include <iostream>
#include <vector>
#include <string>
#include <cmath>
#include <algorithm>
using namespace std;
// 定义大整数结构体
struct bigint {
vector<int> num;
bool isNegative = false;
bigint() {}
bigint(const string& str) {
if (str[0] == '-') {
isNegative = true;
for (int i = str.size() - 1; i > 0; --i) {
num.push_back(str[i] - '0');
}
} else {
for (int i = str.size() - 1; i >= 0; --i) {
num.push_back(str[i] - '0');
}
}
// 去除前导零
while (!num.empty() && num.back() == 0) {
num.pop_back();
}
// 如果结果为 0,去掉负号
if (num.empty()) {
isNegative = false;
}
}
};
// 输出大整数
void bigout(const bigint& res) {
if (res.isNegative && !res.num.empty()) {
cout << "-";
}
if (res.num.empty()) {
cout << "0";
} else {
for (int i = res.num.size() - 1; i >= 0; --i) {
cout << res.num[i];
}
}
cout << endl;
}
// 比较两个大整数的绝对值大小
bool absGreater(const bigint& a, const bigint& b) {
if (a.num.size() != b.num.size()) {
return a.num.size() > b.num.size();
}
for (int i = a.num.size() - 1; i >= 0; --i) {
if (a.num[i] != b.num[i]) {
return a.num[i] > b.num[i];
}
}
return false;
}
// 判断两个大整数是否相等
bool operator==(const bigint& a, const bigint& b) {
return a.isNegative == b.isNegative && a.num == b.num;
}
// 判断大整数 a 是否小于大整数 b
bool operator<(const bigint& a, const bigint& b) {
if (a.isNegative != b.isNegative) {
return a.isNegative;
}
if (a.isNegative) {
return absGreater(a, b);
}
return absGreater(b, a);
}
// 判断大整数 a 是否大于大整数 b
bool operator>(const bigint& a, const bigint& b) {
return b < a;
}
// 判断大整数 a 是否小于等于大整数 b
bool operator<=(const bigint& a, const bigint& b) {
return !(a > b);
}
// 判断大整数 a 是否大于等于大整数 b
bool operator>=(const bigint& a, const bigint& b) {
return !(a < b);
}
// 大整数加法
bigint operator+(const bigint& a, const bigint& b) {
if (a.isNegative == b.isNegative) {
bigint res;
res.isNegative = a.isNegative;
res.num.resize(max(a.num.size(), b.num.size()) + 1);
for (int i = 0; i < a.num.size(); ++i) {
res.num[i] += a.num[i];
}
for (int i = 0; i < b.num.size(); ++i) {
res.num[i] += b.num[i];
}
for (int i = 0; i < res.num.size(); ++i) {
if (res.num[i] >= 10) {
res.num[i + 1]++;
res.num[i] %= 10;
}
}
while (!res.num.empty() && res.num.back() == 0) {
res.num.pop_back();
}
return res;
} else {
if (a.isNegative) {
return b - (-a);
} else {
return a - (-b);
}
}
}
// 取负操作
bigint operator-(const bigint& a) {
bigint res = a;
res.isNegative = !res.isNegative;
return res;
}
// 大整数减法
bigint operator-(const bigint& a, const bigint& b) {
if (a.isNegative == b.isNegative) {
if (absGreater(a, b)) {
bigint res;
res.isNegative = a.isNegative;
res.num.resize(a.num.size());
for (int i = 0; i < a.num.size(); ++i) {
res.num[i] = a.num[i];
}
for (int i = 0; i < b.num.size(); ++i) {
res.num[i] -= b.num[i];
}
for (int i = 0; i < res.num.size(); ++i) {
if (res.num[i] < 0) {
res.num[i] += 10;
res.num[i + 1]--;
}
}
while (!res.num.empty() && res.num.back() == 0) {
res.num.pop_back();
}
return res;
} else {
bigint res = b - a;
res.isNegative = !res.isNegative;
return res;
}
} else {
if (a.isNegative) {
bigint res = a + (-b);
res.isNegative = true;
return res;
} else {
return a + (-b);
}
}
}
// 大整数乘法
bigint operator*(const bigint& a, const bigint& b) {
bigint res;
res.isNegative = a.isNegative != b.isNegative;
res.num.resize(a.num.size() + b.num.size());
for (int i = 0; i < a.num.size(); ++i) {
for (int j = 0; j < b.num.size(); ++j) {
res.num[i + j] += a.num[i] * b.num[j];
}
}
for (int i = 0; i < res.num.size(); ++i) {
if (res.num[i] >= 10) {
res.num[i + 1] += res.num[i] / 10;
res.num[i] %= 10;
}
}
while (!res.num.empty() && res.num.back() == 0) {
res.num.pop_back();
}
return res;
}
// 大整数和整数的乘法
bigint operator*(const bigint& a, int b) {
bigint res;
res.isNegative = (a.isNegative && b > 0) || (!a.isNegative && b < 0);
b = abs(b);
res.num.resize(a.num.size() + 10);
for (int i = 0; i < a.num.size(); ++i) {
res.num[i] += a.num[i] * b;
}
for (int i = 0; i < res.num.size(); ++i) {
if (res.num[i] >= 10) {
res.num[i + 1] += res.num[i] / 10;
res.num[i] %= 10;
}
}
while (!res.num.empty() && res.num.back() == 0) {
res.num.pop_back();
}
return res;
}
// 大整数和整数的加法
bigint operator+(const bigint& a, int b) {
if (b < 0) {
bigint temp;
temp.num.push_back(-b);
return a - temp;
}
bigint res = a;
res.num[0] += b;
for (int i = 0; i < res.num.size(); ++i) {
if (res.num[i] >= 10) {
if (i + 1 == res.num.size()) {
res.num.push_back(0);
}
res.num[i + 1] += res.num[i] / 10;
res.num[i] %= 10;
}
}
while (!res.num.empty() && res.num.back() == 0) {
res.num.pop_back();
}
return res;
}
// 大整数除法和取模的公共处理函数
pair<bigint, bigint> div_mod(const bigint& a, const bigint& b) {
bigint quotient, remainder;
quotient.isNegative = a.isNegative != b.isNegative;
remainder.isNegative = a.isNegative;
quotient.num.clear();
remainder.num.clear();
bigint absA = a;
absA.isNegative = false;
bigint absB = b;
absB.isNegative = false;
if (absB.num.empty()) {
// 除数为 0 的情况
cerr << "Error: division by zero" << endl;
return {quotient, remainder};
}
for (int i = absA.num.size() - 1; i >= 0; --i) {
remainder = remainder * 10 + absA.num[i];
int q = 0;
while (remainder >= absB) {
remainder = remainder - absB;
q++;
}
quotient.num.insert(quotient.num.begin(), q);
}
while (!quotient.num.empty() && quotient.num.back() == 0) {
quotient.num.pop_back();
}
while (!remainder.num.empty() && remainder.num.back() == 0) {
remainder.num.pop_back();
}
return {quotient, remainder};
}
// 大整数除法
bigint operator/(const bigint& a, const bigint& b) {
return div_mod(a, b).first;
}
// 大整数取模
bigint operator%(const bigint& a, const bigint& b) {
return div_mod(a, b).second;
}
int main() {
string strA, strB;
cin >> strA >> strB;
bigint a(strA), b(strB);
// 输出和
bigout(a + b);
// 输出差
bigout(a - b);
// 输出积
bigout(a * b);
// 输出商
bigout(a / b);
// 输出余
bigout(a % b);
return 0;
}
谢谢了!!!!!!!!
2 条评论
-
qeqe 我爱你 LV 7 @ 2025-2-9 16:26:06已修改
你这个应该是只能一个而且要定义:
// 语法格式 bool operator- (const bigint&b) const{ //const bigint&b 是定义的 // 最后的const就是把不需要的定义的修饰为const } // 第一个不需要定义 第二个必须要 // 第一个是this->(当然也可以不加) b(定义的)
抱歉,很难描述这个,不理解可以进一步问我
其他没有报错我具体不知道~
-
2025-2-8 23:49:04@
可惜了 洛谷讨论区在2025.02.08正式倒闭了 不然我就在洛谷上问了
👍 3
- 1
信息
- ID
- 5986
- 时间
- 1000ms
- 内存
- 256MiB
- 难度
- 3
- 标签
- 递交数
- 99
- 已通过
- 14
- 上传者