高精度代码

前方高能!

本文使用C++语言。

本文涉及

C++ Standard Template Library

小学 竖式加减乘除求余 比大小

 加油!

高精度是OI历程中初级阶段必不可少的算法之一。

高精度算法也是一种模拟算法。

当然以下代码实现的是无限精度代码。

只要您的计算机存储得下,它就可以完成计算。

如果有同学试验一下代码发现错误,请在下方评论区留言,并指明测试数据,以便笔者修改。

感谢感谢!

学习学习! 

总头文件

 1 #ifndef HPC_DEF
 2 #define HPC_DEF 1
 3 
 4 #include<typeinfo>
 5 #include<cstdio>
 6 #include<string>
 7 #include<iostream>
 8 #include<cstdlib>
 9 #include<vector>
10 
11 #include"bits/judge_typename.h"
12 #include"bits/list_of_hpc_file.h"
13 #include"bits/hpc_main.h"
14 #include"bits/hpc_func_let.h"
15 #include"bits/hpc_func_relational.h"
16 #include"bits/hpc_func_arithmetic.h"
17 #include"bits/hpc_func_io.h"
18 
19 #endif
View Code

 judge_typename.h

 1 #ifndef _JUDGE_TYPENAME_
 2 #define _JUDGE_TYPENAME_ 1
 3 namespace tool{
 4 #define EQUAL_TYPENAME(x,y) (typeid(x)==typeid(y))
 5   template<typename __Tp>
 6     inline bool is_integer(const __Tp& expr)
 7     {
 8         return
 9 #ifdef _INT128_DEFINED
10             EQUAL_TYPENAME(__Tp,__int128)|| 
11 #endif               
12             EQUAL_TYPENAME(__Tp,short) || 
13             EQUAL_TYPENAME(__Tp,int) || 
14             EQUAL_TYPENAME(__Tp,long) || 
15             EQUAL_TYPENAME(__Tp,long long) ||
16 #if __cplusplus>=201103L
17             EQUAL_TYPENAME(__Tp,char16_t) ||
18             EQUAL_TYPENAME(__Tp,char32_t) ||
19 #endif/*cplusplus*/
20             EQUAL_TYPENAME(__Tp,char) ||
21             EQUAL_TYPENAME(__Tp,wchar_t);
22     }
23   template<typename __Tp>
24     inline bool is_decimal(const __Tp& expr)
25     {
26         return
27 #ifdef _FLOAT128_DEFINED
28             EQUAL_TYPENAME(__Tp,__float128)|| 
29 #endif               
30             EQUAL_TYPENAME(__Tp,float) || 
31             EQUAL_TYPENAME(__Tp,double) || 
32             EQUAL_TYPENAME(__Tp,long double);
33     }
34   template<typename __Tp>
35     inline bool is_boolean(const __Tp& expr)
36     {
37         return 
38             EQUAL_TYPENAME(__Tp,bool);
39     }
40   template<typename __Tp>
41     inline bool is_character(const __Tp& expr)
42     {
43         return
44 #if __cplusplus>=201103L
45             EQUAL_TYPENAME(__Tp,char16_t) ||
46             EQUAL_TYPENAME(__Tp,char32_t) ||
47 #endif/*cplusplus*/
48             EQUAL_TYPENAME(__Tp,char) ||
49             EQUAL_TYPENAME(__Tp,wchar_t);
50     }
51 }
52 #endif/*_JUDGE_TYPENAME_*/
View Code

list_of_hpc_file.h

  1 #ifndef _LIST_OF_HPC_FILE_
  2 #define    _LIST_OF_HPC_FILE_ 1
  3 namespace tool{
  4     struct integer_list;
  5     struct decimal_list;
  6     struct bit_of_dec{
  7         signed char data;
  8         bit_of_dec(){data=(signed char)(0);}
  9         ~bit_of_dec(){}
 10         
 11         operator char()
 12         {
 13             return data;
 14         }
 15         
 16         bit_of_dec(const bit_of_dec&other)
 17         {
 18             data=other.data;
 19             return;
 20         }
 21         
 22         bit_of_dec&operator=(const bit_of_dec&other)
 23         {
 24             data=other.data;
 25             return *this;
 26         }
 27         
 28         friend bit_of_dec operator+(bit_of_dec t,const bit_of_dec&other)
 29         {
 30             t.data+=other.data;
 31             return t;
 32         }
 33         
 34         friend bit_of_dec operator-(bit_of_dec t,const bit_of_dec&other)
 35         {
 36             t.data-=other.data;
 37             return t;
 38         }
 39         
 40         friend bit_of_dec operator*(bit_of_dec t,const bit_of_dec&other)
 41         {
 42             t.data*=other.data;
 43             return t;
 44         }
 45         
 46         friend bit_of_dec operator/(bit_of_dec t,const bit_of_dec&other)
 47         {
 48             t.data/=other.data;
 49             return t;
 50         }
 51         
 52         friend bit_of_dec operator%(bit_of_dec t,const bit_of_dec&other)
 53         {
 54             t.data%=other.data;
 55             return t;
 56         }
 57         
 58         bit_of_dec&operator+=(const bit_of_dec&other)
 59         {
 60             data+=other.data;
 61             return *this;
 62         }
 63         
 64         bit_of_dec&operator-=(const bit_of_dec&other)
 65         {
 66             data-=other.data;
 67             return *this;
 68         }
 69         
 70         bit_of_dec&operator*=(const bit_of_dec&other)
 71         {
 72             data*=other.data;
 73             return *this;
 74         }
 75         
 76         bit_of_dec&operator/=(const bit_of_dec&other)
 77         {
 78             data/=other.data;
 79             return *this;
 80         }
 81         
 82         bit_of_dec&operator%=(const bit_of_dec&other)
 83         {
 84             data%=other.data;
 85             return *this;
 86         }
 87         
 88         friend bool operator<(bit_of_dec t,const bit_of_dec&other)
 89         {
 90             return t.data<other.data;
 91         }
 92         
 93         friend bool operator==(bit_of_dec t,const bit_of_dec&other)
 94         {
 95             return t.data==other.data;
 96         }
 97         
 98         friend bool operator>(bit_of_dec t,const bit_of_dec&other)
 99         {
100             return t.data>other.data;
101         }
102         
103         friend bool operator<=(bit_of_dec t,const bit_of_dec&other)
104         {
105             return t.data<=other.data;
106         }
107         
108         friend bool operator>=(bit_of_dec t,const bit_of_dec&other)
109         {
110             return t.data>=other.data;
111         }
112         
113         friend bool operator!=(bit_of_dec t,const bit_of_dec&other)
114         {
115             return t.data!=other.data;
116         }
117         
118         
119         template<typename __Tp>
120         bit_of_dec(const __Tp&other)
121         {
122             data=other;
123             return;
124         }
125         template<typename __Tp>
126         bit_of_dec&operator=(const __Tp&other)
127         {
128             data=other;
129             return *this;
130         }
131         template<typename __Tp>
132         friend bit_of_dec operator+(bit_of_dec t,const __Tp&other)
133         {
134             static unsigned char c;
135             c=other;
136             t.data+=c;
137             return t;
138         }
139         template<typename __Tp>
140         friend bit_of_dec operator-(bit_of_dec t,const __Tp&other)
141         {
142             static unsigned char c;
143             c=other;
144             t.data-=c;
145             return t;
146         }
147         template<typename __Tp>
148         friend bit_of_dec operator*(bit_of_dec t,const __Tp&other)
149         {
150             static unsigned char c;
151             c=other;
152             t.data*=c;
153             return t;
154         }
155         template<typename __Tp>
156         friend bit_of_dec operator/(bit_of_dec t,const __Tp&other)
157         {
158             static unsigned char c;
159             c=other;
160             t.data/=c;
161             return t;
162         }
163         template<typename __Tp>
164         friend bit_of_dec operator%(bit_of_dec t,const __Tp&other)
165         {
166             static unsigned char c;
167             c=other;
168             t.data%=c;
169             return t;
170         }
171         template<typename __Tp>
172         bit_of_dec&operator+=(const __Tp&other)
173         {
174             data+=other;
175             return *this;
176         }
177         template<typename __Tp>
178         bit_of_dec&operator-=(const __Tp&other)
179         {
180             data-=other;
181             return *this;
182         }
183         template<typename __Tp>
184         bit_of_dec&operator*=(const __Tp&other)
185         {
186             data*=other;
187             return *this;
188         }
189         template<typename __Tp>
190         bit_of_dec&operator/=(const __Tp&other)
191         {
192             data/=other;
193             return *this;
194         }
195         template<typename __Tp>
196         bit_of_dec&operator%=(const __Tp&other)
197         {
198             data%=other;
199             return *this;
200         }
201         template<typename __Tp>
202         friend bool operator<(bit_of_dec t,const __Tp&other)
203         {
204             return t.data<other;
205         }
206         template<typename __Tp>
207         friend bool operator==(bit_of_dec t,const __Tp&other)
208         {
209             return t.data==other;
210         }
211         template<typename __Tp>
212         friend bool operator>(bit_of_dec t,const __Tp&other)
213         {
214             return t.data>other;
215         }
216         template<typename __Tp>
217         friend bool operator<=(bit_of_dec t,const __Tp&other)
218         {
219             return t.data<=other;
220         }
221         template<typename __Tp>
222         friend bool operator>=(bit_of_dec t,const __Tp&other)
223         {
224             return t.data>=other;
225         }
226         template<typename __Tp>
227         friend bool operator!=(bit_of_dec t,const __Tp&other)
228         {
229             return t.data!=other;
230         }
231     };
232     struct integer_list
233     {
234         friend struct decimal_list;
235         
236         typedef bit_of_dec                         bit_type;
237         typedef std::vector<bit_type>             list;
238         typedef typename list::reference        reference;
239         typedef typename list::const_reference    const_reference;
240         typedef typename list::difference_type    ctrl_type;
241         typedef integer_list                    self;
242         
243         list l;
244         
245         integer_list(){}
246         ~integer_list(){}
247         
248         void clear()
249         {
250             l.clear();
251         }
252         self&operator=(const self&other)
253         {
254             l=other.l;
255             return *this;
256         }
257         self&operator=(const decimal_list&other);
258         reference operator[](const ctrl_type&subsize)
259         {
260             return l[subsize];
261         }
262         const_reference operator[](const ctrl_type&subsize)const
263         {
264             return l[subsize];
265         }
266         ctrl_type minnum()const
267         {
268             return 0;
269         }
270         ctrl_type maxnum()const
271         {
272             return l.size()-1;
273         }
274         
275         void resize(const ctrl_type&max_subsize)
276         {
277             l.resize(max_subsize+1);
278         }
279         reference build(const ctrl_type&subsize)
280         {
281             if(subsize>maxnum())l.resize(subsize+1);
282             return l[subsize];
283         }
284     };
285     struct decimal_list
286     {
287         friend struct integer_list;
288         
289         typedef bit_of_dec                         bit_type;
290         typedef std::vector<bit_type>             list;
291         typedef typename list::reference        reference;
292         typedef typename list::const_reference    const_reference;
293         typedef typename list::difference_type    ctrl_type;
294         typedef decimal_list                    self;
295         
296         list front,back;
297         
298         decimal_list(){}
299         ~decimal_list(){}
300         
301         void clear()
302         {
303             front.clear();
304             back.clear();
305         }
306         self&operator=(const self&other)
307         {
308             front=other.front;
309             back=other.back;
310         }
311         self&operator=(const integer_list&other);
312 
313         reference operator[](const ctrl_type&subsize)
314         {
315             return subsize>=0?back[subsize]:front[-subsize-1];
316         }
317         const_reference operator[](const ctrl_type&subsize)const
318         {
319             return subsize>=0?back[subsize]:front[-subsize-1];
320         }
321         ctrl_type minnum()const
322         {
323             return -1*front.size();
324         }
325         ctrl_type maxnum()const
326         {
327             return back.size()-1;
328         }
329         
330         void resize(const ctrl_type&max_or_min_subsize)
331         {
332             if(max_or_min_subsize>0)back.resize(max_or_min_subsize+1);
333             else if(max_or_min_subsize==0)back.clear(),front.clear();
334             else front.resize(-max_or_min_subsize);
335         }
336         reference build(const ctrl_type&subsize)
337         {
338             if(subsize>=0)
339             {
340                 if(subsize>maxnum())back.resize(subsize+1);
341                 return back[subsize];
342             }
343             else
344             {
345                 if(subsize<minnum())front.resize(-subsize);
346                 return front[-subsize-1];
347             }
348         }
349     };
350     integer_list& integer_list::operator=(const decimal_list&other)
351         {l=other.back;return *this;}        
352     decimal_list& decimal_list::operator=(const integer_list&other) 
353         {back=other.l;front.clear();return *this;}
354 }
355 #endif
View Code

