实现大整数相加(考虑符号位,可能有负整数) 思维严谨程度!!
Posted 棋子
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了实现大整数相加(考虑符号位,可能有负整数) 思维严谨程度!!相关的知识,希望对你有一定的参考价值。
//实现大整数相加 //还得考虑符号位,一个比另一个短 #include<iostream> #include <string> #include <cstring> using namespace std; #define maxlen 2001 int a[maxlen]; int b[maxlen]; int len1, len2, i, j; int bigger(int a, int b) { return a>b ? a : b; } void Add(int underzero) { if (underzero) cout << ‘-‘; int big = bigger(len1, len2); for (i = 0; i <= big; i++) { a[i] += b[i]; if (a[i] >= 10) { a[i] -= 10; a[i + 1]++; } } bool flag = false;//用来忽略高位的0,中间的0才是有效数字 for (i = big; i >= 0; i--) { if (flag) cout << a[i]; else if (a[i]) { cout << a[i]; flag = true; } } cout << endl; } void Minus(int aa[], int bb[], int biglen, int underzero) { if (underzero) cout << ‘-‘; for (i = 0; i <= biglen; i++) { if (aa[i] >= bb[i]) aa[i] -= bb[i]; else { aa[i] = aa[i] + 10 - bb[i]; aa[i + 1]--; } } bool flag = false;//同样去掉最高位的无效‘1’ for (i = biglen; i >= 0; i--) { if (flag) cout << aa[i]; else if (aa[i]) { cout << aa[i]; flag = true; } } cout << endl; } int main() { string s1, s2; bool success; while (cin >> s1 >> s2) { memset(a, 0, sizeof(a)); memset(b, 0, sizeof(b)); len1 = s1.length(); len2 = s2.length(); for (i = len1 - 1, j = 0; i>0; i--) a[j++] = s1[i] - ‘0‘; if (s1[0] != ‘-‘) a[j] = s1[0] - ‘0‘; for (i = len2 - 1, j = 0; i>0; i--) b[j++] = s2[i] - ‘0‘; if (s2[0] != ‘-‘) b[j] = s2[0] - ‘0‘; if (s1[0] == ‘-‘ && s2[0] == ‘-‘) Add(1); //和为负 else if (s1[0] != ‘-‘ && s2[0] != ‘-‘) Add(0); //和为正 else if (s1[0] == ‘-‘) { if (len1 - 1 > len2) //负数大 Minus(a, b, len1 - 1, 1); else if (len1 - 1 < len2) //正数大 Minus(b, a, len2, 0); else { success = true; for (i = len1 - 2, j = len2 - 1; j >= 0; j--, i--) //这里的len-2是个关键,len-1是长度,之所以再减去1,是要减去负号的位置 { if (a[i]>b[j]) //负数大 { success = false; Minus(a, b, len2, 1); break; } else if (a[i]<b[j]) //正数大 { success = false; Minus(b, a, len2, 0); break; } } if (success) //相等 cout << "0" << endl; } } else if (s2[0] == ‘-‘) { if (len2 - 1 > len1) //负数大 Minus(b, a, len2 - 1, 1); else if (len2 - 1 < len1) //正数大 Minus(a, b, len1, 0); else { success = true; for (i = len2 - 2, j = len1 - 1; j >= 0; j--, i--) //这里的len-2是个关键,len-1是长度,之所以再减去1,是要减去负号的位置 { if (a[j]>b[i]) //正数大 { success = false; Minus(a, b, len1, 0); break; } else if (a[j]<b[i]) //负数大 { success = false; Minus(b, a, len1, 1); break; } } if (success) //相等 cout << "0" << endl; } } } return 0; }
//实现大整数相加 //还得考虑符号位,一个比另一个短 #include<iostream> #include <string> #include <cstring> using namespace std; #define maxlen 2001 int a[maxlen]; int b[maxlen]; int len1, len2, i, j; int bigger(int a, int b) { return a>b ? a : b; } void Add(int underzero) { if (underzero) cout << ‘-‘; int big = bigger(len1, len2); for (i = 0; i <= big; i++) { a[i] += b[i]; if (a[i] >= 10) { a[i] -= 10; a[i + 1]++; } } bool flag = false;//用来忽略高位的0,中间的0才是有效数字 for (i = big; i >= 0; i--) { if (flag) cout << a[i]; else if (a[i]) { cout << a[i]; flag = true; } } cout << endl; } void Minus(int aa[], int bb[], int biglen, int underzero)//数组的元素下标从小到大对应大整数(低位->高位)哪一个数的绝对值大,那个数就放前面,underzero=1表示最终结果为负数 { if (underzero) cout << ‘-‘; for (i = 0; i <= biglen; i++) { if (aa[i] >= bb[i]) aa[i] -= bb[i]; else { aa[i] = aa[i] + 10 - bb[i]; aa[i + 1]--; } } bool flag = false;//同样去掉最高位的无效‘1’ for (i = biglen; i >= 0; i--) { if (flag) cout << aa[i]; else if (aa[i]) { cout << aa[i]; flag = true; } } cout << endl; } int main() { string s1, s2; bool success; while (cin >> s1 >> s2) { memset(a, 0, sizeof(a)); memset(b, 0, sizeof(b)); len1 = s1.length(); len2 = s2.length(); for (i = len1 - 1, j = 0; i>0; i--)//将大整数的最低位(个位)存入到数组下标最小的位置 a[j++] = s1[i] - ‘0‘; if (s1[0] != ‘-‘) a[j] = s1[0] - ‘0‘; for (i = len2 - 1, j = 0; i>0; i--) b[j++] = s2[i] - ‘0‘; if (s2[0] != ‘-‘) b[j] = s2[0] - ‘0‘; if (s1[0] == ‘-‘ && s2[0] == ‘-‘) Add(1); //和为负 else if (s1[0] != ‘-‘ && s2[0] != ‘-‘) Add(0); //和为正 else if (s1[0] == ‘-‘) { if (len1 - 1 > len2) //负数大 Minus(a, b, len1 - 1, 1); else if (len1 - 1 < len2) //正数大 Minus(b, a, len2, 0); else { success = true; for (i = len1 - 2, j = len2 - 1; j >= 0; j--, i--) //这里的len-2是个关键,len-1是长度,之所以再减去1,是要减去负号的位置 { if (a[i]>b[j]) //负数大 { success = false; Minus(a, b, len2, 1); break; } else if (a[i]<b[j]) //正数大 { success = false; Minus(b, a, len2, 0); break; } } if (success) //相等 cout << "0" << endl; } } else if (s2[0] == ‘-‘) { if (len2 - 1 > len1) //负数大 Minus(b, a, len2 - 1, 1); else if (len2 - 1 < len1) //正数大 Minus(a, b, len1, 0); else { success = true; for (i = len2 - 2, j = len1 - 1; j >= 0; j--, i--) //这里的len-2是个关键,len-1是长度,之所以再减去1,是要减去负号的位置 { if (a[j]>b[i]) //正数大 { success = false; Minus(a, b, len1, 0); break; } else if (a[j]<b[i]) //负数大 { success = false; Minus(b, a, len1, 1); break; } } if (success) //相等 cout << "0" << endl; } } } return 0; }
以上是关于实现大整数相加(考虑符号位,可能有负整数) 思维严谨程度!!的主要内容,如果未能解决你的问题,请参考以下文章