Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <cstdlib>
- using namespace std;
- #define mineMax 999999999
- class bigInt
- {
- private:
- vector<int> x;
- public:
- bigInt(int x);
- bigInt(const char* y);
- //vector<int> getVector();
- friend ostream& operator<<(ostream&, bigInt);
- friend bigInt operator+(bigInt, int);
- friend void operator+= (bigInt& object, int y);
- friend bigInt operator- (bigInt object, int y);
- friend void operator-= (bigInt& object, int y);
- };
- void reverse(string& x);
- ostream& operator<< (ostream& stream, bigInt object);
- bigInt operator+ (bigInt object, int y);
- void operator+= (bigInt& object, int y);
- bigInt operator- (bigInt object, int y);
- void operator-= (bigInt& object, int y);
- int main()
- {
- bigInt a = "999999998";
- cout << a << endl;
- a = a + 1;
- cout << a << endl;
- a -= 1;
- cout << a << endl;
- a += 2;
- cout << a << endl;
- a -= 2;
- cout << a << endl;
- /* a = a + 20;
- cout << a << endl;
- a = a + 300;
- cout << a << endl;
- a = a + 4000;
- cout << a << endl;
- a = a + 50000;
- cout << a << endl;
- a = a + 600000;
- cout << a << endl;
- a = a + 7000000;
- cout << a << endl;
- a = a + 80000000;
- cout << a << endl;
- a = a + 900000000;
- cout << a << endl;
- a = a + 10000000;
- cout << a << endl;*/
- return 0;
- }
- bigInt::bigInt(int x)
- {
- this -> x.push_back(x);
- }
- bigInt::bigInt(const char* y)
- {
- string bufor;
- string x = y;
- int i,j;
- int eraser = 9;
- if(x.size() > 9)
- {
- do
- {
- for (i = x.size(), j = x.size() - eraser; i > j; i--) {
- bufor += x[i];
- }
- reverse(bufor);
- x.erase(x.size()-eraser, eraser);
- (this -> x).push_back(atoi(bufor.c_str()));
- }while(x.size() > eraser);
- }
- if(x.size() > 0 ) bufor = x;
- //cout << "|" << bufor << "|" << endl;
- (this -> x).push_back(atoi(bufor.c_str()));
- }
- void reverse(string& x)
- {
- for(int i=0; i<x.size()/2; i++)
- {
- swap(x[i],x[x.size()-1-i]);
- }
- }
- ostream& operator<< (ostream& stream, bigInt object)
- {
- for(int i=object.x.size()-1; i>=0; i--)
- {
- if(i!=object.x.size()-1)
- {
- if(object.x[i] >= 100000000)
- {
- stream << object.x[i];
- }else if(object.x[i] >= 10000000)
- {
- stream << "0" << object.x[i];
- }else if(object.x[i] >= 1000000)
- {
- stream << "00" << object.x[i];
- }else if(object.x[i] >= 100000)
- {
- stream << "000" << object.x[i];
- }else if(object.x[i] >= 10000)
- {
- stream << "0000" << object.x[i];
- }else if(object.x[i] >= 1000)
- {
- stream << "00000" << object.x[i];
- }else if(object.x[i] >= 100)
- {
- stream << "000000" << object.x[i];
- }else if(object.x[i] >= 10)
- {
- stream << "0000000" << object.x[i];
- }else
- {
- stream << "00000000" << object.x[i];
- }
- }else stream << object.x[i];
- }
- return stream;
- }
- bigInt operator+(bigInt object, int y)
- {
- int i = 1;
- int difference = (object.x[0] + y) - mineMax -1 ;
- if(object.x[0] + y > mineMax)
- {
- if(object.x.size() > 1)
- {
- while(object.x[i] + 1 > mineMax || i == object.x.size())
- {
- i++;
- }
- if(i == object.x.size())
- {
- for(int j=1; j<object.x.size(); j++)
- {
- object.x[j] = 0;
- }
- object.x[0] = difference;
- object.x.push_back(1);
- }else
- {
- for(int j=1; j<i-1; j++)
- {
- object.x[j] = 0;
- }
- object.x[i]++;
- object.x[0] = difference;
- }
- }else
- {
- object.x[0] = difference;
- object.x.push_back(1);
- }
- }else
- {
- object.x[0] += y;
- }
- return object;
- }
- void operator+= (bigInt& object, int y)
- {
- object = object + y;
- }
- bigInt operator- (bigInt object, int y)
- {
- int i = 1;
- int difference = abs(object.x[0] - y);
- if(object.x[0] - y < 0)
- {
- if(object.x.size() > 1)
- {
- while(object.x[i] - 1 < 0 || i == object.x.size())
- {
- i++;
- }
- if(i == object.x.size())
- {
- for(int j=1; j<object.x.size(); j++)
- {
- object.x[j] = 0;
- }
- object.x[0] += (mineMax - y);
- object = object.x[0];
- }else
- {
- for(int j=1; j<i-1; j++)
- {
- object.x[j] = 0;
- }
- if(i == object.x.size()-1 && object.x[i]-1 == 0) object.x.erase(object.x.begin());
- else object.x[i]--;
- object.x[0] += (mineMax - y);
- }
- }else
- {
- object.x[0] -= y;
- object = object.x[0];
- }
- }else
- {
- object.x[0] -= y;
- }
- return object;
- }
- void operator-= (bigInt& object, int y)
- {
- object = object - y;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement