收藏 分销(赏)

c++实现任意长整数的四则运算.doc

上传人:二*** 文档编号:4499128 上传时间:2024-09-25 格式:DOC 页数:14 大小:117KB
下载 相关 举报
c++实现任意长整数的四则运算.doc_第1页
第1页 / 共14页
亲,该文档总共14页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述

1、 实验题目:设计一数据结构可处理任意长度的整数 概要设计1.数据结构的定义采用双向链表存储任意长整数。双向链表的定义如下:class DblList private: DblNode *head, *tail; DblNode *current;int sign;public:DblList(); /构造函数 DblList(); /析构函数bool CreatList(string); /生成一个双向链表,存储整数int GetCount(); /获取整数的长度void Insert(DblNode *); /从表尾插入一个结点 void InsertFront(DblNode *); /从

2、表头插入void Clear(); /去除该链表void operator+(DblList &); /实现两个任意整数的加法void operator*(DblList &); /实现两个任意整数的乘法DblList & operator=(DblList &); /重载赋值运算符 int Compare(DblList &); /两个整数的绝对值比拟 void Display(); /任意长度整数的标准化输出;说明:数据的存储,无外乎顺序或者链表。顺序存储时,定义数组无法实现任意长度,而且需要预设一个maxsize,不是特别的方便。所以采用链式存储方式。而且任意长数据通过字符串输入。在链表

3、的每一个结点中,数据域是在该数位上的数字大小。2 主要功能模块的功能u 任意长整数的输入u 任意长整数的标准化输出u 两个整数的加法u 两个整数的乘法三详细设计主模块流程图五、 使用说明与测试结果1.使用说明:点击打开应用程序pro1.exe。依次输入任意两个整数例如123456,+1234567,按回车,会出现菜单,如下列图:按1那么实现两整数的加法按2那么实现两整数的乘法按#完毕注:菜单可重复出现直至#退出。实现加法,乘法如下列图:2.测试结果:(1) 123456 (2) +1234567 (3) -987654321 (4) 12a3 (5) + 注:当输入错误时,允许重新输入。六、

4、源程序/* 主函数 */*/#include cal.hvoid main()string s; string p; DblList list1;while(1) /输入错误时,允许重新输入 coutInput num1s; bool ok1=list1.CreatList(s);if (!ok1) couterror!endl;elsecoutnum1:;list1.Display();break;DblList list2;while(1)coutInput num2:p; bool ok2=list2.CreatList(p);if (!ok2)couterror!endl;elseco

5、utnum2:;list2.Display();break;string choose;while (1) cout请选择运算法:endl;cout-endl; /*菜单*/cout|1.num1+num2 |endl; /*可以重复输入运算符,按#退出*/cout|2.num1*num2 |endl;cout|#.exit |endl;cout-choose; if (choose=1) list1+list2;break; else if (choose=2) list1*list2;break; else if (choose=#) return; else cout输入有误,请重新输入

6、!endl;continue;/*头文件,包括长整数数据结构的定义,成员函数的定义*/*/#include #include #include using namespace std;struct DblNodeint data; DblNode * prior;DblNode * next;bool IsNum(char a) /判断字符a是否是便是数字 int s=a-0;if(s=0&s=1&snext=NULL;head-prior=NULL; tail=head;current=NULL;sign=0;DblList:DblList() /析构函数while (head-next!=

7、NULL) current=head-next;head-next=current-next;delete current;current=NULL;sign=0;delete head;head=NULL;tail=NULL;int DblList:GetCount() /返回该数字的长度不包括符号位 current=head-next;int count=0;while (current)count+;current=current-next;current=NULL;return count;void DblList:Insert(DblNode *p) /从链表尾部插入一个结点 tai

8、l-next=p; p-prior=tail;tail=p; void DblList:InsertFront(DblNode *q) /从链表头部插入一个结点if (head-next=NULL)head-next=q;q-prior=head;tail=q;elseq-next=head-next;head-next-prior=q;head-next=q;q-prior=head;bool DblList:CreatList(string s) /输入的任意长度的表示数字的字符串 bool j=IsInt(s); /以此生成双向链表if (!j) return j;elseint i=0

