#include <stdio.h> #include <string.h> int main(int argc, const char * argv[]) { char one[100],two[100],sum[100]; int temp=0,lenth,lenthTwo,i,lenthOfSum; scanf("%s",one); getchar();//读取回车字符 scanf("%s",two); lenthTwo = (int)strlen(two); if (strlen(two)>strlen(one))//总是让one存储较长的字符串 { char stringtemp[100]; strcpy(stringtemp, two); strcpy(two, one); strcpy(one, stringtemp); } lenth = (int)(strlen(one)-strlen(two)); for (i=(int)strlen(two)-1; i>=0 ; i--) { sum[i+lenth] = two[i] + one[i+lenth]-‘0‘+temp; if (sum[i+lenth]>57) { sum[i+lenth] = sum[i+lenth] - 10; temp = 1; } else { temp = 0; } } for (i=lenth-1; i>=0; i--) { sum[i] = one[i]+temp; if (sum[i]>57) { sum[i] = sum[i] -10; temp = 1; } else { temp = 0; } } lenthOfSum = strlen(sum); if (temp==1) {//最高位有可能近位,导致数字长度大于string one的长度,所以需要将所有的数字后移一位,前面添一 for (int i=0; i<lenthOfSum; i++) { sum[lenthOfsum-i] = sum[lenthOfsum-i-1]; } sum[0] = ‘1‘; } printf("%s\n",sum); return 0; } //结果演示 //3032832398719038781904783904783294873248783493194394132041834 // 323819473948324891247129084723412348713403284 //------------------------------------------------------------- //3032832398719039105724257853108186120377868216606742845445118
本文出自 “网络学习总结” 博客,请务必保留此出处http://8947509.blog.51cto.com/8937509/1572062
原文地址:http://8947509.blog.51cto.com/8937509/1572062