Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1176997
  • 博文数量: 573
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 66
  • 用 户 组: 普通用户
  • 注册时间: 2016-06-28 16:21
文章分类

全部博文(573)

文章存档

2018年(3)

2016年(48)

2015年(522)

分类: C/C++

2015-12-02 17:05:45


点击(此处)折叠或打开

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

  4. /*2个很长的整数相加*/

  5. /*2个很长的整数相乘*/
  6. void fun(char * shu_1, char * shu_2, char * he)
  7. {
  8.         
  9.         /*先找到数组shu_1,shu_2的最后一个元素*/
  10.         
  11. }


  12. char * Trim_qian0(char * pstr)
  13. {
  14.     if(NULL == pstr)
  15.     {
  16.         return NULL;
  17.     }
  18.     char * phead = pstr;
  19.     char * ptail = pstr + strlen(pstr) - 1;
  20.     while((*phead == '0') && (phead < ptail)) phead++; //去掉前导0
  21.     //while((*ptail == ' ') && (ptail > phead)) ptail--;
  22.     *(ptail + 1) = '\0';
  23.     memcpy(pstr, phead, sizeof(char)*(ptail - phead + 2));
  24.     return pstr;
  25. }

  26. int main(int argc, char * * argv)
  27. {
  28.         char    shu_1[128];
  29.         char    shu_2[128];
  30.         char    he[128];
  31.         
  32.         memset(shu_1, 0, sizeof(shu_1));
  33.         memset(shu_2, 0, sizeof(shu_2));
  34.         memset(he, '0', sizeof(he)); /*先初始化成空格,再用trim掉空格*/

  35.         printf("please long shu_1 :");
  36.         scanf("%s", shu_1);
  37.         printf("please long shu_2 :");
  38.         scanf("%s", shu_2);

  39.         /*fun(shu_1, shu_2, he);*/
  40.         int shul_len = 0; /*数1的长度*/
  41.         int shu2_len = 0;     /*数2的长度*/
  42.         int max_len = 0;     /*数1 数2中最大长度*/
  43.         int he_len = sizeof(he);         /*和数的最大长度*/
  44.         shul_len = strlen(shu_1);
  45.         shu2_len = strlen(shu_2);
  46.         max_len = (shul_len > shu2_len) ? shul_len : shu2_len;
  47.         if(shul_len == max_len)
  48.         {
  49.                 /*
  50.                 if(shu_1 == '9')
  51.                 {
  52.                         he_len
  53.                 }
  54.                 */
  55.         }
  56.         
  57.         printf("shul_len=[%d]\n", shul_len);
  58.         printf("shu2_len=[%d]\n", shu2_len);
  59.         printf("max_len =[%d]\n", max_len); /*he数的最大长度是 max_len 或者 max_len+1*/
  60.         /*先找到数组shu_1,shu_2的最后一个元素*/
  61.         int i=0;
  62.         int j=0;
  63.         int temp=0;
  64.         
  65.         he[he_len-1] = '0'; /*和数的最后一个先写'0'*/
  66.         printf("[%s] + [%s]\n", shu_1, shu_2);
  67.         for(i=0; i<max_len; i++)
  68.         {
  69.                 if((shul_len > i) && (shu2_len > i))
  70.                 {
  71.                         temp = shu_1[shul_len-1-i]-'0' + shu_2[shu2_len-1-i]-'0' + he[he_len-1-i]-'0';
  72.                 }
  73.                 else if((shul_len > i) && (shu2_len <= i))
  74.                 {
  75.                         temp = shu_1[shul_len-1-i]-'0' + he[he_len-1-i]-'0';
  76.                 }
  77.                 else if((shul_len <= i) && (shu2_len > i))
  78.                 {
  79.                         temp = shu_2[shu2_len-1-i]-'0' + he[he_len-1-i]-'0';
  80.                 }
  81.                 
  82.                 printf("temp=[%d]\n", temp);
  83.                 if(temp <= 9) /*没产生进位*/
  84.                 {
  85.                         he[he_len-1-i] = temp+'0'; /*得到个位数*/
  86.                 }
  87.                 else /*产生进位*/
  88.                 {
  89.                         he[he_len-1-i] = (temp % 10) + '0';
  90.                         he[he_len-1-i-1] = 1 + '0';
  91.                 }
  92.         }
  93.         Trim_qian0(he);
  94.         printf("[%s] + [%s]\n", shu_1, shu_2);
  95.         printf("=[%s]\n", he);

  96.         return 0;
  97. }


  98. /*
  99. #include <stdio.h>
  100. #include <stdlib.h>
  101. #include <string.h>
  102. #define MaxSize 200
  103.   
  104. int main()
  105. {
  106.   
  107.     int an1[MaxSize+10];
  108.     int an2[MaxSize+10];
  109.     char input1[MaxSize+10];
  110.     char input2[MaxSize+10];
  111.     int i,j;
  112.   
  113.     //先输入两个加数的值,然后存放在字符串数组中
  114.     scanf("%s %s",input1,input2);
  115.     //printf("%s %s",input1,input2);
  116.     //用memset对an1和an2数组初始化
  117.     memset(an1,0,sizeof(an1));
  118.     memset(an2,0,sizeof(an2));
  119.   
  120.      // for(i=0;i<strlen(input1);i++)
  121.    // printf("%d %d",an1[i],an1[i]);
  122.     //将输入到input1,input2字符数组中的字符转换为数字形式
  123.     int nLen1=strlen(input1);
  124.     for(i=nLen1-1,j=0;i>=0;i--,j++)
  125.     {
  126.         an1[j]=input1[i]-'0';
  127.     }
  128.     int nLen2=strlen(input2);
  129.     for(i=nLen2-1,j=0;i>=0;i--,j++)
  130.     {
  131.         an2[j]=input2[i]-'0';
  132.     }
  133.     //for(i=0;i<strlen(input1);i++)
  134.    // printf("%d %d",an1[i],an2[i]);
  135.    //开始计算,将结果保存到an1中,注意进位
  136.    int temp=0;
  137.     for(i=0;i<MaxSize;i++)
  138.     {
  139.        temp=an1[i]+an2[i];
  140.        if(temp/10==0)
  141.        {
  142.          an1[i]=temp;
  143.        }else{
  144.         an1[i]=temp%10;
  145.         an1[i+1]+=1;
  146.        }
  147.     }
  148.     //输出,跳过开始为0的值,从an1 下标由大到小输出
  149.     int startoutput=0;//加入这个bool变量是为了当发现第一个非0数后一会就不用判断是否为0了,直接输出剩余的数组值
  150.     for(i=MaxSize;i>=0;i--)
  151.     {
  152.         if(startoutput)
  153.             printf("%d",an1[i]);
  154.         else if(an1[i])
  155.         {
  156.             printf("%d",an1[i]);
  157.             startoutput=1;
  158.         }
  159.     }
  160.     printf("\n");
  161.     //之前提交一直wrong answer,看了大家做的,才知道自己忽略了0+0的情况
  162.     if(startoutput==0)//or if(!startoutput)
  163.         printf("0");
  164.   
  165.     return 0;
  166. }

  167. */



  168. /*
  169. #include <stdio.h>
  170. #include <stdlib.h>
  171. #include <string.h>
  172. #define MaxSize 200

  173. int main()
  174. {
  175.     int an1[MaxSize+10];
  176.     int an2[MaxSize+10];
  177.     int bn[MaxSize+MaxSize];//存放处理结果
  178.     char str1[MaxSize+10];
  179.     char str2[MaxSize+10];
  180.     int i,j;
  181.     //输入数据
  182.     scanf("%s %s",str1,str2);
  183.     //初始化int数组
  184.     memset(an1,0,sizeof(an1));
  185.     memset(an2,0,sizeof(an2));
  186.     memset(bn,0,sizeof(bn));
  187.     //将输入的字符数组值转入int数组
  188.     int len1=strlen(str1);
  189.     for(i=len1-1,j=0;i>=0;i--)
  190.     {
  191.         an1[j]=str1[i]-'0';
  192.         j++;
  193.     }
  194.     int len2=strlen(str2);
  195.     for(i=len2-1,j=0;i>=0;i--)
  196.     {
  197.         an2[j]=str2[i]-'0';
  198.         j++;
  199.     }
  200.      
  201.     for(i=0;i<len1;i++)
  202.     {
  203.         printf("%d %d\n",an1[i],an2[i]);
  204.     }
  205.     //计算将结果存储到bn数组中
  206.     int t;
  207.     int temp=0;
  208.     for(j=0;j<len2;j++)
  209.     {
  210.         t=j;
  211.         for(i=0;i<len1;i++)
  212.         {
  213.             temp=an1[i]*an2[j];
  214.             if(temp/10>0)
  215.             {
  216.                bn[t]+=temp%10;
  217.                bn[t+1]+=temp/10;
  218.              // printf("~~1%d,%d,\n",bn[t],bn[t+1]);
  219.             }else
  220.             {
  221.                 bn[t]+=temp;
  222.             }
  223.   
  224.             int dd;
  225.              dd=bn[t];
  226.             if(dd/10>0)
  227.             {
  228.                bn[t]=dd%10;
  229.                bn[t+1]+=dd/10;
  230.             }
  231.           // printf("~~2%d,%d,\n",bn[t],bn[t+1]);
  232.             t++;
  233.         }
  234.   
  235.     }
  236.   
  237.     //显示出来,包括特殊情况的处理,跳过之前为0的数据
  238.     int startoutput=0;
  239.     for(i=MaxSize*2-1;i>=0;i--)
  240.     {
  241.         if(startoutput)
  242.         printf("%d",bn[i]);
  243.         else if(bn[i]!=0)
  244.         {
  245.             printf("%d",bn[i]);
  246.             startoutput=1;
  247.         }
  248.     }
  249.     if(startoutput==0)
  250.     printf("0");
  251.     printf("\n");
  252.   
  253.   
  254.     return 0;
  255. }

  256. */

阅读(505) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~