# 大整数运算

## example1

• 实现了大整数的加法和减法和乘法以及大整数与小整数的乘法、除法运算
#include <iostream>
#include <algorithm>
#define maxSize 1000
using namespace std;
//大整数运算
typedef struct {
int number[maxSize];
int len;
}bigInt;

bigInt charArray2BigInt(char charArray[],int len) {
bigInt result;
for (int i = len - 1; i >= 0; --i) {//要逆着来，把[0]位置的元素当成个位，有利于元素对齐
result.number[len - i - 1] = charArray[i] - '0';
}
result.len = len;
return result;
}

bigInt add(bigInt bigInt1, bigInt bigInt2) {
int carry = 0;//进位
int temp;
bigInt result;
int i;
for (i = 0; i < bigInt1.len || i < bigInt2.len; ++i) {
temp = bigInt1.number[i] + bigInt2.number[i] + carry;
result.number[i] = temp % 10;
carry = temp / 10;
}
result.len = i;
if (carry != 0) {
//还有进位未处理
result.number[result.len] = carry;
++result.len;
}
return result;
}

bigInt myMinus(bigInt bigInt1, bigInt bigInt2) {//已经确保bigint1>=bigint2
bigInt result;
int temp;
for (int i = 0; i < bigInt1.len || i < bigInt2.len; ++i) {
if (bigInt1.number[i] < bigInt2.number[i]) {
--bigInt1.number[i + 1];//借一位
bigInt1.number[i] += 10;
}
temp = bigInt1.number[i] - bigInt2.number[i];
result.number[i] = temp;
}
result.len = bigInt1.len;//这个长度是不准确的，如999-998=1，len=1
for (; result.number[result.len-1] == 0 && result.len >= 2; --result.len) {
//获取真实的长度，但是当结果为0时，也要让len=1，以便输出0
}
return result;

}
void initBigIntNumber2Zero(bigInt &bigInt1){
for(int i=0;i<maxSize;++i){
bigInt1.number[i] = 0;
}
}
bigInt bigIntMultiplybigInt(bigInt bigInt1,bigInt bigInt2){
bigInt result;
initBigIntNumber2Zero(result);
for(int i=0;i<bigInt2.len;++i){
for(int j=0;j<bigInt1.len;++j){
result.number[i+j] += bigInt2.number[i] * bigInt1.number[j];//先不算进位，进位都留在原来的位置
}
}
result.len = bigInt1.len + bigInt2.len;
for(int i=0;i<result.len;++i){
result.number[i+1] += result.number[i]/10;
result.number[i] = result.number[i]%10;//把该进的进位都进上去
if(result.number[i+1] == 0){
result.len = i+1;
break;
}
}
return result;
}

void showBigInt(bigInt bigInt1) {
for (int i = bigInt1.len - 1; i >= 0; --i) {
cout << bigInt1.number[i];
}
}

int getSmallInt(){
cout<<endl;
cout<<"please input a small int in order to calculate bigInt1*smallInt and bigInt1/smallInt"<<endl;
int smallInt;
cin>>smallInt;
return smallInt;
}
bigInt bigIntMultiplySmallInt(bigInt bigInt1,int smallInt){
bigInt result;
int carry = 0;//进位
int eachMultiplyPlusCarryResult = 0;
for(int i=0;i<bigInt1.len;++i){
eachMultiplyPlusCarryResult = bigInt1.number[i]*smallInt + carry;
result.number[i] = eachMultiplyPlusCarryResult%10;
carry = eachMultiplyPlusCarryResult/10;
}
result.len = bigInt1.len;
while(carry!=0){
result.number[result.len] = carry%10;
carry = carry/10;
result.len++;
}
return result;
}
bigInt bigInt1DivideSmallInt(bigInt bigInt1,int smallInt,int &remainder){
bigInt result;
result.len = -1;
for(int i=bigInt1.len-1;i>=0;--i){
if(bigInt1.number[i]>=smallInt){
//这一位够除
result.number[i] = bigInt1.number[i] /  smallInt;
if(result.len == -1){
//第一次够除
result.len = i+1;
}
}
remainder = bigInt1.number[i] % smallInt;
if(i != 0){
bigInt1.number[i-1] += remainder*10;
}
}
return result;
}
int main() {
int n = 0;
//	cin >> n;//先输入第一个数的长度
char tempCharArray[maxSize];
char temp;
//开始接收第一个数
while (1) {
temp = getchar();
if (temp == ' ' || temp == '\n') {
//接收到了空格或者回车,代表接受完毕
break;
}
tempCharArray[n++] = temp;
}
bigInt bigInt1 = charArray2BigInt(tempCharArray, n);//将接收到的数转化为bitInt类型
//开始接收第二个数
n = 0;
while (1) {
temp = getchar();
if (temp == ' ' || temp == '\n') {
//接收到了空格或者回车,代表接受完毕
break;
}
tempCharArray[n++] = temp;
}
//第二个数接收完毕,已经确保第一个数大于等于第二个数
bigInt bigInt2 = charArray2BigInt(tempCharArray, n);//将接收到的数转化为bitInt类型
cout<<endl<<"bigInt1 + bigInt2:";
bigInt minusResult = myMinus(bigInt1, bigInt2);
cout<<endl<<"bigInt1 - bigInt2:";
showBigInt(minusResult);
bigInt bigIntMultiplyBigIntResult = bigIntMultiplybigInt(bigInt1,bigInt2);
cout<<endl<<"bigInt1 * bigInt2:";
showBigInt(bigIntMultiplyBigIntResult);
int smallInt = getSmallInt();
bigInt bigIntMultiplySmallIntResult = bigIntMultiplySmallInt(bigInt1,smallInt);
cout<<endl<<"bigInt1 * smallInt:";
showBigInt(bigIntMultiplySmallIntResult);
int remainder;
bigInt bigInt1DivideSmallIntResult = bigInt1DivideSmallInt(bigInt1,smallInt,remainder);
cout<<endl<<"bigInt1 / smallInt:";
showBigInt(bigInt1DivideSmallIntResult);
cout<<"   remainder:"<<remainder;
return 0;

}


10-28
05-21
04-24 2897
08-25 3118
05-06 6344
01-16 1182
03-06 3592
03-14 1138
01-02 1993
05-22 1467
05-19 5149
06-11
09-30 5975
09-12 2356
04-13 1万+