1 #include<iostream> 2 #include<string> 3 4 using namespace std; 5 6 void BDplus(string str1, string str2) 7 { 8 int len1 = str1.length(); 9 int len2 = str2.length(); 10 char *value = NULL; 11 if (len1 >= len2) 12 { 13 //value= (char*)malloc(len1*sizeof(char)); 14 value = new char[len1*sizeof(char)+1]; 15 } 16 else if (len1 <= len2) 17 { 18 //value = (char*)malloc(len2*sizeof(char)); 19 value = new char[len2*sizeof(char)+1]; 20 } 21 int flag = 0; 22 23 int i = 0; 24 while (len1 > 0 && len2 > 0) 25 { 26 if (len1 <= 0 && len2 > 0) 27 { 28 len1 = 1; 29 str1[0] = '0'; 30 } 31 else if (len1 > 0 && len2 <= 0) 32 { 33 len2 = 1; 34 str2[0] = '0'; 35 } 36 int temp; 37 temp = (int)(str1[len1 - 1] - '0') + (int)(str2[len2 - 1] - '0'); 38 value[i++] = (char)(temp % 10 + flag + (int)'0'); 39 if (temp / 10 == 1) 40 { 41 flag = 1; 42 } 43 else 44 { 45 flag = 0; 46 } 47 len1--; 48 len2--; 49 } 50 if (flag == 1) 51 { 52 value[i++] = '1'; 53 value[i] = '