Chinaunix首页 | 论坛 | 博客
  • 博客访问: 824726
  • 博文数量: 109
  • 博客积分: 650
  • 博客等级: 上士
  • 技术积分: 1483
  • 用 户 组: 普通用户
  • 注册时间: 2012-03-01 17:29
文章分类

全部博文(109)

文章存档

2016年(5)

2015年(21)

2014年(16)

2013年(38)

2012年(29)

分类: C/C++

2013-09-27 13:03:46


点击(此处)折叠或打开

  1. root@ubuntu:/home/chen/study/unix/bigdata# more bigdata2.c
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <iostream>
  5. using namespace std;
  6. int NumAdd(const char *first,const char *second,char *result,int resultlen)
  7. {
  8. int numlen[2];
  9. numlen[0] = strlen(first);
  10. numlen[1] = strlen(second);
  11. int maxlen = numlen[0] > numlen[1] ? numlen[0]:numlen[1];
  12. if (resultlen < maxlen + 1)
  13. return -1;
  14. int n;
  15. int byteValue[2];
  16. int curByteResult=0;
  17. int addByteResult=0;
  18. for(n =maxlen-1;n >= 0; n--)
  19. {
  20. --numlen[0];
  21. --numlen[1];
  22. if(numlen[0] >= 0)
  23. {
  24.   byteValue[0] = first[numlen[0]] - '0';
  25.   printf("numlen0=%c",first[numlen[0]]);
  26. }
  27. else
  28. {
  29.   byteValue[0] = 0;
  30. }
  31. if(numlen[1] >= 0)
  32. {
  33.   byteValue[1] = second[numlen[1]] - '0';
  34.   printf("numlen1=%c",second[numlen[1]]);
  35. }
  36. else
  37. {
  38.   byteValue[1] = 0;
  39. }
  40. printf("\n");
  41. curByteResult = byteValue[0] + byteValue[1] + addByteResult;
  42. if(curByteResult >= 10)
  43. {
  44.   curByteResult -= 10;
  45.   addByteResult = 1;
  46. /*
  47.   if (n == 0)
  48.   {
  49.    result[0] = '1';
  50.    ++result;
  51.   }
  52.   else
  53.   {
  54.    ++result[n-1];
  55.   }
  56. */
  57.  
  58.   result[n] = '0' + curByteResult;
  59. }
  60. else
  61. {
  62. result[n] = '0' + curByteResult;
  63. addByteResult = 0;
  64. }
  65. printf("result[%d]=%c",n,result[n]);
  66. //printf("%c ",first[0]);
  67. }
  68. //result[0]=0;
  69. result[10]=0;
  70. printf("result=%s\n",result);
  71. //int i;
  72. //for (i =0;i<11;i++)
  73. //printf("result[%d]=%c\n",i,result[i]);
  74. //cout<<*result<<endl;
  75. return 1;
  76. /*
  77. char *n="hello world";
  78. int nlen=0;
  79. nlen=strlen(n);
  80. printf("maxlen=%d\n",nlen);

  81. printf("numlen[0]=%d,numlen[1]=%d\n",numlen[0],numlen[1]);
  82. int numtest;
  83. char *n=first;
  84. numtest=sizeof(first);
  85. printf("numtest=%d\n",numtest);
  86. */
  87. }
  88. int main()
  89. {
  90. char szstr1[]="1234560000";
  91. char szstr2[] = "6666";
  92. char result[100];
  93. NumAdd(szstr1,szstr2,result,100);
  94. printf("result is %s",result);
  95. return 0;
  96. }
  97. root@ubuntu:/home/chen/study/unix/bigdata#


输出结果

点击(此处)折叠或打开

  1. root@ubuntu:/home/chen/study/unix/bigdata# g++ bigdata2.c -o big2
  2. root@ubuntu:/home/chen/study/unix/bigdata# ./big2
  3. numlen0=0numlen1=6
  4. result[9]=6numlen0=0numlen1=6
  5. result[8]=6numlen0=0numlen1=6
  6. result[7]=6numlen0=0numlen1=6
  7. result[6]=6numlen0=6
  8. result[5]=6numlen0=5
  9. result[4]=5numlen0=4
  10. result[3]=4numlen0=3
  11. result[2]=3numlen0=2
  12. result[1]=2numlen0=1
  13. result[0]=1result=1234566666
  14. result is 1234566666

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