高精度运算(运算符重载)(二)

2014-11-24 03:18:25 · 作者: · 浏览: 1
0; i++)
{
if(i R[i]=Carry%Base;
Carry/=Base;
}
R.Len=i;
return R;
}
istream &operator>>(istream &In,BigInt &V)
{
char Ch;
for(V=0; In>>Ch;)
{
V=V*10+(Ch-'0');
if(In.peek()<=' ') break;
}
return In;
}
ostream &operator<<(ostream &Out,const BigInt &V)
{
int i;
Out<<(V.Len==0 0:V[V.Len-1]);
for(i=V.Len-2; i>=0; i--) for(int j=Base/10; j>0; j/=10) Out< return Out;
}
int main()
{
BigInt a,b;
cin>>a>>b;
cout<<"+"< cout< if(compare(a, b)>0) cout<<"-"< else cout<<"-"< cout<<"*"< }

高精度运算(运算符重载)

分类: Apr.2013 79人阅读 评论(0) 收藏 举报

终于写好了哇,我的亲娘来,满满的都是泪啊。。。重载你妹啊。。。。。

  1. #include
  2. #include
  3. using namespace std;
  4. const int Base=1000000000;
  5. const int Capacity=100;
  6. typedef long long huge;
  7. struct BigInt
  8. {
  9. int Len;
  10. int Data[Capacity];
  11. BigInt() : Len(0) {}
  12. BigInt (const BigInt &V) : Len(V.Len)
  13. {
  14. memcpy (Data, V.Data, Len*sizeof*Data);
  15. }
  16. BigInt(int V) : Len(0)
  17. {
  18. for(; V>0; V/=Base) Data[Len++]=V%Base;
  19. }
  20. BigInt &operator=(const BigInt &V)
  21. {
  22. Len=V.Len;
  23. memcpy(Data, V.Data, Len*sizeof*Data);
  24. return *this;
  25. }
  26. int &operator[] (int Index)
  27. {
  28. return Data[Index];
  29. }
  30. int operator[] (int Index) const
  31. {
  32. return Data[Index];
  33. }
  34. };
  35. int compare(const BigInt &A, const BigInt &B)
  36. {
  37. if(A.Len!=B.Len) return A.Len>B.Len 1:-1;
  38. int i;
  39. for(i=A.Len-1; i>=0 && A[i]==B[i]; i--);
  40. if(i<0)return 0;
  41. return A[i]>B[i] 1:-1;
  42. }
  43. BigInt operator + (const BigInt &A,const BigInt &B)
  44. {
  45. int i,Carry(0);
  46. BigInt R;
  47. for(i=0; i0; i++)
  48. {
  49. if(i
  50. if(i
  51. R[i]=Carry%Base;
  52. Carry/=Base;
  53. }
  54. R.Len=i;
  55. return R;
  56. }
  57. BigInt operator - (const BigInt &A,const BigInt &B)
  58. {
  59. int i,Carry(0);
  60. BigInt R;
  61. R.Len=A.Len;
  62. for(i=0; i
  63. {
  64. R[i]=A[i]-Carry;
  65. if(i
  66. if(R[i]<0) Carry=1,R[i]+=Base;
  67. else Carry=0;
  68. }
  69. while(R.Len>0&&R[R.Len-1]==0) R.Len--;
  70. return R;
  71. }
  72. BigInt operator * (const BigInt &A,const BigInt &B)
  73. {
  74. int i;
  75. huge Carry(0);
  76. BigInt R;
  77. for(i=0; i0; i++)
  78. {
  79. if(i
  80. R[i]=Carry%Base;
  81. Carry/=Base;
  82. }
  83. R.Len=i;
  84. return R;
  85. }
  86. istream &operator>>(istream &In,BigInt &V)
  87. {
  88. char Ch;
  89. for(V=0; In>>Ch;)
  90. {
  91. V=V*10+(Ch-'0');
  92. if(In.peek()<=' ') break;
  93. }
  94. return In;
  95. }
  96. ostream &operator<<(ostream &Out,const BigInt &V)
  97. {
  98. int i;
  99. Out<<(V.Len==0 0:V[V.Len-1]);
  100. for(i=V.Len-2; i>=0; i--) for(int j=Base/10; j>0; j/=10) Out<
  101. return Out;
  102. }
  103. int main()
  104. {
  105. BigInt a,b;
  106. cin>>a>>b;
  107. cout<<"+"<
  108. cout<
  109. if(compare(a, b)>0) cout<<"-"<
  110. else cout<<"-"<
  111. cout<<"*"<
  112. }