大數運算,佇列實現大數 加法 乘法 除法 模除

2021-08-02 07:20:44 字數 2173 閱讀 9996

以下**可進行大數加法、減法、乘法、除法、餘除,和比較大小的計算(佇列實現)包括負數

【**】:

#include#include#include#include#include#includeusing namespace std;

const int mod=1e9;

typedef long long ll;

const int m=1e8;//億進製

class bignum;

bignum::bignum(ll n=0)

dowhile(n);

}bignum::bignum(char *str)//把字串轉化成佇列型大數

} num+=k*(str[i]-'0');

k*=10;

if(k>=m)

}if(num!=0||q.empty())q.push(num);

if(q.size()==1&&q.front()==0)sign=0;//-0 == 0

}void out(queueq)//遞迴輸出佇列大數

ll num=q.front();q.pop();

out(q);

printf("%08lld",num);

} ostream& operator<<(ostream& stream,bignum& val)

istream& operator>>(istream& stream, bignum& val)

int mycmp(bignum a,bignum b)

if(!flag)return 0;

return k*(flag>0?1:(-1));

}int t=a.q.size()-b.q.size();

return k*( t>0?1:(-1) );

}bignum operator+(bignum a,bignum b)//加

if(!a.sign&&b.sign)

bignum sum;

while(!sum.q.empty())sum.q.pop();

if(a.sign&&b.sign)sum.sign=1;//negetive

while(!a.q.empty()||!b.q.empty())

if(!b.q.empty())

sum.q.push((flag+num1+num2)%m);

flag=(num1+num2+flag)/m;

}

if(flag)

sum.q.push(flag);

return sum;

}bignum operator-(bignum a,bignum b)//a-b

if(a.sign&&!b.sign)

bignum less;

int ship=mycmp(a,b);//同正或同負

if(ship==0)return less;

else if(ship<0)

} else if(a.sign&&b.sign)

while(!less.q.empty())less.q.pop();

while(!a.q.empty())

if(num1s,ans;

while(!a.q.empty())

ll flag=0;//flag移位

while(!s.empty())

//最後的flag為餘數

while(!ans.empty())

if(a.q.empty())a.q.push(0);

if(a.sign+signm==1)a.sign=1;

return a;

}ll operator%(bignum a,ll m)//只能正數

ll flag=0;//flag移位

while(!s.empty())

return flag;

}int main()

{ bignum a,b,c;

ll m;

cin>>a>>b;

c=a+b;

cout<<"a+b="<>a>>m;

c=a/m;

ll d=a%m;

cout<<"a/m="<

大數乘法 大數加法實現

include include include include include include include include using namespace std 實現兩個數的加法 判斷乙個字串是否為有效的數 bool isvalidnumber const string s if 1 len ...

大數加法,大數乘法

include include include typedef struct node pnode 函式宣告 int multiply char strmultipliera,char strmultiplierb,char strrst pnode initlist char strmultipl...

大數加法和大數乘法的實現

問乙個facebook大數相乘的題 兩個大數相乘 char multiply char char 比如 char str1 23456789009877666555544444 char str2 346587436598437594375943875943875 最後求出他們的乘積 就是實現乘法的...