大整数乘法c语言版
来源:互联网 发布:淘宝儿童床哪个品牌好 编辑:程序博客网 时间:2024/06/05 20:37
</pre><pre name="code" class="plain">#include<stdio.h>#include<stdlib.h>char fuhao='+';/*数据节点*/typedef struct node{ int data; struct node * next;}_node;/*链表控制结构*/typedef struct{ _node *head; _node *tail ; int ndcnt;}_ndctl;/**********************************multip function*************************************************//***************************************************************************************************//************print************/void print_num(_ndctl *ctl){ int flag=0; _node *pnt; pnt = ctl->head; while(pnt!=NULL){ if(pnt->data==flag){ pnt=pnt->next; continue; } flag=-1; printf("%d",pnt->data); pnt=pnt->next; } if(flag==0){ printf("0"); } printf("\n");}/*乘数的输入保存*/void create_num(_ndctl *ctl,char *fuhao){ int data = 1 ; _node *pnt =NULL ; char c ='s'; while((c= getchar())!='\n'){ if(c=='-'){ *fuhao=c; continue; } if(c>='0'&&c<='9'){ data = c-'0'; pnt=(_node *)malloc(sizeof(_node)); if(pnt==NULL){ printf("malloc failed!"); return; } pnt->data=data; pnt->next=ctl->head; ctl->head=pnt;//头插法构建栈 ctl->ndcnt++; }else{ printf("your input is illegal !\n"); exit(1); } }}/*为结果链表预先申请空间,比较安全*/void result_malloc(_ndctl *ctl_result,int cnt){ int flag=0; _node *pnt; while(flag<cnt){ pnt=(_node*)malloc(sizeof(_node)); if(pnt==NULL){ printf("malloc filed!\n"); return; } pnt->data=0; pnt->next=ctl_result->head; ctl_result->head=pnt;//头插法构建栈 ctl_result->ndcnt++; flag++; }}/*链表逆序*/void nixu_list(_ndctl * ctl){ _node *h1,*h2,*h3; h1=ctl->head; h2=ctl->head->next; h3=h2->next; h1->next=NULL; while(h3){ h2->next=h1; h1=h2; h2=h3; h3=h2->next; if(h3==NULL){ h2->next=h1;//当h3为空时,h2为最后一个节点,只需让他指向倒数第二个节点 就完成了逆序 } } ctl->head=h2;}/*乘法核心算法*/void multip(_ndctl *ctl_result, _ndctl *ctl1, _ndctl *ctl2){ _node *pnt1=ctl1->head; _node *pnt2=ctl2->head; int add=0; int temp=0; int temp1; _node *qt=ctl_result->head; _node *pnt_result=NULL; while(pnt2!=NULL){ pnt_result=qt; while(pnt1!=NULL){ temp=pnt1->data*pnt2->data+add; temp1=pnt_result->data+temp; pnt_result->data=(temp1%10); add=temp1/10;//进位 pnt1=pnt1->next; pnt_result=pnt_result->next; } pnt_result->data+=add; add=0; pnt1=ctl1->head; pnt2=pnt2->next; qt=qt->next; } nixu_list(ctl_result);}/*free 存储空间*/void free_fun(_ndctl *ctl){ _node *pnt=NULL; _node *qt =NULL; pnt=ctl->head; while(pnt!=NULL){ qt=pnt; pnt=pnt->next; free(qt); }}/***********************begin main********************/int main(){ char fuhao1=' '; char fuhao2=' '; _ndctl num1={NULL,0,0};//乘数 _ndctl num2={NULL,0,0};//被乘数 _ndctl result={NULL,0,0};//结果 int cnt = 0; printf("please input first num:"); create_num(&num1,&fuhao1); printf("please input second num:"); create_num(&num2,&fuhao2); cnt=num1.ndcnt+num2.ndcnt; result_malloc(&result, cnt); multip(&result,&num1,&num2); printf("the result is: "); if(fuhao1!=fuhao2){ printf("-"); } print_num(&result); free_fun(&num1); free_fun(&num2); free_fun(&result); return 0;}
0 0
- 大整数乘法c语言版
- [C语言]大整数乘法
- 大整数乘法(C)
- 大整数乘法c语言
- [c++] 大整数乘法(字符串乘法)
- 大整数乘法(C语言实现)
- c语言 关于大整数乘法问题
- 大整数乘法-C语言实现
- 大整数乘法(C语言)
- 99乘法表(C语言版)
- 大整数乘法
- C++大整数乘法
- 大整数FFT乘法
- 大整数乘法
- 大整数乘法
- 大整数乘法
- 大整数乘法
- 大整数乘法。。。。。
- [LeetCode] Palindrome Number
- mysql中datetime比较大小问题
- OpenAPI学习
- 什么是令牌桶
- 随机产生不同的数
- 大整数乘法c语言版
- Java中Synth外观学习(一)--建立工程加载自定义外观的XML文件
- JMS的消息模式
- Could not find the main class.program will exit错误/////1.改变字体//////2.恢复输入点(.)后显示提示
- 简单十步让你全面理解SQL
- 使用多个values文件造成样式不统一的问题
- 系统故障及软件质量
- 简易的验证码方法
- 【Cocos2d-x 2.2.2 自学笔记之一】利用自己的jar快捷创建项目