Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //============================================================================
- // Name : BigInteger.cpp
- // Author : Oskar Pawica
- // Version :
- // Copyright : Your copyright notice
- // Description : Hello World in C++, Ansi-style
- //============================================================================
- #include <iostream>
- #include <cmath>
- #include <algorithm>
- #include <cstdlib>
- #include <stdio.h>
- #include <string.h>
- using namespace std;
- class BigInteger
- {
- private:
- char* e;
- long size;
- public:
- BigInteger() : e(NULL), size(0) {}
- BigInteger(long a){
- size = 1;
- if(a == 0)
- {
- e = new char[size];
- *e = '0';
- }
- else
- {
- size += numberOfDigits(abs(a));
- e = new char [size];
- if(a > 0)
- *e = '+';
- else {
- *e = '-';
- a = -a;
- }
- int i = 1;
- while(a >= 1)
- {
- sprintf(e+i, "%ld", a%10);
- a/=10;
- i++;
- }
- }
- }
- BigInteger(long ssize, bool): size(ssize), e(NULL) {}
- BigInteger(const char* a){
- size = 1;
- if(*a == '0'){
- e = new char [size];
- *e = '0';
- }
- else
- {
- char j;
- int i = 0;
- if(*a == '+' or *a == '-') {
- j = *a;
- i++;
- }
- else {
- j = '+';
- }
- while(*(a+i)>=48 and *(a+i)<=57) {
- size++;
- i++;
- }
- e = new char [size];
- *e = j;
- i--;
- int k = 1;
- while(*(a+i)>=48 and *(a+i)<=57) {
- *(e+k) = *(a+i);
- i--;
- k++;
- }
- }
- }
- virtual ~BigInteger()
- {
- delete [] e;
- }
- BigInteger (const BigInteger &b):size(b.size)
- {
- e = new char[b.size];
- strcpy(e, b.e);
- }
- BigInteger operator +(const BigInteger &b) const
- {
- if(e[0] == '0')
- return b;
- if(b.e[0] == '0')
- return *this;
- //if(e[0] == '-')
- //return b-(*this);
- //if(b.e[0] == '-')
- //return *this-b;
- if(b.e[0] == '-' and e[0] == '-')
- return -((*this)+b);
- BigInteger temp(this->size, true);
- temp.e = new char [this->size];
- temp.e[0] = '+';
- unsigned int i = 1;
- char temp_c;
- bool t = false;
- while (i <strlen(this->e) and i <strlen(b.e)) {
- temp_c = this->e[i]+b.e[i]-48;
- if(t) {
- temp_c+=1;
- t = false;
- }
- if(temp_c-48 > 10) {
- temp_c = (temp_c-48)%10;
- t = true;
- }
- temp.e[i] = temp_c;
- ++i;
- }
- if (i<strlen(this->e)) {
- memcpy(temp.e+i, e+i, strlen(this->e)-i);
- }
- else if (i<strlen(this->e)) {
- memcpy(temp.e+i, b.e+i, strlen(b.e)-i);
- }
- if(t) {
- BigInteger temp2(this->size+1, true);
- strcpy(temp2.e, temp.e);
- temp2.e[size] = '1';
- return temp2;
- }
- else
- return temp;
- }
- BigInteger operator +(const long &a) const {
- BigInteger temp(a);
- return *this+a;
- }
- BigInteger operator +=(const BigInteger &b) {
- return *this+b;
- }
- BigInteger operator +=(const long &a) {
- return *this+a;
- }
- /*
- BigInteger &operator *(const BigInteger &b)
- {
- BigInteger temp;
- temp = *this;
- delete [] e;
- e = new char [b.size+temp.size];
- }*/
- BigInteger operator -()
- {
- BigInteger temp(*this);
- if (*(this->e) == '-')
- temp.e[0] = '+';
- else if (*(this->e) == '+')
- temp.e[0] = '-';
- return temp;
- }
- BigInteger &operator =(const BigInteger &b)
- {
- size = b.size;
- delete [] e;
- e = new char [size];
- memcpy(e,b.e,size*sizeof(char));
- return *this;
- }
- char &operator [](const int b) {
- return e[size-1-b];
- }
- long numberOfDigits(long a) {
- if(a<=9){
- return 1;
- }
- return 1 + numberOfDigits(a/10);
- }
- friend ostream& operator <<(ostream& os, BigInteger& b);
- friend istream& operator >>(istream& is, BigInteger& b);
- };
- ostream& operator<<(ostream& os, BigInteger& b)
- {
- if (*b.e == '0')
- os << '0';
- else{
- if (*b.e == '-')
- os << '-';
- for (int i = b.size-1; i > 0; i--)
- os << *(b.e+i);
- }
- return os;
- }
- /*istream& operator >>(istream& is, BigInteger& b)
- {
- }*/
- int main() {
- long a = 150;
- char * b = "120";
- BigInteger bigint(a);
- BigInteger bigint2(b);
- BigInteger bigint3;
- bigint3 = (bigint+bigint2);
- cout<<"BI1: "<<bigint<<endl;
- cout<<"BI2: "<<bigint2<<endl;
- cout<<"BI3: "<<bigint3<<endl;
- cout<<"BI1 <- BI2"<<endl<<endl;
- bigint = bigint2;
- cout<<"BI1: "<<bigint<<endl;
- cout<<"BI2: "<<bigint2<<endl;
- bigint2 = -bigint2;
- cout<<"BI2: "<<bigint2<<endl;
- cout<<bigint2[1]<<endl;
- cout<<"KONIEC"<<endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement