ImageVerifierCode 换一换
格式:DOC , 页数:14 ,大小:117KB ,
资源ID:4499128      下载积分:5 金币
快捷注册下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

开通VIP
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.zixin.com.cn/docdown/4499128.html】到电脑端继续下载(重复下载【60天内】不扣币)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

开通VIP折扣优惠下载文档

            查看会员权益                  [ 下载后找不到文档?]

填表反馈(24小时):  下载求助     关注领币    退款申请

开具发票请登录PC端进行申请

   平台协调中心        【在线客服】        免费申请共赢上传

权利声明

1、咨信平台为文档C2C交易模式,即用户上传的文档直接被用户下载,收益归上传人(含作者)所有;本站仅是提供信息存储空间和展示预览,仅对用户上传内容的表现方式做保护处理,对上载内容不做任何修改或编辑。所展示的作品文档包括内容和图片全部来源于网络用户和作者上传投稿,我们不确定上传用户享有完全著作权,根据《信息网络传播权保护条例》,如果侵犯了您的版权、权益或隐私,请联系我们,核实后会尽快下架及时删除,并可随时和客服了解处理情况,尊重保护知识产权我们共同努力。
2、文档的总页数、文档格式和文档大小以系统显示为准(内容中显示的页数不一定正确),网站客服只以系统显示的页数、文件格式、文档大小作为仲裁依据,个别因单元格分列造成显示页码不一将协商解决,平台无法对文档的真实性、完整性、权威性、准确性、专业性及其观点立场做任何保证或承诺,下载前须认真查看,确认无误后再购买,务必慎重购买;若有违法违纪将进行移交司法处理,若涉侵权平台将进行基本处罚并下架。
3、本站所有内容均由用户上传,付费前请自行鉴别,如您付费,意味着您已接受本站规则且自行承担风险,本站不进行额外附加服务,虚拟产品一经售出概不退款(未进行购买下载可退充值款),文档一经付费(服务费)、不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
4、如你看到网页展示的文档有www.zixin.com.cn水印,是因预览和防盗链等技术需要对页面进行转换压缩成图而已,我们并不对上传的文档进行任何编辑或修改,文档下载后都不会有水印标识(原文档上传前个别存留的除外),下载后原文更清晰;试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓;PPT和DOC文档可被视为“模板”,允许上传人保留章节、目录结构的情况下删减部份的内容;PDF文档不管是原文档转换或图片扫描而得,本站不作要求视为允许,下载前可先查看【教您几个在下载文档中可以更好的避免被坑】。
5、本文档所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用;网站提供的党政主题相关内容(国旗、国徽、党徽--等)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。
6、文档遇到问题,请及时联系平台进行协调解决,联系【微信客服】、【QQ客服】,若有其他问题请点击或扫码反馈【服务填表】;文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“【版权申诉】”,意见反馈和侵权处理邮箱:1219186828@qq.com;也可以拔打客服电话:0574-28810668;投诉电话:18658249818。

注意事项

本文(c++实现任意长整数的四则运算.doc)为本站上传会员【二***】主动上传,咨信网仅是提供信息存储空间和展示预览,仅对用户上传内容的表现方式做保护处理,对上载内容不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知咨信网(发送邮件至1219186828@qq.com、拔打电话4009-655-100或【 微信客服】、【 QQ客服】),核实后会尽快下架及时删除,并可随时和客服了解处理情况,尊重保护知识产权我们共同努力。
温馨提示:如果因为网速或其他原因下载失败请重新下载,重复下载【60天内】不扣币。 服务填表

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

