本站首页    管理页面    写新日志    退出

The Neurotic Fishbowl

大整数相乘程序(用C++编写)
launching 发表于 2005/12/13 21:53:41

好像很多学校的编程课都要求学生写这样一个大整数(位数不定)相乘的程序,在这里贴一个吧,供大家参考(不是我写的,我只是稍微对这个程序进行了一下修改) //BigInteger.cpp #include <iostream>#include <stdio.h>#include "BigInteger.h"using namespace std; BigInteger::BigInteger()        //构造函数,将每个节点置空.{ Head=End=TempNode=NULL;} BigInteger::BigInteger(const BigInteger &BigNum)     //拷贝构造{ Node *p; Head=End=TempNode=NULL; p=BigNum.Head; while(p) {  AddEnd(p->Num);  p=p->Next; }} BigInteger::~BigInteger()        //析构{ Node *NextNode; if(Head==NULL)  return; TempNode=Head; while(TempNode) {  NextNode=TempNode->Next;  delete TempNode;  TempNode=NextNode; } Head=NULL; End=NULL; TempNode=NULL;} void BigInteger::AddHead(char Num)        //在链表头插入节点的操作{ TempNode=new Node; TempNode->Num=Num; TempNode->Prev=NULL; if(!Head) {  Head=End=TempNode;  TempNode->Next=NULL; } else {  TempNode->Next=Head;  Head->Prev=TempNode;  Head=TempNode; }} void BigInteger::AddEnd(char Num)       //在链表尾插入节点的操作{ TempNode=new Node; TempNode->Num=Num; TempNode->Next=NULL; if(!Head) {  Head=End=TempNode;  TempNode->Prev=NULL; } else {  TempNode->Prev=End;  End->Next=TempNode;  End=TempNode; }} void BigInteger::GetNumber()            //输入部分{ char key; int count=0,num=0; while((key=getchar())!=10)            //判断输入的是否是回车,不是的话将内容从后到前放到链表中. {  if(key>='0' && key<='9')  {   num=key-'0';   AddEnd(num);   num=0;  } }} BigInteger BigInteger::operator + (const BigInteger &BigNum2)    //重载"+"{ BigInteger &BigNum1=*this,result; Node *temp1,*temp2; int TempNum,rest=0; temp1=BigNum1.End;            //将临时链表首地址放置到输入链表的尾部 temp2=BigNum2.End; while(temp1 && temp2) {  TempNum=int(temp1->Num)+int(temp2->Num)+rest;         //节点内元素相加并加上进位rest  if(TempNum>9)                  //判断相加结果是否会产生进位.  {   TempNum=TempNum-10;   rest=1;  }  else   rest=0;  result.AddHead(char(TempNum));          //将结果放置到最终结果链表里  temp1=temp1->Prev;  temp2=temp2->Prev; } if(temp2)temp1=temp2; while(temp1) {  int(TempNum)=int(temp1->Num)+rest;           //节点内元素加上进位rest  if(TempNum>9)  {   TempNum=TempNum-10;   rest=1;  }  else   rest=0;  result.AddHead(char(TempNum));             //将结果放置到最终结果链表里  temp1=temp1->Prev; } if(rest)  result.AddHead(char(rest));                //考虑最后的进位是否存在,如果存在则存入链表的首部. return result;} BigInteger BigInteger::operator * (const BigInteger &BigNum2)     //对*进行重载{ BigInteger &BigNum1=*this,temp,result; Node *temp1,*temp2,*tempa,*tempb; int TempNum,rest,i=0,rest2; temp1=BigNum1.End; temp2=BigNum2.End; while(temp2)        //由乘数的存在与否判断是否去乘被乘数的每个位 {  rest=0;  while(temp1!=NULL)  {   TempNum=int(temp1->Num)*int(temp2->Num)+rest;   if(TempNum>9)   {     rest=TempNum/10;                 //进位由相乘结果与10做商求得    TempNum=TempNum%10;                 //由相乘结果与10求模取个位   }   else    rest=0;   temp.AddHead(char(TempNum));        //存入临时链表   temp1=temp1->Prev;  }  if(rest!=0)temp.AddHead(char(rest));  for(int k=i;k>=1;k--)temp.AddEnd(char(0));       //判断应该在链表后面补几个0  i++;            //每次乘完后计数,用来下一次的补0  temp1=BigNum1.End;             //把被乘数重新置到尾,用来让乘数下一次去乘每个元素  temp2=temp2->Prev;              //将乘数取出链表的前驱  tempa=result.End;                  //下面进行的是将每次乘数与被乘数的相乘结果累加放到最终链表里等待输出  if(result.Head!=NULL)           //下面过程与"+"重载基本一样,只是多了对临时链表的置空,所以不在做详细的注释.  {   result.End=temp.Head;   result.Head=NULL;  }  tempb=temp.End;  rest2=0;  while(tempa!=NULL && tempb!=NULL)  {   TempNum=int(tempa->Num)+int(tempb->Num)+rest2;   if(TempNum>9)   {    TempNum=TempNum-10;    rest2=1;   }   else    rest2=0;   result.AddHead(char(TempNum));   tempa=tempa->Prev;   tempb=tempb->Prev;  }  if(tempb)tempa=tempb;  while(tempa)  {   int(TempNum)=int(tempa->Num)+rest2;   if(TempNum>9)   {    TempNum=TempNum-10;    rest2=1;   }   else    rest2=0;   result.AddHead(char(TempNum));   tempa=tempa->Prev;  }  if(rest2)   result.AddHead(char(rest2));  if(temp.Head!=NULL)  {   temp.End=temp.Head;   temp.Head=NULL;  }  tempb=NULL; } return result;} BigInteger BigInteger::operator = (const BigInteger &BigNum)          //对=号进行重载{ if(this==&BigNum)  return *this; Node *p; TempNode=Head=End=NULL; p=BigNum.Head; while(p) {  AddEnd(p->Num);  p=p->Next; } return *this;} void BigInteger::disp()                    //输出链表{ if(Head) {  cout<<int(Head->Num);  TempNode=Head->Next; } else return; while(TempNode) {  cout<<int(TempNode->Num);  TempNode=TempNode->Next; } cout<<endl;}     //BigInteger.h struct Node        //定义了节点的结构{ char Num; Node *Prev,*Next;}; class BigInteger      //定义BigInteger 类{ Node *Head,*End,*TempNode; void AddHead(char Num); void AddEnd(char Num);public: BigInteger(); BigInteger(const BigInteger &BigNum); void GetNumber(); void disp(); BigInteger operator + (const BigInteger &BigNum); BigInteger operator * (const BigInteger &BigNum); BigInteger operator = (const BigInteger &BigNum); ~BigInteger();};     //main.cpp #include <iostream>#include "BigInteger.h"using namespace std; void main(){ BigInteger BigNum1,BigNum2,BigNum3;  cout<<"被乘数: "<<endl; BigNum1.GetNumber(); cout<<"乘数: "<<endl; BigNum2.GetNumber(); BigNum3=BigNum1*BigNum2; cout<<"\n相乘的结果是:"<<endl; BigNum3.disp();  }    

