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] = ‘\0‘; 54 } 55 else 56 { 57 value[i] = ‘\0‘; 58 } 59 i--; 60 for(; i >= 0; i--) 61 { 62 cout << value[i]; 63 } 64 } 65 void BDminus(string str1, string str2) 66 { 67 int len1, len2; 68 len1 = str1.length(); 69 len2 = str2.length(); 70 int flag = 0, i = 0; 71 int Case = 0; 72 char *value = NULL; 73 74 /*因为 string类重载了与string相比较的所有函数,包括<,>,==,>=,<=,!=*/ 75 76 //if (len1 == len2)//两字符串长度相等 77 //{ 78 // int j = 0; 79 // while ((j < len1) && (str1[j] == str2[j])) 80 // { 81 // j++; 82 // } 83 // if (j >= len1)//字符串1等于字符串2 84 // { 85 // Case = 1; 86 // } 87 // else //字符串1不等于字符串2 88 // { 89 // if (str1[j]>str2[j])//字符串1大于字符串2 90 // { 91 // Case = 1; 92 // } 93 // else //字符串1小于字符串2 94 // { 95 // Case = 2; 96 // } 97 // } 98 99 //} 100 //else if (len1 > len2)//字符串1长度大于字符串2 101 //{ 102 // Case = 1; 103 //} 104 //else //字符串1长度小于字符串2 105 //{ 106 // Case = 2; 107 //} 108 109 if(str1>=str2)// (Case==1)//情况一 大减小 110 { 111 value = (char *)malloc(len1*sizeof(char)); 112 int temp; 113 while (len1 > 0 && len2 > 0) 114 { 115 if (flag == 1)//借位处理 116 { 117 if (str1[len1 - 1] == ‘0‘) 118 { 119 str1[len1 - 1] = ‘9‘; 120 flag = 1; 121 } 122 else if (str1[len1 - 1] != ‘0‘) 123 { 124 str1[len1 - 1] = str1[len1 - 1] - 1; 125 flag = 0; 126 } 127 } 128 if (str1[len1 - 1] >= str2[len2 - 1]) 129 { 130 temp = (str1[len1 - 1] - ‘0‘) - (str2[len2 - 1] - ‘0‘); 131 flag = 0; 132 } 133 else 134 { 135 temp = (str1[len1 - 1] - ‘0‘) + 10 - (str2[len2 - 1] - ‘0‘); 136 flag = 1; 137 } 138 value[i++] = (char)(temp+(int)‘0‘); 139 len1--; 140 len2--; 141 if (len1 > 0 && len2 <= 0) 142 { 143 len2 = 1; 144 str2[0] = ‘0‘; 145 } 146 } 147 } 148 else if (str1<str2)//(Case==2)//情况二 小减大 149 { 150 value = (char *)malloc(len2*sizeof(char)); 151 int temp; 152 while (len1 > 0 && len2 > 0) 153 { 154 if (flag == 1)//借位处理 155 { 156 if (str2[len2 - 1] == ‘0‘) 157 { 158 str2[len2 - 1] = ‘9‘; 159 flag = 1; 160 } 161 else if (str2[len2 - 1] != ‘0‘) 162 { 163 str2[len2 - 1] = str2[len2 - 1] - 1; 164 flag = 0; 165 } 166 } 167 if (str2[len2 - 1] >= str1[len1 - 1]) 168 { 169 temp = str2[len2 - 1] -str1[len1 - 1]; 170 flag = 0; 171 } 172 else 173 { 174 temp = str2[len2 - 1] + 10 - str1[len1 - 1]; 175 flag = 1; 176 } 177 value[i++] = (char)(temp + (int)‘0‘); 178 len1--; 179 len2--; 180 if (len1 <= 0 && len2 > 0) 181 { 182 len1 = 1; 183 str1[0] = ‘0‘; 184 } 185 } 186 cout << "-"; 187 } 188 189 value[i] = ‘\0‘; 190 i--; 191 while (i >= 0 && value[i] == ‘0‘) 192 { 193 i--; 194 } 195 if (i < 0) 196 { 197 cout << 0; 198 } 199 else 200 { 201 for(; i >= 0; i--) 202 { 203 cout << value[i]; 204 } 205 } 206 } 207 int main(void) 208 { 209 string str1,str2,result; 210 cin >> str1>>str2; 211 BDplus(str1, str2); 212 BDminus(str1, str2); 213 214 return 0; 215 }
时间: 2024-11-07 14:34:36