码迷,mamicode.com
首页 > 其他好文 > 详细

第五章Biginteger部分实现

时间:2017-07-22 22:35:09      阅读:235      评论:0      收藏:0      [点我收藏+]

标签:sscanf   bool   names   return   div   out   width   else   static   

  1 #include <cstdio>
  2 #include <iostream>
  3 #include <vector>
  4 
  5 using namespace std;
  6 
  7 struct BigInteger
  8 {
  9     static const int BASE=100000000;
 10     static const int WIDTH=8;
 11 
 12     vector<int> s;
 13 
 14     BigInteger(long long int num=0){ *this=num; }
 15 
 16     BigInteger operator=(long long num)
 17     {
 18         s.clear();
 19 
 20         do
 21         {
 22             s.push_back(num%BASE);
 23             num/=BASE;
 24 
 25         }while(num>0);
 26 
 27         return *this;
 28     }
 29 
 30     BigInteger operator=(const string& num)
 31     {
 32         s.clear();
 33 
 34         for(int end=num.length()-1;;)
 35         {
 36             int x,start=end-WIDTH+1;
 37 
 38             if(start>=0)
 39             {
 40                 sscanf(num.substr(start,WIDTH).c_str(),"%d",&x);
 41                 s.push_back(x);
 42                 end=start-1;
 43             }
 44             else if(end>=0)
 45             {
 46                 sscanf(num.substr(0,end+1).c_str(),"%d",&x);
 47                 s.push_back(x);
 48                 break;
 49             }
 50             else
 51                 break;
 52         }
 53 
 54         return *this;
 55     }
 56 
 57     BigInteger operator+(const BigInteger& b)
 58     {
 59         BigInteger c;
 60         c.s.clear();                 //必须先clear为空不然会有一个默认的0值在c.s 中
 61 
 62         int c1=0;
 63 
 64         for(unsigned int i=0;;i++)
 65         {
 66             if(i<s.size() && i<b.s.size())
 67             {
 68                 int sum=s[i]+b.s[i]+c1;
 69                 c1=sum/BASE;
 70                 sum%=BASE;
 71                 c.s.push_back(sum);
 72             }
 73             else if(i<s.size() && i>=b.s.size())
 74             {
 75                 int sum=s[i]+c1;
 76                 c1=sum/BASE;
 77                 sum%=BASE;
 78                 c.s.push_back(sum);
 79             }
 80             else if(i>=s.size() && i<b.s.size())
 81             {
 82                 int sum=b.s[i]+c1;
 83                 c1=sum/BASE;
 84                 sum%=BASE;
 85                 c.s.push_back(sum);
 86             }
 87             else if(i>=s.size() && i>=b.s.size())
 88             {
 89                 if(c1==1)
 90                     c.s.push_back(c1);
 91                 else
 92                     break;
 93             }
 94                 
 95         }
 96 
 97         return c;
 98             
 99     }
100 
101     bool operator<(const BigInteger& b)
102     {
103         if(s.size()!=b.s.size()) return s.size()<b.s.size();
104 
105         for(unsigned int i=s.size()-1;i>=0;i--)
106             if(s[i]!=b.s[i])
107                 return s[i]<b.s[i];
108 
109         return false;
110     }
111 
112     friend ostream& operator<<(ostream& out,const BigInteger& x);
113     friend istream& operator>>(istream& in,const BigInteger& x);
114 };
115 
116 ostream& operator<<(ostream& out,const BigInteger& x)
117 {
118 
119 
120     for(int i=x.s.size()-1;i>=0;i--)
121     {
122         if(x.s[i]==0)
123         {
124             for(int j=1;j<=x.WIDTH;j++)
125                 out<<0;
126         }
127         else
128             out<<x.s[i];
129     }
130 
131          //cout<<x.s.size()<<endl;
132     return out;
133 }
134 
135 istream& operator>>(istream& in,BigInteger& x)
136 {
137     string s;
138     if(!(in>>s)) return in;
139 
140     x=s;
141     return in;
142 }
143 
144 int main()
145 {
146     BigInteger bi1;
147     BigInteger bi2;
148 
149     cin>>bi1;
150     cin>>bi2;
151 
152 
153     if(bi1<bi2)
154     {
155         cout<<bi1<<endl;
156         cout<<bi2<<endl;
157     }
158     else
159     {
160         cout<<bi2<<endl;
161         cout<<bi1<<endl;
162     }
163 
164     cout<<bi2+bi1<<endl;
165     return 0;
166 
167 }

 

第五章Biginteger部分实现

标签:sscanf   bool   names   return   div   out   width   else   static   

原文地址:http://www.cnblogs.com/tclan126/p/7222792.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!