大数相加
两个很大的数相加,假如直接定义成int ,long,long long ;肯定都是不行的,因为存不下这么大的数;会数据溢出。所以,可以用两个数组把两个数的每一个的位数存下来,再各个位上的数相加,当然,要注意进位。
#include <iostream>
#include<cstring>
using namespace std;
int a[500]={0};<<对数组进行初始化,方便后面不用进行两个数的位数大小判断就可以直接相加
int b[500]={0};
int c[501]={0};
char number1[500];
char number2[500];
int main()
{
int i,j,t1,t2,x,bit=0,maxt;
cin>>number1>>number2;
t1=strlen(number1);<<判断输入的字符长度
t2=strlen(number2);
for(i=t1-1,j=0;i>=0;i--,j++)
{
a[j]=number1[i]-'0';<<将每个字符化成数字存起来
}
for(i=t2-1,j=0;i>=0;i--,j++)
{
b[j]=number2[i]-'0';<<将每个字符化成数字存起来
}
maxt=(t1>t2?t1:t2);<<取位数最大的,进行判断
for(j=0;j<maxt;j++)
{