高精度

 

本来就是那么简单的原理,就是麻烦,不好写。

不过好处就是总算把运算符重载给搞懂了。
下面是代码
高精度加法:
 
 
# include <iostream>
# include <fstream>
# include <cstdio>
# include <cstdlib>
# include <cstring>
# include <cmath>
# include <iomanip>
# include <algorithm>
# include <vector>
# include <deque>
# include <list>
# include <map>
 
 
using namespace std ;
 
typedef deque < int > type ;
 
type a , b , ans ;
 
type &operator+ ( type& , type& ) ;
void init () ;
int main ()
{
freopen ( "gjd+.in"  , "r" , stdin  ) ;
freopen ( "gjd+.out" , "w" , stdout ) ;
init () ;
type c = a + b ;
for ( int i = c.size() - 1 ; i >= 0 ; i -- )
{
cout << c[ i ] ;
}
return 0 ;
}
void init ()
{
char c ;
c = getchar () ;
while ( c != '\n' )
{
a.push_front ( c - 48 ) ;
c = getchar () ;
}
c = getchar () ;
while ( c != EOF )
{
b.push_front ( c - 48 ) ;
c = getchar () ;
}
}
type &operator+ ( type &aa , type &bb )
{
short int temp = 0 ;
ans.clear () ;
while ( ! ( aa.empty () && bb.empty () ) )
{
if ( ! aa.empty () )
{
   temp += aa.front () ;
   aa.pop_front () ;
   }
   if ( ! bb.empty () )
   {
   temp += bb.front () ;
   bb.pop_front () ;
   }
   ans.push_back ( temp % 10 ) ;
temp /= 10 ;
}
return ans ;
}
加法倒是没什么可注意的,不愧为高精度里面最简单的一个。
 
 
# include <iostream>
# include <cstdio>
# include <cstdlib>
# include <cmath>
# include <cstring>
# include <deque>
# include <algorithm>
 
 
using namespace std ;
 
typedef deque <short> type ;
type a , b , ans ;
 
void init () ;
void work () ;
type &operator- ( type& , type& ) ;
int main ()
{
freopen ( "gjd-.in" , "r" , stdin ) ;
freopen ( "gjd-.out" , "w" , stdout ) ;
init () ;
work () ;
}
void init ()
{
char c ;
c = getchar () ;
while ( c != '\n' && c != ' ' )
{
a.push_front ( c - 48 ) ;
c = getchar () ;
}
c = getchar () ;
while ( c != EOF && c != ' ' && c != '\n' )
{
b.push_front ( c - 48 ) ;
c = getchar () ;
}
return ;
}
void work ()
{
int aa = a.size () , bb = b.size () ;
bool f ;
if ( aa > bb )
{
ans = a - b ;
}
if ( aa < bb )
{
cout << '-' ;
ans = b - a ;
}
if ( aa == bb )
{
aa -- ;
bb -- ;
while ( a[ aa ] == b[ bb ] && aa != 0 )
{
aa -- ;
bb -- ;
}
if ( a[ aa ] > a[ bb ] ) ans = a - b ;
else
{
ans = b - a ;
cout << '-' ;
}
}
while ( ans.back () == 0 && ! ans.empty () )
 ans.pop_back () ;
while ( ! ans.empty () )
{
cout << ans.back () ;
ans.pop_back () ;
}
return ;
}
type &operator- ( type &aa , type &bb )
{
int temp = 0 , mid = 0 ;
while ( ! ( aa.empty () && bb.empty () ) )
{
mid = temp + aa.front () ;
aa.pop_front () ;
if ( ! bb.empty () )
{
   mid -= bb.front () ;
   bb.pop_front () ;
   }
   if ( mid < 0 )
   {
   ans.push_back ( mid + 10 ) ;
   temp = -1 ;
   }
   else
{
ans.push_back ( mid ) ;
temp = 0 ;
}
}
return ans ;
}
减法需要注意:输出时要去掉数组首位的0
无论如何temp都要归零或-1
 
 
下面就是高精度乘法
就是模拟的笔算

没什么技术含量

打一遍就是为了注意
 
# include <iostream>
# include <fstream>
# include <cstdio>
# include <cstdlib>
# include <cstring>
# include <cmath>
# include <iomanip>
# include <algorithm>
# include <vector>
# include <deque>
# include <list>
# include <map>
 
 
using namespace std ;
 
typedef deque <short> type ;
type a , b , ans ;
 
void init () ;
void work () ;
type &operator* ( type& , type& ) ;
int main ()
{
freopen ( "gjdx.in"  , "r" , stdin  ) ;
freopen ( "gjdx.out" , "w" , stdout ) ;
init () ;
work () ;
return 0 ;
}
void init ()
{
char c ;
c = getchar () ;
while ( c != '\n' && c != ' ' )
{
a.push_front ( c - 48 ) ;
c = getchar () ;
}
a.push_front ( 0 ) ;
c = getchar () ;
while ( c != EOF && c != ' ' && c != '\n' )
{
b.push_front ( c - 48 ) ;
c = getchar () ;
 }
 b.push_front ( 0 ) ;
 return ;
}
void work ()
{
ans = a * b ;
while ( ( ! ans.empty () ) && ans.back () == 0 )
ans.pop_back () ;
if ( ans.size () == 0 ) cout << 0 ;
while ( ans.size () >= 2 )
{
cout << ans.back () ;
ans.pop_back () ;
}
ans.clear () ;
return ;
}
type &operator* ( type &aa , type &bb )
{
int e , the , la = aa.size () - 1 , lb = bb.size () - 1 ;
for ( int i = 0 ; i <= la + lb ; i ++ )
ans.push_back ( 0 ) ;
for ( int i = 1 ; i <= la ; i ++ )
for ( int j = 1 ; j <= lb ; j ++ )
{
ans[ i + j - 1 ] += a[ i ] * b[ j ] ;
ans[ i + j ] += ans[ i + j - 1 ] / 10 ;
ans[ i + j - 1 ] %= 10 ;
}
return ans ;
}

 

posted on 2012-10-15 21:33  Stery  阅读(139)  评论(0编辑  收藏  举报

导航