用链表实现大数相加减[汇编]

上传人:粗**** 文档编号:140298838 上传时间:2020-07-28 格式:PDF 页数:15 大小:138.67KB
返回 下载 相关 举报
用链表实现大数相加减[汇编]_第1页
第1页 / 共15页
用链表实现大数相加减[汇编]_第2页
第2页 / 共15页
用链表实现大数相加减[汇编]_第3页
第3页 / 共15页
用链表实现大数相加减[汇编]_第4页
第4页 / 共15页
用链表实现大数相加减[汇编]_第5页
第5页 / 共15页
点击查看更多>>
资源描述

《用链表实现大数相加减[汇编]》由会员分享,可在线阅读,更多相关《用链表实现大数相加减[汇编](15页珍藏版)》请在金锄头文库上搜索。

1、高级语言程序设计作业报告 作业名称 : 大整数相加减 学院计算机科学与工程学院 专业计算机软件 学生awell 任课教师xxx 提交日期2012.6.28 一、问题描述 实现两个大整数的相加减 二、系统设计 1、结构说明 建立结构类型node: struct node int num;/ 每个位数用一个int 类型表示,其中num=0 if(arr2.size()-falg2=arr1.size()-falg1) for(int i=0;iarr2i+falg2) falg=true; break; if(i=arr1.size()-falg1-1) falg=false; if(falg)

2、for(int i=arr1.size()-1;i=falg1;i-) node *s; s=new node; s-num=arr1i-0;/从输入的整数尾部逆向建立链表 if(fir=NULL) fir=s; else efir-next=s; efir=s; efir-next=NULL;/ 尾结点的 next 一定要赋NULL,不然会出现错误 for(int i=arr2.size()-1;i=falg2;i-) node *s; s=new node; s-num=arr2i-0; if(sec=NULL) sec=s; else esec-next=s; esec=s; esec-

3、next=NULL; else for(int i=arr2.size()-1;i=falg2;i-) node *s; s=new node; s-num=arr2i-0; if(fir=NULL) fir=s; else efir-next=s; efir=s; efir-next=NULL; for(int i=arr1.size()-1;i=falg1;i-) node *s; s=new node; s-num=arr1i-0; if(sec=NULL) sec=s; else esec-next=s; esec=s; esec-next=NULL; if(opration=+) r

4、eturn; if(!falg) f=!f;/f表示整个运算结果的正负 void add() / 实现加法 bool falg=false;/ 标记是否进位 int num2=sec-num; for(node *head1=fir,*head2=sec;head1;head1=head1-next)/因为以 fir 为表头建立的 链表所保存的整数较大,所以,以sec 为表头建立的链表会先或者同时遍历到表位 int num1=head1-num; head1-num=(falg+num1+num2)%10; falg=(falg+num1+num2)/10; if(head2-next) /

5、判断以 sec为表头的链表是否达到表尾 head2=head2-next; num2=head2-num; else num2=0; if(f) cout-; if(falg) coutnext) ansi=head1-num+0; i+; for(i-;i=0;i-) coutnum; bool falg=false;/ 判是否退位 for(node *head1=fir,*head2=sec;head1;head1=head1-next) int num1=head1-num; head1-num=num1-num2-falg; if(num1-falg-num2num=head1-num

6、+10; else falg=false; if(head2-next) head2=head2-next; num2=head2-num; else num2=0; int i=0; char *ans=new charsizeof(arr1)+sizeof(arr2); for(node *head1=fir;head1;head1=head1-next) ansi=head1-num+0; i+; i-; falg=false; for(int j=0;j=i;j+) if(ansj!=0) break; if(j=i) falg=true; cout0; return; if(f) c

7、out=0;i-) coutnext; delete tem; for(;sec;) tem=sec; sec=sec-next; delete tem; tem=fir=efir=sec=esec=NULL; 程序流程图 开始 输入式子 判操作 实现加法实现减法 输出结果 结束 三、程序测试 1、设计测试用例 0 + 0 1 + 1 10 + 1 1 + 10 00000 + -1 + 1 1 + -1 -10 + 100 100 + -10 -1000000 + -0000000000 10 - 10 1 - 10 10 - 1 1 - 7 7 - 1 5555555 - 55555 -1

