如何模拟实现两个大数加法?

sherrik 2009-02-23 08:03:00
两个大数(整数)相加,假定每个数都可以有1000位,如何实现两个大数的相加?
...全文
685 21 打赏 收藏 转发到动态 举报
写回复
用AI写文章
21 条回复
切换为时间正序
请发表友善的回复…
发表回复
ypb362148418 2009-02-25
  • 打赏
  • 举报
回复
两个大数相加的问题要采用链表,然后将每一位数字作为一个ListNode,然后每个Node相加,最后再将List输出就可以了,乘法也是这样的,但是除法似乎很难
ysysbaobei 2009-02-25
  • 打赏
  • 举报
回复
mark,顶
redoffice 2009-02-25
  • 打赏
  • 举报
回复
学习咯,感觉这下赚了
insulted 2009-02-25
  • 打赏
  • 举报
回复
mark and study!
太乙 2009-02-25
  • 打赏
  • 举报
回复
http://download.csdn.net/source/403510

n年前写的,lz可以看看~~参考参考

仅限参考哈~~!~
xianyuxiaoqiang 2009-02-25
  • 打赏
  • 举报
回复
你可以把两个数看作字符串,先对齐,然后从低位向高位依次相加并处理进位。

结果放在第三个字符串中。

自己写一个比较有意义。
lzonline01 2009-02-24
  • 打赏
  • 举报
回复
就用数组存储吧
  • 打赏
  • 举报
回复
楼主是想学习大数运算的算法,还是基于应用?基于应用直接下openSSL,crypt++等大数库就可以用了.
要算法的,我以前也找了不少,发一部分你看看吧.


/****************************************************************************************
大数相加
调用形式:N.Add(A)
返回值:N+A
****************************************************************************************/
CBigInt CBigInt::Add(CBigInt& A)
{
CBigInt X;
X.Mov(*this);
unsigned carry=0;
unsigned __int64 sum=0;
if(X.m_nLength<A.m_nLength)X.m_nLength=A.m_nLength;
for(unsigned i=0;i<X.m_nLength;i++)
{
sum=A.m_ulValue[i];
sum=sum+X.m_ulValue[i]+carry;
X.m_ulValue[i]=(unsigned long)sum;
carry=(unsigned)(sum>>32);
}
X.m_ulValue[X.m_nLength]=carry;
X.m_nLength+=carry;
return X;
}

CBigInt CBigInt::Add(unsigned long A)
{
CBigInt X;
X.Mov(*this);
unsigned __int64 sum;
sum=X.m_ulValue[0];
sum+=A;
X.m_ulValue[0]=(unsigned long)sum;
if(sum>0xffffffff)
{
unsigned i=1;
while(X.m_ulValue[i]==0xffffffff){X.m_ulValue[i]=0;i++;}
X.m_ulValue[i]++;
if(m_nLength==i)m_nLength++;
}
return X;
}
lzh9955 2009-02-24
  • 打赏
  • 举报
回复
up
elmnd 2009-02-24
  • 打赏
  • 举报
回复
俺也发一个, 不过是俺自己写的。写的很烂。。。

#include <cstdlib>
#include <iostream>
using namespace std;

struct BigNum
{
char string[100];
int len;
int istr[100];
}a, b, c;

void CalLen(BigNum &a)
{
a.len = 0;
for(int i = 0; a.string[i] != '\0'; ++i, ++a.len);
}

void char_to_int(BigNum &a)
{
for(int i = 0; i <= a.len; ++i)
a.istr[i] = a.string[i] - 48;
}

void zero(BigNum &a)
{
for(int i = 0; i < a.len; ++i)
a.istr[i] = 0;
}
/////////////////////////////////////////////////////////////////
void CalPlus(BigNum &c, BigNum &a, BigNum &b)
{
c.len = (a.len>=b.len?a.len:b.len);
for(int i = 0; i <= c.len; ++i)
{
c.istr[i] += a.istr[i] + b.istr[i];
if(c.istr[i] >= 10)
{
c.istr[i - 1] += c.istr[i]%10;
c.istr[i]/= 10;
}
// printf("%d ", c.istr[i]);
}
}

