// bigNum.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include <iostream>
#include <cstring>
using namespace std;
#define MAXDIGITS 100110
#define PLUS 1
#define MINUS -1
typedef struct {
char digits[MAXDIGITS];
int signbit;
int lastdigit;
}bignum;
void initialize_bignum(bignum* c) {
memset(c, 0, sizeof(bignum));
c->signbit = PLUS;
}
void print_bignum(const bignum* const n) {
int i;
if (n->signbit == MINUS) printf("-");
for (i = n->lastdigit; i >= 0; i--)
printf("%c", '0' + n->digits[i]);
printf("\n");
}
void zero_justify(bignum* n) {
while ((n->lastdigit > 0) && (n->digits[n->lastdigit] == 0))
n->lastdigit--;
if ((n->lastdigit == 0) && (n->digits[0] == 0))
n->signbit = PLUS;
}
int compare_bignum(bignum* a, bignum* b) {
int i;
if ((a->signbit == MINUS) && (b->signbit == PLUS)) return PLUS;
if ((a->signbit == PLUS) && (b->signbit == MINUS)) return MINUS;
if ((b->lastdigit > a->lastdigit)) return (PLUS * a->signbit);
if ((a->lastdigit > b->lastdigit)) return (MINUS * a->signbit);
for (i = a->lastdigit; i >= 0; i--) {
if (a->digits[i] > b->digits[i])
return (MINUS * a->signbit);
if (b->digits[i] > a->digits[i])
return (PLUS * a->signbit);
}
return 0;
}
void add_bignum(bignum* a, bignum* b, bignum* c);
void subtract_bignum(bignum* a, bignum* b, bignum* c) {
int borrow;
int v; int i;
if ((a->signbit == MINUS) || (b->signbit == MINUS)) {
b->signbit = -1 * b->signbit;
add_bignum(a, b, c);
b->signbit = -1 * b->signbit;
return;
}
if (compare_bignum(a, b) == PLUS) {
subtract_bignum(b, a, c);
c->signbit = MINUS;
return;
}
c->lastdigit = max(a->lastdigit, b->lastdigit);
borrow = 0;
for (i = 0; i <= (c->lastdigit); i++) {
v = (a->digits[i] - borrow - b->digits[i]);
if (a->digits[i] > 0)
borrow = 0;
if (v < 0) {
v = v + 10;
borrow = 1;
}
c->digits[i] = (char)v % 10;
}
zero_justify(c);
}
void add_bignum(bignum* a, bignum* b, bignum* c) {
int carry;
int i;
initialize_bignum(c);
if (a->signbit == b->signbit) c->signbit = a->signbit;
else {
if (a->signbit == MINUS) {
a->signbit = PLUS;
subtract_bignum(b, a, c);
a->signbit = MINUS;
}
else {
b->signbit = PLUS;
subtract_bignum(a, b, c);
b->signbit = MINUS;
}
return;
}
c->lastdigit = max(a->lastdigit, b->lastdigit) + 1;
carry = 0;
for (i = 0; i <= (c->lastdigit); i++) {
c->digits[i] = (char)
(carry + a->digits[i] + b->digits[i]) % 10;
carry = (carry + a->digits[i] + b->digits[i]) / 10;
}
zero_justify(c);
}
void digit_shift(bignum* n, int d) {
int i;
if ((n->lastdigit == 0) && (n->digits[0] == 0)) return;
for (i = n->lastdigit; i >= 0; i--)
n->digits[i + d] = n->digits[i];
for (i = 0; i < d; i++) n->digits[i] = 0;
n->lastdigit = n->lastdigit + d;
}
void multiply_bignum(bignum* a, bignum* b, bignum* c) {
bignum row;
bignum tmp;
int i, j;
initialize_bignum(c);
row = *a;
for (i = 0; i <= b->lastdigit; i++) {
for (j = 1; j <= b->digits[i]; j++) {
add_bignum(c, &row, &tmp);
*c = tmp;
}
digit_shift(&row, 1);
}
c->signbit = a->signbit * b->signbit;
zero_justify(c);
}
void divide_bignum(bignum* a, bignum* b, bignum* c) {
bignum row;
bignum tmp;
int asign, bsign;
int i; // j;
initialize_bignum(c);
asign = a->signbit;
bsign = b->signbit;
a->signbit = PLUS;
b->signbit = PLUS;
initialize_bignum(&row);
initialize_bignum(&tmp);
c->lastdigit = a->lastdigit;
for (i = a->lastdigit; i >= 0; i--) {
digit_shift(&row, 1);
row.digits[0] = a->digits[i];
c->digits[i] = 0;
while (compare_bignum(&row, b) != PLUS) {
c->digits[i]++;
subtract_bignum(&row, b, &tmp);
row = tmp;
}
}
zero_justify(c);
a->signbit = asign;
b->signbit = bsign;
}
void int_to_bignum(int s, bignum* n) {
int i;
int t;
if (s >= 0) n->signbit = PLUS;
else n->signbit = MINUS;
for (i = 0; i < MAXDIGITS; i++) n->digits[i] = (char)0;
n->lastdigit = -1;
t = abs(s);
while (t > 0) {
n->lastdigit++;
n->digits[n->lastdigit] = (t % 10);
t = t / 10;
}
if (s == 0) n->lastdigit = 0;
}
void string_to_bignum(string s, bignum* n) {
int i;
int t;
if (s.size() >= MAXDIGITS) return;
if (s[0] == '-') n->signbit = MINUS;
else n->signbit = PLUS;
for (i = 0; i < MAXDIGITS; i++) n->digits[i] = (char)0;
n->lastdigit = -1;
if (s[0] == '-' || s[0] == '+') s.erase(s.begin());
int len = s.size()-1;
for (; len >= 0; len--) {
n->lastdigit++;
n->digits[n->lastdigit] = s[len]-'0';
}
if(s=="0")n->lastdigit = 0;
return;
}
int main()
{
string sa, sb;
cin >> sa >> sb;
bignum a, b, c;
string_to_bignum(sa, &a);
string_to_bignum(sb, &b);
//int_to_bignum(2,&a);
//int_to_bignum(1, &b);
divide_bignum(&a, &b, &c);
print_bignum(&c);
return 0;
}
标签:digits,bignum,int,加减,lastdigit,signbit,PLUS,提供商,除法
From: https://www.cnblogs.com/itdef/p/18546378