[FZH] 写了个大数库,欢迎拍砖。
by Devil Wang
*
*#include<iostream>
#include<deque>
#include<string>
#include<cassert>
#include<exception>
class BigInt
{
public:
explicit BigInt(int i);
BigInt(const BigInt &other);
BigInt(const std::string &);
~BigInt();
friend BigInt operator += (BigInt &, const BigInt &);
friend BigInt operator -= (BigInt &, const BigInt &);
friend BigInt operator *= (BigInt &, int );
friend BigInt operator *= (BigInt &, const BigInt &);
friend BigInt operator +(const BigInt & , const BigInt & );
friend BigInt operator -(const BigInt & , const BigInt & );
friend BigInt operator - (const BigInt &);
friend BigInt operator *(const BigInt & , int);
friend BigInt operator *( const int ,const BigInt &);
friend BigInt operator *(const BigInt & , const BigInt & );
friend bool operator == (const BigInt &, const BigInt & );
friend bool operator > ( const BigInt &, const BigInt &);
friend bool operator < ( const BigInt &, const BigInt & );
friend bool operator >= ( const BigInt &, const BigInt &);
friend bool operator <= ( const BigInt &, const BigInt &);
BigInt & operator = (const BigInt &);
friend std::ostream & operator << ( std::ostream & out, BigInt &bg);
friend BigInt fab(const BigInt &);
friend BigInt pow ( const BigInt &,int );
protected:
void trim(){
while( 0 == dt.back() )
{
dt.pop_back();
}
}
private:
bool sign;
static const unsigned int Base;
std::deque<unsigned int> dt;
};
const unsigned int BigInt::Base =10;
BigInt::BigInt(int i=0)
{
sign= i >= 0? true: false;
i = i > 0 ? i : -i;
assert(dt.empty());
for ( ; i > 0; i /= Base)
{
dt.push_back(i%Base);
}
}
BigInt::BigInt(const BigInt &other)
{
assert(this->dt.empty());
dt=other.dt;
sign=other.sign;
}
BigInt & BigInt::operator = (const BigInt & other)
{
this->sign=other.sign;
this->dt=other.dt;
return *this;
}
BigInt::BigInt( const std::string & s)
{
try{
std::string::const_reverse_iterator it=s.rbegin();
for( ; it!=s.rend(); it++)
{
if ( * it == '-' || (* it <='9' && * it >='0'))
{
if( *it != '-')
dt.push_back((unsigned)( *it-'0' ));
else
sign=false;
}
else
throw std::exception();
}
}catch ( std::exception e)
{
std::cerr<<e.what()<<std::endl;
}
}
BigInt::~BigInt()
{
dt.clear();
}
std::ostream & operator << ( std::ostream & out, BigInt &bg)
{
if( 0 == bg.dt.size())
out<< unsigned(0);
if( !bg.sign)
out<<"-";
std::deque<unsigned int>::const_reverse_iterator rit = bg.dt.rbegin();
for( ; rit != bg.dt.rend() ; rit++ )
{
out<<*rit;
}
return out;
}
BigInt operator - (const BigInt & bg)
{
BigInt ret(bg);
ret.sign=!ret.sign;
return ret;
}
bool operator == ( const BigInt& op1 , const BigInt &op2)
{
return op1.sign==op2.sign && op1.dt== op2.dt;
}
bool operator > ( const BigInt &op1, const BigInt & op2)
{
if( op1.sign> op2.sign)
return true;
if ( op1 == op2 )
return false;
if( op1.dt.size()== op2.dt.size())
{
std::deque<unsigned>::const_reverse_iterator it,jt;
it=op1.dt.rbegin();
jt=op2.dt.rbegin();
while( *it == *jt && it != op1.dt.rend() && jt !=op2.dt.rend() )
{
it++;
jt++;
}
if( op1.sign > 0 )
return *it > *jt;
else
return *it < *jt;
}
else
{
if( op1.sign > 0 )
return op1.dt.size()>op2.dt.size()? true:false;
else
return op1.dt.size()>op2.dt.size()? false: true;
}
}
bool operator >= ( const BigInt & op1, const BigInt & op2 )
{
return op1 == op2 || op1 > op2;
}
bool operator <= ( const BigInt & op1, const BigInt & op2 )
{
return op1 == op2 || op1 < op2;
}
bool operator < ( const BigInt & op1, const BigInt & op2)
{
if( op1.sign > op2.sign)
return true;
else if( op1.sign < 0 && op2.sign <0 )
{
return fab(op1) > fab(op2) ;
}
else
return ! (op1 > op2);
}
BigInt operator += ( BigInt & op1,const BigInt & op2 )
{
if (op1.sign != op2.sign)
{
BigInt tmp(op2);
tmp=-tmp;
op1-=tmp;
return op1;
}
BigInt bg,other;
bg=fab(op1)>fab(op2)?op1:op2;
other= bg==op1? op2: op1;
unsigned int carry = 0;
std::deque<unsigned int>::iterator it;
std::deque<unsigned int>::const_iterator jt;
for( it = bg.dt.begin(),jt = other.dt.begin(); it < bg.dt.end() || jt <
other.dt.end() || carry > 0 ; )
{
if( it < bg.dt.end() && jt < other.dt.end() )
{
carry += * it + *jt ;
*it = carry % BigInt::Base;
carry /= BigInt::Base;
it ++;
jt ++;
}
else if ( it < bg.dt.end() )
{
carry += *it;
*it = carry % BigInt::Base;
carry /= BigInt::Base;
it ++;
}
else if( jt< other.dt.end())
{
carry += *jt;
bg.dt.push_back(carry % BigInt::Base);
carry /= BigInt::Base;
jt++;
}
else
{
while ( carry > 0 )
{
bg.dt.push_back(carry%BigInt::Base);
carry /= BigInt::Base;
}
}
}
op1=bg;
return op1;
}
BigInt operator + ( const BigInt & op1, const BigInt & op2 )
{
BigInt ret(op1);
ret+=op2;
return ret;
}
BigInt operator -=( BigInt & bg, const BigInt &other )
{
if ( bg.sign != other.sign )
{
BigInt tmp(other);
tmp=-tmp;
bg+=tmp;
return bg;
}
int carry=0;
BigInt op1,op2;
if ( bg.dt.size() == other.dt.size() )
{
op1 = bg.dt.back() >= other.dt.back() ? bg: other;
op2 = bg.dt.back() >= other.dt.back() ? other: bg;
}
else
{
op1 = bg.dt.size() > other.dt.size()? bg:other;
op2 = bg.dt.size() > other.dt.size()? other:bg;
}
op1.sign= (op1 == bg );
assert( op1.dt.size() >= op2.dt.size() );
std::deque<unsigned int>::iterator it;
std::deque<unsigned int>::const_iterator jt;
try
{
for( it = op1.dt.begin(),jt = op2.dt.begin(); it<
op1.dt.end()||jt<op2.dt.end()|| carry < 0; )
{
if( it < op1.dt.end() && jt<op2.dt.end() )
{
carry += (int) *it- (int)*jt;
*it= carry >=0 ? (unsigned)(carry % BigInt::Base) :(unsigned) (carry
+ BigInt::Base)% BigInt::Base;
carry = carry >= 0 ? carry/BigInt::Base: -((-carry +
BigInt::Base)/(BigInt::Base));
it++;
jt++;
}
else if ( it < op1.dt.end() )
{
carry += (int)* it;
*it= carry >=0 ?(unsigned) (carry % BigInt::Base) :(unsigned) (carry
+ BigInt::Base)% BigInt::Base;
carry = carry >= 0 ? carry/BigInt::Base: -((-carry +
BigInt::Base)/(BigInt::Base));
it++;
}
else if( jt < op2.dt.end() )
{
throw std::exception();
}
}
}catch( std::exception e )
{
std::cerr<<e.what()<<std::endl;
}
op1.trim();
bg=op1;
return bg;
}
BigInt operator -( const BigInt & op1, BigInt &op2)
{
BigInt ret(op1);
ret-=op2;
return ret;
}
BigInt operator *= ( BigInt & bg, int other)
{
bg.sign = bg.sign== (other>=0?true:false) ? true:false;
other = other>=0? other:-other;
unsigned int carry=0;
if ( 0 == other )
{
bg.dt.clear();
bg.dt.push_back(0);
return bg;
}
std::deque<unsigned int>::iterator it=bg.dt.begin();
for( ; it != bg.dt.end(); it ++ )
{
carry += *it* other;
*it= carry % BigInt::Base;
carry /= BigInt::Base;
}
while ( carry > 0 )
{
bg.dt.push_back(carry%BigInt::Base);
carry/=BigInt::Base;
}
return bg;
}
BigInt operator * ( int n, BigInt & other)
{
BigInt bg(other);
bg*=n;
return bg;
}
BigInt operator *( const int n,const BigInt & bg)
{
BigInt ret(bg);
ret*=n;
return ret;
}
BigInt operator *=(BigInt& op1, const BigInt & op2)
{
if ( op1 == BigInt(0) || op2 == BigInt(0) )
return BigInt (0);
BigInt out(0);
BigInt tmp;
int carry=0;
for( std::deque<unsigned>::const_iterator it = op2.dt.begin(); it !=
op2.dt.end();it ++,carry++ )
{
tmp= op1* (*it);
for( int i=0;i<carry; i++)
{
tmp.dt.push_front(unsigned(0));
}
out+=tmp;
}
op1=out;
op1.sign= !op1.sign ^ op2.sign;
return op1;
}
BigInt operator * ( const BigInt &op1, const BigInt & op2 )
{
BigInt ret(op1);
ret*=op2;
return ret;
}
BigInt operator * ( const BigInt & op1, int other)
{
BigInt ret(op1);
ret*=other;
return ret;
}
BigInt fab( const BigInt & bg)
{
BigInt ret(bg);
ret.sign=true;
return ret;
}
BigInt pow( const BigInt & bg, int n)
{
assert( n >= 0 );
if ( n == 0 )
return BigInt(0);
BigInt ret(bg);
for( int i=0; i < n-1 ; i++)
{
ret*=bg;
}
return ret;
}
#include<climits>
#include<ctime>
int main()
{
BigInt sum(0);
for( int i =1 ; i<=10;i++)
{
BigInt temp=pow(BigInt(i),i);
sum+=temp;
}
std::cout<<sum<<std::endl;
}
--
Thanks & Regards
Linux Developer : Devil Wang
13 years, 11 months
[FZH] #T Fwd: 对system-config-date.master-timezones.zh_CN.po的翻译更新
by Tommy He
---------- Forwarded message ----------
From: chenh <chenhuan.gt(a)gmail.com>
Date: 2010/6/5
Subject: 对system-config-date.master-timezones.zh_CN.po的翻译更新
To: fedora-trans-zh_cn(a)redhat.com
在这个文件中,有一处英文条目写错了,“Vostok Station, S Magnetic Pole”,东方站实际上是南极(或地球)的寒极,不是地磁南极。
有些中文条目也许是从繁体(正体)直接转成简体的,现在改成大陆更熟悉的名字。
对east,west这些方位词是放在地点之前还是之后,不知道有没有规范可循?
对Goose Bay、Yellowknife、panhandle neck这类词是音译好还是意译好?
对一些英文缩写要不要翻译,如CET、UCT?
谢谢!
--
trans-zh_cn mailing list
trans-zh_cn(a)lists.fedoraproject.org
https://admin.fedoraproject.org/mailman/listinfo/trans-zh_cn
--
Take a Deep Breath out of Windows
13 years, 11 months
[FZH] audacity 无法 编辑 mp3
by victor lee
如题,请各位赐教。
________________________python3.1_________________________
print('{}{}'.format('BOW ','Q'*3))
13 years, 11 months
[FZH] 关于合并pdf 的问题
by 馬小布
大家好:
最近遇到个问题,就是有两个文件1.pdf , 2.pdf ,我想把它们合并到一起,形成3.pdf ;
在网上搜索了下,发现下面的网址介绍了有个工具叫"pdftk",网站地址为:
http://www.dedoimedo.com/computers/pdftk.html
按照它介绍的,我使用yum install pdftk 进行安装,
test@xx ~: which pdftk
pdftk-1.41-22.fc12.i686
然后使用:
test@xx ~: pdftk A=1.pdf B=2.pdf output 3.pdf
出现以下的错误:
“
java.lang.ArrayIndexOutOfBoundsException: -661312968
at java.util.ArrayList.get(libgcj.so.10)
at com.lowagie.text.pdf.PdfReader.removeUnusedNode(itext-2.1.7.jar.so)
at com.lowagie.text.pdf.PdfReader.removeUnusedObjects(itext-2.1.7.jar.so)
at com.lowagie.text.pdf.PdfReader.readPdf(itext-2.1.7.jar.so)
at com.lowagie.text.pdf.PdfReader.<init>(itext-2.1.7.jar.so)
at com.lowagie.text.pdf.PdfReader.<init>(itext-2.1.7.jar.so)
Error: Unexpected Exception in open_reader()
Error: Failed to open PDF file:
4.pdf
Errors encountered. No output created.
Done. Input errors, so no output created.
Exit ”
网上查了下可能的原因是:
Pdftk 工具使用的是iText(Java-PDF的库)编译的,而这个版本仅仅支持标准的加密文件
以下是摘录的内容:
You (should) know that Pdftk is nothing more than a very old version of
iText (a Java-PDF library) compiled with GCJ and extended with some command
line functionality.
The keywords in the above statement are "VERY OLD". The iText version that
was used to create Pdftk only supported standard encryption (Alleged RC4);
it didn't support AES encryption (yet).
各位同鞋们有没什么好的建议阿?谢谢...
13 years, 11 months
[FZH] [ot] code.google的搭建平台
by peter linux
不好意思,问个与列表无关的问题,谢谢!
请问有人知道code.google.com的项目管理WEB用的是什么平台搭建的?因为感觉在google上存放重要的东东还不如在自己的服务器上放心,想自己在公司的服务器上搭建个。
code.google可以很好的实现wiki+SVN的项目代码管理,感觉那个平台很好,可以发布文章和讨论等。
--
Best Regards,
Peter Zhou
13 years, 11 months