做了个高精度除法的程序
刚开始显示加法有问题
把"[i]"改成".at(i)"之后显示
Code:
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <cstdio>
#include <string>
#include <Windows.h>
#include <stack>
#include <queue>
#include <fstream>
using namespace std;
string removeLeadingZeros(const std::string& str) {
size_t pos = str.find_first_not_of('0');
if (pos != std::string::npos) {
return str.substr(pos);
}
return "0";
}
class BigNumber {
public:
std::string number;
int len;
bool empty = true;
BigNumber() {
}
BigNumber(int n) {
number = std::to_string(n);
len = number.size();
empty = false;
}
BigNumber(std::string NewNum) {
number = NewNum;
len = NewNum.size();
empty = false;
}
void Delete() {
empty = true;
number = "";
len = 0;
}
char operator[](int b) {
return number[b];
}
bool operator<(BigNumber a) {
if (len < a.len)return true;
if (len > a.len)return false;
for (int i = len; i >= 0; i--)if (number[i] < a.number[i])return true; else if (number[i] > a.number[i])return false;
return false;
}
bool operator>(BigNumber a) {
if (len > a.len)return true;
if (len < a.len)return false;
for (int i = len; i >= 0; i--)if (number[i] > a.number[i])return true; else if (number[i] < a.number[i])return false;
return false;
}
bool operator==(BigNumber a) {
if (len != a.len)return false;
for (int i = 0; i < len; i++)if (number[i] != a.number[i])return false;
return true;
}
bool operator!=(BigNumber a) {
if (BigNumber(number) == a)return false;
return true;
}
};
BigNumber a;
std::ostream& operator<<(std::ostream& output, BigNumber& num) {
output << num.number;
return output;
}
std::istream& operator >>(std::istream& input, BigNumber& num) {
input >> num.number;
num.len = num.number.size();
num.empty = false;
return input;
}
bool operator<=(BigNumber a, BigNumber b) {
if (a < b or a == b)return true;
return false;
}
bool operator>=(BigNumber a, BigNumber b) {
if (a > b or a == b)return true;
return false;
}
BigNumber operator+(BigNumber a, BigNumber b) {
BigNumber* p;
if (a.empty or b.empty)throw "BigNumber_Is_Empty";
removeLeadingZeros(a.number);
removeLeadingZeros(b.number);
BigNumber c;
c.empty = false;
if (a.len != b.len) {
int temp = max(a.len, b.len) - min(a.len, b.len);
bool ABig = a.len > b.len;
if (ABig)p = &a;
else p = &b;
for (int i = 0; i <= temp - 1; i++)p->number.insert(0, "0");
bool flag = false;
for (int i = a.len - 1; i >= 0; i++) {
c.number[i] = a.number[i] + b.number[i] + flag - '0';
flag = false;
if (c.number[i] > '9')c.number.at(i) -= 10, flag = true;
}
if (flag)c.number.insert(0, "1");
return c;
}
}
BigNumber operator*(BigNumber a, BigNumber b) {
int* A, * B;
A = (int*)malloc(5050 * 4);
B = (int*)malloc(5050 * 4);
BigNumber D;
for (int i = a.len; i >= 0; i--)A[a.len - i] = a[i] - '0';
for (int i = b.len; i >= 0; i--)B[b.len - i] = b[i] - '0';
for (int i = 1; i <= a.len; i++)for (int j = 1; j <= b.len; j++) D.number[i + j - 1] += a[i] * b[j];
D.len = a.len + b.len;
for (int i = 1; i <= D.len; i++) {
D.number[i + 1] += D[i] / 10;
D.number[i] %= 10;
}
for (; !D.number[D.len];)D.len--;
std::string c;
for (int i = max(1, D.len); i >= 1; i--) {
c += D[i] + '0';
}
BigNumber C(c);
return C;
}
BigNumber operator-(BigNumber num1, BigNumber num2) {
int len1 = num1.len;
int len2 = num2.len;
if (len1 < len2)num1 = string(len2 - len1, '0') + num1;
else if (len1 > len2)num2 = string(len1 - len2, '0') + num2;
int borrow = 0;
string result = "";
for (int i = num1.len - 1; i >= 0; i--) {
int digit1 = num1[i] - '0';
int digit2 = num2[i] - '0';
int diff = digit1 - digit2 - borrow;
if (diff < 0) {
diff += 10;
borrow = 1;
}
else borrow = 0;
result = to_string(diff) + result;
}
while (result.length() > 1 && result == "0")result = result.substr(1);
a = result;
a.len = a.number.size();
return a;
}
BigNumber operator/(BigNumber a, BigNumber b) {
BigNumber dividend = a.number;
BigNumber divisor = b.number;
BigNumber quotient, remainder;
int len1 = dividend.number.length(), len2 = divisor.number.length();
if (len1 < len2 || (len1 == len2 && dividend.number < divisor.number)) {
quotient.number = "0";
remainder = dividend;
}
else {
BigNumber tmpDividend = dividend;
int k = len1 - len2;
divisor.number.append(k, '0');
quotient.number.resize(k + 1);
for (int i = k; i >= 0; i--) {
while (tmpDividend.number.substr(i, len2) >= divisor) {
tmpDividend = (tmpDividend.number.substr(0, i) + tmpDividend.number.substr(i + len2) - divisor) + tmpDividend.number.substr(i + len2);
quotient.number[i] += 1;
}
}
quotient.number.erase(0, min(quotient.number.find_first_not_of('0'), quotient.number.size() - 1));
remainder = tmpDividend.number.erase(0, min(tmpDividend.number.find_first_not_of('0'), tmpDividend.number.size() - 1));
}
a = quotient;
a.len = a.number.size();
return a;
}
int main() {
BigNumber a, b;
cin >> a >> b;
BigNumber c = a / b;
cout << c.number;
}