9、;sign=JudSign(s);if (s0=+|s0=-)i+;while (si!=0)int ia=CtoI(si);current=new DblNode();current-data=ia; current-next=NULL;current-prior=NULL;Insert(current);i+;current=NULL;return true;void DblList:Clear()while (head-next) current=head-next;head-next=current-next;delete current;tail=head;sign=0;curren

10、t=NULL;int DblList:Compare(DblList & s) /任意两个长度数字绝对值比拟int a=GetCount();int b=s.GetCount();if (ab) return 1;else if (anext;s.current=s.head-next;while (current!=NULL)int re=current-data-s.current-data;if (re0)return 1;else if (renext; s.current=s.current-next;current=NULL;s.current=NULL;return 0;DblL

11、ist & DblList:operator =(DblList &s) Clear();sign=s.sign; s.current=s.head-next;while (s.current!=NULL)current=new DblNode();current-data=s.current-data;Insert(current);s.current=s.current-next;s.current=NULL;current=NULL;return *this;void DblList:operator +(DblList & s) /实现加法包括减法DblList temp;int da

12、;int f=0;int si=Compare(s); if (si=0&(sign+s.sign=0)temp.sign=0;else if (si=0) temp.sign=sign;else if(si0) temp.sign=sign; else temp.sign=s.sign; current=tail; s.current=s.tail; while (1)if (current=head&s.current=s.head) if (f) da=f; temp.current=new DblNode(); temp.current-data=f; temp.InsertFront

13、(temp.current);if (!f) break;f=0;else if (current!=head&s.current=s.head)temp.current=new DblNode(); temp.current-data=current-data+f;temp.InsertFront(temp.current);current=current-prior;f=0;else if (current=head&s.current!=s.head)temp.current=new DblNode();temp.current-data=s.current-data+f;temp.In

14、sertFront(temp.current);s.current=s.current-prior;f=0; elseda=current-data*sign+s.current-data*s.sign+f;if (da*temp.sign=10)da=da-10*temp.sign;f=temp.sign;else if (da*temp.signnext=NULL;temp.current-data=abs(da);temp.InsertFront(temp.current);current=current-prior;s.current=s.current-prior;current=N

15、ULL;s.current=NULL;temp.current=temp.head-next;if (temp.current!=NULL)while (temp.current-data=0)temp.head-next=temp.current-next;delete temp.current;temp.current=temp.head-next; temp.current=NULL;coutnum1+num2=;temp.Display(); void DblList:operator*(DblList & s) /实现乘法int cf=0;int ans;int i,j;int co

16、unt=0;DblList temp;temp.sign=sign*s.sign;int a1=GetCount();int a2=s.GetCount();int a=a1+a2; for (i=0;idata=0;temp.current-next=NULL;temp.current-prior=NULL;temp.InsertFront(temp.current); s.current=s.tail;while (s.current!=s.head)current=tail;temp.current=temp.tail;for (i=0;iprior;for(j=0;jdata*curr

17、ent-data+temp.current-data+cf; temp.current-data=ans%10; cf=ans/10;current=current-prior;temp.current=temp.current-prior;if (cf!=0)temp.current-data=temp.current-data+cf;cf=0;s.current=s.current-prior;temp.current=temp.tail;count+; if(temp.head-next-data=0)temp.current=temp.head-next;temp.head-next=

18、temp.current-next;delete temp.current;temp.current=NULL;coutnum1*num2=; temp.Display();void DblList:Display() /任意长数字的标准化输出int count=GetCount();if (sign=0)cout0endl;return ;else if (sign=-1)coutnext;while (current!=NULL) if(count0)coutdata; count-;if (count%3=0&count!=0) coutnext;current=NULL;coutendl;cout-endl;14 / 14

展开阅读全文
相似文档                                   自信AI助手自信AI助手
猜你喜欢                                   自信AI导航自信AI导航
搜索标签

当前位置:首页 > 通信科技 > 开发语言

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        获赠5币

©2010-2024 宁波自信网络信息技术有限公司  版权所有

客服电话:4008-655-100  投诉/维权电话:4009-655-100

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :gzh.png    weibo.png    LOFTER.png 

客服