这个大数加法该怎么样实现?

projoy 2009-03-03 09:40:19
如123+34,首先在前面加0不齐,0123,0034,然后进行运算,最后把和去掉多余的0,即0157变为157,我想知道如何在前面补0和删除0,难道是用数组来回移动吗?
...全文
202 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
yichuankun 2009-03-05
  • 打赏
  • 举报
回复
大数运算一般都是用数组来实现。
tommyxdf 2009-03-05
  • 打赏
  • 举报
回复
刚好我前两天做了个,用字符串输入,再用数组切割运算。
这是我的代码,希望对你有点用处;
#include <iostream>
#include <string>
#include <stdlib.h>
using namespace std;

int* addNumber(int*, int*);

int main()
{
string a, b; //two strings to store the input
int index; //the variable to mark the position
int* first = new int[100]; //pointer to an array storing the first number
int* second = new int[100]; //pointer to an array storing the second number
int* total = new int[101]; //pointer to an array storing the result

//initialize the arrays
for (int n=0; n<100; n++)
{
first[n] = 0;
second[n] = 0;
}

//prompt the user to input the first number
cout<<"Please input the first number:"<<endl;
cin>>a;

//the length of string a
int lengthA = a.length();

//prompt the user to input the first number
cout<<"Please input the second number:"<<endl;
cin>>b;

//the length of string b
int lengthB = b.length();

//convert every three characters in string a to numbers and store them in array
for(int i=0; i<(lengthA/3); i++)
{
first[i] = atoi(a.substr((lengthA-3*i-3),3).c_str());
}
if(lengthA%3!=0)
{
first[lengthA/3] = atoi(a.substr(0,lengthA%3).c_str());
}

//convert every three characters in string b to numbers and store them in array
for (int j=0; j<(lengthB/3); j++)
{
second[j] = atoi(b.substr((lengthB-3*j-3),3).c_str());
}
if(lengthB%3!=0)
{
second[lengthB/3] = atoi((b.substr(0,lengthB%3).c_str()));
}

//add the numbers in two arrays and store them in total
total = addNumber(first, second);

cout<<endl;

//find out the index of the first element of the result
for (int k=100; k>=0; k--)
{
if(total[k]!=0)
{
index = k;
break;
}
}


//Print out the final result
cout<<"The result is: "<<endl;
cout<<total[index];
for (int l=index-1; l>=0; l--)
{
if(total[l]<100 && total[l]>=10)
{
cout<<"0"<<total[l];
}

else if(total[l]<10)
{
cout<<"00"<<total[l];
}

else
{
cout<<total[l];
}
}
cout<<endl;

return 0;
}

//add every element in two arrays
int* addNumber(int* first, int* second)
{
int* total = new int[101];
for (int i=0; i<101; i++)
{
total[i] = 0;
}

for (int j=0; j<100; j++)
{
if(first[j]+second[j]>=1000)
{
total[j] = total[j] + first[j] + second[j] - 1000;
total[j+1] = 1;
}

else
{
total[j] = total[j] + first[j] + second[j];
}
}
return total;
}
icedfire2002 2009-03-05
  • 打赏
  • 举报
回复
mark
sxbwelcome 2009-03-05
  • 打赏
  • 举报
回复
在将数据存储到数组时应该以小数点为基准对其
qqlpp 2009-03-05
  • 打赏
  • 举报
回复
mark
glorywu 2009-03-05
  • 打赏
  • 举报
回复
高精度运算
定义数组a[100]存1234
a[0]表示4位,然后逆序存,即a[1]=4,a[2]=3,a[3]=2,a[4]=1
同样可以开一个b数组。
然后依次相加就可以,如果大于10进位。
kingmon0532 2009-03-04
  • 打赏
  • 举报
回复
这是一个关于计算器的问题,你可以用字符串搜索,就是那些+-*/符号,接着将符号前后的数字联立起来计算即可
  • 打赏
  • 举报
回复
做大数算法直接用gmp库就可以了.
zceve 2009-03-04
  • 打赏
  • 举报
回复
不知道LZ在想什么。
0035前面的0有什么意义吗》》???
如果你要实现“计算器”
建议你看下“编译原理”中关于:文法,与语法的章节。
“语法分析树”

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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