Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Trần Việt Anh - 7/5/2019 - 10:04PM
- #include<iostream>
- #include<cstring>
- using namespace std;
- class BigInt {
- string value;
- int sign;
- public:
- BigInt();
- BigInt(const char*);
- BigInt(int );
- BigInt(const BigInt& );
- friend ostream& operator<< (ostream& , const BigInt& );
- BigInt& operator=(int );
- BigInt& operator=(const char*);
- BigInt& operator=(const BigInt& );
- friend BigInt operator+(const BigInt& , const BigInt & );
- friend BigInt operator+(const BigInt& , int );
- friend BigInt operator+(int , const BigInt &);
- friend BigInt operator-(const BigInt& , const BigInt & );
- friend BigInt operator-(const BigInt& , int );
- friend BigInt operator-(int , const BigInt &);
- BigInt& operator+=(int );
- BigInt& operator+=(const BigInt& );
- BigInt& operator-=(int );
- BigInt& operator-=(const BigInt& );
- };
- ostream& operator<<(ostream& os, const BigInt& num) {
- if (num.sign == 0) cout << "-";
- int startIndex = 0;
- while (num.value[startIndex] == '0' && startIndex < num.value.length() - 1) startIndex++;
- for (int i = startIndex; i < num.value.length(); i++) cout << num.value[i];
- return os;
- }
- BigInt::BigInt() {
- this -> value = "0";
- this -> sign = 1;
- }
- BigInt::BigInt(const char *num) {
- this -> value = ""; int startIndex;
- if (num[0] == '-') {
- this -> sign = 0; startIndex = 1;
- }
- else {
- this -> sign = 1; startIndex = 0;
- }
- for (int i = startIndex; i < strlen(num); i++)
- this -> value += num[i];
- }
- BigInt::BigInt(int num) {
- if (num < 0) {
- this -> sign = 0;
- num *= -1;
- }
- else this -> sign = 1;
- this -> value = "";
- while (num != 0) {
- int value = num % 10;
- this -> value = char(value + 48) + this -> value;
- num /= 10;
- }
- }
- BigInt::BigInt(const BigInt& other) {
- this -> value = other.value;
- this -> sign = other.sign;
- }
- BigInt& BigInt::operator=(int num) {
- if (num < 0) {
- this -> sign = 0;
- num *= -1;
- }
- else this -> sign = 1;
- this -> value = "";
- while (num != 0) {
- int value = num % 10;
- this -> value = char(value + 48) + this -> value;
- num /= 10;
- }
- return *this;
- }
- BigInt& BigInt::operator=(const BigInt& num) {
- this -> value = num.value;
- this -> sign = num.sign;
- return *this;
- }
- BigInt& BigInt::operator=(const char *num) {
- this -> value = ""; int startIndex;
- if (num[0] == '-') {
- this -> sign = 0; startIndex = 1;
- }
- else {
- this -> sign = 1; startIndex = 0;
- }
- for (int i = startIndex; i < strlen(num); i++)
- this -> value += num[i];
- return *this;
- }
- BigInt operator+(const BigInt& num, const BigInt& other)
- {
- BigInt result;
- if (num.sign == 1 && other.sign == 0) {
- BigInt newNum(other); newNum.sign = 1;
- return num - newNum;
- }
- if (num.sign == 0 && other.sign == 1) {
- BigInt newNum(num); newNum.sign = 1;
- return other - newNum;
- }
- if (num.sign == 0 && other.sign == 0) result.sign = 0;
- else result.sign = 1;
- string num1 = num.value, num2 = other.value;
- while (num1.length() < num2.length()) num1 = "0" + num1;
- while (num2.length() < num1.length()) num2 = "0" + num2;
- result.value = num1;
- int remember = 0;
- for (int i = num1.length() - 1; i >= 0; i--) {
- int value = num1[i] - 48 + num2[i] - 48 + remember;
- result.value[i] = value % 10 + 48;
- remember = value / 10;
- }
- if (remember != 0) result.value = "1" + result.value;
- return result;
- }
- BigInt operator+(const BigInt& num, int other) {
- return num + BigInt(other);
- }
- BigInt operator+(int num, const BigInt& other) {
- return BigInt(num) + other;
- }
- bool isBiggerThanOrEqual(string s1, string s2) {
- if (s1.length() > s2.length()) return true;
- else if (s1.length() < s2.length()) return false;
- for (int i = 0; i < s1.length(); i++) {
- if (s1[i] < s2[i]) return false;
- else if (s1[i] > s2[i]) return true;
- }
- return true;
- }
- BigInt operator-(const BigInt& num, const BigInt& other)
- {
- BigInt result;
- if (num.sign == 0 && other.sign == 1) {
- BigInt newNum(num); newNum.sign = 1;
- result = newNum + other;
- result.sign = 0;
- return result;
- }
- if (num.sign == 1 && other.sign == 0) {
- BigInt newNum(other); newNum.sign = 1;
- return num + newNum;
- }
- if (num.sign == 0 && other.sign == 0) {
- BigInt newNum1(num); newNum1.sign = 1;
- BigInt newNum2(other); newNum2.sign = 1;
- BigInt result(newNum1 + newNum2);
- result.sign = 0;
- return result;
- }
- string num1 = num.value, num2 = other.value;
- if (!isBiggerThanOrEqual(num1, num2)) {
- string mid = num1; num1 = num2; num2 = mid;
- result.sign = 0;
- }
- else result.sign = 1;
- while (num1.length() < num2.length()) num1 = "0" + num1;
- while (num2.length() < num1.length()) num2 = "0" + num2;
- result.value = num1;
- int remember = 0;
- for (int i = num1.length() - 1; i >= 0; i--) {
- int value = (num1[i] - 48) - (num2[i] - 48) + remember;
- if (value < 0) {
- result.value[i] = value + 10 + 48;
- remember = -1;
- }
- else {
- result.value[i] = value + 48;
- remember = 0;
- }
- }
- return result;
- }
- BigInt operator-(const BigInt& num, int other)
- {
- return num - BigInt(other);
- }
- BigInt operator-(int num, const BigInt& other)
- {
- return BigInt(num) - other;
- }
- BigInt& BigInt::operator+=(int num)
- {
- BigInt result(*this + BigInt(num));
- *this = result;
- return *this;
- }
- BigInt& BigInt::operator+=(const BigInt& num)
- {
- BigInt result(*this + num);
- *this = result;
- return *this;
- }
- BigInt& BigInt::operator-=(int num)
- {
- BigInt result(*this - BigInt(num));
- *this = result;
- return *this;
- }
- BigInt& BigInt::operator-=(const BigInt& num)
- {
- BigInt result(*this - num);
- *this = result;
- return *this;
- }
- int main() {
- cout << BigInt() << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement