高精度

说明

希望出了错误, 您能够大方地花些时间, 斧正代码。 感谢!

数组模拟

#include<cstdio>
#include<string>
#include<algorithm>
#include<iostream>
using namespace std;
const int N = 100000;

int a[N], b[N], c[N], lena, lenb, lenc;
string x, y;

void jinwei() {
	for(int i = 1; i <= lenc; i++) if(c[i] >= 10) {
		c[i+1] += c[i]/10;
		c[i] %= 10;
		if(i == lenc) lenc++;
	}
	while(lenc && c[lenc] == 0) lenc--;//保证lenc不为负 
}

int main() {
	cin>>x>>y;
	lena = x.length(), lenb = y.length();
	/*if(lena < lenb || (lena==lenb && x < y)) {//减法时使用 
		swap(x, y);
		swap(lena, lenb);//保证a>=b 
		printf("-");
	}*/
	for(int i = 0; i < lena; i++) a[lena-i] = x[i]-'0';
	for(int i = 0; i < lenb; i++) b[lenb-i] = y[i]-'0';
//	for(int i = lena; i >= 1; i--) printf("%d", a[i]);
//	printf("
");
//	for(int i = lenb; i >= 1; i--) printf("%d", b[i]);
//	printf("
");
	
//高精加: 非负数相加 
/*	lenc = max(lena, lenb);
	for(int i = 1; i <= lenc; i++) c[i] = a[i]+b[i];
	jinwei();*/
	
//高精减 
/*	for(int i = 1, j = 1; i <= lena || j <= lenb; i++, j++) {
		if(a[i] < b[i]) {a[i] += 10; --a[i+1];}
		c[i] = a[i] - b[i];
	}
	lenc = lena;
	while(c[lenc] == 0 && lenc) lenc--;//注意使lenc不为负 */
	
//高精乘
/*	for(int i = 1; i <= lena; i++) 
		for(int j = 1; j <= lenb; j++) 
			c[i+j-1] += a[i]*b[j]; 
	lenc = lena+lenb-1;
	jinwei();*/	
	if(lenc == 0) printf("0");
	else for(int i = lenc; i > 0; i--)  printf("%d", c[i]);
	return 0;
}
//高精除低精 
/*	for(int i = lena; i >= 1; i--) {
		a[i-1] += (a[i]%k)*10;
		a[i] = a[i]/k;
	}
	while(a[lena] == 0 && lena) lena--;
	if(lena == 0) printf("0");
	else for(int i = lena; i >= 1; i--) printf("%d", a[i]);*/
//高精%低精 
/*	int yushu = 0;
	for(int i = lena; i >= 1; i--) {
		yushu = yushu*10+a[i];
		yushu %= k;
	} 
	printf("%d", yushu);*/

结构体模拟

//暂时只支持正整数(也就是为0的时候会出bug

#include<cstdio>
#include<cstring>
#include<vector>
#include<iostream>
using namespace std;
const int N = 1000000;//共几位(乘法时使用 
int C[N];

struct BigInteger {
    static const int BASE = 10000;
    static const int WIDTH = 4;//记得修改压位的时候,下面重载“<<”中的sprintf处也要修改
    vector<int> s;
    void cr() {//去除前导0 
        while(s.size() && s.back() == 0) s.pop_back();
        return ;
    }

    BigInteger(long long num = 0) { *this = num; }// 构造函数
    BigInteger operator = (long long num) { // 赋值运算符
        s.clear();
        do {
            s.push_back(num % BASE);
            num /= BASE;
        } while(num > 0);
        return *this;
    }
    BigInteger(const string& str) {*this = str;}   // 构造函数
    BigInteger operator = (const string& str) { // 赋值运算符
        s.clear();
        int x, len = (str.length() - 1) / WIDTH + 1;
        for(int i = 0; i < len; i++) {
            int end = str.length() - i*WIDTH;//stl左闭右开
            int start = max(0, end - WIDTH);//“开着的右”-“真实长度”=“闭着的左”
            sscanf(str.substr(start, end-start).c_str(), "%d", &x);
            //str.substr(start, len): 从string类型的str中,截取以start为左端点,长度为len的string
            //str.c_str(): 把string类型的str转化成数组??//这两个我也没仔细查,以后会补上的
            s.push_back(x);
        }
        return *this;
    }

    BigInteger operator + (const BigInteger& b) const {//重载运算符
        BigInteger c;
        c.s.clear();
        for(int i = 0, g = 0; g != 0 || i < s.size() || i < b.s.size(); i++) {
            int x = g;//g表示进位数
            if(i < s.size()) x += s[i];
            if(i < b.s.size()) x += b.s[i];
            c.s.push_back(x % BASE);
            g = x / BASE;
        }
        c.cr();
        return c;
    }
    BigInteger operator - (const BigInteger& b) const {//s > b.s时使用
        BigInteger c;
        c.s.clear();
        for(int i = 0, y = 0; y != 0 || i < s.size(); i++) {
            int x = s[i]-y;//y表示余数
            y = 0;
            if(i < b.s.size()) x -= b.s[i];
            if(x < 0) x += BASE, y = 1;
            c.s.push_back(x % BASE);
        }
        c.cr();
        return c;
    }

    bool operator < (const BigInteger& b) const {
        if(s.size() != b.s.size()) return s.size() < b.s.size();
        for(int i = s.size() - 1; i >= 0; i--) if(s[i] != b.s[i]) return s[i] < b.s[i];
        return false;//相等
    }
    
    BigInteger operator * (const BigInteger& b) const {
        int lenc = s.size()+b.s.size();
        for(int i = 0; i < lenc; i++) C[i] = 0;//记得清0 
        for(int i = 0; i < s.size(); i++) 
            for(int j = 0; j < b.s.size(); j++){
                C[i+j] += s[i]*b.s[j];
                C[i+j+1] += C[i+j]/BASE;
                C[i+j] %= BASE;
            }
        BigInteger c; c.s.clear();//把已压位的C数组放进struct c 
        while(C[lenc-1] == 0) --lenc;
        for(int i = 0; i < lenc; i++) c.s.push_back(C[i]);
//      c.cr();//上面已经删去前导0了 
        return c;
    }
};

ostream& operator << (ostream &out,const BigInteger& x) {
    out << x.s.back();
    for(int i = x.s.size()-2; i >= 0; i--) {
        char buf[20];
        sprintf(buf, "%04d", x.s[i]);
        for(int j = 0; j < strlen(buf); j++) out << buf[j];
    }
    return out;
}

istream& operator >> (istream &in, BigInteger& x) {
    string s;
    if(!(in >> s)) return in;
    x = s;
//  x.cr();//在这不加时,运算并不会出错,但直接输入输出会有点问题
    return in;
}

//#include<set>
//#include<map>
//set<BigInteger> s;
//map<BigInteger, int> m;

int main() {
    BigInteger a, b;
    cin>>a>>b;
//  if(a < b) { printf("-"); swap(a, b);} //减法
//  cout<<a-b;
    
//  cout<<a+b; //加法
 
    cout<<a*b; //乘法(不能压8位, 会溢出,建议压4位 
    return 0;
}
原文地址:https://www.cnblogs.com/tyner/p/11805752.html