递归程序C语言源代码.docx
- 文档编号:24720968
- 上传时间:2023-05-31
- 格式:DOCX
- 页数:15
- 大小:16.32KB
递归程序C语言源代码.docx
《递归程序C语言源代码.docx》由会员分享,可在线阅读,更多相关《递归程序C语言源代码.docx(15页珍藏版)》请在冰豆网上搜索。
递归程序C语言源代码
#include"stdafx.h"
#include
#include
usingnamespacestd;
#defineMAXL250
classbig_num
{
public:
intnum[MAXL];
intlength;
boolmark;
voidinit()
{
inti;
for(i=0;i mark=false; length=1; } voidturn() { inti; inta,b,c; for(i=0;i { if(num[i]==0)break; if(num[i]%10000>0) { num[i+1]=num[i]/10000; num[i]=num[i]%10000; } } } voidleng() { inti; length=1; for(i=250;i>0;i--) { if(num[i-1]! =0){length=i;break;} } } booloperator<(big_numn) { inti; if(length if(length>n.length)returnfalse; for(i=length-1;i>=0;i--) { if(num[i]>n.num[i])returnfalse; if(num[i] } returnfalse; } booloperator<=(big_numn) { inti; if(length if(length>n.length)returnfalse; for(i=length-1;i>=0;i--) { if(num[i]>n.num[i])returnfalse; if(num[i] } returntrue; } big_numoperator*(big_numobj) { big_numother; big_numa; inti,j; intc=0; intk; boolmak; mak=false; if(*this { a=*this;*this=obj;obj=a;mak=true; } for(i=0;i for(j=0;j { c=other.num[i+j]; other.num[i+j]=(c+num[j]*obj.num[i])%10000; other.num[i+j+1]+=(c+num[j]*obj.num[i])/10000; } other.leng(); if(mak) { a=*this;*this=obj;obj=a; } if(mark==obj.mark)other.mark=false; elseother.mark=true; returnother; } big_numoperator+(big_numobj) { big_numother; inti; intc=0; intk; boolm1,m2; if(mark==obj.mark) { for(i=0;i { k=(num[i]+obj.num[i]+c)%10000; c=(num[i]+obj.num[i]+c)/10000; other.num[i]=k; } other.leng(); other.mark=mark; returnother; } else { m1=mark;m2=obj.mark; mark=false;obj.mark=false; other=*this-obj; mark=m1;obj.mark=m2; if(*this else { if(other.length==1&&other.num[0]==0)other.mark=false; elseother.mark=mark; } returnother; } } big_numoperator-(big_numobj) { big_numother,a; inti; intc=0; intk; boolmak; mak=false; if(mark==obj.mark) { if(*this { a=*this;*this=obj;obj=a;mak=true; } for(i=0;i { if(i>length&&i>obj.length&&num[i]==0&&obj.num[i]==0)break; other.num[i]=num[i]-obj.num[i]; } other.leng(); for(i=0;i { if(other.num[i]<0) { if((i+1) other.num[i+1]--; } } if(mak) { a=*this;*this=obj;obj=a; } if(*this mark; elseother.mark=mark; if(other.num[other.length-1]<0)other.num[other.length-1]=-other.num[other.length-1]; if(other.length==1&&other.num[0]==0)other.mark=false; returnother; } else { obj.mark=mark; other=*this+obj; returnother; } } big_num() { init(); } /*big_numoperator=(stringstr) { big_numn; intlen,i,mark,tem,k,j,sum; len=str.length(); if(str[0]=='-')n.mark=true; elsen.mark=false; mark=len/4; if(len>mark*4)mark++; n.length=mark; for(i=0;i { tem=1; sum=0; for(j=len-(i*4)-1;j>len-(i+1)*4-1&&j>=0+n.mark;j--) { sum+=(str[j]-'0')*tem; tem*=10; } n.num[i]=sum; } returnn; }*/ }; big_numtobignum(stringstr) { big_numn; intlen,i,mark,tem,k,j,sum; len=str.length(); if(str[0]=='-')n.mark=true; elsen.mark=false; mark=len/4; if(len>mark*4)mark++; n.length=mark; for(i=0;i { tem=1; sum=0; for(j=len-(i*4)-1;j>len-(i+1)*4-1&&j>=0+n.mark;j--) { sum+=(str[j]-'0')*tem; tem*=10; } n.num[i]=sum; } n.leng(); returnn; } stringtstring(big_numn) { stringst,str=""; inti; intm,tem; boolmark; chara1,a2,a3,a4,a5; /*if(n.length==1&&n.num[0]==0)return"0";*/ m=n.num[n.length-1]; tem=10; for(i=0;i<4;i++) { a1=m%10+'0'; m=m/10; str=a1+str; } if(n.mark)str='-'+str; for(i=n.length-2;i>=0;i--) { a1=n.num[i]/1000+'0'; a2=(n.num[i]/100)%10+'0'; a3=(n.num[i]/10)%10+'0'; a5=n.num[i]%10+'0'; str=str+a1; str+=a2;str+=a3;str+=a5; } mark=false; for(i=0;i { if(str[i]! ='0'&&! mark)mark=true; if(mark)st=st+str[i]; } returnst; } big_numdiv(big_numthiss,big_numobj) { big_numother,n1,n2; stringst1,st2,sm,st3=""; sm=""; chars; boolm1,m2,mak; intsum,i; m1=thiss.mark;m2=obj.mark; if(thiss thiss.mark=false;obj.mark=false; st1=tstring(thiss); st2=tstring(obj); n2=tobignum(st2); i=0; while (1) { if(st3=="") { st3+=st1[i]; i++; if(i>st1.length())break; } /*if(st3=="0") { st3=""; sm+='0'; st3+=st1[i]; i++; if(i>st1.length())break; } if(st3! ="") {*/ n1=tobignum(st3); /*}elsen1=tobignum("0");*/ if(n2<=n1) { sum=0; while(n2<=n1) { n1=n1-n2; sum++; } s=sum+'0'; sm+=s; st3=tstring(n1); st3=st3+st1[i]; i++; if(i>st1.length())break; } else { sm+='0'; st3=st3+st1[i]; i++; if(i>st1.length())break; } } other=tobignum(sm); if(m1==m2)other.mark=false; elseother.mark=true; other.leng(); returnother; } voidoutput(big_numn) { inti; intm; if(n.mark)cout<<'-'; cout< for(i=n.length-2;i>=0;i--) { if(n.num[i]==0){cout<<"0000";continue;} if(n.num[i]/10==0){cout<<"000"< if(n.num[i]/100==0){cout<<"00"< if(n.num[i]/1000==0){cout<<"0"< elsecout< } } big_numinput() { stringstr; big_numn; intlen,i,mark,tem,k,j,sum; cin>>str; len=str.length(); if(str[0]=='-')n.mark=true; elsen.mark=false; mark=len/4; if(len>mark*4)mark++; n.length=mark; for(i=0;i { tem=1; sum=0; for(j=len-(i*4)-1;j>len-(i+1)*4-1&&j>=0+n.mark;j--) { sum+=(str[j]-'0')*tem; tem*=10; } n.num[i]=sum; } returnn; } int_tmain(intargc,_TCHAR*argv[]) { big_numn,a,c; stringstr; while (1) { n=input(); a=input(); c=n+a; output(c); cout< c=n-a; output(c); cout< c=n*a; output(c); cout< c=div(n,a); output(c); cout< } return0; }
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 递归 程序 语言 源代码
![提示](https://static.bdocx.com/images/bang_tan.gif)