8、0 - -10 -11 - -1 -1 - -15515 -1 - -8 -8 - -1 - - -66666666 2、程序测试结果 0 + 0 = 0 1 + 1 = 2 10 + 1 = 11 1 + 10 = 11 00000 + = -1 + 1 = 0 1 + -1 = 0 -10 + 100 = 90 100 + -10 = 90 -1000000 + -0000000000 = -0001000000 10 - 10 = 0 1 - 10 = -9 10 - 1 = 9 1 - 7 = -6 7 - 1 = 6 5555555 - 55555 = 0000000 -10 - -

9、10 = 0 -11 - -1 = -10 -1 - -15515 = 15514 -1 - -8 = 7 -8 - -1 = -7 - - -66666666 = - 四、使用说明 能正确实现两个大整数(包括正负)相加减的运算 五、收获体会及建议 完整代码: #include #include #include #include usingnamespacestd; struct node int num; node * next; ; bool f,falg1,falg2; string arr1,arr2; charopration; node *fir=NULL ,*efir=NULL

10、 ,*sec=NULL ,*esec =NULL ; void creatlist() bool falg=false; if(arr1.size()-falg1arr2.size()-falg2) falg=true; if(arr2.size()-falg2=arr1.size ()-falg1) for(int i=0;iarr2i+falg2) falg=true; break; if(i=arr1.size()-falg1-1) falg=false; if(falg) for(int i=arr1.size ()-1;i=falg1;i-) node *s; s=new node;

11、 s-num=arr1i-0; if(fir=NULL ) fir=s; elseefir-next=s; efir=s; efir-next=NULL ; for(int i=arr2.size ()-1;i=falg2;i-) node *s; s=new node; s-num=arr2i-0; if(sec=NULL ) sec=s; elseesec -next=s; esec=s; esec -next=NULL ; else for(int i=arr2.size ()-1;i=falg2;i-) node *s; s=new node; s-num=arr2i-0; if(fi

12、r=NULL ) fir=s; elseefir-next=s; efir=s; efir-next=NULL ; for(int i=arr1.size ()-1;i=falg1;i-) node *s; s=new node; s-num=arr1i-0; if(sec=NULL ) sec=s; elseesec -next=s; esec=s; esec -next=NULL ; if(opration=+) return; if(!falg) f=!f; void add() bool falg=false; int num2=sec-num; for(node *head1=fir

13、,*head2=sec;head1;head1=head1-next) int num1=head1-num; head1-num=(falg+num1+num2)%10; falg=(falg+num1+num2)/10; if(head2-next) head2=head2-next; num2=head2-num; elsenum2=0; if(f) cout-; if(falg) coutnext) ansi=head1-num+0; i+; for(i-;i=0;i-) coutnum; bool falg=false; for(node *head1=fir,*head2=sec;

14、head1;head1=head1-next) int num1=head1-num; head1-num=num1-num2-falg; if(num1-falg-num2num=head1-num+10; elsefalg=false; if(head2-next) head2=head2-next; num2=head2-num; elsenum2=0; int i=0; char *ans=new charsizeof(arr1)+sizeof(arr2); for(node *head1=fir;head1;head1=head1-next) ansi=head1-num+0; i+

15、; i-; falg=false; for(int j=0;j=i;j+) if(ansj!=0) break; if(j=i) falg=true; cout0; return; if(f) cout=0;i-) coutnext; delete tem; for(;sec;) tem=sec; sec=sec-next; delete tem; tem=fir=efir=sec=esec =NULL ; int main() cout输入算式,形如: endl; couta + b or a - barr1oprationarr2) chartem=opration; f=false,falg1=false,falg2=false; if(arr10=-) falg1=true; if(arr20=-) falg2=true; if

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 大杂烩/其它

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号