void print(BigNum & a)
{
for(int i = 0; i < a.len; ++i)
{
printf("%d ", a.istr[i]);
}
printf("\n");
}
////////////////////////////////////////////////////////////////
int main(int argc, char *argv[])
{
printf("input a and b:\n");
scanf("%s", &a.string);
scanf("%s", &b.string);

CalLen(a);
CalLen(b);
zero(a);
zero(b);
char_to_int(a);
char_to_int(b);
CalPlus(c, a, b);
printf("The result is: \n");
print(c);

printf("\n");
system("PAUSE");
return EXIT_SUCCESS;
}

pengzhixi 2009-02-24
  • 打赏
  • 举报
回复
mark
zyaijo01 2009-02-24
  • 打赏
  • 举报
回复
大体思想知道,不过比较难写。还有个相似的问题:计算R^n其中R是一实数,n为一正整数,求其精确值。正在研究中……
dongpy 2009-02-24
  • 打赏
  • 举报
回复
用数组存储大数,然后循环相加。
waizqfor 2009-02-23
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 sherrik 的回复:]
有没有简单点的呢?
[/Quote]
里面也有减法的操作 通过的是链表实现的
tangshuiling 2009-02-23
  • 打赏
  • 举报
回复

以前写的,楼主看看,没考虑负数、小数的情况,不过应该差不多
#include <string>
#include <algorithm>
#include <iostream>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
string one(1000,'9');
string two(800,'8');
int dalt=abs(one.size()-two.size());
string temp(dalt,'0');
int maxsize=one.size()>=two.size()?one.size()+1:two.size()+1;
if(one.size()>=two.size())
{
temp+=two.c_str();
two=temp;
}
else
{
temp+=one.c_str();
one=temp;
}
string result(maxsize,'0');
cout<<one<<endl;
cout<<'+'<<endl;
cout<<two<<endl;
cout<<'='<<endl;
size_t i=0,carry=0;
for( ;i<maxsize;++i)
{
result[i] =(one[i]-'0'+two[i]-'0'+carry)%10+'0';
carry=(one[i]-'0'+two[i]-'0'+carry)/10;
}
result[i]=carry+'0';
cout<<result<<endl;
return 0;
}
maenxiang 2009-02-23
  • 打赏
  • 举报
回复

class Integer
{
private:
unsigned char buf[128]; // 够长了吧
public:
Integer & operator+(Integer & i)
{
int tmpi=0;
for(int i = 0; i < 128; i++)
{
tmpi = tmpi>>8; // 上次的进位
tmpi = buf[i] + i.buf[i] + tmpi;
buf[i] = tmpi;
}
}
}


注:字节序和Integer构造没考虑
resultridgepole 2009-02-23
  • 打赏
  • 举报
回复
采用高精度来实现,把数字像字母一样一个一个读入,然后按照位数单独相加;
写了一个C语言的:
#include <stdio.h>
#include <stdlib.h>

main()
{
int a[2000];
int b[2000];
int c[2000];
char ch;

int m,n;
int i;
int j;
int k;
int flag;

i=1;
scanf("%c",&ch);
while(ch>='0'&&ch<='9')
{
a[i]=ch-'0';
i++;
scanf("%c",&ch);
}
m=i-1;

i=1;
scanf("%c",&ch);
while(ch>='0'&&ch<='9')
{
b[i]=ch-'0';
i++;
scanf("%c",&ch);
}
n=i-1;

i=m;
j=n;
k=1;
flag=0;

while(i>=1&&j>=1)
{
c[k]=(a[i]+b[j]+flag)%10;
flag=(a[i]+b[j]+flag)/10;
k++;
i--;
j--;
}

while(i>=1)
{
c[k]=(a[i]+flag)%10;
flag=(a[i]+flag)/10;
k++;
i--;
}

while(j>=1)
{
c[k]=(b[j]+flag)%10;
flag=(b[j]+flag)/10;
k++;
j--;
}
if (flag==1)
printf("1");
for(i=k-1;i>=1;i--)
printf("%d",c[i]);

system("pause");
return 0;
}
sherrik 2009-02-23
  • 打赏
  • 举报
回复
有没有简单点的呢?
waizqfor 2009-02-23
  • 打赏
  • 举报
回复
转一个算法 大数相加的

#include <iostream>
#include <string>
#include <iomanip>
#include <vector>
using namespace std;

struct List{
int data;
List * next;
};

