Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "BigInteger.h"
- BigInteger::BigInteger(): BigInt(new char[4])
- {
- strcpy_s(BigInt, 3, "111");
- }
- BigInteger::BigInteger(char * BigInt):BigInt(new char[strlen(BigInt+1)])
- {
- strcpy_s(this->BigInt, strlen(BigInt + 1), BigInt);
- }
- BigInteger::~BigInteger()
- {
- if (BigInt != nullptr) {
- delete[] BigInt;
- BigInt = nullptr;
- }
- }
- BigInteger::BigInteger(const BigInteger & rhs):BigInt(new char[strlen(rhs.BigInt)+1])
- {
- strcpy_s(this->BigInt, strlen(BigInt)+1,rhs.BigInt);
- }
- BigInteger & BigInteger::operator=(const BigInteger & rhs)
- {
- if (this != &rhs) {
- if (BigInt != nullptr) {
- delete[] BigInt;
- BigInt = nullptr;
- }
- BigInt = new char[strlen(rhs.BigInt + 1)];
- strcpy_s(BigInt, strlen(rhs.BigInt) + 1, rhs.BigInt);
- return *this;
- }
- }
- int BigInteger::printBigInt() const
- {
- for (int i = 0;i < strlen(this->BigInt);i++) {
- std::cout << BigInt[i];
- }
- return 0;
- }
- int BigInteger::setBigInt(char * BigInt)
- {
- if (this->BigInt != nullptr) {
- delete[] this->BigInt;
- this->BigInt = nullptr;
- }
- this->BigInt = new char[strlen(BigInt) + 1];
- strcpy_s(this->BigInt, strlen(BigInt) + 1, BigInt);
- return 0;
- }
- char * BigInteger::getBigInt() const
- {
- return BigInt;
- }
- BigInteger operator+(const BigInteger & lhs, const BigInteger & rhs)
- {
- BigInteger res;
- int len = strlen(lhs.BigInt);
- int len2 = strlen(rhs.BigInt);
- int remainder = 0;
- int tmp = 0;
- int size = ((len > len2) ? len : len2);
- int size2 = ((len < len2) ? len : len2);
- for (int i = 0;i < size;i++) {
- if (i < size2 - 1) {
- tmp = (int)lhs.BigInt[i] + (int)rhs.BigInt[i] + remainder;
- }
- else {
- if (size2 < len) {
- tmp = (int)rhs.BigInt[i] + remainder;
- }
- else {
- tmp = (int)lhs.BigInt[i] + remainder;
- }
- }
- remainder = tmp / 10;
- res.BigInt[i] = (char)(tmp % 10);
- }
- return res;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement