Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1436351
  • 博文数量: 241
  • 博客积分: 10
  • 博客等级: 民兵
  • 技术积分: 2253
  • 用 户 组: 普通用户
  • 注册时间: 2012-04-11 22:27
个人简介

--

文章分类

全部博文(241)

文章存档

2021年(3)

2019年(6)

2018年(1)

2017年(9)

2016年(21)

2015年(50)

2014年(125)

2013年(26)

我的朋友

分类: C/C++

2014-03-21 20:43:45

题目描述:
两大数,用字符串表示,求两数之和,也用字符串表示;
代码:

点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <string.h>

  3. #define N 100

  4. int main()
  5. {
  6.     char str1[N]={0};
  7.     char str2[N]={0};
  8.     char tmp[N] = {0};
  9.     int len_1;
  10.     int len_2;
  11.     int len = 0;
  12.     int flag = 0;
  13.     int i;
  14.     int j;
  15.     int sum=0;
  16.     char* p=NULL;
  17.     scanf("%s",str1);
  18.     scanf("%s",str2);
  19.     if (NULL == str1 || NULL == str2)
  20.     {
  21.         return 0;
  22.     }
  23.     len_1 = strlen(str1);
  24.     len_2 = strlen(str2);
  25.     if (len_1<len_2)
  26.     {
  27.         strncpy(tmp,str1,len_1);
  28.         strncpy(str1,str2,len_2);
  29.         strncpy(str2,tmp,len_1);
  30.         str1[len_2]='\0';
  31.         str2[len_1]='\0';
  32.     }

  33.     len_1 = strlen(str1);
  34.     len_2 = strlen(str2);
  35.     len = len_1>len_2?len_1:len_2;
  36.     memset(tmp,'\0',N);
  37.     for (i=len_1-1,j=len_2-1; i>=0&&j>=0;i--,j--)
  38.     {
  39.         if (flag == 0)
  40.         {
  41.             sum = str1[i]-'0'+str2[j]-'0';
  42.             if (sum>9)
  43.             {
  44.                 flag = sum/10;
  45.                 tmp[len] = '0' + sum%10;
  46.             }
  47.             else
  48.             {
  49.                 tmp[len] = '0' + sum%10;
  50.             }
  51.         }
  52.         else
  53.         {
  54.             sum = str1[i]-'0'+str2[j]-'0' + flag;
  55.             if (sum>9)
  56.             {
  57.                 flag = sum/10;
  58.                 tmp[len] = '0' + sum%10;
  59.             }
  60.             else
  61.             {
  62.                 tmp[len] = '0' + sum%10;
  63.                 flag = 0;
  64.             }
  65.         }
  66.         len--;
  67.     }

  68.     while (i>=0)
  69.     {
  70.         if (flag == 0)
  71.         {
  72.             tmp[len] = str1[i];
  73.         }
  74.         else
  75.         {
  76.             sum = str1[i]-'0' + flag;
  77.             if (sum>9)
  78.             {
  79.                 flag = sum/10;
  80.                 tmp[len] = '0' + sum%10;
  81.             }
  82.             else
  83.             {
  84.                 tmp[len] = '0' + sum%10;
  85.                 flag = 0;
  86.             }
  87.         }
  88.         i--;
  89.         len--;
  90.     }
  91.     if (flag != 0)
  92.     {
  93.         tmp[len] = '0'+flag;
  94.     }
  95.     tmp[len_1+1]='\0';
  96.     p = tmp;
  97.     if (*p == '\0')
  98.     {
  99.         p++;
  100.     }
  101.     printf("%s\n",p);
  102.     return 0;
  103. }
阅读(910) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~