華為上機題:高精度整數加法

來源:互聯網
上載者:User

int getnum(const char* num,int i)
{
int k=0;
if (i<0)
k='0';
else
if ((num[i]<'0')||(num[i]>'9'))
{
k='0';
}
else
k=num[i];
return k;
}
void add (const char *num1, const char *num2, char *result) 
{
int num1_length=strlen(num1);
int num2_length=strlen(num2);
int i;
int k=num1_length>num2_length?num1_length:num2_length;
int num1_factnum,num2_factnum;
int is_lastfull=0;
char* retu=result;
if((num1[0]=='-')&&(num2[0]=='-'))//兩個數同負
{
for (i=0;i<k-1;i++)
{
num1_factnum=getnum(num1,num1_length-1-i);
num2_factnum=getnum(num2,num2_length-1-i);
if(num1_factnum-'0'+num2_factnum-'0'+is_lastfull>9)
{
retu[k-1-i]=num1_factnum-'0'+num2_factnum+is_lastfull-10;
is_lastfull=1;
}
else
{
retu[k-1-i]=num1_factnum-'0'+num2_factnum+is_lastfull;
is_lastfull=0;
}
}
retu[k]='\0';
if (retu[1]>'9')
{
retu[1]=retu[1]-10;
strcpy(retu+2,retu+1);
retu[1]='1';
}
retu[0]='-';
}
if((num1[0]!='-')&&(num2[0]!='-'))//兩個正數相加
{
for (i=0;i<k;i++)
{
num1_factnum=getnum(num1,num1_length-1-i);//有用的數字
num2_factnum=getnum(num2,num2_length-1-i);
if(num1_factnum-'0'+num2_factnum-'0'+is_lastfull>9)
{
retu[k-1-i]=num1_factnum-'0'+num2_factnum+is_lastfull-10;
is_lastfull=1;
}
else
{
retu[k-1-i]=num1_factnum-'0'+num2_factnum+is_lastfull;
is_lastfull=0;
}
}
retu[k]='\0';
if (retu[0]>'9')
{
retu[0]=retu[0]-10;
strcpy(retu+1,retu);
retu[0]='1';
}

}
  if(((num1[0]=='-')&&(num2[0]!='-'))||((num1[0]!='-')&&(num2[0]=='-')))//一正一負的情況

char *postive=const_cast<char *>(num1);
char *native=const_cast<char *>(num2);
if(num1[0]=='-')
{
postive=const_cast<char *>(num2);
native=const_cast<char *>(num1);
}
int postive_length=strlen(postive);
int native_length=strlen(native);
bool ispositive=false;
if (postive_length>native_length-1)
{
ispositive=true;
}
else
if (postive_length==native_length-1)
{
for (i=0;i<postive_length;i++)
{
if (postive[i]>native[i+1])
{
ispositive=true;
}
}
}
if (ispositive)
{
for (i=0;i<k-1;i++)
{
num1_factnum=getnum(native,native_length-1-i);
num2_factnum=getnum(postive,postive_length-1-i);
if(num2_factnum<num1_factnum)
{
retu[k-1-i]=num2_factnum-num1_factnum+10-is_lastfull+'0';
is_lastfull=1;
}
else
{
retu[k-1-i]=num2_factnum-num1_factnum-is_lastfull+'0';
is_lastfull=0;
}
}
retu[k]='\0';
for(i=0;(retu[i]<'1')||(retu[i]>'9');i++);//去掉前面的無效數字0
if (i)
{
strcpy(retu,retu+i);
}
}
else//結果是負數的情況
{
for (i=0;i<k-1;i++)
{
num1_factnum=getnum(native,native_length-1-i);
num2_factnum=getnum(postive,postive_length-1-i);
if(num1_factnum-is_lastfull<num2_factnum)
{
retu[k-1-i]=num1_factnum-num2_factnum+10-is_lastfull+'0';
is_lastfull=1;
}
else
{
retu[k-1-i]=num1_factnum-num2_factnum-is_lastfull+'0';
is_lastfull=0;
}
}
retu[k]='\0';
for(i=0;(retu[i+1]<'1')||(retu[i+1]>'9');i++);//去掉前面的無效數字0
if (i)
{
strcpy(retu+1,retu+1+i);
}
retu[0]='-';
}
}
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.