大数相加
书本p119相关题目,但我由于时间关系,没有考虑输入的数字中有负数的情况。
思路:
(用字符串存储大数)将字符串number1与number2对齐,在长度较小的字符串上高位补0,生成新的两个长度一致的字符串;
字符串从低位相加,产生每位的相加结果及其进位,最后判断是否在最高位有进位。
代码:void addBigDigit(char* number1, char* number2, int length1, int length2); void printChar(char* number); //两个字符串表示的大数相加 void addBigDigit(char* number1, char* number2, int length1, int length2) { int maxLength = length1; if (length1 < length2) maxLength = length2; //新建两个字符串存储补齐0之后长度一致的字符串 char* fullNumber1 = new char[maxLength + 1]; char* fullNumber2 = new char[maxLength + 1]; //初始化字符串,这个很重要,因为后续将number1,number2 copy到新字符串中时会覆盖掉这里初始化的0,就不用了特意往高位补0了。 memset(fullNumber1, '0', maxLength); fullNumber1[maxLength] = '\0'; memset(fullNumber2, '0', maxLength); fullNumber2[maxLength] = '\0'; //复制旧字符串到新字符串中 if (maxLength != length1) { int i = maxLength - 1, j = length1 - 1; while (i>=0&&j>=0) { fullNumber1[i--] = number1[j--]; } strcpy(fullNumber2, number2); } if (maxLength != length2) { int i = maxLength - 1, j = length2 - 1; while (i >= 0 && j >= 0) { fullNumber2[i--] = number2[j--]; } strcpy(fullNumber1, number1); } //刚开始忘了这个,若没有,则新字符串就全是0了 if(maxLength == length2&&maxLength==length1) { strcpy(fullNumber2, number2); strcpy(fullNumber1, number1); } //printChar(fullNumber1); //printChar(fullNumber2); //本位相加结果和进位结果 int nSum = 0,nTakeOver = 0; //判断在最高位是否越界,即是否产生了新的超过字符串长度的最高位 bool isOverflow = false; //相加后的结果字符串 char* addNumber = new char[maxLength+1]; memset(addNumber, '0', maxLength); addNumber[maxLength] = '\0'; for (int i = maxLength - 1; i >= 0; i--) { nSum = fullNumber1[i] - '0' + fullNumber2[i] - '0' + nTakeOver; //相加完之后设置进位为0 nTakeOver = 0; if (nSum >= 10) { nSum -= 10; nTakeOver = 1; } addNumber[i] = nSum + '0'; //溢出:计算到最高位且进位不为0 if (i == 0 && nTakeOver > 0) isOverflow = true; } if (isOverflow) cout << '1'; printChar(addNumber); return; } void printChar(char* number) { for (int i = 0; i < strlen(number); i++) cout << number[i]; cout << endl; } int main() { char number1[] = "1234567"; char number2[] = "46781"; int length1 = strlen(number1); int length2 = strlen(number2); addBigDigit(number1, number2, length1,length2); }
更新后,用数组表示大数
/* 大数相加问题: 1.大数表示:数组表示 2.大数相加:较少位数高位补0;相加进位;是否溢出; */ //高位补0 void CompleteZero(vector<int> &number,int len,int bigLen) { vector<int> completeNumber(bigLen, 0); int gap = bigLen - len; for (int i = 0; i < len; ++i) { completeNumber[i + gap] = number[i]; } number.clear(); number.reserve(bigLen); number = completeNumber; } //大数相加 void AddBigData(vector<int> number1, vector<int> number2) { int len1 = number1.size(), len2 = number2.size(); if (len1 < len2) { CompleteZero(number1, len1, len2); } if (len1 > len2) CompleteZero(number2, len2,len1); int len = max(len1, len2); //存储相加结果,多一位是怕有溢出 vector<int> addResult(len + 1, 0); //判断最高位是否会溢出 bool isOverFlow = false; //本位对下一位的进位 int nTakeOver = 0; for (int i = len-1; i >= 0; --i) { //本位相加和,可能会大于等于10 int nSum = number1[i] + number2[i]+nTakeOver; //cout << nSum << " " << nTakeOver << endl; if (nSum >= 10) { nTakeOver = 1; nSum -= 10; //若是在最高位有进位,则溢出 if (i == 0) { isOverFlow = true; } addResult[i+1] = nSum; } else { nTakeOver = 0; addResult[i+1] = nSum; } } int i=1; if (isOverFlow) i = 0; for (; i < len + 1; ++i) cout << addResult[i]; cout << endl; } int main() { vector<int> number1 = { 1,2,3,4,5,6,7}; vector<int> number2 = { 5,6,7,8 }; AddBigData(number1, number2); return 0; }