实验一无限位长整数运算.docx

上传人:b****5 文档编号:2898254 上传时间:2022-11-16 格式:DOCX 页数:14 大小:76.50KB
下载 相关 举报
实验一无限位长整数运算.docx_第1页
第1页 / 共14页
实验一无限位长整数运算.docx_第2页
第2页 / 共14页
实验一无限位长整数运算.docx_第3页
第3页 / 共14页
实验一无限位长整数运算.docx_第4页
第4页 / 共14页
实验一无限位长整数运算.docx_第5页
第5页 / 共14页
点击查看更多>>
下载资源
资源描述

实验一无限位长整数运算.docx

《实验一无限位长整数运算.docx》由会员分享,可在线阅读,更多相关《实验一无限位长整数运算.docx(14页珍藏版)》请在冰豆网上搜索。

实验一无限位长整数运算.docx

实验一无限位长整数运算

 

一、问题描述:

输入一个加法或者是减法的式子,整形的树树的长度可以任意给定,将结果运算出来

二、概要设计

利用链表进行两个数的存贮,然后利用移位算法将结果算出来。

三、详细设计

运用两个链表,将整个式子分离出来,从而接着按位进行运算

四、测试及结果

五、心得体会

通过这次试验我明白了链表的基本应用,在做这个程序时最大问题就是在于对于出现的一些不合法的输入判断和矫正,使其得到正确的结果,这就是算法健壮性的表现,本人基本上实现了基本的输入的判断与改正,虽然功能能实现,但是自己程序代码不够模块化,设计不够合理,自己在改进中

六、源代码清单

//长整数四则运算.cpp:

定义控制台应用程序的入口点。

//

#include"stdafx.h"

#include"function.h"

#include"iostream"

usingnamespacestd;

#defineN100

voidInitNode(DLNode*&head)//初始化链表的函数

{

if((head=newDLNode)==NULL)

exit

(1);

head->prior=NULL;

head->next=NULL;

}

voidInsertNode(DLNode*&head,DataTypea)//插入元素的到指定的位置加法中需要在头部插入

{

DLNode*newNode;

InitNode(newNode);

newNode->data=a;

newNode->next=head;

head=newNode;

}

voidPrintNode(DLNode*head,chara)//打印输出链表

{

DLNode*p=head;

while(p->data=='0')

{

p=p->next;

if(p==NULL)

{

cout<<'0'<

return;

}

}

if(a=='-')

cout<<'-';

while(p!

=NULL)

{

cout<data;

p=p->next;

}

cout<

}

voidDestoryNode(DLNode*&head)//销毁链表

{

DLNode*p,*p1;

p=head;

while(p!

=NULL)

{

p1=p;

p=p->next;

deletep1;

}

deletep;

head=NULL;

}

intCalLongNode(DLNode*head)//计算链表的长度

{

DLNode*p;

p=head;

intn=0;

while(p!

=NULL)

{

p=p->next;

n++;

}

returnn;

}

charCheckNode(DLNode*head)//检查链表的运算符

{

DLNode*p=head;

if(p->data=='+'||p->data=='-')

{

p=p->next;

}

while(p->data!

='+'&&p->data!

='-')

{

p=p->next;

}

returnp->data;

}

intAtoiChar(chara)//将字符转化为数字便于计算

{

return(a-'0');

}

voidToLastNode(DLNode*&head)//寻到最后一个节点

{

while(head->next!

=NULL)

{

head=head->next;

}

}

voidAddNode(DLNode*h1,DLNode*h2)//计算加法传递参数勿用head传递

{

intl1,l2,a;//用来记录链表的长度

intcarry=0;//存贮进位

charcal;//存贮最终结果的符号

intm,n;

boolf1=0,f2=0;

DLNode*p;

DLNode*h3,*h4;

InitNode(p);

InitNode(h3);

InitNode(h4);

h3=h1;

h4=h2;

l1=CalLongNode(h1);

l2=CalLongNode(h2);

if(h3->data=='-')

{

f1=1;

l1=l1-1;

h3=h3->next;

h1=h1->next;

}

if(h4->data=='-')

{

f2=1;

l2=l2-1;

h2=h2->next;

h4=h4->next;

}

if(f1==0)

cal='+';

elsecal='-';

if(l1

{

p=h1;

h1=h2;

h2=p;

h3=h1;

h4=h2;

a=l1;

l1=l2;

l2=a;

}

ToLastNode(h3);

ToLastNode(h4);

for(inti=0;i

{

m=h3->data-'0';

n=h4->data-'0';

m=m+n+carry;

carry=0;

if(m>9)

{

carry=1;

m=m-10;

}

h3->data=m+'0';

m=0;

n=0;

h3=h3->prior;

h4=h4->prior;

}

for(inti=0;i

{

m=h3->data-'0';

//cout<

m=m+carry;

//cout<

//cout<

carry=0;

if(m>9)

{

carry=1;

m=m-10;

}

h3->data=m+'0';

m=0;

h3=h3->prior;

}

if(carry==1)

{

charm;

m='1';

InsertNode(h1,m);

}

PrintNode(h1,cal);

}

voidCalNode(DLNode*h1,DLNode*h2,charoper)//计算减法

{

intl1,l2,a;

intborrow=0;//存贮借位的值

charcal;//存贮最终结果的正负

intm=0,n=0;

boolf1=0,f2=0;

DLNode*p;

DLNode*h3,*h4;

InitNode(p);

InitNode(h3);

InitNode(h4);

l1=CalLongNode(h1);

l2=CalLongNode(h2);

if(h1->data=='-')

{

f1=1;

l1=l1-1;

h1=h1->next;

}

if(h2->data=='-')

{

f2=1;

l2=l2-1;

h2=h2->next;

}

h3=h1;

h4=h2;

if(l1>l2)

{

if(f1==0)

cal='+';

else

cal='-';

}

if(l1

{

if((oper=='+'&&f2==0)||(oper=='-'&&f2==1))

{

cal='+';

}

else

{

cal='-';

}

}

if(l1

{

p=h1;

h1=h2;

h2=p;

h3=h1;

h4=h2;

a=l1;

l1=l2;

l2=a;

}

if(l1==l2)

{

DLNode*h5,*h6;

InitNode(h5);

InitNode(h6);

h5=h1;

h6=h2;

booljudge=0;

for(inti=0;i

{

if(h5->data>h6->data)

{

cal='+';

judge=1;

break;

}

if(h5->datadata)

{

cal='-';

judge=1;

p=h1;

h2=h1;

h1=p;

break;

}

else

{

h5=h5->next;

h6=h6->next;

}

}

if(judge==0)

{

cout<<"0";

exit(0);

}

}

ToLastNode(h3);

ToLastNode(h4);

for(inti=0;i

{

m=h3->data-'0';

n=h4->data-'0';

m=m-n-borrow;

borrow=0;

if(m<0)

{

borrow=1;

m=m+10;

}

h3->data=m+'0';

m=0;

n=0;

h3=h3->prior;

h4=h4->prior;

}

for(inti=0;i

{

m=h3->data-'0';

//cout<

m=m-borrow;

//cout<

//cout<<

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > PPT模板 > 其它模板

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1