hpc_main.h

  1 #ifndef _HPC_
  2 #define _HPC_ 1
  3 //namespace tool
  4 #define str_num_base(X) #X
  5 #define str_num(X) str_num_base(X)
  6 namespace tool{
  7     class integer;
  8     class decimal;
  9     class integer:private integer_list
 10     {
 11         private:
 12             friend class decimal;
 13             integer_list l;
 14             bool sign;    
 15             void clear()
 16             {
 17                 sign=false;
 18                 l.clear();
 19             }
 20             void del_zero()
 21             {
 22                 while(l.l.back()==0&&l.l.size()>0)l.l.pop_back();
 23                 if(l.l.size()==0)sign=false;
 24             }
 25             integer abs()const
 26             {
 27                 static integer res;
 28                 res=*this;
 29                 res.sign=false;
 30                 return res;
 31             }
 32             integer oppo()const
 33             {
 34                 static integer res;
 35                 res=*this;
 36                 res.sign=!res.sign;
 37                 return res;
 38             }
 39             void move(ctrl_type num)//->+INF
 40             {
 41                 if(num==0)return ;
 42                 ctrl_type smax=l.maxnum(),smin=l.minnum(); 
 43                 if(num>0||num+l.maxnum()>=0)l.build(num+l.maxnum());
 44                 else if(num+l.maxnum()<0)
 45                 {
 46                     l.clear();
 47                     return;
 48                 }
 49                 if(num>0)
 50                     for(ctrl_type i=smax;i>=smin;--i)l.build(num+i)=l.build(i),l.build(i)=0;
 51                 else if(num<0)
 52                     for(ctrl_type i=-num;i<=smax;++i)l.build(i+num)=l.build(i),l.build(i)=0;
 53                 if(num>0)l.resize(num+smax);
 54                 del_zero();
 55             }
 56             void get()
 57             {
 58                 std::string buffer;
 59                 std::cin>>buffer;
 60                 operator=(buffer);
 61             }
 62             void put()const
 63             {
 64                 if(l.maxnum()<0)putchar('0');
 65                 if(sign)putchar('-');
 66                 for(integer_list::ctrl_type i=l.maxnum();i>=0;i--)putchar(l[i]+48);
 67                 return;
 68             }
 69         public:
 70             //let operator
 71             integer&operator=(const integer&other);
 72             integer&operator=(const decimal&other);
 73             integer&operator=(const std::string& str);
 74             integer&operator=(const std::wstring& str);
 75             integer&operator=(const char* str);
 76             integer&operator=(const wchar_t* str);
 77           template<typename __Tp>
 78             integer&operator=(__Tp expr);        
 79             //construct,copy_struct,destruct
 80           template<typename __Tp>
 81             integer(const __Tp&expr){operator=(expr);}
 82             integer(){sign=false;}
 83             ~integer(){} 
 84             //relational operator
 85             friend bool operator<(const integer&a,const integer&b);
 86             friend bool operator==(const integer&a,const integer&b);
 87             friend bool operator>(const integer&a,const integer&b);
 88             friend bool operator<=(const integer&a,const integer&b);
 89             friend bool operator>=(const integer&a,const integer&b);
 90             friend bool operator!=(const integer&a,const integer&b);
 91             //arithmetic operator
 92             friend integer operator+(const integer&a,const integer&b);
 93             friend integer operator-(const integer&a,const integer&b);
 94             friend integer operator*(const integer&a,const integer&b);
 95             friend integer operator/(const integer&a,const integer&b);
 96             friend integer operator%(const integer&a,const integer&b);
 97             friend integer operator<<(const integer&t,const ctrl_type&s);
 98             friend integer operator>>(const integer&t,const ctrl_type&s);
 99             
100             integer& operator+=(const integer&b);
101             integer& operator-=(const integer&b);
102             integer& operator*=(const integer&b);
103             integer& operator/=(const integer&b);
104             integer& operator%=(const integer&b);
105             integer& operator<<=(const ctrl_type&b);
106             integer& operator>>=(const ctrl_type&b);
107             
108             integer&operator++();
109             integer operator++(int);
110             integer&operator--();
111             integer operator--(int);
112             
113             integer operator+()const;
114             integer operator-()const; 
115             //io operator
116             friend std::istream&operator>>(std::istream&,integer&);
117             friend std::ostream&operator<<(std::ostream&,const integer&);
118     };
119     class decimal:private decimal_list
120     {
121         private:
122             friend class integer;
123             decimal_list l;
124             bool sign;
125             void clear()
126             {
127                 sign=false;
128                 l.clear();
129             }
130             void del_zero()
131             {
132                 while(l.back.size()>0&&l.back.back()==0)l.back.pop_back();
133                 while(l.front.size()>0&&l.front.back()==0)l.front.pop_back();
134                 if(l.back.empty()&&l.front.empty())sign=false;
135             }
136             decimal abs()const
137             {
138                 static decimal res;
139                 res=*this;
140                 res.sign=false;
141                 return res;
142             }
143             decimal oppo()const
144             {
145                 static decimal res;
146                 res=*this;
147                 res.sign=!res.sign;
148                 return res;
149             }
150             void move(ctrl_type num)//->+INF
151             {
152                 ctrl_type smax=l.maxnum(),smin=l.minnum(); 
153                 l.build(num+l.maxnum());
154                 l.build(num+l.minnum());
155                 if(num>0)
156                     for(ctrl_type i=smax;i>=smin;--i)l.build(num+i)=l.build(i),l.build(i)=0;
157                 else if(num<0)
158                     for(ctrl_type i=smin;i<=smax;++i)l.build(i+num)=l.build(i),l.build(i)=0;
159                 
160                 if(num>0)l.resize(num+smax);
161                 if(num<0)l.resize(num+smin);
162                 del_zero();
163             }
164             void get()
165             {
166                 std::string buffer;
167                 std::cin>>buffer;
168                 operator=(buffer);
169                 return;
170             }
171             void put()const
172             {
173                 if(sign)putchar('-');
174                 if(l.maxnum()<0)
175                 {
176                     putchar('0');
177                 }
178                 for(decimal_list::ctrl_type i=l.maxnum();i>=0;i--)putchar(l[i]+48);
179                 if(l.minnum()<0)putchar('.');
180                 for(decimal_list::ctrl_type i=-1;i>=l.minnum();i--)putchar(l[i]+48);
181                 return;
182             }
183         public:
184             //let operator
185             decimal&operator=(const integer&other);
186             decimal&operator=(const decimal&other);
187             decimal&operator=(const std::string& str);
188             decimal&operator=(const std::wstring& str);
189             decimal&operator=(const char* str);
190             decimal&operator=(const wchar_t* str);
191           template<typename __Tp>
192             decimal&operator=(__Tp expr);
193             //construct,copy_struct,destruct            
194           template<typename __Tp>
195               decimal(const __Tp&expr){operator=(expr);}
196             decimal(){sign=false;}
197             ~decimal(){}
198             //relational operator
199             friend bool operator<(const decimal&a,const decimal&b);
200             friend bool operator==(const decimal&a,const decimal&b);
201             friend bool operator>(const decimal&a,const decimal&b);
202             friend bool operator<=(const decimal&a,const decimal&b);
203             friend bool operator>=(const decimal&a,const decimal&b);
204             friend bool operator!=(const decimal&a,const decimal&b);
205             //arithmetic operator
206             friend decimal operator+(const decimal&a,const decimal&b);
207             friend decimal operator-(const decimal&a,const decimal&b);
208             friend decimal operator*(const decimal&a,const decimal&b);
209             friend decimal operator/(const decimal&a,const decimal&b);
210             friend decimal operator<<(const decimal&t,const ctrl_type&s);
211             friend decimal operator>>(const decimal&t,const ctrl_type&s);
212             
213             decimal& operator+=(const decimal&b);
214             decimal& operator-=(const decimal&b);
215             decimal& operator*=(const decimal&b);
216             decimal& operator/=(const decimal&b);
217             decimal& operator<<=(const ctrl_type&b);
218             decimal& operator>>=(const ctrl_type&b);
219             
220             decimal&operator++();
221             decimal operator++(int);
222             decimal&operator--();
223             decimal operator--(int);
224             
225             decimal operator+()const;
226             decimal operator-()const;
227             //io operator
228             friend std::istream&operator>>(std::istream&,decimal&);
229             friend std::ostream&operator<<(std::ostream&,const decimal&);
230     };
231 }
232 #endif
View Code

