免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12
最近访问板块 发新帖
楼主: pgy
打印 上一主题 下一主题

[函数] 大数阶乘的函数 [复制链接]

论坛徽章:
0
11 [报告]
发表于 2004-12-16 12:47 |只看该作者

大数阶乘的函数

看来还没有一个现成的网,比如,C/C++库大全。
如果谁遇到某类编程问题,在这个网上一找,就找到了,那不是很方便!而且,也是前人经验的一个累积!

论坛徽章:
1
程序设计版块每日发帖之星
日期:2015-11-25 06:20:00
12 [报告]
发表于 2011-08-01 13:04 |只看该作者
  1. #ifndef BIGINT_H_
  2. #define BIGINT_H_

  3. #include <iostream>
  4. using namespace std;

  5. class Error
  6. {
  7. public:
  8.     Error(char *rea):_rea(rea){}
  9.     char *what(){return _rea;}
  10. private:
  11.     char *_rea;
  12. };

  13. const int Max_Bit_Len= 1024;

  14. class bigint
  15. {
  16. public:
  17.     bigint();
  18.     bigint(int num);
  19.     bigint(const char *num);
  20.     bigint(const bigint& obj);       //复制构造函数
  21.     inline bigint operator-()const;  //取相反值
  22.     bigint operator()()const;        //取绝对值仿函数
  23.     int get_len()const{return len;}; //大数位数

  24.     bigint operator+(const bigint& obj)const;
  25.     bigint operator-(const bigint& obj)const;
  26.     bigint& operator+=(const bigint& obj);
  27.     bigint operator*(const bigint& obj)const;
  28.     bigint operator/(const bigint& obj)const;
  29.     bigint operator%(const bigint& obj)const;
  30.     bool operator>(const bigint& obj)const;
  31.     bool operator<(const bigint& obj)const;
  32.     bool operator>=(const bigint& obj)const;
  33.     bool operator==(const bigint& obj)const;

  34.     friend std::istream& operator>>(std::istream& in,bigint& obj);
  35.     friend std::ostream& operator<<(std::ostream& out,bigint& obj);

  36. private:
  37.     int len;
  38.     int sign;
  39.     int BitArray[Max_Bit_Len];

  40.     void set_len();         //位数处理
  41.     void shift(int bit);    //移位 >=0 右移 否则 左移 用于除法
  42. };

  43. #endif
