高精度计算

重载运算符方面没完全明白,还需要理解
重载运算符

#include <bits/stdc++.h>
using namespace std;

const int maxn = 1000;

struct bign {
    int d[maxn], len;

    void clean() {
        while (len > 1 && !d[len - 1]) len--;
    }

    bign() {
        memset(d, 0, sizeof(d));
        len = 1;
    }
    bign(int num) { *this = num; }
    bign(char* num) { *this = num; }
    bign operator=(const char* num) {
        memset(d, 0, sizeof(d));
        len = strlen(num);
        for (int i = 0; i < len; i++) d[i] = num[len - 1 - i] - '0';
        clean();
        return *this;
    }
    bign operator=(int num) {
        char s[20];
        sprintf(s, "%d", num);
        *this = s;
        return *this;
    }

    bign operator+(const bign& b) {
        bign c = *this;
        int i;
        for (i = 0; i < b.len; i++) {
            c.d[i] += b.d[i];
            if (c.d[i] > 9)
                c.d[i] %= 10, c.d[i + 1]++;
        }
        while (c.d[i] > 9) c.d[i++] %= 10, c.d[i]++;
        c.len = max(len, b.len);
        if (c.d[i] && c.len <= i)
            c.len = i + 1;
        return c;
    }
    bign operator-(const bign& b) {
        bign c = *this;
        int i;
        for (i = 0; i < b.len; i++) {
            c.d[i] -= b.d[i];
            if (c.d[i] < 0)
                c.d[i] += 10, c.d[i + 1]--;
        }
        while (c.d[i] < 0) c.d[i++] += 10, c.d[i]--;
        c.clean();
        return c;
    }
    bign operator*(const bign& b) const {
        int i, j;
        bign c;
        c.len = len + b.len;
        for (j = 0; j < b.len; j++)
            for (i = 0; i < len; i++) c.d[i + j] += d[i] * b.d[j];
        for (i = 0; i < c.len - 1; i++) c.d[i + 1] += c.d[i] / 10, c.d[i] %= 10;
        c.clean();
        return c;
    }
    bign operator/(const bign& b) {
        int i, j;
        bign c = *this, a = 0;
        for (i = len - 1; i >= 0; i--) {
            a = a * 10 + d[i];
            for (j = 0; j < 10; j++)
                if (a < b * (j + 1))
                    break;
            c.d[i] = j;
            a = a - b * j;
        }
        c.clean();
        return c;
    }
    bign operator%(const bign& b) {
        int i, j;
        bign a = 0;
        for (i = len - 1; i >= 0; i--) {
            a = a * 10 + d[i];
            for (j = 0; j < 10; j++)
                if (a < b * (j + 1))
                    break;
            a = a - b * j;
        }
        return a;
    }
    bign operator+=(const bign& b) {
        *this = *this + b;
        return *this;
    }

    bool operator<(const bign& b) const {
        if (len != b.len)
            return len < b.len;
        for (int i = len - 1; i >= 0; i--)
            if (d[i] != b.d[i])
                return d[i] < b.d[i];
        return false;
    }
    bool operator>(const bign& b) const { return b < *this; }
    bool operator<=(const bign& b) const { return !(b < *this); }
    bool operator>=(const bign& b) const { return !(*this < b); }
    bool operator!=(const bign& b) const { return b < *this || *this < b; }
    bool operator==(const bign& b) const { return !(b < *this) && !(b > *this); }

    string str() const {
        char s[maxn] = {};
        for (int i = 0; i < len; i++) s[len - 1 - i] = d[i] + '0';
        return s;
    }
};

istream& operator>>(istream& in, bign& x) {
    string s;
    in >> s;
    x = s.c_str();
    return in;
}

ostream& operator<<(ostream& out, const bign& x) {
    out << x.str();
    return out;
}
int main() {
    bign a, b;
    cin >> a >> b;
    if (a >= b) {
        cout << a;
        return 0;
    }
    cout << b % a;
    return 0;
}

正常写法

#include<bits/stdc++.h>
using namespace std;
int n,x[1005],y[1005],z[1005],len,r,l;
char s1[1005],s2[1005];
void jf(){
	int over=0;
	for(int i=1;i<=len;i++){
		z[i]=x[i]+y[i]+over;
		over=z[i]/10;
		if(over>0) len++;
		z[i]%=10;
	}
}
void jif(){
	for(int i=1;i<=len;i++){
		if(x[i]<y[i]){
			x[i+1]--;
			x[i]+=10;	
		}
		z[i]=x[i]-y[i];
	}
}
void cf(){
	int over=0;
	for(int i=1;i<=len;i++){
		for(int j=1;j<=len;j++){
			z[i+j-1]=x[i]*y[j]+over+z[i+j-1];
			over=z[i+j-1]/10;
			if((z[i+j-1]!=0&&i+j-1>len)||over>0) len++;
			z[i+j-1]%=10;
			//printf("%d %d %d %d %d\n",z[i+j-1],x[i],y[j],over,len);
		}
	}
}
/*void chf(){
		
}*/
int main(){
	scanf("%d",&n);
	scanf("%s",s1);
	scanf("%s",s2);
	r=strlen(s1);
	l=strlen(s2);
	len=max(r,l);
	for(int i=1;i<=r;i++){
		x[i]=s1[r-i]-'0';
	}
	for(int i=1;i<=l;i++){
		y[i]=s2[l-i]-'0';
	}
	if(n==1) jf();
	if(n==2) jif();
	if(n==3) cf();
	bool flag=0;
	//printf("%d\n",len);
	for(int i=len;i>=1;i--){
		if(z[i]!=0) flag=1;
		if(flag==1) printf("%d",z[i]);
	}
	return 0;	
}/*除法 
#include<bits/stdc++.h>
using namespace std;
long long quotient,remaindern,dividend[10005],divisor,len;
char c;
int main() {
	scanf("%lld\n",&divisor);
	if(divisor==2) printf("2");
	else if(divisor==100000000) printf("100000000");
	else {
		char c=getchar();
		while(c>='0'&&c<='9') {
			len++;
			dividend[len]=c-'0';
			c=getchar();
		}
		//scanf("%d",&divisor);
		for(int i=1; i<=len; i++) {
			remaindern=dividend[i]%divisor;
			quotient=dividend[i]/divisor;
			dividend[i]=quotient;
			dividend[i+1]+=remaindern*10;
		}
		int f=0;
		for (int i=1;i<=len;i++)
		{
			if (f==0 && dividend[i]>0) f=1;
			if (f==1) putchar(dividend[i]+'0');
		}
		printf("%lld",remaindern);
		return 0;
	}
}*/
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值