hpc_func_let.h

  1 #ifndef _HPC_FUNC_LET_
  2 #define _HPC_FUNC_LET_ 1
  3 #ifndef __terminate__
  4 #define __terminate__(X) (printf(X),exit(-1))
  5 #endif
  6 namespace tool{
  7 //function
  8     integer& integer::operator=(const integer&other)
  9         {l=other.l;sign=other.sign;return *this;}
 10     integer& integer::operator=(const decimal&other)
 11         {l=other.l;sign=other.sign;return *this;}
 12     decimal& decimal::operator=(const integer&other)
 13         {l=other.l;sign=other.sign;return *this;}
 14     decimal& decimal::operator=(const decimal&other)
 15         {l=other.l;sign=other.sign;return *this;}
 16     integer& integer::operator=(const std::string& str)
 17     {
 18         clear();
 19         ctrl_type i=0;
 20         std::vector<unsigned char>vec;
 21         while(str[i]==' '&&i!=str.size())++i;
 22         if(i==str.size())__terminate__("No data!");
 23         if(str.size()>1&&(str[i]=='+'||str[i]=='-'))sign=(str[i]=='+')?0:1,++i;
 24         else if(str.size()==1&&(str[i]=='+'||str[i]=='-'))
 25         {
 26             clear();
 27             __terminate__("Data error!");
 28         }
 29         while(i<str.size())
 30         {
 31             while(str[i]==' ')++i;
 32             if(str[i]>='0'&&str[i]<='9')vec.push_back(str[i]-'0');
 33             else if(str[i]=='.')
 34             {
 35                 ++i;
 36                 bool err=true;
 37                 while(i<str.size())
 38                 {
 39                     if(str[i]==' '||(str[i]>='0'&&str[i]<='9'))++i,err=(str[i]==' '?err:false);
 40                     else 
 41                     {
 42                         clear();
 43                         __terminate__("Data error!");
 44                     }
 45                 }
 46                 if(err)
 47                 {
 48                     clear();
 49                     __terminate__("Data error!");
 50                 }
 51             }
 52             else
 53             {
 54                 clear();
 55                 __terminate__("Data error!");
 56             }
 57             ++i;
 58         }
 59         for(i=0;i!=vec.size();++i)l.build(vec.size()-1-i)=vec[i];
 60         del_zero();
 61         return *this;
 62     }
 63     integer& integer::operator=(const std::wstring& str)
 64     {
 65         clear();
 66         ctrl_type i=0;
 67         std::vector<unsigned char>vec;
 68         while(str[i]==L' '&&i!=str.size())++i;
 69         if(i==str.size())__terminate__("No data!");
 70         if(str.size()>1&&(str[i]==L'+'||str[i]==L'-'))sign=(str[i]==L'+')?0:1,++i;
 71         else if(str.size()==1&&(str[i]==L'+'||str[i]==L'-'))
 72         {
 73             clear();
 74             __terminate__("Data error!");
 75         }
 76         while(i<str.size())
 77         {
 78             while(str[i]==L' ')++i;
 79             if(str[i]>=L'0'&&str[i]<=L'9')vec.push_back(str[i]-L'0');
 80             else if(str[i]==L'.')
 81             {
 82                 ++i;
 83                 bool err=true;
 84                 while(i<str.size())
 85                 {
 86                     if(str[i]==L' '||(str[i]>=L'0'&&str[i]<=L'9'))++i,err=(str[i]==L' '?err:false);
 87                     else 
 88                     {
 89                         clear();
 90                         __terminate__("Data error!");
 91                     }
 92                 }
 93                 if(err)
 94                 {
 95                     clear();
 96                     __terminate__("Data error!");
 97                 }
 98             }
 99             else
100             {
101                 clear();
102                 __terminate__("Data error!");
103             }
104             ++i;
105         }
106         for(i=0;i!=vec.size();++i)l.build(vec.size()-1-i)=vec[i];
107         del_zero();
108         return *this;
109     }
110     decimal& decimal::operator=(const std::string& str)
111     {
112         clear();
113         ctrl_type i=0;
114         std::vector<unsigned char>vec;
115         while(str[i]==' '&&i!=str.size())++i;
116         if(i==str.size())__terminate__("No data!");
117         if(str.size()>1&&(str[i]=='+'||str[i]=='-'))sign=(str[i]=='+')?0:1,++i;
118         else if(str.size()==1&&(str[i]=='+'||str[i]=='-'))
119         {
120             clear();
121             __terminate__("Data error!");
122         }
123         while(i!=str.size())
124         {
125             while(str[i]==' ')++i;
126             if(str[i]>='0'&&str[i]<='9')vec.push_back(str[i]-'0');
127             else if(str[i]=='.')
128             {
129                 ++i;
130                 break;
131             }
132             else
133             {
134                 clear();
135                 __terminate__("Data error!");
136             }
137             ++i;
138         }
139         while(i!=str.size())
140         {
141             while(str[i]==' ')++i;
142             if(str[i]>='0'&&str[i]<='9')l.build(l.minnum()-1)=str[i]-'0';
143             else{
144                 clear();
145                 __terminate__("Data error!");
146             }
147             ++i;
148         }
149         for(i=0;i!=vec.size();++i)l.build(vec.size()-1-i)=vec[i];
150         del_zero();
151         return *this;
152     }
153     decimal& decimal::operator=(const std::wstring& str)
154     {
155         clear();
156         ctrl_type i=0;
157         std::vector<unsigned char>vec;
158         while(str[i]==L' '&&i!=str.size())++i;
159         if(i==str.size())__terminate__("No data!");
160         if(str.size()>1&&(str[i]==L'+'||str[i]==L'-'))sign=(str[i]==L'+')?0:1,++i;
161         else if(str.size()==1&&(str[i]==L'+'||str[i]==L'-'))
162         {
163             clear();
164             __terminate__("Data error!");
165         }
166         while(i!=str.size())
167         {
168             while(str[i]==L' ')++i;
169             if(str[i]>=L'0'&&str[i]<=L'9')vec.push_back(str[i]-L'0');
170             else if(str[i]==L'.')
171             {
172                 ++i;
173                 break;
174             }
175             else
176             {
177                 clear();
178                 __terminate__("Data error!");
179             }
180             ++i;
181         }
182         while(i!=str.size())
183         {
184             while(str[i]==' ')++i;
185             if(str[i]>=L'0'&&str[i]<=L'9')l.build(l.minnum()-1)=str[i]-L'0';
186             else{
187                 clear();
188                 __terminate__("Data error!");
189             }
190             ++i;
191         }
192         for(i=0;i!=vec.size();++i)l.build(vec.size()-1-i)=vec[i];
193         del_zero();
194         return *this;
195     }
196     integer& integer::operator=(const char* str)
197         {return operator=(std::string(str));}
198     integer& integer::operator=(const wchar_t* str)
199         {return operator=(std::wstring(str));}
200     decimal& decimal::operator=(const char* str)
201         {return operator=(std::string(str));}
202     decimal& decimal::operator=(const wchar_t* str)
203         {return operator=(std::wstring(str));}
204   template<typename __Tp>
205     integer& integer::operator=(__Tp expr)
206     {
207         clear();
208         if(is_character(expr))
209         {
210             l.build(0)=expr-48;
211         }
212         else if(is_integer(expr))
213         {
214             if(expr<0)expr=-expr,sign=1;
215             unsigned long long temp=expr;
216             if(temp==0)l.build(0)=0;
217             while(temp)
218             {
219                 l.build(l.maxnum()+1)=temp%10;
220                 temp/=10;
221             }
222         }
223         else if(is_decimal(expr))
224         {
225             if(expr<0)expr=-expr,sign=1;
226             unsigned long long temp=expr;
227             if(temp==0)l.build(0)=0;
228             while(temp)
229             {
230                 l.build(l.maxnum()+1)=temp%10;
231                 temp/=10;
232             }
233         }
234         else if(is_boolean(expr))
235         {
236             l.build(0)=expr==false?0:1;
237         }
238         del_zero();
239         return *this;
240     }
241   template<typename __Tp>
242     decimal& decimal::operator=(__Tp expr)
243     {
244         clear();
245         if(is_character(expr))
246         {
247             l.build(0)=expr-48;
248         }
249         else if(is_integer(expr))
250         {
251             if(expr<0)expr=-expr,sign=1;
252             unsigned long long temp=expr;
253             if(temp==0)l.build(0)=0;
254             while(temp)
255             {
256                 l.build(l.maxnum()+1)=temp%10;
257                 temp/=10;
258             }
259         }
260         else if(is_decimal(expr))
261         {
262             if(expr<0)expr=-expr,sign=1;
263             unsigned long long temp=expr;
264             if(temp==0)l.build(0)=0;
265             expr-=temp;
266             while(temp)
267             {
268                 l.build(l.maxnum()+1)=temp%10;
269                 temp/=10;
270             }
271             while(expr)
272             {
273                 expr*=10;
274                 temp=expr;
275                 l.build(l.minnum()-1)=temp;
276                 expr-=temp;
277             }
278         }
279         else if(is_boolean(expr))
280         {
281             l.build(0)=expr==false?0:1;
282         }
283         del_zero();
284         return *this;
285     }
286 }
287 #endif
View Code

