高精度模板

upd:程序设计课写了个链表实现的实数加减乘除,输入格式要严格按照程序结尾的数据来(嫌麻烦没有压位)

#include<bits/stdc++.h>
using namespace std;
#define gc getchar
#define pc putchar
typedef long long ll;
struct node{
	int x;
	struct node*pre,*ne;
};
void insert(node *p,int x){//p后插入x 
	node *q=new node;
	q->ne=p->ne;
	q->pre=p;
	p->ne->pre=q;
	p->ne=q;
	q->x=x;
}
node* del(node *p){
	p->ne->pre=p->pre;
	p->pre->ne=p->ne;
	node *q=p;
//	cout<<(q->x)<<endl;
	p=p->ne;
	delete q;
	return p;
}
struct NUM{
	int n,sgn,exp;
	node *head;
	char get(){
		char c=gc();
		int pos=-1;
		if (c=='-') sgn=-1,c=gc();
		else sgn=1;
		node*p=head=new node;
		p->pre=p->ne=p;
		do{
			if (c!='.'){
				n++;
				insert(p,c-'0');
				p=p->ne;
			}else pos=n;
			c=gc();
		}while('0'<=c&&c<='9' || c=='.');
		if (pos!=-1) exp=n-pos;
		else exp=0;
		return c;
	}
	void print(){
		node *p=head;
		if (sgn==-1) pc('-');
		int cnt=0;
		do{
			p=p->ne;
			printf("%d",p->x);
			cnt++;
			if (exp && cnt==n-exp) pc('.');
		}while(p->ne!=head);
		puts("");
	}
	void copy(NUM a){
		n=a.n,sgn=a.sgn,exp=a.exp;
		node *p=head=new node,*q=a.head;
		p->ne=p->pre=p;
		for (int i=0;i<n;i++) q=q->ne,insert(p,q->x),p=p->ne;
	}
	void destroy(){
		exp=sgn=0;
		node *p=head;
		while (n--) p=del(p);
		n=0;
		delete p;
	}
	void back(){
		node *p=head->ne;
		while (p->x==0 && n>exp+1) p=del(p),n--;
	}
	friend bool operator<(NUM a,NUM b){//小数点位置相同比较绝对值大小 
		if (a.n!=b.n) return a.n<b.n;
		node *p=a.head,*q=b.head;
		for (int i=0;i<a.n;i++){
			p=p->ne,q=q->ne;
			if (p->x!=q->x) return p->x<q->x;
		}
		return 0;
	}
	friend NUM operator+(NUM a,NUM b){
		NUM A,B;
		A.copy(a),B.copy(b);
		if (A.sgn!=B.sgn){
			B.sgn*=-1;
			int fl=A.sgn;
			A=A-B;
			A.sgn*=fl;
			B.destroy();
			return A;
		}
		if (A.exp>B.exp) swap(A,B);
		node *p=A.head->pre;
		for (int i=0;i<B.exp-A.exp;i++) A.n++,insert(p,0),p=p->ne;
		A.exp=B.exp;
		if (A.n<B.n) swap(A,B);
		p=A.head->pre;
		node *q=B.head->pre;
		bool fl=0;
		for (int i=0;i<A.n;i++){
			p->x+=(i<B.n?q->x:0)+fl;
			if (p->x>=10) fl=1,p->x-=10;
			else fl=0;
			p=p->pre,q=q->pre;
		}
		if (fl) insert(p,1),A.n++;
		B.destroy();
		return A;
	}
	friend NUM operator-(NUM a,NUM b){
		NUM A,B;
		A.copy(a),B.copy(b);
		if (A.sgn!=B.sgn){
			B.sgn*=-1;
			int fl=A.sgn;
			A=A+B;
			A.sgn*=fl;
			B.destroy();
			return A;
		}
		if (A.exp>B.exp) swap(A,B);
		node *p=A.head->pre;
		for (int i=0;i<B.exp-A.exp;i++) A.n++,insert(p,0),p=p->ne;
		A.exp=B.exp;
		if (A<B) swap(A,B),A.sgn*=-1;
		p=A.head->pre;
		node *q=B.head->pre;
		bool fl=0;
		for (int i=0;i<A.n;i++){
			p->x-=(i<B.n?q->x:0)+fl;
			if (p->x<0) fl=1,p->x+=10;
			else fl=0;
			p=p->pre,q=q->pre;
		}
		A.back();
		B.destroy();
		return A;
	}
	friend NUM operator*(NUM a,NUM b){
		NUM A,B,C;
		A.copy(a),B.copy(b);
		node *pc=C.head=new node;
		C.sgn=A.sgn*B.sgn;
		C.exp=A.exp+B.exp;
		C.n=A.n+B.n;
		pc->ne=pc->pre=pc;
		for (int i=0;i<A.n+b.n;i++) insert(pc,0),pc=pc->ne;
		node *p=A.head;
		for (int i=0;i<A.n;i++){
			p=p->pre,pc=C.head;
			for (int j=0;j<i;j++) pc=pc->pre;
			node *q=B.head;
			for (int j=0;j<B.n;j++){
				pc=pc->pre,q=q->pre;
				pc->x+=p->x*q->x;
				pc->pre->x+=pc->x/10;
				pc->x%=10;
			}
		}
		C.back();
		A.destroy(),B.destroy();
		return C;
	}
	friend NUM operator/(NUM a,NUM b){
		NUM A,B,C;
		A.copy(a),B.copy(b);
		node *pc=C.head=new node;
		pc->ne=pc->pre=pc;
		C.sgn=A.sgn*B.sgn;
		B.sgn=A.sgn;//后面有减法 
		A.exp-=B.exp,B.exp=0;
		node *p=A.head->pre;
		for (;A.exp<0;A.exp++) A.n++,insert(p,0),p=p->ne;
		C.exp=A.exp;
		if (A<B) C.n=1;
		else C.n=A.n-B.n+1;
		for (int i=0;i<C.n;i++) insert(pc,0),pc=pc->ne;
		node *q=B.head->pre;
		for (int i=0;i<C.n-1;i++) insert(q,0),q=q->ne,B.n++;
		pc=C.head;
		for (int i=0;i<C.n;i++){
			pc=pc->ne;
			for (;!(A<B);pc->x++) A=A-B;
			if (i<C.n-1) del(B.head->pre),B.n--;
		}
		C.back();
		A.destroy(),B.destroy();
		return C;
	}
}F,G,K;
char opt;
int main(){
	while(1){
		opt=F.get();
		G.get();
		switch(opt){
			case'+':K=F+G;break;
			case'-':K=F-G;break;
			case'*':K=F*G;break;
			case'/':K=F/G;break;
		}
		K.print();
		F.destroy();
		G.destroy();
		K.destroy();
		gc();
	}
}
/*
-101.3/4.2=
89+31=
77-21=
22-112=
22.123+234.32=
1.237*7.231=
-213812.321838*2312.123=
23121933098913.212-823827338497.178=
3123212312.92*-923822131219.23=
101/3=
*/

