加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

两大数的相加减问题

发布时间:2020-12-14 03:28:59 所属栏目:大数据 来源:网络整理
导读://实现两大数相加,大数也可以是负数 #include cstdio #include stack #include string #include iostream using namespace std; char* AddTwoPositiveNumber(char* n1,int len1,char* n2,int len2,int lensmall) { stackint s; int CarryBit = 0; for(int i
//实现两大数相加,大数也可以是负数 #include <cstdio> #include <stack> #include <string> #include <iostream> using namespace std; char* AddTwoPositiveNumber(char* n1,int len1,char* n2,int len2,int lensmall) { stack<int> s; int CarryBit = 0; for(int i = 0; i < lensmall; i++) { int ss = (n1[len1 - i - 1] - '0') + (n2[len2 - i - 1] - '0') + CarryBit; if(ss >= 10) { CarryBit = 1; s.push(ss - 10); } else { CarryBit = 0; s.push(ss); } } if(len2 <= lensmall) { char* sum = new char[len1 + 2],int i = 0; strncpy(sum,n1,len1 - lensmall); sum[len1 + 1] = ''; while(!s.empty()) { sum[len1 - lensmall + i] = s.top()+'0'; i++; s.pop(); } return sum; } if(len1 <= lensmall) { char* sum = new char[len2 + 2],n2,len2 - lensmall); sum[len2 + 1] = ''; while(!s.empty()) { sum[len2-lensmall+i] = s.top() + '0'; i++; s.pop(); } return sum; } } char* AddOneIsMinusNumber(char* n1,int len2) //n2是负数 { bool sumIsMinus = false; stack<int> s; int CarryBit = 0; if(len1 < len2) { sumIsMinus = true; for(int i = 0; i < len1; i++) { int ss = (0-(n1[len1-i-1]-'0'))+(n2[len2-i-1]-'0')+CarryBit; if(ss < 0) { CarryBit = -1; s.push(ss + 10); } else { CarryBit = 0; s.push(ss); } } int c = n2[len2 - len1 - 1]-'0'+CarryBit; s.push(c); for(int j = len2 - len1 - 2; j >= 0; j--) s.push(n2[j] - '0'); } else { for(int i = 0; i < len2; i++) { int ss = (n1[len1-i-1]-'0')+(0-(n2[len2-i-1]-'0'))+CarryBit; if(ss < 0) { CarryBit = -1; s.push(ss + 10); } else { CarryBit = 0; s.push(ss); } } if(CarryBit == -1 && len1 == len2) sumIsMinus == true; if(len1 > len2) { int c = n1[len1 - len2 - 1]-'0'+CarryBit; s.push(c); for(int j = len1 - len2 - 2; j >= 0; j--) s.push(n1[j] - '0'); } } int lenmax = len1 > len2 ? len1 : len2; char* sum = new char[lenmax + 2]; if(sumIsMinus) { ? ?sum = new char[lenmax + 3]; sum[0] = '-'; sum[lenmax + 2] = ''; int i = 1; bool firstIsZero = true; while(!s.empty()) { if(firstIsZero && s.top() != 0) firstIsZero = false; if(firstIsZero) s.pop(); sum[i] = s.top() + '0'; i++; s.pop(); } sum[i] = ''; } else { sum = new char[lenmax + 2]; sum[lenmax + 1] = ''; int i = 0; bool firstIsZero = true; while(!s.empty()) { if(firstIsZero && s.top() != 0) firstIsZero = false; if(firstIsZero) s.pop(); sum[i] = s.top() + '0'; i++; s.pop(); } sum[i] = ''; } return sum; } bool IsValidInput(const char* num) { while(*num != '') { if(*num < '0' || *num > '9') return false; else num++; } return true; } char* Add(char* n1,char* n2) { bool n1_IsMinus = false,n2_IsMinus = false; if(n1[0] == '-')? { n1++; n1_IsMinus = true; } if(n2[0] == '-')? { n2++; n2_IsMinus = true; } int len1 = strlen(n1),len2 = strlen(n2); int lensmall; if(len1 >= len2) lensmall = len2; else lensmall = len1; if(!IsValidInput(n1) || !IsValidInput(n2)) { cout << "非法输入" << endl; exit(-1); } if(!n1_IsMinus && !n2_IsMinus) { return AddTwoPositiveNumber(n1,len1,len2,lensmall); } if(!n1_IsMinus && n2_IsMinus) ? //n2为负数 { return AddOneIsMinusNumber(n1,len2); } if(n1_IsMinus && !n2_IsMinus) ? //n1为负数 { return AddOneIsMinusNumber(n2,len1); } if(n1_IsMinus && n2_IsMinus) ? //n1,n2为负数 { int lenmax = len1 > len2 ? len1 : len2; char* sum = new char[lenmax + 2]; sum[0] = '-'; char* sum1 = ?AddTwoPositiveNumber(n1,lensmall); int len = strlen(sum1); strcpy(sum + 1,sum1); sum[len] = ''; return sum; } } int main() { char* n1 = "232323"; char* n2 = "-11212312"; char* sum = Add(n1,n2); cout << sum << endl; cout << Add("-123","-3456") << endl; cout << Add("-123","3456") << endl; cout << Add("0","-3456") << endl; cout << Add("-11243882413427423","-123464367674576") << endl; cout << Add("11243882413427423","123464367674576") << endl; cout << Add("1124-388","-12346436") << endl; return 0; }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读