List *make_list(string &str)
{
List *head=new List;
List *last=head,*p;
string num;
int pos=str.size();
while(!str.empty()){
pos=str.rfind(',',pos);
if (pos==str.npos){
last->data=atoi(str.c_str());
str.clear();
}
else {
num=str.substr(pos+1,str.size()-pos); //pos还是正向搜索时在字符串中的位置
//str=str.substr(pos+1,str.size()-pos);
str.erase(pos,str.size()-pos);
last->data=atoi(num.c_str());
p=new List;
last->next=p;
last=p;
}
}
last->next=NULL;
return head;
}

List *Add(const List *list1,const List *list2)
{ //类似于链表合并操作
List *mylist=new List;
List *head,*last;
last=head=mylist;
int sum,inc=0;
const List *p=list1;
const List *q=list2;
while(p!=NULL&&q!=NULL){
sum=p->data+q->data+inc;
head->data=sum%1000;
inc=sum/1000;
p=p->next;
q=q->next;
last=new List;
head->next=last;
head=last;
}
while(p!=NULL){
sum=p->data+inc;
head->data=sum%1000;
inc=sum/1000;
p=p->next;
if(p==NULL)
head->next=NULL;
else{
last=new List;
head->next=last;
head=last;
}
}
while(q!=NULL){
sum=q->data+inc; //如果链表中结点的值与进位和大于1000,可能一直有进位
head->data=sum%1000;
inc=sum/1000;
q=q->next;
if(q==NULL)
head->next=NULL;
else{
last=new List;
head->next=last;
head=last;
}
}
head->next=NULL;
return mylist;
}

List *Minus(const List *list1,const List *list2)
{
List *mylist=new List;
List *head,*last;
last=head=mylist;
int sum,inc=0;
const List *p=list1;
const List *q=list2;
while(p!=NULL&&q!=NULL){
sum=p->data-q->data-inc;
if(sum<0)
{
head->data=1000+sum; //被减数不够减时
inc=1; //向上边最多借位为1,即1000
}
else {
head->data=sum;
inc=0;
}
p=p->next;
q=q->next;
if(p==NULL) head->next=NULL; //p指向空,head同时结束
else { //否则再建一新的结点
last=new List;
head->next=last;
head=last;
}
}
while(p!=NULL){
sum=p->data-inc; //如果链表中结点的值一直为1,可能一直借位
if(sum<0)
{
head->data=1000+sum;
inc=1;
}
else {
head->data=sum;
inc=0;
}
p=p->next;
if(p==NULL)
head->next=NULL;
else{
last=new List;
head->next=last;
head=last;
}
}
return mylist;
}

List *Mus(const List *list1,const List *list2,int &sign)
{
const List *p=list1;
const List *q=list2;
int inc=0;
while (p->next!=NULL&&q->next!=NULL)
{
if(p->data>q->data) inc=1; //当被减数与减数在链表中有相同的长度时,要找出其中较大者
else inc=0;
p=p->next;
q=q->next;
}
if(p->next==NULL&&q->next==NULL&&inc||p->next!=NULL) //被减数较大
return Minus(list1,list2);
else{
sign=1; //减数较大,标记为负
return Minus(list2,list1);
}
}

void show(const List *mylist,int sign=0)
{
vector<int> ivec;
while (mylist!=NULL)
{
ivec.push_back(mylist->data);
mylist=mylist->next;;
}
vector<int>::reverse_iterator ite=ivec.rbegin(); //反向迭代器
while(*ite==0) ite++;
cout<<"The result is :"<<endl;
cin.sync();
if(sign) cout<<'-';
cout<<*ite;
for (ite++;ite!=ivec.rend();ite++)
{
cout<<',';
cout<<setw(3)<<setfill('0')<<*ite;
}
cout<<endl;
}

int main()
{
List *list1,*list2,*list3;
string str;
char c;
int sign=0;
cout<<"Please input the first number."<<endl;
cin>>str;
list1=make_list(str);
cout<<"Please input the second number."<<endl;
cin>>str;
list2=make_list(str);
cout<<"Please input the operator."<<endl;
cin>>c;
if (c=='+')
list3=Add(list1,list2);
else if(c=='-')
list3=Mus(list1,list2,sign);
show(list3,sign);
system("pause");
return 0;
}
fenjianren 2009-02-23
  • 打赏
  • 举报
回复
http://www.ithao123.com/beginner/10099.html
这个可以看看,虽然效率不高,也可以学习一下
加载更多回复(1)

64,646

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