hpc_func_relational.h

  1 #ifndef _HPC_FUNC_RELATIONAL_
  2 #define _FPC_FUNC_RELATIONAL_
  3 #ifndef __terminate__
  4 #define __terminate__(X) (printf(X),exit(-1))
  5 #endif
  6 namespace tool{
  7     bool operator<(const integer&a,const integer&b)
  8     {
  9         if(a.sign==0&&b.sign==1)return false;
 10         else if(a.sign==1&&b.sign==0)return true;
 11         else if(a.sign==0&&b.sign==0)
 12         {
 13             if(a.l.maxnum()>b.l.maxnum())return false;
 14             else if(a.l.maxnum()<b.l.maxnum())return true;
 15             for(integer::ctrl_type i=a.l.maxnum();i>=0;--i)
 16             {
 17                 if(a.l[i]<b.l[i])return true;
 18                 else if(a.l[i]>b.l[i])return false;
 19             }
 20             return false;
 21         }
 22         else {
 23             if(a.l.maxnum()>b.l.maxnum())return true;
 24             else if(a.l.maxnum()<b.l.maxnum())return false;
 25             for(integer::ctrl_type i=a.l.maxnum();i>=0;--i)
 26             {
 27                 if(a.l[i]<b.l[i])return false;
 28                 else if(a.l[i]>b.l[i])return true;
 29             }
 30             return false;
 31         }
 32     }
 33     bool operator==(const integer&a,const integer&b)
 34     {
 35         return a.sign==b.sign&&a.l.l==b.l.l;
 36     } 
 37     bool operator>(const integer&a,const integer&b)
 38     {
 39         return b<a;
 40     }
 41     bool operator<=(const integer&a,const integer&b)
 42     {
 43         return !(b<a);
 44     }
 45     bool operator>=(const integer&a,const integer&b)
 46     {
 47         return !(a<b);
 48     }
 49     bool operator!=(const integer&a,const integer&b)
 50     {
 51         return !(a==b);
 52     }
 53     
 54       bool operator<(const decimal&a,const decimal&b)
 55       {
 56         if(a.sign==0&&b.sign==1)return false;
 57         else if(a.sign==1&&b.sign==0)return true;
 58         else if(a.sign==0&&b.sign==0)
 59         {
 60             decimal::ctrl_type mini=
 61                 a.l.minnum()>b.l.minnum()?a.l.minnum():b.l.minnum();
 62             if(a.l.maxnum()>b.l.maxnum())return false;
 63             else if(a.l.maxnum()<b.l.maxnum())return true;
 64             for(decimal::ctrl_type i=a.l.maxnum();i>=0;--i)
 65             {
 66                 if(a.l[i]<b.l[i])return true;
 67                 else if(a.l[i]>b.l[i])return false;
 68             }
 69             for(decimal::ctrl_type i=-1;i>=mini;--i)
 70             {
 71                 if(a.l[i]<b.l[i])return true;
 72                 else if(a.l[i]>b.l[i])return false;
 73             }
 74             if(a.l.minnum()<mini)return false;
 75             if(b.l.minnum()<mini)return true;
 76             return false;
 77         }
 78         else {
 79             decimal::ctrl_type mini=
 80                 a.l.minnum()>b.l.minnum()?a.l.minnum():b.l.minnum();
 81             if(a.l.maxnum()>b.l.maxnum())return true;
 82             else if(a.l.maxnum()<b.l.maxnum())return false;
 83             for(decimal::ctrl_type i=a.l.maxnum();i>=0;--i)
 84             {
 85                 if(a.l[i]<b.l[i])return false;
 86                 else if(a.l[i]>b.l[i])return true;
 87             }
 88             for(decimal::ctrl_type i=-1;i>=mini;--i)
 89             {
 90                 if(a.l[i]<b.l[i])return false;
 91                 else if(a.l[i]>b.l[i])return true;
 92             }
 93             if(a.l.minnum()<mini)return true;
 94             if(b.l.minnum()<mini)return false;
 95             return false;
 96         }
 97     }
 98     bool operator==(const decimal&a,const decimal&b)
 99     {
100         return a.sign==b.sign&&a.l.front==b.l.front&&a.l.back==b.l.back;
101     }
102     bool operator>(const decimal&a,const decimal&b)
103     {
104         return b<a;
105     }
106     bool operator<=(const decimal&a,const decimal&b)
107     {
108         return !(b<a);
109     }
110     bool operator>=(const decimal&a,const decimal&b)
111     {
112         return !(a<b);
113     }
114     bool operator!=(const decimal&a,const decimal&b)
115     {
116         return !(a==b);
117     }
118 #define OTHER_RELAT(SELF,OP)\
119   template<typename __Tp>\
120       bool operator OP(const SELF&self,const __Tp&other)\
121       {\
122           static SELF temp;\
123           temp=other;\
124           return self OP temp;\
125     }
126     OTHER_RELAT(integer,<)
127     OTHER_RELAT(integer,==)
128     OTHER_RELAT(integer,>)
129     OTHER_RELAT(integer,<=)
130     OTHER_RELAT(integer,>=)
131     OTHER_RELAT(integer,!=)
132     
133     OTHER_RELAT(decimal,<)
134     OTHER_RELAT(decimal,==)
135     OTHER_RELAT(decimal,>)
136     OTHER_RELAT(decimal,<=)
137     OTHER_RELAT(decimal,>=)
138     OTHER_RELAT(decimal,!=)
139 #undef OTHER_RELAT
140 }
141 #endif 
View Code

