本文主要是介绍大整数加减法(C++实现),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
头文件:LargeInteger.h
#ifndef LARGEINTEGER_H_INCLUDED
#define LARGEINTEGER_H_INCLUDED#include <iostream>using namespace std;class LargeInteger {// 大整数(用字符串表示)
public:LargeInteger() { this->numStr = "0"; }// 默认为 0LargeInteger(const string& numStr);// 用字符串初始化LargeInteger(const int& num);// 用 int 型数据初始化string getNum() const { return numStr; }void setNum(const string& numStr);void setNum(const int& num);void trimNum();// 去掉数字前面所有的 0friend bool isNum(const string& numStr);friend istream& operator>>(istream& in, LargeInteger& largeInteger);friend ostream& operator<<(ostream& out, const LargeInteger& largeInteger);friend const LargeInteger operator+(const LargeInteger& A, const LargeInteger& B);friend const LargeInteger operator-(const LargeInteger& A, const LargeInteger& B);
private:string numStr;
};#endif // LARGEINTEGER_H_INCLUDED
源文件:LargeInteger.cpp
#include "LargeInteger.h"
#include <sstream>bool isNum(const string& numStr) { // 判断一个 string 型的 numStr 里面存放的是不是表示的一个整数if(numStr.empty()) return false;if('-' == numStr[0] || '+' == numStr[0]) {if(1 == numStr.size()) return false;}for(string::size_type i=1; i < numStr.size(); i++) {if(numStr[i] < '0' || numStr[i] > '9')return false;}return true;
}void LargeInteger::trimNum() {// 去掉数字前面所有的 0 字符string sign = "";string::size_type beginIndex = 0;if('-' == this->numStr[0]) {sign = "-";beginIndex++;}for(; beginIndex < this->numStr.size(); beginIndex++) {if('0' != this->numStr[beginIndex])break;}if(numStr.size() == beginIndex)this->numStr = "0";elsethis->numStr = sign + this->numStr.substr(beginIndex);
}LargeInteger::LargeInteger(const string& numStr) {this->setNum(numStr);
}LargeInteger::LargeInteger(const int& num) {this->setNum(num);
}void LargeInteger::setNum(const string& numStr) {if(isNum(numStr)) {if('+' == numStr[0])this->numStr = numStr.substr(1);elsethis->numStr = numStr;} elsethis->numStr = "0";this->trimNum();
}void LargeInteger::setNum(const int& num) {stringstream ss;ss << num;this->numStr = ss.str();
}istream& operator>>(istream& in, LargeInteger& largeInteger) {in >> largeInteger.numStr;if(isNum(largeInteger.numStr)) {if('+' == largeInteger.numStr[0])largeInteger.numStr = largeInteger.numStr.substr(1);} elselargeInteger.numStr = "0";largeInteger.trimNum();return in;
}ostream& operator<<(ostream& out, const LargeInteger& largeInteger) {out << largeInteger.numStr;return out;
}void Add(string& sumStr, string aStr, int lengthA, string bStr, int lengthB) {// 两正整数相加int n = lengthA;if(lengthA < lengthB) {n = lengthB;aStr = string(n - lengthA, '0') + aStr;} else {bStr = string(n - lengthB, '0') + bStr;}int carry = 0;while(--n >= 0) {int a = aStr[n] - '0';int b = bStr[n] - '0';int sum = a + b + carry;carry = sum / 10;sum %= 10;char c = sum + '0';sumStr = c + sumStr;}if(carry > 0) {char c = carry + '0';sumStr = c + sumStr;}
}LargeInteger const operator+(const LargeInteger& A, const LargeInteger& B) {string sumStr = "";int lengthA = A.numStr.size();int lengthB = B.numStr.size();if('0' <= A.numStr[0] && A.numStr[0] <= '9' && '0' <= B.numStr[0] && B.numStr[0] <= '9') {// 两正整数相加Add(sumStr, A.numStr, lengthA, B.numStr, lengthB);} else if('-' == A.numStr[0] && '-' == B.numStr[0]) {// 两负整数相加Add(sumStr, A.numStr.substr(1), --lengthA, B.numStr.substr(1), --lengthB);sumStr = "-" + sumStr;} else if('-' == A.numStr[0]) {string temp = A.numStr.substr(1);return (B - temp);} else {string temp = B.numStr.substr(1);return (A - temp);}return sumStr;
}void Minus(string& minusStr, string aStr, int lengthA, string bStr, int lengthB) {// 两正整数相减bool flag = true;// 默认符号位为正if(lengthA < lengthB || (lengthA == lengthB && aStr[0] < bStr[0])) {// 如果 A <= B,则计算 B-A,并设置符号位为负string tempStr = aStr;aStr = bStr;bStr = tempStr;lengthA = aStr.size();lengthB = bStr.size();flag = false;}int n = lengthA;bStr = string(n - lengthB, '0') + bStr;int borrow = 0, index = n;while(--n >= 0) {int a = aStr[n] - '0';if(a > 0) {a -= borrow;borrow = 0;} else {if(borrow > 0)a = 9;}int b = bStr[n] - '0';char c;if(0 == b) {c = a + '0';} else {if(a >= b) {c = a - b + '0';} else {a += 10;c = a - b + '0';borrow++;}}minusStr = c + minusStr;if(minusStr[0] != '0')index = n;}if(index == n)minusStr = "0";else {minusStr = minusStr.substr(index);if(!flag) {minusStr = "-" + minusStr;}}
}LargeInteger const operator-(const LargeInteger& A, const LargeInteger& B) {string minusStr = "";int lengthA = A.numStr.size();int lengthB = B.numStr.size();if('0' < A.numStr[0] && A.numStr[0] <= '9' && '0' < B.numStr[0] && B.numStr[0] <= '9') {// 两正整数相减Minus(minusStr, A.numStr, lengthA, B.numStr, lengthB);return minusStr;} else if('-' == A.numStr[0] && '-' == B.numStr[0]) { // 两负整数相减Minus(minusStr, A.numStr.substr(1), lengthA-1, B.numStr.substr(1), lengthB-1);if('-' == minusStr[0])minusStr = minusStr.substr(1);elseminusStr = "-" + minusStr;return minusStr;} else {if('-' == B.numStr[0]) {// 正整数减负整数(即,两正整数相加)Add(minusStr, A.numStr, lengthA, B.numStr.substr(1), lengthB-1);return minusStr;} else { // 负整数减正整数(即,负的 两正整数相加)Add(minusStr, A.numStr.substr(1), lengthA-1, B.numStr, lengthB);minusStr = "-" + minusStr;return minusStr;}}
}
主函数测试:
#include "LargeInteger.h"int main() {LargeInteger A, B;LargeInteger C(145265555), D(145325563);LargeInteger E("12345678912345678145912345678987654321"), F("15483395674458414455268441265444556845555445677");cin >> A >> B;cout << A << " + " << B << " = " << A + B << endl;cout << A << " - " << B << " = " << A - B << endl;cout << C << " + " << D << " = " << C + D << endl;cout << C << " - " << D << " = " << C - D << endl;cout << E << " + " << F << " = " << E + F << endl;cout << E << " - " << F << " = " << E - F << endl;return 0;
}
这篇关于大整数加减法(C++实现)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!