Large integer addition,Big integer subtraction,Large integer multiplication , division of large integer (not written yet)

Big integer addition: preprocessing data first is to make sure that the digits of two big integers are equal, and the one with less digits is filled with 0 in the high order, so that the digits of the two integers are equal, which is convenient to calculate the carry of data. Only check whether the highest carry exists at the end of function call. If carry is needed, only add the carry to the return value at last. Addition is to add two numbers from low to high. Carry is calculated for each added value, and modulus is added to the current position.

Big integer subtraction: the function first compares two equal numbers and returns 0 if they are equal. Then it judges the size of the two numbers. If the number to be subtracted is greater than the number to be subtracted, the sign takes negative and the two numbers exchange positions. Then it adds 0 to the small number's high position according to the method of addition, and then subtracts from the low position to the high position according to the bits.

Multiplication of large integers: there is a great difference between multiplication of large integers and addition subtraction. Although multiplication is also bitwise multiplication, strings cannot be directly and simply spliced, for example, multiplication of two two digitsAmong themAfter that, if the string is spliced directly in the way of addition or subtraction, the answer must be wrong, so the principle of bit multiplication should be used here

That is to say, before calculating, the reason should be correct first.String conversion bits all 0 strings, and length bits. Then, we multiply the data bit by bit in two-level cycle and sum the product withAdd the characters in the corresponding position in. If the carry is encountered, it will be added directly to the value of the previous bit, and cannot be left for the next calculation. Finally, we need to deal with the first carry problem, and still use oneCharacters are stored and added before the return value at the end of return.

#include<string.h>
#include<iostream>


std::string add(std::string s1,std::string s2) {
	std::string s3;
	if(s1.length()<s2.length()) {
		while(s1.length()<s2.length()) {
			s1="0"+s1;
		}
	} else {
		while(s2.length()<s1.length()) {
			s2="0"+s2;
		}
	}
	int up=0;
	int k;
	int len=s1.length()-1;
	while(len>=0) {
		k=(s1.at(len) + s2.at(len) + up - '0' - '0');
		up = k/10;
		s3=(char)(k % 10 + '0') + s3;
		len--;
	}
	if(up)
		s3="1"+s3;
	return s3;
}

std::string sub(std::string s1,std::string s2) {
	if(s1==s2) {
		return "0";
	}
	std::string s3;
	bool flag=false;
	if(s1.length()<s2.length() || (s1.length()==s2.length() && s1<s2)) {
		std::string s=s1;
		s1=s2;
		s2=s;
		flag=true;
	} else {
		s3="";
	}
	while(s2.length()<s1.length()) {
		s2="0" + s2;
	}
	int k,down=0;
	for(int i=s1.length()-1; i>=0; i--) {
		k=s1[i] -s2[i] +down;
		if(k<0) {
			down=-1;
			k=10+k;
		} else {
			down=0;
		}
		s3=(char)('0' + k) + s3;
	}
	k=0;
	while(s3[k]=='0' ) {
		k++;
	}
	s3=s3.substr(k);
	if(flag)
		s3="-"+s3;
	return s3;
}

std::string mul(std::string s1,std::string s2) {
	if(s1=="0" || s2=="0")
		return "0";
	if(s1=="1")
		return s2;
	if(s2=="1")
		return s1;
	std::string s3;
	int len=s1.length() + s2.length() -1;
	for(int i=0; i<len; i++) {
		s3+="0";
	}
	int up=0,k;
	std::string flag="";
	for(int i=s1.length()-1; i>=0; i--) {
		for(int j=s2.length()-1; j>=0; j--) {
			k=s3[i+j]-'0' + (s1[i]-'0')*(s2[j]-'0') ;
			up=k/10;
			s3[i+j]=(char)(k%10+'0');
			int d=1;

			while(up>0) {
				//First carry
				if(i==0 && j==0 && up>0) {
					flag=(char)("0"+ up);
					break;
				}
				k=(char)(s3[i+j-d]+up-'0');
				up=k/10;
				s3[i+j-d]=(char)(k%10+'0');
				d++;
			}

		}
	}
	return flag + s3;
}