加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

大数乘法

发布时间:2020-12-14 01:59:10 所属栏目:大数据 来源:网络整理
导读:用数组做了一个大数乘法的算法,很粗糙,但很实用 ? 1 #include apue.h ? 2 #define ?MAX ? 100 ? 3? ? 4 void ?my_mulity(char *buf1,char *buf2); ? 5? ? 6 void ?my_mulity(char *buf1,char *buf2) ? 7 { ? 8 ? ? int len1 = 0; ? 9 ? ? int len2 = 0; ?1

用数组做了一个大数乘法的算法,很粗糙,但很实用

? 1 #include <apue.h>
? 2 #define ?MAX ? 100
? 3?
? 4 void ?my_mulity(char *buf1,char *buf2);
? 5?
? 6 void ?my_mulity(char *buf1,char *buf2)
? 7 {
? 8 ? ? int len1 = 0;
? 9 ? ? int len2 = 0;
?10 ? ? int i = 0;
?11 ? ? int j = 0;
?12 ? ? int len = 0;
?13?
?14 ? ? len1 = strlen(buf1);
?15 ? ? len2 = strlen(buf2);
?16 ? ? len = len1 + len2;
?17?
?18 ? ? int *result = (int *)malloc(sizeof(int) * len) ;
?19 ? ? if(result == NULL){
?20 ? ? ? ? return ;
?21 ? ? }

22?
?23 ? ? for(i = 0; i < len; i++){
?24 ? ? ? ? result[i] = 0;
?25 ? ? }
?26?
?27 ? ? for(i = 0; i < len1; i++){
?28 ? ? ? ? for(j = 0; j < len2; j++){
?29 ? ? ? ? ? ? result[i + j + 1] += (buf1[i] - '0')*(buf2[j] - '0');//为何i+j+1呢?对应起来
?30 ? ? ? ? }
?31 ? ? }

?32?
?33 ? ? for(i = len - 1; i >= 0; i--){
?34 ? ? ? ? if(result[i] >= 10){
?35 ? ? ? ? ? ? result[i] = result[i] % 10; ? //进行进位和取余
?36 ? ? ? ? ? ? result[i - 1] += result[i]/10;
?37 ? ? ? ? }
?38 ? ? }
?39 ? ? i = 0;
?40 ? ? while(result[i] == 0) i++; ? ?//防止一开始是0 ?41 ? ? printf("result:n"); ?42 ? ? for(j = i; j < len; ++j){ ?43 ? ? ? ? ?printf("%d",result[j]); ?44 ? ? } ?45? ?46 } ?47? ?48 int main(int ac,char **av) ?49 { ?50 ? ? char buf1[MAX] = {0}; ?51 ? ? char buf2[MAX] = {0}; ?52? ?53 ? ? printf("please input : mulitiedn"); ?54 ? ? scanf("%s",buf1); ?55 ? ? printf("please input : mulitiedn"); ?56 ? ? scanf("%s",buf2); ?57? ?58 ? ? my_mulity(buf1,buf2); ?59 ? ? printf("n"); ?60 ? ? return 0; ?61 }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读