利用字符实现大数除法运算(纯c实现)
2011-07-08 23:20
323 查看
那年还是语言刚入门的我,不知纠结了几多次,才完成了能支持大数的算术四则运算,现在看起来别是一番滋味哈。
算术四则运算中就数除法的实现最为繁杂,基本思路按照除法笔算的步骤进行,计算商时一位一位的进行,当计算某一位商时则不断地与除数相减,得到的相减次数作为本位商的结果,实现代码如下,代码可读性不高,效率有待优化,英雄莫见笑。
结果:(每一行对应于main中的每一次调用div所得到的结果)
======= welcome to my HomePage(http://blog.csdn.net/zhanxinhang) to have a communication =======
算术四则运算中就数除法的实现最为繁杂,基本思路按照除法笔算的步骤进行,计算商时一位一位的进行,当计算某一位商时则不断地与除数相减,得到的相减次数作为本位商的结果,实现代码如下,代码可读性不高,效率有待优化,英雄莫见笑。
/* * author:Zhanhang * * qq:273711460 * E-mail:zhanxinhang@gmail.com * HomePage:http://blog.csdn.net/zhanxinhang */ #include<stdio.h> #include<string.h> #include<malloc.h> //清多余的零 void clz(char r[]) { if(r[0]=='-')r++; //使r指针向前移一位; int len = strlen(r); int i=0,k=0; if(len<2)return; if(r[i]=='0') { for(;r[k]=='0'&&r[k+1]!='.';k++); for(;i<len-k;i++) { r[i]=r[i+k]; } r[i]='\0'; } } //清除数可不要的小数点 void clDot(char r[]) { int len = strlen(r); if(r[len-1]=='.') r[len-1]=0; } //为除计算之用的减法 void dsub(char a[],char b[]) { int i,alen,blen; char r,t,jiewei=0; char *pb ; alen = strlen(a); blen = strlen(b); pb = (char *)calloc(alen,sizeof(char)); //使pb与a位数对齐// memset(pb,'0',alen); t = alen; for(i=blen-1;i>=0;i--) pb[--t]=b[i]; for(i=alen-1;i>=0;i--) //相减运算 { t = a[i]-jiewei; if(t<pb[i]) jiewei=1; else jiewei = 0; r = (t+jiewei*10)-pb[i]; a[i] = r+'0'; } a[alen]='\0'; clz(a); free(pb); } //比较两数的大小 int cmp(const char a[],const char b[]) { int alen = strlen(a); int blen = strlen(b); if(alen>blen) return 1; else if(alen == blen) { if(strcmp(a,b)>0) return 1; if(strcmp(a,b)==0) return 0; } else return -1; } //除法计算 void div(const char a[],const char b[],char result[]) { bool isNegative = false; char *op1,*pa,*pb,*pr; int up,alen,blen,adotp,bdotp,i,k,dotp,t,t1,j,quo_size; /////////////判定符号/////////////// //如果为异号 if((a[0] == '-'||b[0] == '-')&&a[0] != b[0]) result[0] = '-',isNegative = true; //去除负号 if(a[0] == '-')a++; if(b[0] == '-')b++; /////////////////////////////////// alen = strlen(a)-1; //减去一位小数点 blen = strlen(b)-1; ///////获取被除数小数点移位后的位置////////// adotp = strchr(a,'.')-a; bdotp = strchr(b,'.')-b; //计算商小数点位置 dotp = adotp+blen-bdotp; if(isNegative)dotp++; //////////准备数据///////////// op1 = (char *)calloc(alen+blen+1,sizeof(char)); pa = (char *)calloc(alen+blen+1,sizeof(char)); pb = (char *)calloc(blen+1,sizeof(char)); pr = (char *)calloc(alen+blen+1,sizeof(char)); for(i = 0,t=0; i<=alen; i++) { if(a[i]!='.') pa[t++] = a[i]; } for(;t<dotp-1;t++) { pa[t] = '0'; } for(;t<blen;t++) { pa[t] = '0'; } pa[t] = '\0'; for(i = 0,t=0; i<=blen; i++) { if(b[i]!='.') pb[t++] = b[i]; } pb[t] = '\0'; clz(pa); clz(pb); ////////取得被除数的高位数op1,且op1大于被除数b////////// strncpy(op1,pa,strlen(pb)); if(strcmp(op1,pb)<0) { strncpy(op1,pa,strlen(pb)+1); } /////计算////// j = k = strlen(op1); t1=0; quo_size = strlen(pa)+1-k; //获取商的长度 while(t1<quo_size) { up = 0; t = cmp(op1,pb); while(t>=0) { dsub(op1,pb); t = cmp(op1,pb); up++; } pr[t1++] = up+'0'; op1[strlen(op1)]=pa[j++]; clz(op1); } quo_size+=50;//加50精度 while(t1<quo_size&&(cmp(op1,(char *)"0")>0)) { up = 0; op1[strlen(op1)]='0'; t = cmp(op1,pb); while(t>=0) { dsub(op1,pb); t = cmp(op1,pb); up++; } pr[t1++] = up+'0'; } ////////////////////////////// if(isNegative)t=1; else t=0; for(i=0;i<=t1;i++)//复制结果并给商加上小数点 { if(t==dotp) result[t++] = '.'; result[t++]=pr[i]; } clz(result); clDot(result); free(op1); free(pa); free(pb); } main() { char result[500]={0}; div("-9999.","3.",result); printf("%s\n",result); div("10000000000000000000000000.","5.",result); printf("%s\n",result); div("12345678900.","12345678900.",result); printf("%s\n",result); div("1.","3.",result); printf("%s\n",result); div("-444.","-2.",result); printf("%s\n",result); div("6660.","-3330.",result); printf("%s\n",result); div("1.","3333333333333333333333.",result); //bug printf("%s\n",result); }
结果:(每一行对应于main中的每一次调用div所得到的结果)
======= welcome to my HomePage(http://blog.csdn.net/zhanxinhang) to have a communication =======
相关文章推荐
- 利用字符实现大数除法运算(纯c实现)
- 大数运算,队列实现大数 加法、乘法、除法、模除
- 大数的四则运算之除法----Java代码实现
- 利用ANTLR4实现一个简单的四则运算计算器
- 练习:逻辑运算与位运算实现除法运算,加法运算
- 位运算之美--用 +、- 和位运算实现正整数除法和取模(2)
- 多项式的加法运算实现(利用链表结构)
- 汇编编写程序实现大数运算
- 大数运算之加法乘法---JAVA实现
- 利用字符重复出现的次数,编写一个方法,实现基本的字符串压缩功能。比如,字符串“aabcccccaaa”经压缩会变成“a2b1c5a3”。若压缩后的字符串没有变短,则返回原先的字符串。
- 用+,-,移位实现除法运算
- 不用‘/’ 实现除法运算
- 位运算之美——用+,-和位运算实现正整数除法和取模(二)
- 在树莓派上利用神经网络字符识别实现智能送餐车
- [原创]汇编实现大数乘除运算的雏形
- 实现顺序表的各种基本运算(利用线性表来实现)
- 利用字符重复出现的次数,编写一个方法,实现基本的字符串压缩功能。比如,字符串“aabcccccaaa”经压缩会变成“a2b1c5a3”。若压缩后的字符串没有变短,则返回原先的字符串。
- 再探利用C++的STL和堆栈编程思想实现数学四则运算计算结果
- 利用python3随机生成中文字符的实现方法
- 利用BP神经网络实现手写字符识别