100位有符号整形数加法(数组)
1 //加法器,若要变为减法器,只需改动相应的判断条件即可 2 #include <iostream> 3 using namespace std; 4 struct num//储存多位的整形数字 5 { 6 char input[102]; 7 char sign; 8 char data[101]; 9 int len; 10 }; 11 struct num plus_(struct num a, struct num b);//无符号整形的加法 12 struct num minus_(struct num a, struct num b);//无符号整形的减法,要求a>b 13 int main() 14 { 15 while (1)//while循环用作多次测试 16 { 17 struct num a, b, c;//a,b为加数,c储存二者相加结果 18 cin >> a.input; 19 cin >> b.input; 20 if (a.input[0] == '-')//把a和b的符号存起来,把去掉符号的数放到结构体的另一个数组里面 21 { 22 a.sign = '-'; 23 for (int i = 1; i <= strlen(a.input); i++) 24 { 25 a.data[i - 1] = a.input[i]; 26 } 27 } 28 else 29 { 30 a.sign = '+'; 31 for (int i = 0; i <= strlen(a.input); i++) 32 { 33 a.data[i] = a.input[i]; 34 } 35 } 36 if (b.input[0] == '-') 37 { 38 b.sign = '-'; 39 for (int i = 1; i <= strlen(b.input); i++) 40 { 41 b.data[i - 1] = b.input[i]; 42 } 43 } 44 else 45 { 46 b.sign = '+'; 47 for (int i = 0; i <= strlen(b.input); i++) 48 { 49 b.data[i] = b.input[i]; 50 } 51 } 52 //根据a,b符号的各种情况,调用plus_e和minus_函数进行运算 53 if (a.sign == '+' && b.sign == '+') 54 {//a,b为正数,直接相加 55 c = plus_(a, b); 56 const int len_ = strlen(c.data); 57 int i = 0; 58 for (i = 0; i < len_; i++) 59 { 60 if ((len_ - i) % 4 == 0 && i!=0) 61 cout << ','; 62 cout << c.data[i]; 63 } 64 cout << endl << "位数:" << strlen(a.data) << "," << strlen(b.data) << "," << i; 65 } 66 else if (a.sign == '-' && b.sign == '-') 67 {//a,b皆为负数,则a,b去掉负号后相加,在结果前添一个负号 68 c = plus_(a, b); 69 if (c.data[0] != 0) 70 cout << '-'; 71 int i = 0; 72 const int len_ = strlen(c.data); 73 for (i = 0; i < len_; i++) 74 { 75 if ((len_ - i) % 4 == 0 && i != 0) 76 cout << ','; 77 cout << c.data[i]; 78 } 79 cout << endl << "位数:" << strlen(a.data) << "," << strlen(b.data) << "," << i; 80 } 81 else 82 {//a,b一正一负,将a,b去掉负号后,用它们之间大的减去小的 83 int e_flag = 0;//a比b长(也即a>b)则为1,a比b短(即a<b)则为-1 84 int z_flag = 0;//结果为0标志 85 int la = strlen(a.data); 86 int lb = strlen(b.data); 87 if (la > lb)e_flag = 1; 88 else if (la < lb)e_flag = -1; 89 else//若a,b等长 90 {//下面判断它们的大小 91 for (int i = 0, j = 0; i < la, j < lb; i++, j++) 92 { 93 if (a.data[i] > b.data[j]) 94 { 95 e_flag = 1;//a>b 96 break; 97 } 98 else if (a.data[i] < b.data[j]) 99 { 100 e_flag = -1;//a<b 101 break; 102 } 103 } 104 } 105 if (e_flag == 1)//a>b,则c=a-b 106 { 107 c = minus_(a, b); 108 if (a.sign == '-' && b.sign == '+')//如果a负b正且a大于b,输出'-' 109 cout << '-'; 110 } 111 else if (e_flag == -1)//a<b,则c=b-a,结果前面添负号 112 { 113 c = minus_(b, a); 114 if (a.sign == '+' && b.sign == '-')//如果a正b负且b大于a,输出'-' 115 cout << '-'; 116 } 117 else//a,b相等的情况 118 { 119 c.data[0] = '0'; 120 c.data[1] = '\0'; 121 } 122 int k = -1;//k为数组下标 123 while (c.data[++k] == '0')//k移动到第一个不为'0'的地方 124 { 125 if (k == strlen(c.data) - 1)//如果k移到了数组尾部,说明全为0 126 { 127 z_flag = 1;// ”为零“标志置1 128 break; 129 } 130 } 131 int cnt = 0;//输出计数器,代表结果的位数,也用于控制','的输出 132 const int len_ = strlen(c.data); 133 for (int i = k; i < len_; i++) 134 { 135 if ((len_ - i) % 4 == 0 && i!=k) 136 cout << ','; 137 cout << c.data[i]; 138 cnt++; 139 } 140 cout << endl << "位数:" << strlen(a.data) << "," << strlen(b.data) << ","; 141 cout << cnt; 142 } 143 cout << endl << endl; 144 } 145 return 0; 146 } 147 struct num plus_(struct num a, struct num b) 148 { 149 int la = strlen(a.data); 150 int lb = strlen(b.data); 151 struct num c = {}; 152 char s[103];//存放计算结果的栈 153 int r = -1;//栈顶指针 154 int cx = 0;//进位标志,须初始化为0 155 int i, j; 156 for (i = la - 1, j = lb - 1; i >= 0 && j >= 0; i--, j--) 157 { 158 int t1 = a.data[i] - '0';// -'0'操作将char转换为int 159 int t2 = b.data[j] - '0'; 160 int t3 = t1 + t2; 161 if (t3 + cx >= 10)//如果a,b同一位相加大于等于10,则相加结果-10并将cx置1 162 { 163 t3 = t3 + cx - 10; 164 cx = 1; 165 } 166 else//否则将cx置0 167 { 168 cx = 0; 169 } 170 s[++r] = t3 + '0';//将此位相加结果压入s 171 } 172 if (i < 0)//如果b比a长,则将b剩余数据放入s 173 { 174 for (int k = j; k >= 0; k--) 175 { 176 int t = b.data[k] - '0'; 177 if (t + cx >= 10)//注意遗留的cx 178 { 179 t = t + cx - 10; 180 cx = 1; 181 } 182 else 183 { 184 t = t + cx; 185 cx = 0; 186 } 187 s[++r] = t + '0'; 188 } 189 } 190 else if (j < 0)//同上 191 { 192 for (int k = i; k >= 0; k--) 193 { 194 int t = a.data[k] - '0'; 195 if (t + cx >= 10) 196 { 197 t = t + cx - 10; 198 cx = 1; 199 } 200 else 201 { 202 t = t + cx; 203 cx = 0; 204 } 205 s[++r] = t + '0'; 206 } 207 } 208 if (cx == 1)//第一位数(最高位)是不是有进位,如果是则压入s 209 { 210 s[++r] = cx + '0'; 211 } 212 int i1, i2;//将栈s的数据放到结果c的data数组中 213 for (i1 = 0, i2 = r; i2 >= 0; i1++, i2--) 214 { 215 c.data[i1] = s[i2]; 216 } 217 c.data[i1] = '\0'; 218 return c; 219 } 220 struct num minus_(struct num a, struct num b)//与plus_函数相似,+变为-,cx由进位变借位 221 { 222 int la = strlen(a.data); 223 int lb = strlen(b.data); 224 struct num c = {}; 225 char s[103]; 226 int r = -1; 227 int cx = 0; 228 int i, j; 229 for (i = la - 1, j = lb - 1; i >= 0 && j >= 0; i--, j--) 230 { 231 int t1 = a.data[i] - '0'; 232 int t2 = b.data[j] - '0'; 233 int t3 = t1 - t2; 234 if (t3 - cx < 0) 235 { 236 t3 = t3 - cx + 10; 237 cx = 1; 238 } 239 else 240 { 241 t3 = t3 - cx; 242 cx = 0; 243 } 244 s[++r] = t3 + '0'; 245 } 246 if (i < 0) 247 { 248 for (int k = j; k >= 0; k--) 249 { 250 int t = b.data[k] - '0'; 251 if (t - cx < 0) 252 { 253 t = t - cx + 10; 254 cx = 1; 255 } 256 else 257 { 258 t = t - cx; 259 cx = 0; 260 } 261 s[++r] = t + '0'; 262 } 263 } 264 else if (j < 0) 265 { 266 for (int k = i; k >= 0; k--) 267 { 268 int t = a.data[k] - '0'; 269 if (t - cx < 0) 270 { 271 t = t - cx + 10; 272 cx = 1; 273 } 274 else 275 { 276 t = t - cx; 277 cx = 0; 278 } 279 s[++r] = t + '0'; 280 } 281 } 282 int i1, i2; 283 for (i1 = 0, i2 = r; i2 >= 0; i1++, i2--) 284 { 285 c.data[i1] = s[i2]; 286 } 287 c.data[i1] = '\0'; 288 return c; 289 }