bign在c++中使用实现高精度运算,,在c语言中有没有高精度运算符
1个回答
展开全部
// 无符号整数的高精度加减乘除运算
#include < stdio.h >
#include < string.h >
const int DEV = 10000 ; //定义每个整数存放大整数中四位(104)
const int MAXN = 1000 ; //最大整数长度 MAXN×4
struct BIG_INT {
int bit [ MAXN ], cnt;
bool positive ;
BIG_INT (){ cnt=0; positive = true ; memset ( bit , 0 , sizeof ( bit ) ); }
//-------------------------------------------------输入字符串,转化为大整数的表示
void Input () {
char ts[1000] ;
scanf ( "%s", ts);
int i = 0 , j = 0 , pt = 0 ;
int len = strlen (ts);
for ( i = len-1 ; i >=0 ; i -= 4 )
{
int temp = 0 ;
for ( j = ( i-3 >= 0)?i-3:0 ; j <= i; j++ )
temp = temp * 10 + ts[j] - '0';
bit[ pt++ ] = temp ;
}
cnt = pt ;
while ( cnt > 0 && bit [ cnt - 1 ] == 0 ) cnt -- ;
}
//------------------------------------------------输出大整数
void print () {
int i ;
if( cnt == 0 ) printf ( "%d\n", 0 );
else{
printf ( "%d", bit[cnt-1] );
for ( i=cnt-2 ; i >=0; i -- )
printf ( "%04d", bit );
printf ("\n") ;
}
}
//------------------------------------------------------加法
void add ( BIG_INT& fir, BIG_INT& sec ) {
cnt = 0 ;
int left = 0 ;
int pt = 0 ;
int temp = 0 ;
while ( pt < fir.cnt || pt < sec.cnt || left >0 ) {
temp = left ;
if ( pt < fir.cnt ) temp += fir.bit[pt] ;
if ( pt < sec.cnt ) temp += sec.bit[pt] ;
bit[ pt++ ] = temp%DEV ;
left = temp/DEV ;
}
cnt = pt;
}
// ----------------------------------减法,必须fir>sec !
void sub ( BIG_INT& fir, BIG_INT& sec ) {
cnt = 0 ;
int left = 0 ;
int pt = 0 ;
int temp = 0 ;
while ( pt < fir.cnt ){
temp = fir.bit[pt] - left ;
if ( pt < sec.cnt ) temp -= sec.bit[pt] ;
if( temp < 0){ temp += DEV; left = 1;}
else left = 0;
bit [pt++] = temp ;
}
cnt = pt ;
while ( bit[cnt-1] == 0 ) cnt-- ;
}
//---------------------------------乘法
void multiple ( BIG_INT& fir, BIG_INT& sec ) {
int i , j ;
cnt = 0 ;
memset ( bit , 0 ,sizeof ( bit ) );
int left = 0 ;
int temp = 0 ;
for ( i = 0 ; i < fir.cnt ; i++ )
{
left = 0 ;
for ( j = 0 ; j < sec.cnt ; j++ )
{
temp = left + fir.bit [ i ] * sec.bit [ j ] + bit [ i+j ];
bit [ i+j ] = temp % DEV ;
left = temp / DEV ;
}
while ( left > 0 )
{
temp = left + bit [ i+j ] ;
bit [ i+j ] = temp % DEV;
left = temp / DEV;
j++ ;
}
}
for ( i = MAXN - 1 ; i >= 0 ; i-- )
if ( bit [ i ] > 0) break;
cnt = i + 1 ;
}
// --------------------------------------比较fir与sec 大小
//--------------------------------------If ( fir < sec ) return true
bool smaller ( BIG_INT& fir, BIG_INT& sec){
while ( fir.cnt > 0 && fir.bit [ fir.cnt - 1 ] == 0 ) fir.cnt -- ;
while ( sec.cnt > 0 && sec.bit [ sec.cnt - 1 ] == 0 ) sec.cnt -- ;
if ( fir.cnt < sec.cnt ) return true;
else if ( fir.cnt > sec.cnt ) return false;
int i ;
for ( i = fir.cnt - 1 ; i >= 0 ; i -- ){
if ( fir.bit [ i ] < sec.bit [ i ] ) return true;
else if ( fir.bit [ i ] > sec.bit [ i ] ) return false;
}
return false;
}
// ----------------------------------------------返回 fir/sec 且fir = fir%sec
// -------------------------------------------- 供 devide 调用
int get_div ( BIG_INT& fir, BIG_INT& sec ){
int pt = 0 ;
BIG_INT tadd ;
while ( 1 ) {
if ( smaller ( fir, sec ) ) return pt ;
tadd.sub ( fir , sec );
fir = tadd ;
pt ++ ;
}
}
//-----------------------------------------------除法
void Devide ( BIG_INT& fir, BIG_INT& sec, BIG_INT& remd ) {
if( sec.cnt == 0 || ( sec.cnt == 1 && sec.bit [0] == 0 ) ) {
printf ( "Devide Error ! \n" ) ;
return ;
}
cnt = 0 ;
remd.cnt = 0;
memset ( bit , 0 , sizeof ( bit ) ) ;
memset ( remd.bit , 0 , sizeof ( remd.bit ) ) ;
int i , j ;
for ( i = fir.cnt - 1 ; i >= 0 ; i-- )
{
for ( j = remd.cnt - 1 ; j >= 0 ; j -- ) remd.bit [ j+1 ] = remd.bit [ j ] ;
remd.bit [ 0 ] = fir.bit [ i ] ;
remd.cnt ++ ;
int ret_n = get_div ( remd , sec ) ;
bit [ i ] = ret_n ;
}
for ( i = fir.cnt - 1 ; i >= 0 ; i-- )
if ( bit [ i ] > 0 ) break ;
cnt = i + 1 ;
}
};
//----------------------------------------------------调用参考
int main()
{
BIG_INT fir,sec,res;
while(1)
{
fir.Input();
sec.Input();
BIG_INT rem;
res.Devide ( fir , sec , rem );
res.print();
rem.print();
}
return 0;
}
#include < stdio.h >
#include < string.h >
const int DEV = 10000 ; //定义每个整数存放大整数中四位(104)
const int MAXN = 1000 ; //最大整数长度 MAXN×4
struct BIG_INT {
int bit [ MAXN ], cnt;
bool positive ;
BIG_INT (){ cnt=0; positive = true ; memset ( bit , 0 , sizeof ( bit ) ); }
//-------------------------------------------------输入字符串,转化为大整数的表示
void Input () {
char ts[1000] ;
scanf ( "%s", ts);
int i = 0 , j = 0 , pt = 0 ;
int len = strlen (ts);
for ( i = len-1 ; i >=0 ; i -= 4 )
{
int temp = 0 ;
for ( j = ( i-3 >= 0)?i-3:0 ; j <= i; j++ )
temp = temp * 10 + ts[j] - '0';
bit[ pt++ ] = temp ;
}
cnt = pt ;
while ( cnt > 0 && bit [ cnt - 1 ] == 0 ) cnt -- ;
}
//------------------------------------------------输出大整数
void print () {
int i ;
if( cnt == 0 ) printf ( "%d\n", 0 );
else{
printf ( "%d", bit[cnt-1] );
for ( i=cnt-2 ; i >=0; i -- )
printf ( "%04d", bit );
printf ("\n") ;
}
}
//------------------------------------------------------加法
void add ( BIG_INT& fir, BIG_INT& sec ) {
cnt = 0 ;
int left = 0 ;
int pt = 0 ;
int temp = 0 ;
while ( pt < fir.cnt || pt < sec.cnt || left >0 ) {
temp = left ;
if ( pt < fir.cnt ) temp += fir.bit[pt] ;
if ( pt < sec.cnt ) temp += sec.bit[pt] ;
bit[ pt++ ] = temp%DEV ;
left = temp/DEV ;
}
cnt = pt;
}
// ----------------------------------减法,必须fir>sec !
void sub ( BIG_INT& fir, BIG_INT& sec ) {
cnt = 0 ;
int left = 0 ;
int pt = 0 ;
int temp = 0 ;
while ( pt < fir.cnt ){
temp = fir.bit[pt] - left ;
if ( pt < sec.cnt ) temp -= sec.bit[pt] ;
if( temp < 0){ temp += DEV; left = 1;}
else left = 0;
bit [pt++] = temp ;
}
cnt = pt ;
while ( bit[cnt-1] == 0 ) cnt-- ;
}
//---------------------------------乘法
void multiple ( BIG_INT& fir, BIG_INT& sec ) {
int i , j ;
cnt = 0 ;
memset ( bit , 0 ,sizeof ( bit ) );
int left = 0 ;
int temp = 0 ;
for ( i = 0 ; i < fir.cnt ; i++ )
{
left = 0 ;
for ( j = 0 ; j < sec.cnt ; j++ )
{
temp = left + fir.bit [ i ] * sec.bit [ j ] + bit [ i+j ];
bit [ i+j ] = temp % DEV ;
left = temp / DEV ;
}
while ( left > 0 )
{
temp = left + bit [ i+j ] ;
bit [ i+j ] = temp % DEV;
left = temp / DEV;
j++ ;
}
}
for ( i = MAXN - 1 ; i >= 0 ; i-- )
if ( bit [ i ] > 0) break;
cnt = i + 1 ;
}
// --------------------------------------比较fir与sec 大小
//--------------------------------------If ( fir < sec ) return true
bool smaller ( BIG_INT& fir, BIG_INT& sec){
while ( fir.cnt > 0 && fir.bit [ fir.cnt - 1 ] == 0 ) fir.cnt -- ;
while ( sec.cnt > 0 && sec.bit [ sec.cnt - 1 ] == 0 ) sec.cnt -- ;
if ( fir.cnt < sec.cnt ) return true;
else if ( fir.cnt > sec.cnt ) return false;
int i ;
for ( i = fir.cnt - 1 ; i >= 0 ; i -- ){
if ( fir.bit [ i ] < sec.bit [ i ] ) return true;
else if ( fir.bit [ i ] > sec.bit [ i ] ) return false;
}
return false;
}
// ----------------------------------------------返回 fir/sec 且fir = fir%sec
// -------------------------------------------- 供 devide 调用
int get_div ( BIG_INT& fir, BIG_INT& sec ){
int pt = 0 ;
BIG_INT tadd ;
while ( 1 ) {
if ( smaller ( fir, sec ) ) return pt ;
tadd.sub ( fir , sec );
fir = tadd ;
pt ++ ;
}
}
//-----------------------------------------------除法
void Devide ( BIG_INT& fir, BIG_INT& sec, BIG_INT& remd ) {
if( sec.cnt == 0 || ( sec.cnt == 1 && sec.bit [0] == 0 ) ) {
printf ( "Devide Error ! \n" ) ;
return ;
}
cnt = 0 ;
remd.cnt = 0;
memset ( bit , 0 , sizeof ( bit ) ) ;
memset ( remd.bit , 0 , sizeof ( remd.bit ) ) ;
int i , j ;
for ( i = fir.cnt - 1 ; i >= 0 ; i-- )
{
for ( j = remd.cnt - 1 ; j >= 0 ; j -- ) remd.bit [ j+1 ] = remd.bit [ j ] ;
remd.bit [ 0 ] = fir.bit [ i ] ;
remd.cnt ++ ;
int ret_n = get_div ( remd , sec ) ;
bit [ i ] = ret_n ;
}
for ( i = fir.cnt - 1 ; i >= 0 ; i-- )
if ( bit [ i ] > 0 ) break ;
cnt = i + 1 ;
}
};
//----------------------------------------------------调用参考
int main()
{
BIG_INT fir,sec,res;
while(1)
{
fir.Input();
sec.Input();
BIG_INT rem;
res.Devide ( fir , sec , rem );
res.print();
rem.print();
}
return 0;
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询