大数相加

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

int helpAdd(const char *a,const char *b,char * result)
{   
    int len_a=strlen (a);
    int len_b=strlen (b);
    int len_max=len_a>len_b?len_a:len_b;
    int len_min=len_a>len_b?len_b:len_a;
    int len_r=len_max+1;
    int carry=0;
    int count=1;
    char * re=new char [len_max+2]; 
    re[len_r] ='\0';
    while(count <=len_min )
    {
       int tmp=(a[len_a-count]-48)+(b[len_b-count]-48)+carry;
       if(tmp>=10)
       {
          carry=1;
          tmp=tmp%10;
       }
       re[len_r-count]=tmp+48;  
       count++;       
    }
    while(count<=len_max)
    {   
        int tmp=0;
        if(len_a>len_b)
        {
            tmp=a[len_a-count]-48+carry;               
        }   
        else
        {
            tmp=b[len_b-count]+carry;    
        }
        if(tmp>=10)
        {
          carry=1;
          tmp=tmp%10;
        }
        re[len_r-count]=tmp+48;  
        count++; 
           
    }
    if(carry=1)
    re[len_r-count]=carry+48;
    else
     re[len_r-count]=0+48;
     
    char *t=re;
     
    while(*re!='\0')
     *(result++)=*(re++);
    *result='\0';
    delete []t;
    return 0;
}

int main()
{
    int r,n;
    char * result=new char (100);
    memset(result,0,100);
    int carry=helpAdd("999342142349","999932142143125",result);
    cout<<result<<endl;
    
    system("pause");
    return 0;    
}

 

转载于:https://www.cnblogs.com/lxdonge/p/4135495.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值