下面的是几年前的

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=10002,M=1e9;
struct NUM{
    int t;
    ll a[N/9];
    friend void print(NUM x){
        if (!x.t){puts("0");return;}
        printf("%lld",x.a[--x.t]);
        int t[9];
        for (int i=x.t-1;i>=0;i--){
            memset(t,0,sizeof(t));
            for (int j=0;j<9;j++) t[j]=x.a[i]%10,x.a[i]/=10;
            for (int j=8;j>=0;j--) putchar(t[j]|48);
        }
        puts("");
    }
    friend bool operator>(NUM x,NUM y){
        if (x.t!=y.t) return x.t>y.t;
        for (int i=x.t-1;i>=0;i--)
            if (x.a[i]!=y.a[i]) return x.a[i]>y.a[i];
        return 0;
    }
    friend NUM operator+(NUM x,NUM y){
        int i;
        for (i=0;i<x.t || i<y.t || x.a[i];i++){
            if (i+1>=x.t) x.a[i+1]=0;
            if (i<y.t) x.a[i]+=y.a[i];
            if (x.a[i]>=M) x.a[i+1]++,x.a[i]-=M;
        }
        x.t=i;
        return x;
    }
    friend NUM operator-(NUM x,NUM y){
        if (y>x) swap(x,y);
        for (int i=0;i<y.t;i++){
            x.a[i]-=y.a[i];
            if (x.a[i]<0) x.a[i]+=M,x.a[i+1]--;
        }
        while (x.t && !x.a[x.t-1]) x.t--;
        return x;
    }
    friend NUM operator*(NUM x,NUM y){
        NUM z;z.t=x.t+y.t;
        memset(z.a,0,z.t<<3);
        for (int i=0;i<x.t;i++)
            for (int j=0;j<y.t;j++){
                z.a[i+j]+=x.a[i]*y.a[j];
                z.a[i+j+1]+=z.a[i+j]/M;
                z.a[i+j]%=M;
            }
        if (z.t && !z.a[z.t-1]) z.t--;
        return z;
    }
    friend NUM operator*(NUM x,int y){
        int i;
        for (i=0;i<x.t;i++) x.a[i]*=y;
        for (i=0;i<x.t || x.a[i];i++){
            if (i+1>=x.t) x.a[i+1]=0;
            x.a[i+1]+=x.a[i]/M,x.a[i]%=M;
        }
        x.t=i;
        return x;
    }
    friend NUM operator/(NUM x,int y){
        ll k=0;
        for (int i=x.t-1;i>=0;i--){
            k=k*M+x.a[i];
            x.a[i]=k/y;
            k%=y;
        }
        while (x.t && !x.a[x.t-1]) x.t--;
        return x;
    }
    friend NUM div(NUM x,NUM y,bool fl){
        NUM z;z.t=x.t-y.t+1;
        if (z.t<=0){
            z.t=0;
            return fl?z:x;
        }
        memset(z.a,0,z.t<<3);
        for (int i=z.t-1;i>=0;i--){
            for (int j=1<<29;j;j>>=1){
                NUM tmp=y*j;bool fl=tmp.t+i<=x.t;
                if (!fl) continue;
                if (tmp.t+i==x.t){
                    for (int k=tmp.t-1;k>=0;k--)
                        if (x.a[k+i]!=tmp.a[k]){
                            fl=tmp.a[k]<x.a[k+i];
                            break;
                        }
                    if (!fl) continue;
                }
                z.a[i]|=j;
                for (int k=0;k<tmp.t;k++){
                    x.a[k+i]-=tmp.a[k];
                    if (x.a[k+i]<0) x.a[k+i+1]--,x.a[k+i]+=M;
                }
                while (x.t && !x.a[x.t-1]) x.t--;
            }
        }
        while (z.t && !z.a[z.t-1]) z.t--;
        return fl?z:x;
    }
    friend NUM operator/(NUM x,NUM y){return div(x,y,1);}
    friend NUM operator%(NUM x,NUM y){return div(x,y,0);}
    friend NUM gcd(NUM x,NUM y){
        int cnt=0;NUM T;
        while (1){
            if (!x.t || !y.t){
                NUM p,z;
                p.a[0]=2;
                p.t=z.t=z.a[0]=1;
                for (;cnt;cnt>>=1,p=p*p)
                    if (cnt&1) z=z*p;
                if (x.t) return z*x;
                else return z*y;
            }
            bool f1=0,f2=0;
            if (!(x.a[0]&1)) x=x/2,f1=1;
            if (!(y.a[0]&1)) y=y/2,f2=1;
            if (f1 && f2) cnt++;
            if (x>y) x=x-y;
            else T=x,x=y-x,y=T;
        }
    }
}E;
NUM toNUM(char s[]){
    ll f[10];
    f[0]=1;
    for (int i=1;i<10;i++) f[i]=f[i-1]*10;
    NUM A;
    A.t=strlen(s);
    memset(A.a,0,(A.t-1)/9+1<<3);
    for (int i=A.t-1;i>=0;i--) A.a[(A.t-1-i)/9]+=f[(A.t-1-i)%9]*(s[i]^48);
    A.t=(A.t-1)/9+1;
    return A;
}
int main(){
	E.t=E.a[0]=1;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值