阅读全文(4392) | 回复(-1) | 编辑 | 精华

 



发表评论:
昵称:
密码:
主页:
标题:
验证码:  (不区分大小写,请仔细填写,输错需重写评论内容!)

 
 



 

.: 公告

换个皮肤:)
 

Bloginess

«August 2025»
12
3456789
10111213141516
17181920212223
24252627282930
31

.: 我的分类(专题)

首页(36)


In the Bowl

.: 最新日志

blog转到msn spaces
quartz32.dll - 症状:无
susan说
At last, my paper is
Be busy with amendin
码字
con一个
给中国学生的第四封信 (共四封 by 李
给中国学生的第三封信 (共四封 by 李
给中国学生的第二封信 (共四封 by 李
给中国学生的第一封信 (共四封 by 李
竟然踩到整,con一个
北大清华与香港大学谁更有吸引力?(转载自
西湖美景三月天,春雨如酒柳如烟
过完生日,很happy
重回高中
中国互联网热升温的冷思考
终于考完了
有志出国的人引以为鉴(1)
I am Back
写在访问量过万
示意图
电脑SeQing狂
怀念老歌手之李丽芬——爱江山更爱美人(转
白色恋人

偶遇
随笔
回顾我的2005
各位圣诞快乐:)
人工智能大作业-五子棋(修缮版本)
人工智能大作业——五子棋
打造超级快的windows xp带sp2
大整数相乘程序(用C++编写)
我写的一个java记事本,有加密解密和大
Launching的心情(从原来的blo


.: 最新回复

回复:我写的一个java记事本,有加密解
回复:我写的一个java记事本,有加密解
回复:我写的一个java记事本,有加密解
回复:电脑SeQing狂
回复:电脑SeQing狂
回复:电脑SeQing狂
回复:我写的一个java记事本,有加密解
回复:西湖美景三月天,春雨如酒柳如烟
回复:电脑SeQing狂
回复:电脑SeQing狂
回复:电脑SeQing狂
回复:电脑SeQing狂
回复:电脑SeQing狂
回复:电脑SeQing狂
回复:我写的一个java记事本,有加密解
回复:电脑SeQing狂
回复:随笔
回复:随笔
回复:西湖美景三月天,春雨如酒柳如烟
回复:西湖美景三月天,春雨如酒柳如烟
回复:电脑SeQing狂
回复:电脑SeQing狂
回复:电脑SeQing狂
回复:电脑SeQing狂
回复:我写的一个java记事本,有加密解
回复:电脑SeQing狂
回复:blog转到msn spaces
回复:susan说
回复:susan说
回复:susan说
回复:susan说
回复:电脑SeQing狂
回复:码字
回复:码字
回复:码字
回复:给中国学生的第一封信 (共四封 b
回复:给中国学生的第一封信 (共四封 b
回复:电脑SeQing狂
回复:电脑SeQing狂
回复:电脑SeQing狂


The Fishkeeper
blog名称:Launching Is Launching
日志总数:36
评论数量:79
留言数量:1
访问次数:229866
建立时间:2005年12月11日



Text Me

.: 留言板

签写新留言


Other Fish in the Sea

.: 链接





站点首页 | 联系我们 | 博客注册 | 博客登陆

Sponsored By W3CHINA
W3CHINA Blog 0.8 Processed in 0.043 second(s), page refreshed 144772287 times.
《全国人大常委会关于维护互联网安全的决定》  《计算机信息网络国际联网安全保护管理办法》
苏ICP备05006046号