C语言大数除法

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

#includeiostream#includestdio.h#includemath.h#defineN200/*˼·ÊÇʹÓÃƽʱ´ò²Ý¸åʱµÄÔËËã¹ý³Ì*/intBigIntSub(chardata1[],chardata2[]);/*Á½¸öÊýµÄÏà¼õ*/intBigIntDiv(chardiv1[],chardiv2[],charresult[]);/*Á½¸öÊýµÄÏà³ý*/intBigIntTrim(charvalue[]);/*ÕûÀíÕâ¸öÊýµÄÇ°ÃæûÓжàÓà0*/intAddDivMub(charvalue[],charch);/*ÍùÕâ¸öÊýÀïÃæÌí¼ÓÒ»¸öλÊý£¬Ä©Î²Ìí¼Ó*/intBigIntEqual(charvalue1[],charvalue2[]);intmain(){chardiv1[N],div2[N],result[N];while(scanf(%s%s,div1,div2)==2){BigIntDiv(div1,div2,result);printf(%s\n,result);}return0;}intBigIntDiv(chardiv1[],chardiv2[],charresult[]){inti,k,m,len1,len2;charch,*remain;len1=(int)strlen(div1);len2=(int)strlen(div2);remain=(char*)malloc((len2+2)*sizeof(char));strcpy(remain,div1);remain[len2]='\0';m=0;/*Ê×ÏÈ°ÑÓàÊý²¿·ÖÉèÖÃΪºÍDIV2µÈ³¤¶ÈµÄDIV1*/for(i=len2-1;ilen1;){BigIntTrim(remain);/*ÕûÀíÓàÊý*/if(BigIntEqual(remain,div2)==-1){/*°ÑÓàÊýºÍ³ýÊý±È½Ï´óС£¬Èç¹ûÓàÊýС*/AddDivMub(remain,div1[++i]);/*¾ÍÈ¡³öDIV1ÖÐÏÂһλ·ÅÈëµ½ÓàÊýÖÐ*/result[m++]='0';/*²¢ÇÒ°ÑÉÌÌí¼ÓÒ»¸öÁã*/}else{k=0;/*·ñÔò¾Í°ÑDIV2ºÍÓàÊý½øÐбȽÏÈç¹û´óÓÚ»òµÈÓÚDIV2½øÈëÑ­»·*/while(BigIntEqual(remain,div2)=0){BigIntSub(remain,div2);/*ÓàÊýºÍDIV2Ïà¼õ*/k++;}AddDivMub(remain,div1[++i]);/*Ìí¼ÓÓàÊý*/result[m++]=k+'0';}}result[m]='\0';/*·â×°ÉÌ*/BigIntTrim(result);return0;}intBigIntTrim(charvalue[]){inti,len=strlen(value);for(i=0;value[i];i++)if(value[i]!='0')break;if(i==len)strcpy(value,0);elsestrcpy(value,&value[i]);return0;}intBigIntEqual(charvalue1[],charvalue2[]){intlen1,len2;len1=(int)strlen(value1);len2=(int)strlen(value2);if(len1len2)return1;elseif(len1len2)return-1;elsereturnstrcmp(value1,value2);}intBigIntSub(chardata1[],chardata2[]){intvalue1,value2;inti,len1,len2,flag;char*data,*sub1,*sub2;len1=(int)strlen(data1);len2=(int)strlen(data2);sub1=(char*)malloc((len1+1)*sizeof(char));sub2=(char*)malloc((len1+1)*sizeof(char));data=(char*)malloc((len1+1)*sizeof(char));for(i=0;ilen1;i++)sub2[i]='0';data[i]='\0';strcpy(sub1,data1);strcpy(sub2+(len1-len2),data2);for(i=len1-1,flag=0;i=0;i--){value1=sub1[i]-'0'+flag;value2=sub2[i]-'0';if(value1-value20){data[i]=value1+10-value2+'0';flag=-1;}else{data[i]=value1-value2+'0';flag=0;}}BigIntTrim(data);strcpy(data1,data);free(sub1);free(sub2);free(data);return0;}intAddDivMub(charvalue[],charch){intlen=(int)strlen(value);char*back;back=(char*)malloc((len+2)*sizeof(char));strcpy(back,value);back[len]=ch;back[len+1]='\0';strcpy(value,back);free(back);return0;}

1 / 3
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功