写了幂函数,除法没想好怎么做

This commit is contained in:
hinomori_minori
2026-11-07 04:10:29 +09:00
parent 5d9f828fae
commit 0e3c85ac25
+41
View File
@@ -16,6 +16,8 @@ struct fn {
struct bn denominator; struct bn denominator;
}; };
#define DECIMAL_PLACES 1024
#define BN_ZERO (struct bn){NULL,0} #define BN_ZERO (struct bn){NULL,0}
bool bn_bigger(struct bn _bn,unsigned int _i){ bool bn_bigger(struct bn _bn,unsigned int _i){
@@ -221,6 +223,45 @@ struct bn bn_muti_bn(struct bn _bn1,struct bn _bn2,struct bn* output,unsigned lo
} }
} }
void bn_copy(struct bn* _des,struct bn _src){
if(_des->_p==NULL){
_des->_p = malloc(_src._i*sizeof(unsigned int));
_des->_i=_src._i;
}else{
unsigned int* _np = realloc(_des->_p,_src._i*sizeof(unsigned int));
if(_np!=NULL){
_des->_p=_np;
_des->_i=_src._i;
return;
}
}
memcpy(_des->_p,_src._p,_des->_i*sizeof(unsigned int));
return;
}
struct bn bn_power(struct bn* _bn,unsigned long index){
if(index<0){
return BN_ZERO;
}
if(index==0){
struct bn res = nn2bn(1);
bn_copy(_bn,res);
free(res._p);
return nn2bn(1);
}
if(index==1){
return *_bn;
}
struct bn res = BN_ZERO,copy = BN_ZERO;
bn_copy(&res,*_bn);bn_copy(&copy,*_bn);
for(int i=0;i<index-1;i++){
bn_muti_bn(*_bn,copy,&res,0);
bn_copy(_bn,res);
}
free(res._p);free(copy._p);
return *_bn;
}
struct bn nn2bn(unsigned long _i){ struct bn nn2bn(unsigned long _i){
if(_i>UINT32_MAX){ if(_i>UINT32_MAX){
unsigned int* _p=malloc(2*sizeof(int)); unsigned int* _p=malloc(2*sizeof(int));