1 #include <iostream> 2 using namespace std; 3 struct num 4 { 5 char input[102]; 6 char sign; 7 char data[101]; 8 int len; 9 }; 10 struct num plus_(struct num a,struct num b); 11 struct num minus_(struct num a,struct num b); 12 int main() 13 { 14 while (1) 15 { 16 struct num a, b, c; 17 cin >> a.input; 18 cin >> b.input; 19 if (a.input[0] == '-') 20 { 21 a.sign = '-'; 22 for (int i = 1; i <= strlen(a.input); i++) 23 { 24 a.data[i - 1] = a.input[i]; 25 } 26 } 27 else 28 { 29 a.sign = '+'; 30 for (int i = 0; i <= strlen(a.input); i++) 31 { 32 a.data[i] = a.input[i]; 33 } 34 } 35 if (b.input[0] == '-') 36 { 37 b.sign = '-'; 38 for (int i = 1; i <= strlen(b.input); i++) 39 { 40 b.data[i - 1] = b.input[i]; 41 } 42 } 43 else 44 { 45 b.sign = '+'; 46 for (int i = 0; i <= strlen(b.input); i++) 47 { 48 b.data[i] = b.input[i]; 49 } 50 } 51 if (a.sign == '+' && b.sign == '-') 52 { 53 c = plus_(a, b); 54 int i = 0; 55 for (i = 0; i < strlen(c.data); i++) 56 { 57 if (i % 4 == 0 && i != 0) 58 cout << ','; 59 cout << c.data[i]; 60 } 61 cout << endl << "位数:" << strlen(a.data) << "," << strlen(b.data) << "," << i; 62 } 63 else if (a.sign == '-' && b.sign == '+') 64 { 65 c = plus_(a, b); 66 if (c.data[0] != 0) 67 cout << '-'; 68 int i = 0; 69 for (i = 0; i < strlen(c.data); i++) 70 { 71 if (i % 4 == 0 && i != 0) 72 cout << ','; 73 cout << c.data[i]; 74 } 75 cout << endl << "位数:" << strlen(a.data) << "," << strlen(b.data) << "," << i; 76 } 77 else 78 { 79 int e_flag = 0; 80 int z_flag = 0; 81 int la = strlen(a.data); 82 int lb = strlen(b.data); 83 if (la > lb)e_flag = 1; 84 else if (la < lb)e_flag = -1; 85 else 86 { 87 for (int i = 0, j = 0; i < la, j < lb; i++, j++) 88 { 89 if (a.data[i] > b.data[j]) 90 { 91 e_flag = 1; 92 break; 93 } 94 else if (a.data[i] < b.data[j]) 95 { 96 e_flag = -1; 97 break; 98 } 99 } 100 } 101 if (e_flag == 1) 102 { 103 c = minus_(a, b); 104 } 105 else if (e_flag == -1) 106 { 107 c = minus_(b, a); 108 cout << '-'; 109 } 110 else 111 { 112 c.data[0] = '0'; 113 c.data[1] = '\0'; 114 } 115 int k = -1; 116 while (c.data[++k] == '0') 117 { 118 if (k == strlen(c.data) - 1) 119 { 120 z_flag = 1; 121 break; 122 } 123 } 124 int cnt = 0; 125 for (int i = k; i < strlen(c.data); i++) 126 { 127 if (cnt % 4 == 0 && cnt != 0) 128 cout << ','; 129 cout << c.data[i]; 130 cnt++; 131 } 132 cout << endl << "位数:" << strlen(a.data) << "," << strlen(b.data) << ","; 133 cout << cnt; 134 } 135 cout << endl << endl; 136 } 137 return 0; 138 } 139 struct num plus_(struct num a, struct num b) 140 { 141 int la = strlen(a.data); 142 int lb = strlen(b.data); 143 struct num c = {}; 144 char s[103]; 145 int r = -1; 146 int cx = 0; 147 int i, j; 148 for (i = la - 1, j = lb - 1; i >= 0 && j>=0; i--,j--) 149 { 150 int t1 = a.data[i] - '0'; 151 int t2 = b.data[j] - '0'; 152 int t3 = t1 + t2; 153 if (t3 + cx >= 10) 154 { 155 t3 = t3+cx-10; 156 cx = 1; 157 } 158 else 159 { 160 cx = 0; 161 } 162 s[++r] = t3+'0'; 163 } 164 if (i < 0) 165 { 166 for (int k = j; k >= 0; k--) 167 { 168 int t = b.data[k] - '0'; 169 if (t+cx >= 10) 170 { 171 t = t+cx- 10; 172 cx = 1; 173 } 174 else 175 { 176 t = t + cx; 177 cx = 0; 178 } 179 s[++r] = t + '0'; 180 } 181 } 182 else if (j < 0) 183 { 184 for (int k = i; k >= 0; k--) 185 { 186 int t = a.data[k] - '0'; 187 if (t + cx >= 10) 188 { 189 t = t + cx - 10; 190 cx = 1; 191 } 192 else 193 { 194 t = t + cx; 195 cx = 0; 196 } 197 s[++r] = t + '0'; 198 } 199 } 200 if (cx == 1) 201 { 202 s[++r] = cx + '0'; 203 } 204 int i1, i2; 205 for (i1 = 0, i2 = r; i2 >= 0; i1++, i2--) 206 { 207 c.data[i1] = s[i2]; 208 } 209 c.data[i1] = '\0'; 210 return c; 211 } 212 struct num minus_(struct num a, struct num b) 213 { 214 int la = strlen(a.data); 215 int lb = strlen(b.data); 216 struct num c = {}; 217 char s[103]; 218 int r = -1; 219 int cx = 0; 220 int i, j; 221 for (i = la - 1, j = lb - 1; i >= 0 && j >= 0; i--, j--) 222 { 223 int t1 = a.data[i] - '0'; 224 int t2 = b.data[j] - '0'; 225 int t3 = t1 - t2; 226 if (t3 - cx < 0) 227 { 228 t3 = t3 - cx + 10; 229 cx = 1; 230 } 231 else 232 { 233 t3 = t3 - cx; 234 cx = 0; 235 } 236 s[++r] = t3 + '0'; 237 } 238 if (i < 0) 239 { 240 for (int k = j; k >= 0; k--) 241 { 242 int t = b.data[k] - '0'; 243 if (t - cx < 0) 244 { 245 t = t - cx + 10; 246 cx = 1; 247 } 248 else 249 { 250 t = t - cx; 251 cx = 0; 252 } 253 s[++r] = t + '0'; 254 } 255 } 256 else if (j < 0) 257 { 258 for (int k = i; k >= 0; k--) 259 { 260 int t = a.data[k] - '0'; 261 if (t - cx < 0) 262 { 263 t = t - cx + 10; 264 cx = 1; 265 } 266 else 267 { 268 t = t - cx; 269 cx = 0; 270 } 271 s[++r] = t + '0'; 272 } 273 } 274 int i1, i2; 275 for (i1 = 0, i2 = r; i2 >= 0; i1++, i2--) 276 { 277 c.data[i1] = s[i2]; 278 } 279 c.data[i1] = '\0'; 280 return c; 281 }
100位整形数加减法
猜你喜欢
转载自www.cnblogs.com/2020R/p/12953719.html
今日推荐
周排行