hpc_func_arithmetic.h

  1 #ifndef _HPC_FUNC_ARITHMETIC_
  2 #define _HPC_FUNC_ARITHMETIC_ 1
  3 #ifndef __terminate__
  4 #define __terminate__(X) (printf(X),exit(-1))
  5 #endif
  6 namespace tool{
  7     std::ptrdiff_t max_dec_digit=6;
  8     integer operator+(const integer&a,const integer&b)
  9     {
 10         static integer res;
 11         res=0;
 12         if(a.sign==b.sign)
 13         {
 14             res.sign=a.sign;
 15             
 16             integer::ctrl_type stop_num=
 17                 a.l.maxnum()>b.l.maxnum()?b.l.maxnum():a.l.maxnum();
 18             res.l.build((a.l.maxnum()<b.l.maxnum()?b.l.maxnum():a.l.maxnum())+1);
 19             for(integer::ctrl_type i=0;i<=stop_num;++i)
 20             {
 21                 res.l[i]+=a.l[i]+b.l[i];
 22                 res.l[i+1]=res.l[i]/10;
 23                 res.l[i]%=10;
 24             }
 25             if(a.l.maxnum()>stop_num)
 26             {
 27                 res.build(a.l.maxnum()+1);
 28                 for(integer::ctrl_type i=stop_num+1;i<=a.l.maxnum();++i)
 29                 {
 30                     res.l[i]+=a.l[i];
 31                     res.l[i+1]=res.l[i]/10;
 32                     res.l[i]%=10;
 33                 }
 34             }
 35             if(b.l.maxnum()>stop_num)
 36             {
 37                 res.build(b.l.maxnum()+1);
 38                 for(integer::ctrl_type i=stop_num+1;i<=b.l.maxnum();++i)
 39                 {
 40                     res.l[i]+=b.l[i];
 41                     res.l[i+1]=res.l[i]/10;
 42                     res.l[i]%=10;
 43                 }
 44             }
 45             res.del_zero();
 46             return res;
 47         }
 48         else{
 49             if(a.sign)return b-a.abs();
 50             else return a-b.abs();
 51         }
 52     }
 53     integer operator-(const integer&a,const integer&b)
 54     {
 55         static integer res;
 56         res=0;
 57         if(a.sign==b.sign)
 58         {
 59             signed int buf=0; 
 60             if(a.abs()>b.abs())
 61             {
 62                 res.sign=a.sign;
 63                 integer::ctrl_type stop_num=b.l.maxnum();
 64                 res.l.build(stop_num+1);
 65                 for(integer::ctrl_type i=0;i<=stop_num;++i)
 66                 {
 67                     res.l[i]+=a.l[i]-b.l[i]+buf,buf=0;
 68                     if(res.l[i]<0)
 69                     buf-=1,res.l[i]+=10;
 70                 }
 71                 res.build(a.l.maxnum()+1);
 72                 for(integer::ctrl_type i=stop_num+1;i<=a.l.maxnum();++i)
 73                 {
 74                     res.l.build(i)+=a.l[i]+buf,buf=0;
 75                     if(res.l[i]<0)
 76                     buf-=1,res.l[i]+=10;
 77                 }
 78             }
 79             else 
 80             {
 81                 res.sign=!a.sign;
 82                 integer::ctrl_type stop_num=a.l.maxnum();
 83                 res.l.build(stop_num+1);
 84                 for(integer::ctrl_type i=0;i<=stop_num;++i)
 85                 {
 86                     res.l[i]+=b.l[i]-a.l[i]+buf,buf=0;
 87                     if(res.l[i]<0)
 88                     buf-=1,res.l[i]+=10;
 89                 }
 90                 res.build(b.l.maxnum()+1);
 91                 for(integer::ctrl_type i=stop_num+1;i<=b.l.maxnum();++i)
 92                 {
 93                     res.l.build(i)+=b.l[i]+buf,buf=0;
 94                     if(res.l[i]<0)
 95                     buf-=1,res.l[i]+=10;
 96                 }
 97             }
 98             res.del_zero();
 99             return res;
100         }
101         else{
102             if(a.sign)return (a.abs()+b).oppo();
103             else return (b.abs()+a);
104         }
105     } 
106     integer operator*(const integer&a,const integer&b)
107     {
108         static integer res;
109         res=0;
110         res.sign=a.sign==b.sign?0:1;
111         res.l.build(a.maxnum()+b.maxnum()+1);
112         for(integer::ctrl_type i=0;i<=a.l.maxnum();++i)
113         {
114             for(integer::ctrl_type j=0;j<=b.l.maxnum();++j)
115             {
116                 res.l.build(i+j)+=a.l[i]*b.l[j];
117                 res.l.build(i+j+1)+=res.l[i+j]/10;
118                 res.l[i+j]%=10;
119             }
120         }
121         res.del_zero();
122         return res;
123     }
124     integer operator/(const integer&a,const integer&b)
125     {
126         static integer tempa;
127         static integer tempb;
128         static integer res;
129         res=0;
130         tempa=a;
131         tempb=b;
132         tempa.sign=false;
133         tempb.sign=false;
134         if(tempb==0)__terminate__("Divided by zero!");
135         if(tempa==0)return res;
136         if(tempa<tempb)return res;
137         res.sign=a.sign==b.sign?0:1;
138         integer::ctrl_type subsize=a.l.maxnum()-b.l.maxnum();
139         res.l.build(subsize);
140         tempb.move(tempa.l.maxnum()-tempb.l.maxnum());
141         while(!(tempa<b.abs()))
142         {    
143             while(tempa>=tempb)tempa=tempa-tempb,res.l.build(subsize)=res.l.build(subsize)+1;
144             --subsize;
145             tempb.move(-1);
146         }
147         res.del_zero();
148         return res;
149     }
150     integer operator%(const integer&a,const integer&b)
151     {
152         static integer res;
153         res=a-a/b*b;
154         return res;
155     }
156     
157     decimal operator+(const decimal&a,const decimal&b)
158     {
159         static decimal res;
160         res=0;
161         if(a.sign==b.sign)
162         {
163             res.sign=a.sign;
164             decimal::ctrl_type stop_num=
165                 a.l.minnum()>b.l.minnum()?a.l.minnum():b.l.minnum();
166             if(a.l.minnum()<b.l.minnum())
167             {
168                 int buf=0;
169                 res.l.build(a.l.minnum());
170                 for(decimal::ctrl_type i=a.l.minnum();i<b.l.minnum();++i)
171                 res.l.build(i)=a.l[i];
172                 for(decimal::ctrl_type i=b.l.minnum();i<0;++i)
173                 {
174                     res.l.build(i)=a.l[i]+b.l[i]+buf;
175                     buf=res.l.build(i)/10;
176                     res.l.build(i)%=10;
177                 }
178                 res.l.build(0)=buf; 
179             }
180             else {
181                 int buf=0;
182                 res.l.build(b.l.minnum());
183                 for(decimal::ctrl_type i=b.l.minnum();i<a.l.minnum();++i)
184                 res.l.build(i)=b.l[i];
185                 for(decimal::ctrl_type i=a.l.minnum();i<0;++i)
186                 {
187                     res.l.build(i)=a.l[i]+b.l[i]+buf;
188                     buf=res.l.build(i)/10;
189                     res.l.build(i)%=10;
190                 }
191                 res.l.build(0)=buf; 
192             }
193             stop_num=a.l.maxnum()>b.l.maxnum()?b.l.maxnum():a.l.maxnum();
194             res.l.build((a.l.maxnum()<b.l.maxnum()?b.l.maxnum():a.l.maxnum())+1);
195             for(decimal::ctrl_type i=0;i<=stop_num;++i)
196             {
197                 res.l[i]+=a.l[i]+b.l[i];
198                 res.l[i+1]=res.l[i]/10;
199                 res.l[i]%=10;
200             }
201             if(a.l.maxnum()>stop_num)
202             {
203                 res.build(a.l.maxnum()+1);
204                 for(decimal::ctrl_type i=stop_num+1;i<=a.l.maxnum();++i)
205                 {
206                     res.l[i]+=a.l[i];
207                     res.l[i+1]=res.l[i]/10;
208                     res.l[i]%=10;
209                 }
210             }
211             if(b.l.maxnum()>stop_num)
212             {
213                 res.build(b.l.maxnum()+1);
214                 for(decimal::ctrl_type i=stop_num+1;i<=b.l.maxnum();++i)
215                 {
216                     res.l[i]+=b.l[i];
217                     res.l[i+1]=res.l[i]/10;
218                     res.l[i]%=10;
219                 }
220             }
221             res.del_zero();
222             return res;
223         }
224         else{
225             if(a.sign)return b-a.abs();
226             else return a-b.abs();
227         }
228     }
229     decimal operator-(const decimal&a,const decimal&b)
230     {
231         static decimal res;
232         res=0;
233         if(a.sign==b.sign)
234         {
235             signed int buf=0; 
236             if(a.abs()>b.abs())
237             {
238                 res.sign=a.sign;
239                 decimal::ctrl_type stop_num=a.l.minnum()>b.l.minnum()?a.l.minnum():b.l.minnum();
240                 res.l.build(a.l.minnum()<b.l.minnum()?a.l.minnum():b.l.minnum()); 
241                 if(b.l.minnum()<a.l.minnum())
242                 {
243                     for(decimal::ctrl_type i=b.l.minnum();i<a.l.minnum();++i)
244                     {
245                         res.l[i]=res.l[i]-b.l[i]+buf,buf=0;
246                         if(res.l[i]<0)
247                         buf-=1,res.l[i]+=10;
248                     }
249                     res.l.build(a.l.minnum())=buf;
250                     buf=0;
251                 }
252                 else
253                 {
254                     for(decimal::ctrl_type i=a.l.minnum();i<b.l.minnum();++i)
255                     {
256                         res.l[i]=res.l[i]+a.l[i]+buf,buf=0;
257                         if(res.l[i]<0)
258                         buf-=1,res.l[i]+=10;
259                     }
260                     res.l.build(b.l.minnum())=buf;
261                     buf=0;
262                 }
263                 for(decimal::ctrl_type i=stop_num;i<0;++i)
264                 {
265                     res.l[i]+=a.l[i]-b.l[i]+buf,buf=0;
266                     if(res.l[i]<0)
267                     buf-=1,res.l[i]+=10;
268                 }
269                 res.l.build(0)+=buf;
270                 buf=0; 
271                 stop_num=b.l.maxnum();
272                 res.l.build(stop_num+1);
273                 for(decimal::ctrl_type i=0;i<=stop_num;++i)
274                 {
275                     res.l[i]+=a.l[i]-b.l[i]+buf,buf=0;
276                     if(res.l[i]<0)
277                     buf-=1,res.l[i]+=10;
278                 }
279                 res.build(a.l.maxnum()+1);
280                 for(decimal::ctrl_type i=stop_num+1;i<=a.l.maxnum();++i)
281                 {
282                     res.l.build(i)+=a.l[i]+buf,buf=0;
283                     if(res.l[i]<0)
284                     buf-=1,res.l[i]+=10;
285                 }
286             }
287             else 
288             {
289                 res.sign=!a.sign;
290                 decimal::ctrl_type stop_num=a.l.minnum()>b.l.minnum()?a.l.minnum():b.l.minnum();
291                 res.l.build(a.l.minnum()<b.l.minnum()?a.l.minnum():b.l.minnum()); 
292                 if(b.l.minnum()<a.l.minnum())
293                 {
294                     for(decimal::ctrl_type i=b.l.minnum();i<a.l.minnum();++i)
295                     {
296                         res.l[i]=res.l[i]-a.l[i]+buf,buf=0;
297                         if(res.l[i]<0)
298                         buf-=1,res.l[i]+=10;
299                     }
300                     res.l.build(a.l.minnum())=buf;
301                     buf=0;
302                 }
303                 else
304                 {
305                     for(decimal::ctrl_type i=a.l.minnum();i<b.l.minnum();++i)
306                     {
307                         res.l[i]=res.l[i]+b.l[i]+buf,buf=0;
308                         if(res.l[i]<0)
309                         buf-=1,res.l[i]+=10;
310                     }
311                     res.l.build(b.l.minnum())=buf;
312                     buf=0;
313                 }
314                 for(decimal::ctrl_type i=stop_num;i<0;++i)
315                 {
316                     res.l[i]+=b.l[i]-a.l[i]+buf,buf=0;
317                     if(res.l[i]<0)
318                     buf-=1,res.l[i]+=10;
319                 }
320                 res.l.build(0)+=buf;
321                 buf=0; 
322                 stop_num=a.l.maxnum();
323                 res.l.build(stop_num+1);
324                 for(decimal::ctrl_type i=0;i<=stop_num;++i)
325                 {
326                     res.l[i]+=b.l[i]-a.l[i]+buf,buf=0;
327                     if(res.l[i]<0)
328                     buf-=1,res.l[i]+=10;
329                 }
330                 res.build(b.l.maxnum()+1);
331                 for(decimal::ctrl_type i=stop_num+1;i<=b.l.maxnum();++i)
332                 {
333                     res.l.build(i)+=b.l[i]+buf,buf=0;
334                     if(res.l[i]<0)
335                     buf-=1,res.l[i]+=10;
336                 }
337             }
338             res.del_zero();
339             return res;
340         }
341         else{
342             if(a.sign)return (a.abs()+b).oppo();
343             else return (b.abs()+a);
344         }
345     }
346     decimal operator*(const decimal&a,const decimal&b)
347     {
348         static decimal res;
349         res=0;
350         res.sign=a.sign==b.sign?0:1;
351         res.l.build(a.maxnum()+b.maxnum()+1);
352         res.l.build(a.minnum()+b.minnum());
353         for(decimal::ctrl_type i=a.l.minnum();i<=a.l.maxnum();++i)
354         {
355             for(decimal::ctrl_type j=b.l.minnum();j<=b.l.maxnum();++j)
356             {
357                 res.l.build(i+j)+=a.l[i]*b.l[j];
358                 res.l.build(i+j+1)+=res.l[i+j]/10;
359                 res.l[i+j]%=10;
360             }
361         }
362         res.del_zero();
363         return res;
364     } 
365     decimal operator/(const decimal&a,const decimal&b)
366     {
367         static decimal tempa;
368         static decimal tempb;
369         static decimal res;
370         res=0;
371         tempa=a;
372         tempb=b;
373         tempa.sign=false;
374         tempb.sign=false;
375         if(tempb==0)__terminate__("Divided by zero!");
376         if(tempa==0)return res;
377         res.sign=a.sign==b.sign?0:1;
378         decimal::ctrl_type subsize=a.l.maxnum()-b.l.maxnum();
379         res.l.build(subsize);
380         res.l.build(-max_dec_digit-1);
381         tempb.move(tempa.l.maxnum()-tempb.l.maxnum());
382         while(subsize>=-max_dec_digit)
383         {    
384             if(tempa==0)break;
385             while(tempa>=tempb)tempa=tempa-tempb,
386             res.l.build(subsize)=res.l.build(subsize)+1;
387             --subsize;
388             tempb.move(-1);
389         }
390         res.del_zero();
391         return res;
392     }
393 #define OTHER_ARITH(SELF,OP)\
394   template<typename __Tp>\
395       SELF operator OP(const SELF&self,const __Tp&other)\
396       {\
397           static SELF temp;\
398           temp=other;\
399           return self OP temp;\
400     }
401     OTHER_ARITH(integer,+)
402     OTHER_ARITH(integer,-)
403     OTHER_ARITH(integer,*)
404     OTHER_ARITH(integer,/)
405     OTHER_ARITH(integer,%)
406     
407     OTHER_ARITH(decimal,+)
408     OTHER_ARITH(decimal,-)
409     OTHER_ARITH(decimal,*)
410     OTHER_ARITH(decimal,/)
411 #define ARITH_TYPE(SELF,OPE,OPC)\
412       SELF& SELF::operator OPE(const SELF&other)\
413       {\
414           *this=*this OPC other;\
415           return *this;\
416     }
417     ARITH_TYPE(integer,+=,+)
418     ARITH_TYPE(integer,-=,-)
419     ARITH_TYPE(integer,*=,*)
420     ARITH_TYPE(integer,/=,/)
421     ARITH_TYPE(integer,%=,%)
422     
423     ARITH_TYPE(decimal,+=,+)
424     ARITH_TYPE(decimal,-=,-)
425     ARITH_TYPE(decimal,*=,*)
426     ARITH_TYPE(decimal,/=,/)
427 #define OTHER_ARITH_TYPE(SELF,OPE)\
428   template<typename __Tp>\
429     SELF& operator OPE(SELF&t,const __Tp&other)\
430       {\
431           t OPE other;\
432           return t;\
433     }
434     OTHER_ARITH_TYPE(integer,+=)
435     OTHER_ARITH_TYPE(integer,-=)
436     OTHER_ARITH_TYPE(integer,*=)
437     OTHER_ARITH_TYPE(integer,/=)
438     OTHER_ARITH_TYPE(integer,%=)
439     
440     OTHER_ARITH_TYPE(decimal,+=)
441     OTHER_ARITH_TYPE(decimal,-=)
442     OTHER_ARITH_TYPE(decimal,*=)
443     OTHER_ARITH_TYPE(decimal,/=)
444 #define ADD_SUB(SELF)\
445     SELF& SELF::operator ++()\
446       {\
447           *this=*this+1;\
448           return *this;\
449     }\
450     SELF SELF::operator ++(int)\
451       {\
452           static SELF res;\
453           res=*this;\
454           ++*this;\
455         return res;\
456     }\
457     SELF& SELF::operator --()\
458       {\
459           *this=*this-1;\
460           return *this;\
461     }\
462     SELF SELF::operator --(int)\
463       {\
464           static SELF res;\
465           res=*this;\
466           --*this;\
467         return res;\
468     }
469     ADD_SUB(integer)
470     ADD_SUB(decimal)
471 #define POSI_NEGA(SELF)\
472     SELF SELF::operator+()const{return *this;}\
473     SELF SELF::operator-()const{return this->oppo();}
474     POSI_NEGA(integer)
475     POSI_NEGA(decimal)
476 #define ROTATE(SELF)\
477     SELF operator>>(const SELF&t,const std::ptrdiff_t&s)\
478     {\
479         static SELF res;\
480         res=t; \
481         res.move(-s);\
482         return res;\
483     }\
484     SELF operator<<(const SELF&t,const std::ptrdiff_t&s)\
485     {\
486         static SELF res;\
487         res=t; \
488         res.move(s);\
489         return res;\
490     }
491     ROTATE(integer)
492     ROTATE(decimal)
493 #define OTHER_ROTATE(SELF)\
494     SELF& SELF::operator>>=(const std::ptrdiff_t&s)\
495     {\
496         move(-s);\
497         return *this;\
498     }\
499     SELF& SELF::operator<<=(const std::ptrdiff_t&s)\
500     {\
501         move(s);\
502         return *this;\
503     }
504     OTHER_ROTATE(integer)
505     OTHER_ROTATE(decimal)
506 #undef OTHER_ARITH
507 #undef OTHER_ARITH_TYPE
508 #undef ARITH_TYPE
509 #undef ADD_SUB
510 #undef POSI_NEGA
511 #undef ROTATE
512 #undef OTHER_ROTATE
513 }
514 #endif
View Code

hpc_func_io.h

 1 #ifndef __HPC_IO
 2 #define __HPC_IO
 3 namespace tool{
 4     std::istream&operator>>(std::istream&s,integer&t)
 5     {
 6         t.get();
 7         return s;
 8     }
 9     std::ostream&operator<<(std::ostream&s,const integer&t)
10     {
11         t.put();
12         return s;
13     }
14     std::istream&operator>>(std::istream&s,decimal&t)
15     {
16         t.get();
17         return s;
18     }
19     std::ostream&operator<<(std::ostream&s,const decimal&t)
20     {
21         t.put();
22         return s;
23     }
24 }
25 #endif
View Code

以上为代码

The End

猜你喜欢

转载自www.cnblogs.com/ColinWang-OIer/p/10457848.html
今日推荐