Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1126078
  • 博文数量: 241
  • 博客积分: 4385
  • 博客等级: 上校
  • 技术积分: 2383
  • 用 户 组: 普通用户
  • 注册时间: 2009-06-07 23:13
文章分类

全部博文(241)

文章存档

2013年(1)

2012年(8)

2011年(62)

2010年(109)

2009年(61)

分类: C/C++

2009-10-02 22:19:42

#include
#include
using namespace std;
char *AddBigInt(char *str1, char *str2)
{
 int carry = 0;           //进位,初始化为0
 int i = strlen(str1) - 1;    //指向第一个加数的最低位
 int j = strlen(str2) - 1;    //指向第二个加数的最低位
 int maxLen = strlen(str1) >= strlen(str2) ?
  (strlen(str1) + 1) : (strlen(str2) + 1);
 char *result = new char[maxLen + 1];
 result[maxLen] = '\0';   //字符串最后一位为'\0'
 int k = strlen(result) - 1;  //指向结果数组的最低位
 while((i >= 0)&&(j >= 0))
 {
  result[k] = ((str1[i] - '0') + (str2[j] - '0' + carry))%10 + '0';
  carry = ((str1[i] - '0') + (str2[j] - '0' + carry))/10;
  --i;
  --j;
  --k;
 }
 while(i >= 0)
 {
  result[k] = ((str1[i] - '0') + carry)%10 + '0';
  carry = ((str1[i] - '0') + carry)/10;
  --i;
  --k;
 } 
 while(j >= 0)
 {
  result[k] = ((str2[j] - '0') + carry)%10 + '0';
  carry = ((str2[j] - '0') + carry)/10;
  --j;
  --k;
 }
 result[0] = carry + '0';
 
 if(result[0] != '0')
  return result;
 else
  return (result + 1);
}
int main()
{
 char num1[] = "123456789323";
 char num2[] = "45671254563123";
 char *result = NULL;
 result = AddBigInt(num1, num2);
 cout<管理员在2009年8月13日编辑了该文章文章。
-->
阅读(1846) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~