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

第五章Biginteger的部分实现

时间:2017-07-22 15:29:42      阅读:204      评论:0      收藏:0      [点我收藏+]

标签:ret   names   back   turn   star   sign   stream   一个   span   

  1 #include <cstdio>
  2 #include <iostream>
  3 #include <vector>
  4 
  5 using namespace std;
  6 
  7 struct BigInteger
  8 {
  9     static const int BASE=1000000000;
 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                 c.s.push_back(sum);
 77                 c1=0;
 78             }
 79             else if(i>=s.size() && i<b.s.size())
 80             {
 81                 int sum=b.s[i]+c1;
 82                 c.s.push_back(sum);
 83                 c1=0;
 84             }
 85             else
 86                 break;
 87         }
 88 
 89         return c;
 90     }
 91 
 92     bool operator<(const BigInteger& b)
 93     {
 94         if(s.size()!=b.s.size()) return s.size()<b.s.size();
 95 
 96         for(unsigned int i=s.size()-1;i>=0;i--)
 97             if(s[i]!=b.s[i])
 98                 return s[i]<b.s[i];
 99 
100         return false;
101     }
102 
103     friend ostream& operator<<(ostream& out,const BigInteger& x);
104     friend istream& operator>>(istream& in,const BigInteger& x);
105 };
106 
107 ostream& operator<<(ostream& out,const BigInteger& x)
108 {
109 
110 
111     for(int i=x.s.size()-1;i>=0;i--)
112         out<<x.s[i];
113 
114          //cout<<x.s.size()<<endl;
115     return out;
116 }
117 
118 istream& operator>>(istream& in,BigInteger& x)
119 {
120     string s;
121     if(!(in>>s)) return in;
122 
123     x=s;
124     return in;
125 }
126 
127 int main()
128 {
129     BigInteger bi1;
130     BigInteger bi2;
131 
132     cin>>bi1;
133     cin>>bi2;
134 
135 
136     if(bi1<bi2)
137     {
138         cout<<bi1<<endl;
139         cout<<bi2<<endl;
140     }
141     else
142     {
143         cout<<bi2<<endl;
144         cout<<bi1<<endl;
145     }
146 
147     cout<<bi2+bi1<<endl;
148     return 0;
149 
150 }

 

第五章Biginteger的部分实现

标签:ret   names   back   turn   star   sign   stream   一个   span   

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

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