1、 实验题目:设计一数据结构可处理任意长度的整数 概要设计 1.数据结构的定义 采用双向链表存储任意长整数。双向链表的定义如下: class DblList { private: DblNode *head, *tail; DblNode *current; int sign; public: DblList(); //构造函数 ~DblL

2、ist(); //析构函数 bool CreatList(string); //生成一个双向链表,存储整数 int GetCount(); //获取整数的长度 void Insert(DblNode *); //从表尾插入一个结点 void InsertFront(DblNode *); //从表头插入 void Clear();

3、 //去除该链表 void operator+(DblList &); //实现两个任意整数的加法 void operator*(DblList &); //实现两个任意整数的乘法 DblList & operator=(DblList &); //重载赋值运算符 int Compare(DblList &); //两个整数的绝对值比拟 void Display();

4、 //任意长度整数的标准化输出 }; 说明:数据的存储,无外乎顺序或者链表。顺序存储时,定义数组无法实现任意长度,而且需要预设一个maxsize,不是特别的方便。所以采用链式存储方式。而且任意长数据通过字符串输入。在链表的每一个结点中,数据域是在该数位上的数字大小。 2. 主要功能模块的功能 u 任意长整数的输入 u 任意长整数的标准化输出 u 两个整数的加法 u 两个整数的乘法 三.详细设计〔主模块流程图〕 五、 使用说明与测试结果 1.使用说明: 点击打开应用程序pro1.exe。依次输入任意两个整数〔例如123456,

5、1234567〕,按回车,会出现菜单,如下列图: 按‘1’那么实现两整数的加法 按‘2’那么实现两整数的乘法 按‘#’完毕 注:菜单可重复出现直至‘#’退出。 实现加法,乘法如下列图: 2.测试结果: (1) 123456 (2) +1234567 (3) -987654321 (4) 12a3 (5) + 注:当输入错误时,允许重新输入。 六、 源程序 /* 主函数 */ /************************

6、/ #include "cal.h" void main(){ string s; string p; DblList list1; while(1){ //输入错误时,允许重新输入 cout<<"Input num1"<>s; bool ok1=list1.CreatList(s); if (!ok1) { cout<<"error!"<

7、e{ cout<<"num1:"; list1.Display(); break; } } DblList list2; while(1){ cout<<"Input num2:"<>p; bool ok2=list2.CreatList(p); if (!ok2) { cout<<"error!"<

8、 while (1) { cout<<"请选择运算法:"<

9、 |"<>choose; if (choose=="1") { list1+list2; break; } else if (choose=="2") { list1*list2; break; } else if (choose=="#") {

10、 return; } else { cout<<"输入有误,请重新输入!!"< #include #include using namespace std; struct DblNode{ i

11、nt data; DblNode * prior; DblNode * next; }; bool IsNum(char a){ //判断字符a是否是便是数字 int s=a-'0'; if(s>=0&&s<10) return true; else return false; } bool IsInt(string a){ //判断字符串a是否表达一串数字 bool Jud=1; int i=1; char s=a[0]; if (a=="

12、"||a=="-") return false; if (s=='+'||s=='-') {} else if (s>='1'&&s<='9'){} else if (a[0]=='0'&&a[1]=='\0') return true; else return false; while (a[i]!='\0') { Jud=IsNum(a[i]); if (Jud==0) return false; i++; } return true; } int JudSign(string s){

13、 //返回数字的符号 if (s[0]=='-') return -1; else if(s[0]=='0'&&s[1]=='\0') return 0; else return 1; } int CtoI(char a){ int i=a-'0'; return i; } class DblList { //定义一个双向链表类,存储任意长度的数字 private: //并可以进展标准化输出和加法,乘法。 DblNode *head

14、 *tail; DblNode *current; int sign; public: DblList(); //构造函数 ~DblList(); //析构函数 bool CreatList(string); //生成一个双向链表 int GetCount(); //获取整数的长度 void Insert(DblNode *);

15、 //从表尾插入一个结点 void InsertFront(DblNode *); //从表头插入一个结点 void Clear(); //去除该链表 void operator+(DblList &); //实现两个任意整数的加法 void operator*(DblList &); //实现两个任意整数的乘法 DblList & operator=(DblList &); //重载赋值运算符 int Compare

16、DblList &); //两个整数的绝对值比拟 void Display(); //任意长度整数的标准化输出 }; DblList::DblList(){ head=new DblNode(); //构造函数 head->next=NULL; head->prior=NULL; tail=head; current=NULL; sign=0; } DblList::~DblList

17、){ //析构函数 while (head->next!=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->

18、next; int count=0; while (current) { count++; current=current->next; } current=NULL; return count; } void DblList::Insert(DblNode *p){ //从链表尾部插入一个结点 tail->next=p; p->prior=tail; tail=p; } void DblList::InsertFront(DblNode *q){ /

19、/从链表头部插入一个结点 if (head->next==NULL) { head->next=q; q->prior=head; tail=q; } else{ q->next=head->next; head->next->prior=q; head->next=q; q->prior=head; } } bool DblList::CreatList(string s){ //输入的任意长度的表示数字的字符串 bool j=IsInt(s);

20、//以此生成双向链表 if (!j) return j; else{ int i=0; sign=JudSign(s); if (s[0]=='+'||s[0]=='-') i++; while (s[i]!='\0') { int ia=CtoI(s[i]); current=new DblNode(); current->data=ia; current->next=NULL; current->prior=NULL; Insert(current); i++; cu

21、rrent=NULL; } return true; } } void DblList::Clear(){ while (head->next) { current=head->next; head->next=current->next; delete current; } tail=head; sign=0; current=NULL; } int DblList::Compare(DblList & s){ //任意两个长度数字绝对值比拟 int a=GetCount();

22、 int b=s.GetCount(); if (a>b) return 1; else if (anext; s.current=s.head->next; while (current!=NULL) { int re=current->data-s.current->data; if (re>0) return 1; else if (re<0) return -1; else { current=current->ne

23、xt; s.current=s.current->next; } } current=NULL; s.current=NULL; return 0; } } DblList & DblList::operator =(DblList &s){ Clear(); sign=s.sign; s.current=s.head->next; while (s.current!=NULL) { current=new DblNode(); current->data=s.current->dat

24、a; Insert(current); s.current=s.current->next; } s.current=NULL; current=NULL; return *this; } void DblList::operator +(DblList & s){ //实现加法〔包括减法〕 DblList temp; int da; int f=0; int si=Compare(s); if (si==0&&(sign+s.sign==0)) temp.sign=0; else { if (si==0)

25、 temp.sign=sign; else if(si>0) 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.curren

26、t->data=f; temp.InsertFront(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;

27、f=0; } else if (current==head&&s.current!=s.head) { temp.current=new DblNode(); temp.current->data=s.current->data+f; temp.InsertFront(temp.current); s.current=s.current->prior; f=0; } else{ da=current->data*sign+s.current->data*s.sign+f;

28、 if (da*temp.sign>=10) { da=da-10*temp.sign; f=temp.sign; } else if (da*temp.sign<0) { da=da+10*temp.sign; f=-temp.sign; } else f=0; temp.current=new DblNode(); temp.current->next=NULL; temp.current->data=abs(da);

29、 temp.InsertFront(temp.current); current=current->prior; s.current=s.current->prior; } } current=NULL; 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.cu

30、rrent; temp.current=temp.head->next; } temp.current=NULL; cout<<"num1+num2="; temp.Display(); } void DblList::operator*(DblList & s){ //实现乘法 int cf=0; int ans; int i,j; int count=0; DblList temp; temp.sign=sign*s.sign; int a1=GetCount(); int a2=

31、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

32、tail; for (i=0;iprior; for(j=0;jdata*current->data+temp.current->data+cf; temp.current->data=ans%10; cf=ans/10; current=current->prior; temp.current=temp.current->prior; } if (cf!=0) { te

33、mp.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=temp.current->next; delete temp.current; temp.current=NULL; } cout<<"n

34、um1*num2="; temp.Display(); } void DblList::Display(){ //任意长数字的标准化输出 int count=GetCount(); if (sign==0) { cout<<"0"<next; while (current!=NULL) { if(count>0){ cout<data; count--; if (count%3==0&&count!=0) cout<<","; current=current->next; } } current=NULL; cout<

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

关于我们      便捷服务       自信AI       AI导航        抽奖活动

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

客服电话:0574-28810668  投诉电话:18658249818

gongan.png浙公网安备33021202000488号   

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

关注我们 :微信公众号    抖音    微博    LOFTER 

客服