复制代码
  1. #include <cstring>
  2. #include <cctype>
  3. #include <string>
  4. #include "bigint.h"

  5. bigint::bigint()
  6. {
  7.     memset(BitArray,0x0,sizeof(BitArray));
  8.     len=1;
  9.     sign=1;
  10. }
  11. bigint::bigint(int num)
  12. {
  13.     if(num < 0)
  14.     {
  15.         sign=-1;
  16.         num=-num;
  17.     }
  18.     else
  19.         sign=1;

  20.     int *ptr=BitArray;
  21.     for(; num!=0; ptr++)
  22.     {
  23.         *ptr=num%10;
  24.         num /= 10;
  25.     }
  26.     len=(int)(ptr-BitArray);
  27.     len=len?len:1;
  28.     memset(ptr,0x0,sizeof(int)*(Max_Bit_Len-len));
  29. }
  30. bigint::bigint(const bigint& obj)
  31. {
  32.     if(this!=&obj)
  33.     {
  34.         len=obj.len;
  35.         sign=obj.sign;
  36.         memcpy(BitArray,obj.BitArray,sizeof(BitArray));
  37.     }
  38. }
  39. bigint::bigint(const char *num)
  40. {
  41.     len=strlen(num);
  42.     sign=1;
  43.     if(*num=='-'||*num=='+')
  44.     {
  45.         if(*num=='-')
  46.             sign=-1;
  47.         num++;
  48.         len--;
  49.     }
  50.     for(int i=0;i<len;i++)
  51.     {
  52.         if(isdigit(*num))
  53.             BitArray[len-i-1]=*num++ -'0';
  54.         else
  55.             throw Error("Input Error!");
  56.     }
  57.     if(len==0)
  58.         len=1;
  59.     memset(BitArray+len,0x0,(Max_Bit_Len-len)*sizeof(int));
  60. }
  61. bigint bigint::operator-()const   //-a
  62. {
  63.     bigint res(*this);
  64.     res.sign=-sign;
  65.     return res;
  66. }
  67. bigint bigint::operator+(const bigint& obj)const
  68. {
  69.     bigint res;
  70.     if(sign==obj.sign)
  71.     {
  72.         res.len=0;
  73.         int k=(len>obj.len) ? len : obj.len;
  74.         for(int i=0,x=0; i<k||x; i++)
  75.         {
  76.             if(i<len)     x+=BitArray[i];
  77.             if(i<obj.len) x+=obj.BitArray[i];
  78.             //x+=BitArray[i]+obj.BitArray[i];
  79.             res.BitArray[res.len++]=x%10;
  80.             if(res.len >= Max_Bit_Len) throw Error("位数超过最大值");
  81.             x /= 10;
  82.         }
  83.         res.sign=sign;
  84.     }
  85.     else
  86.         res=(*this)-(-obj);
  87.     return res;
  88. }
  89. bigint bigint::operator()()const  //|a|
  90. {
  91.     bigint res(*this);
  92.     res.sign=1;
  93.     return res;
  94. }
  95. void bigint::set_len()
  96. {
  97.     while(len>1 && !BitArray[len-1])
  98.         len--;
  99. }
  100. bigint bigint::operator-(const bigint& obj)const
  101. {
  102.     bigint res;
  103.     if(sign==obj.sign)
  104.     {
  105.         const bigint *max,*min;
  106.         if((*this)()>obj())
  107.         {
  108.             max=this;   min=&obj;   res.sign=sign;
  109.         }
  110.         else
  111.         {
  112.             max=&obj;   min=this;   res.sign=-sign;
  113.         }
  114.         res.len=max->len;
  115.         for(int i=0;i<max->len;i++)
  116.         {
  117.             res.BitArray[i]+= max->BitArray[i]-min->BitArray[i];
  118.             if(res.BitArray[i] < 0)
  119.             {
  120.                 res.BitArray[i] += 10;
  121.                 res.BitArray[i+1]--;
  122.             }
  123.         }
  124.         res.set_len();
  125.     }
  126.     else
  127.         res=(*this)+(-obj);
  128.     return res;
  129. }
  130. bool bigint::operator>(const bigint& obj)const
  131. {
  132.     if(sign > obj.sign) return true;
  133.     if(sign < obj.sign) return false;

  134.     if(sign==1)
  135.     {
  136.         if(len!=obj.len) return len > obj.len;
  137.         for(int i=len-1;i>=0;i--)
  138.         {
  139.             if(BitArray[i]!=obj.BitArray[i])
  140.                 return BitArray[i] > obj.BitArray[i];
  141.         }
  142.     }
  143.     else
  144.     {
  145.         if(len!=obj.len) return len < obj.len;
  146.         for(int i=len-1;i>=0;i--)
  147.         {
  148.             if(BitArray[i]!=obj.BitArray[i])
  149.                 return BitArray[i] < obj.BitArray[i];
  150.         }
  151.     }
  152.     return false; //==
  153. }
  154. bool bigint::operator<(const bigint& obj)const
  155. {
  156.     return obj>*this;
  157. }
  158. bool bigint::operator>=(const bigint& obj)const
  159. {
  160.     return !(obj > *this);
  161. }
  162. bool bigint::operator==(const bigint& obj)const
  163. {
  164.     return (!(obj>*this))&&(!(*this>obj));
  165. }
  166. bigint& bigint::operator+=(const bigint& obj)
  167. {
  168.     *this=*this+obj;
  169.     return *this;
  170. }
  171. bigint bigint::operator*(const bigint& obj)const
  172. {
  173.     bigint res;
  174.     res.len=len+obj.len;
  175.     for(int i=0;i<len;i++)
  176.     {
  177.         for(int j=0;j<obj.len;j++)
  178.         {
  179.             if(i+j >= Max_Bit_Len) throw Error("位数超过最大值");
  180.             res.BitArray[i+j] += BitArray[i]*obj.BitArray[j];
  181.         }
  182.     }
  183.     for(int i=0;i<res.len-1;i++)
  184.     {
  185.         res.BitArray[i+1] += res.BitArray[i]/10;
  186.         res.BitArray[i] %= 10;
  187.     }
  188.     res.set_len();
  189.     res.sign=(sign==obj.sign) ? 1 : -1;
  190.     return res;
  191. }
  192. void bigint::shift(int bit)
  193. {
  194.     if(bit >=0)  // >=0 右移
  195.     {
  196.         for(int i=len+bit-1;i>=0;i--)
  197.             BitArray[i]=(i>=bit) ? BitArray[i-bit] : 0;
  198.     }
  199.     else        // <0  左移
  200.     {
  201.         for(int i=0;i<len;i++)
  202.             BitArray[i]=(i-bit<len) ? BitArray[i-bit] : 0;
  203.     }
  204.     len += bit;
  205. }
  206. bigint bigint::operator/(const bigint& obj)const
  207. {
  208.     if(obj==bigint(0))
  209.         throw Error("Dirisor can not be zero!");//除数不能为零
  210.     bigint did=(*this)();//取绝对值
  211.     bigint dir=obj();
  212.     if(dir > did) return bigint(0);
  213.     bigint res;
  214.     int m=len-obj.len;
  215.     dir.shift(m);       //右移m位
  216.     while(m>=0)
  217.     {
  218.         if(did >= dir)  //被除数>=除数
  219.         {
  220.             did =did- dir;
  221.             res.BitArray[m]++;
  222.         }
  223.         else
  224.             {
  225.                 m--;
  226.                 res.len++;    //???
  227.                 dir.shift(-1);//左移一位
  228.             }
  229.     }
  230.     res.set_len();
  231.     res.sign=(sign==obj.sign) ? 1 :-1;
  232.     return res;
  233. }
  234. bigint bigint::operator%(const bigint& obj)const
  235. {
  236.     bigint res;
  237.     res=(*this)-((*this)/obj)*obj;
  238.     return res;
  239. }
  240. std::istream& operator>>(std::istream& in,bigint& obj)
  241. {
  242.     string tmp;
  243.     in>>tmp;
  244.     obj=tmp.c_str();
  245.     return in;
  246. }
  247. std::ostream& operator<<(std::ostream& out,bigint& obj)
  248. {
  249.     if(obj.sign==-1)
  250.         out<<'-';
  251.     for(int i=obj.len-1;i>=0;i--)
  252.         out<<(obj.BitArray[i]);
  253.     return out;
  254. }

复制代码
  1. #include <iostream>
  2. #include "bigint.h"
  3. using namespace std;
  4. bigint test(unsigned);
  5. int main(void)
  6. {
  7.     bigint c;
  8.     /*bigint a("-11"),b("-11");
  9.     c=a/b;
  10.     cout<<c<<endl;
  11.     cout<<c.get_len()<<endl;
  12.     */
  13.     c=test(458);
  14.     cout<<c<<endl;
  15.     cout<<c.get_len()<<endl;
  16.     return 0;
  17. }
  18. bigint test(unsigned n)
  19. {
  20.     bigint res(1);
  21.     for(unsigned i=2;i<=n;i++)
  22.         res= res*i;
  23.     return res;
  24. }
复制代码

论坛徽章:
1
程序设计版块每日发帖之星
日期:2015-11-25 06:20:00
13 [报告]
发表于 2011-08-01 13:10 |只看该作者
以上为C++大数类,可以此进行阶乘

论坛徽章:
0
14 [报告]
发表于 2011-08-01 15:50 |只看该作者
大数运算,acm入门的吧。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP