BigInteger類型的解析_超詳細解析


  1 /*9876543210987654234522345
  2 214748364723453452323452345
  3 2147483647234523452323452345
  4 181760911432744962345234523
  5 2345234523434656346345634563測試數據*/
  6 #include<iostream>
  7 #include<cstdlib>
  8 #include<cstring>
  9 #include<iterator>
 10 #include<set>
 11 #include<vector>
 12 #include<map>
 13 #include<utility>
 14 #include<algorithm>
 16 using namespace std;
 17 
 18 struct BigInteger {
 19     static const int BASE = 100000000;    //靜態成員變量---屬於BigInteger這個類型的,不屬於他的結構體變量 
 20     static const int WIDTH = 8;           
 21     vector<int> s;                        //儲存大整數 
 22     
 23     BigInteger(long long num = 0) {       //構造函數(帶默認值) 
 24         *this = num;
 25     }
 26     BigInteger operator = (long long num) {//面對不超過long long的整數 
 27         s.clear();                         //清空本對象的vector容器內容--以免有上一次數據影響 
 28         //將整數里面的數八位八位的 儲存到 vector里面去 
 29         do {
 30             s.push_back(num % BASE);      //假如整數是9位--得到了后面的八位 
 31             num /= BASE;                  //繼續得到前面的位數 
 32         } while (num > 0);
 33         return *this;                     //返回本對象 
 34     }
 35     //long long類型不夠的情況 
 36     BigInteger operator = (const string& str) { //賦值運算符 
 37         s.clear(); 
 38         int x, len = (str.length() - 1) / WIDTH + 1; //有幾個八位一組(多出來的不足8位舍去) 
 39         //從右向左8位8位的儲存整數到vector<int>中 
 40         for (int i = 0; i < len; i++) {              //按一共有幾組八位儲存 
 41             int end = str.length() - i*WIDTH;        //i個八位一組--字符串長度 - 幾個八位 = 前面剩余的位數
 42             //取max(0,前面位數 - 8個長度),如果該剩余位數不為0,就取大的
 43             int start = max(0,end-WIDTH);
 44             //成為查找一組數的開始位置,如果取的是end-WIDTH,,那么end-start = WIDTH得到了一個八位,當做查找字符串的長度
 45             //sscanf()是將字符以某種形式取出存放到相應的類型中(這里是int),substr(start--開始搜尋的位置(包括),
 46             //end-start----是代表搜尋的長度),c_str()返回一個指針指向字符串相當於一個'\0'結尾的字符數組->用來終止一次 
 47             //8位的取出到x 
 48             sscanf(str.substr(start,end-start).c_str(), "%d", &x);
 49             //要注意在最高位前面的幾組8位中,如果他們的第八位為0,那么組成整數的時候就不會有八位,輸出時要注意 
 50             s.push_back(x); //將取出的整數push到vector的底部---大數是從右往左的一點點儲存進去 
 51         }
 52         return *this;
 53     }
 54     //重載 + 運算符
 55     BigInteger operator + (const BigInteger& b) const
 56     {
 57         BigInteger c;
 58         c.s.clear();                 //清空c類型中vector內的元素 
 59         for (unsigned int i = 0, g = 0; ; i++)
 60         {
 61             if (g == 0 && i >= s.size() && i >= b.s.size())//如果位數已經到了最高位,i大於本對象對象且大於b對象的大小 
 62                 break;                                       //跳出循環 
 63             int x = g;
 64             if (i < s.size()) x += s[i];      
 65             if (i < b.s.size()) x += b.s[i];
 66             c.s.push_back(x % BASE);          //八位八位的取出取出后(從右向左)
 67             g = x/BASE;                       //得到最高位向后的位數(一直到從右向左出現的第一次8位前面停止) 
 68         }
 69         return c;    
 70     } 
 71 
 72     BigInteger operator += (const BigInteger &b)
 73     {
 74         *this = *this + b;
 75         return *this;
 76     }
 77     
 78 
 79     //比較運算符
 80     bool operator < (const BigInteger &b) const 
 81     {
 82         if (s.size() != b.s.size())
 83             return s.size() < b.s.size();
 84         for (int i = s.size() - 1; i >= 0; i--)    //從后往前面比較,因為(低位在vector的前面)注:這樣做的前提是兩個 
 85         {                                          //都沒有前導0,否則不能比較 
 86             if (s[i] != b.s[i])    
 87                 return s[i] < b.s[i];
 88         }
 89         return false;                              //相等    
 90     }
 91     bool operator > (const BigInteger &b) const
 92     {
 93         return b < *this;
 94     }
 95     bool operator <= (const BigInteger &b) const
 96     {
 97         return !(b < *this);                    //相當於返回 本對象 <= b對象    
 98     }
 99     bool operator >= (const BigInteger &b) const
100     {
101         return !(*this < b);    
102     }  
103     bool operator != (const BigInteger &b) const
104     {
105         return b < *this || *this < b;
106     }
107     bool operator == (const BigInteger &b) const
108     {
109         return !(b < *this) || !(*this <  b);
110     }
111 };
112 
113 ostream& operator << (ostream &out, const BigInteger &x)
114 { 
115     out << x.s.back();            //從右向左輸出(因為最高位是最后儲存進去的)
116     //八位八位的寫 
117     for (int i = x.s.size()-2; i >= 0; i--) {  //從倒數第二位開始輸出 
118         char buf[20]; 
119         sprintf(buf, "%08d", x.s[i]);          //將整數當成字符寫到buf中輸出,不足八位的補零(因為在前面的幾組八位中
120         //如果不足八位說明在形成整數的過程中最高位為0) 
121         for (unsigned int j = 0; j < strlen(buf); j++)  //輸出八位 
122             out << buf[j];
123     }
124     return out; 
125 } 
126 
127 istream& operator >> (istream &in, BigInteger &x) {
128     string s;
129     if (! (in >> s)) 
130         return in;
131     x = s;              //輸入正確的流 
132     return in;
133 } 
134 
135 struct cmp {
136     bool operator () (const BigInteger &a, const BigInteger &b) const {    //升序比較 
137         return a < b; 
138     }
139 };
140 
141 struct cmpL{
142     bool operator () (const BigInteger &a, const BigInteger &b) const {    //降序比較 
143         return a > b;
144     }
145 };
146 int main(void)
147 {
148     BigInteger bi, sum1, mus, b2;
149     
150     set<BigInteger,cmp> s1;
151     
152     vector<BigInteger> s2;
153     
154     map<BigInteger,char,cmpL> s3; 
155     
156     pair<BigInteger,char> s4;
157     
158     string ss; 
159     int i = 1;
160     
161     /*while (cin >> b2)        //利用了重載 >> 提取運算符 
162     {
163         mus += b2;    
164     }
165     cout << mus << endl;*/
166     
167     while (cin >> ss)
168     {
169         bi = ss;
170         sum1 += bi;
171         s1.insert(bi);
172         s2.push_back(bi);
173         s4.first = bi;
174         s4.second = 'a'+i;        //用來檢驗map里面元素也是可以自動排序的
175         i++;
176         s3.insert(s4);
177
178     }
179     
180     for (set<BigInteger,cmp>::iterator it = s1.begin(); it != s1.end(); ++it) //set升序 
181         cout << *it << endl;
182 //    cout << sum1 << endl;
183     cout << endl;
184     
185     sort(s2.begin(),s2.end(),cmpL());       //sort 降序 
186     for (vector<BigInteger>::iterator it = s2.begin(); it != s2.end(); ++it)
187         cout << *it << endl;
188     
189     for (map<BigInteger,char,cmpL>::iterator it = s3.begin(); it != s3.end(); ++it) //map降序 
190         cout << (*it).second << endl;
191     return 0;
192 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM