本文整理汇总了C++中Bigint::getlength方法的典型用法代码示例。如果您正苦于以下问题:C++ Bigint::getlength方法的具体用法?C++ Bigint::getlength怎么用?C++ Bigint::getlength使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Bigint
的用法示例。
在下文中一共展示了Bigint::getlength方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: bigcmp
int bigcmp(const Bigint& num1, const Bigint& num2)
{
int nlen1 = num1.getlength(), nlen2 = num2.getlength();
if(nlen1 > nlen2) return 1;
if(nlen1 < nlen2) return -1;
return strcmp(num1.getptr(), num2.getptr());
}
示例2: ptr
Bigint operator+(const Bigint& num1,
const Bigint& num2)
{
int nlen1 = num1.getlength();
int nlen2 = num2.getlength();
const Bigint &maxint = nlen1>=nlen2?num1:num2;
const Bigint &minint = nlen1>=nlen2?num2:num1;
//申请的字节数永远比最长的数多1.但计算的时候按不进位计算
//memory leak
ptr_array<char> ptr(new char[maxint.getlength()+1+1]);
Bigint temp = ptr.getptr();
memset(temp.getptr(), 0, maxint.getlength()+1+1);
int maxindex = maxint.getlength() -1;
int minindex = minint.getlength() -1;
int nindex = strlen(maxint.getptr()) -1;
int nmax = maxint.getlength();
int nmin = minint.getlength();
bool flag = false;
for(int max = maxint.getlength(); max>0; max--)
{
temp.getindex(nindex) = (maxint.getindex(maxindex--) + (minindex>=0?minint.getindex(minindex--):'0') +flag -'0');
flag = false;
if( temp.getindex(nindex--)> '9')
{
flag = true;
temp.getindex(nindex+1) = (temp.getindex(nindex+1) % ('9'+1)) +'0';
}
}
if(flag)
{
for (int i=temp.getlength(); i>0; i--)
{
temp.getindex(i) = temp.getindex(i-1);
}
temp.getindex(0) = '1';
}
return temp;
}
示例3: ten
//right param is litter one
Bigint operator *(Bigint num1,Bigint num2)
{
#if 1
static bool s_first = 1;
if(num2.getlength() == 0|| num1.getlength() ==0||
strcmp(num2.getptr(), "0") == 0 ||
strcmp(num1.getptr(), "0") == 0)
return "0";
if (s_first && num1<num2){num1.swap(num2);}
s_first = 0;
#endif
int nlen1 = num1.getlength();
int nlen2 = num2.getlength();
//memory leak
ptr_array<char> sum = new char[nlen1+1+1];
memset(sum.getptr() , 0, nlen1+2);
int flag = 0;
int nindex = 0;
for(; nindex < nlen1; nindex++)
{
int num = (num2.getindex(nlen2-1)-'0')*(num1.getindex(nlen1 - nindex - 1)-'0') +flag;
sum[nindex] = num%10 +'0';
flag = num/10;
}
if(flag) sum[nindex] = flag +'0';
makestr(sum.getptr());
//memory leak
ptr_array<char> pleft =new char[nlen2+1];
strcpy(pleft.getptr(), num2.getptr());
pleft[num2.getlength() - 1] = 0;
// Bigint temp =ten(num1*pleft);
return (Bigint(sum.getptr()) + ten(num1*pleft.getptr()));
}