Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- #include <string>
- long int rem;
- //using namespace std;
- class BigInt {
- public:
- char *big;
- int big_size;
- BigInt();
- BigInt(long);
- BigInt(std::string);
- //~BigInt();
- int which_is_bigger(BigInt B)
- {
- if(big_size>B.big_size)
- return 1;
- else if (big_size<B.big_size)
- return 2;
- else
- for(int i=1; i<big_size; i++)
- {
- if(big[i]>B.big[i])
- return 1;
- else if (big[i]<B.big[i])
- return 2;
- }
- return 0;
- }
- BigInt& operator+(BigInt B)
- {
- if((big[0]=='-' && B.big[0]=='+') && -*this==B)
- {
- *this = -*this;
- BigInt zeros("+0");
- return zeros;
- }
- if((big[0]=='+' && B.big[0]=='-') && *this==-B)
- {
- B=-B;
- BigInt zeros("+0");
- return zeros;
- }
- if(big[1]=='0')
- return B;
- if(B.big[1]=='0')
- return *this;
- int temp_size;
- char *tmp_ar;
- char *tmp_ar2;
- if(big_size>B.big_size)
- {
- temp_size = big_size+1;
- int war = B.big_size;
- int roz = big_size - B.big_size;
- tmp_ar= new char[big_size];
- for(int i=1; i<B.big_size; i++)
- {
- tmp_ar[i+roz] = B.big[i];
- war = war-1;
- if(war==1)
- break;
- }
- for(int i=0; i<=roz; i++)
- tmp_ar[i]='0';
- tmp_ar[0]='+';
- tmp_ar2=new char[big_size];
- for(int i=0; i<big_size; i++)
- tmp_ar2[i] = big[i];
- }
- else
- {
- temp_size = B.big_size+1;
- int war = big_size;
- int roz = B.big_size - big_size;
- tmp_ar= new char[B.big_size];
- for(int i=1; i<big_size; i++)
- {
- tmp_ar[i+roz] = big[i];
- war = war-1;
- if(war==1)
- break;
- }
- for(int i=0; i<=roz; i++)
- tmp_ar[i]='0';
- tmp_ar[0]='+';
- tmp_ar2=new char[B.big_size];
- for(int i=0; i<B.big_size; i++)
- tmp_ar2[i] = B.big[i];
- }
- BigInt suma(temp_size, 1);
- /*for (int i = 1; i < temp_size; i++){
- suma.big[i] = '0';
- std::cout <<"S"<< suma.big[i];}*/
- int flag = 0;
- char temp;
- //std::cout << "temp_size: " << temp_size << std::endl;
- //if(big[0]=='+' && B.big[0] == '+')
- //{
- if (big[0]=='+' && B.big[0] == '+') suma.big[0] = '+';
- else if (big[0]=='-' && B.big[0] == '-') suma.big[0] = '-';
- int b = B.big_size > big_size ? B.big_size -1 : big_size -1 ;
- //std::cout << b;
- for(int i=temp_size-1; i>1; i--, b--)
- { //std::cout << "flag" << flag <<std::endl;
- suma.big[i] = flag;
- flag = 0;
- temp = (tmp_ar2[b]-'0') + (tmp_ar[b] - '0') + 48;
- //std::cout << "temp: " << temp << "!"<< std::endl;
- if(temp>57)
- {
- temp=temp-10;
- flag = 1;
- suma.big[i] += temp;
- }
- else if (temp == 57 && suma.big[i] != '0') //kiedy 9
- {
- temp = '0';
- flag = 1;
- suma.big[i] = temp;
- }
- else suma.big[i] += temp;
- //std::cout<< "a "<<suma.big[i] << "c " << std::endl;
- if (i == 2)
- {
- //std::cout << "flag" << flag <<std::endl;
- suma.big[i-1] = flag+48;
- //std::cout<< "a "<<suma.big[i-1] << " c " << std::endl;
- break;
- }
- }
- if (suma.big[1] == '0')
- {
- BigInt sum1(suma.big_size-1,1); //zmiejszamy rozmiar o jedno 0
- sum1.big[0] = suma.big[0] == '+' ? '+' : '-';
- for (int i = sum1.big_size-1; i > 0; i--)
- sum1.big[i] = suma.big[i+1];
- sum1.remove_zeros();
- return sum1;
- }
- suma.remove_zeros();
- return suma;
- }
- BigInt& operator-(BigInt B)
- {
- if((big[0]=='+' && B.big[0]=='-') && *this==-B)
- {
- B=-B;
- BigInt zeros("+0");
- return zeros;
- }
- if((big[0]=='-' && B.big[0]=='-') && *this==B)
- {
- BigInt zeros("+0");
- return zeros;
- }
- int temp_size, small_size;
- if(big_size>B.big_size){
- temp_size = big_size;
- small_size = B.big_size;
- }
- else{
- temp_size = B.big_size;
- small_size = big_size;
- }
- BigInt roznica(temp_size, 1);
- roznica.big[0]='+';
- int flag=0;
- int i=0;
- if(big[0]=='+' && B.big[0]=='+'){
- if(big_size>B.big_size){
- for(i=temp_size-1; i>0; i--)
- {
- if(flag>0)
- if(roznica[i]<49)
- flag++;
- else
- roznica[i]=roznica[i]-flag;
- roznica.big[i]=big[i]-(B.big[small_size-1]-48);
- if(roznica[i]<48){
- roznica[i]+=10;
- flag = 1;
- }
- if(small_size==1)
- break;
- small_size--;
- }
- for(i; i>0; i--)
- roznica.big[i]=big[i];
- roznica.big[0]='+';
- }
- else if(big_size<B.big_size){
- roznica=B-*this;
- roznica.big[0]='-';
- }else {
- if(*this>B)
- {
- for(i=temp_size-1; i>0; i--)
- {
- if(flag>0)
- if(roznica[i]<49)
- flag++;
- else
- roznica[i]=roznica[i]-flag;
- roznica.big[i]=big[i]-(B.big[small_size-1]-48);
- if(roznica[i]<48){
- roznica[i]+=10;
- flag = 1;
- }
- if(small_size==1)
- break;
- small_size--;
- }
- for(i; i>0; i--)
- roznica.big[i]=big[i];
- roznica.big[0]='+';
- }
- else if(B>*this)
- {
- roznica=B-*this;
- roznica.big[0]='-';
- }
- else
- {
- BigInt zero('0');
- return zero;
- }
- }
- }
- if(big[0]=='+' && B.big[0]=='-'){
- B=-B;
- roznica=*this+B;
- B=-B;
- }
- if(big[0]=='-' && B.big[0]=='+'){
- *this=-*this;
- roznica=*this+B;
- *this=-*this;
- }
- roznica.remove_zeros();
- return roznica;
- }
- const BigInt operator*(const BigInt& B) const
- {
- if(big[1]=='0' || B.big[1]=='0')
- {
- BigInt zeros("+0");
- return zeros;
- }
- BigInt iloczyn(big_size+B.big_size-1, 1); //-1 bo jeden z dwĂłch znakĂłw jest niepotrzebny
- //std::cout << iloczyn.big_size;
- if(big[0]=='-' && B.big[0]=='-')
- iloczyn[0] = '+';
- else if(big[0]=='-' || B.big[0]=='-')
- iloczyn[0] = '-';
- else
- iloczyn[0] = '+';
- int bigger, smaller;
- if(big_size>B.big_size){bigger = big_size; smaller = B.big_size;}
- else {bigger = B.big_size; smaller = big_size;}
- BigInt rows[smaller-1]; //-1 bo znak
- for (int i = 0; i < smaller-1; i++) //-1
- {
- rows[i].big_size = bigger + i+1; //+1 bo liczba może byc o 1 większa, +i na zera
- //std::cout << "b" <<rows[i].big_size;
- rows[i].big = new char[rows[i].big_size];
- for (int j = 0; j < rows[i].big_size; j++)
- rows[i].big[j] = '0';
- rows[i].big[0] = '+';
- }
- //for (int i = 0; i < smaller-1; i++){
- // std::cout << rows[i].big;
- // std::cout << std::endl;}
- int rest;
- int s;
- int ct=0;
- for (int i = smaller; i > 1; i--,ct++) //wymnażamy rzędy do dodania
- {
- rest = 0;
- s = rows[smaller-i].big_size-1;
- for (int j = bigger; j > 1; j--,s--)
- {
- //std::cout << "ct: " << ct << std::endl;
- //std::cout << i <<" " << big[i-1] << " " << B.big[j-1] << std::endl;
- if (big_size < B.big_size)
- {
- rows[smaller-i].big[s-ct] = (((big[i-1]-48) * (B.big[j-1]-48) + rest) % 10)+48; //char
- rest = (((big[i-1]-48) * (B.big[j-1]-48) + rest) / 10); //int
- //std::cout << "rest = " <<rest <<std::endl;
- if (j == 2)
- rows[smaller-i].big[s-ct-1] = rest + 48;
- //std::cout << "\trows[smaller-i].big[s]" << smaller-i << " "<< s << " "<< rows[smaller-i].big[s-ct] <<std::endl;
- }
- else
- {
- rows[smaller-i].big[s-ct] = (((B.big[i-1]-48) * (big[j-1]-48) + rest) % 10)+48; //char
- rest = (((B.big[i-1]-48) * (big[j-1]-48) + rest) / 10); //int
- //std::cout << "rest = " <<rest <<std::endl;
- if (j == 2)
- rows[smaller-i].big[s-ct-1] = rest + 48;
- //std::cout << "\trows[smaller-i].big[s]" << smaller-i << " "<< s-ct << " " << s-ct-1 << " " << rows[smaller-i].big[s-ct] <<std::endl;
- }
- }
- }
- /*for (int i = 0; i < smaller-1; i++){
- std::cout << rows[i].big;
- std::cout << std::endl;}*/
- for (int i = 1; i < iloczyn.big_size; i++) //zerujemy zeby mozna było dodawać
- iloczyn.big[i] = '0';
- //std::cout << iloczyn.big <<std::endl;
- for (int i = 0; i < smaller-1; i++)
- {
- //std::cout << "A"<< rows[i].big;
- iloczyn = iloczyn + rows[i];
- //std::cout << "p " << iloczyn.big <<std::endl;
- }
- for (int i = 0; i <= bigger; i++) //żeby zera sie zgadzały
- {
- if(iloczyn.big[1] == '0' && iloczyn.big_size != 2)
- {
- BigInt iloczyn1(iloczyn.big_size - 1,1);
- //iloczyn1.big_size = iloczyn.big_size - 1; //zmiejszamy rozmiar o jedno 0
- //iloczyn1.big = new char[iloczyn1.big_size];
- iloczyn1.big[0] = iloczyn.big[0];
- for (int i = iloczyn1.big_size-1; i > 0; i--)
- iloczyn1.big[i] = iloczyn.big[i+1];
- iloczyn = iloczyn1; //trzeba przepisac bo wynik1 to obiekt tymczasowy
- }
- }
- iloczyn.remove_zeros();
- if(big[0]=='-' && B.big[0]=='-')
- iloczyn[0] = '+';
- else if(big[0]=='-' || B.big[0]=='-')
- iloczyn[0] = '-';
- else
- iloczyn[0] = '+';
- return iloczyn;
- }
- void remove_zeros(){
- if(big[1] == '0' && big_size > 2) {
- int i=1;
- while(big[i] == '0')
- {
- i++;
- }
- int temp_size = big_size-i+1;
- BigInt tmp(temp_size, 1);
- tmp.big[0] = big[0];
- int k=1;
- for(int j=i; j<big_size; j++)
- {
- tmp.big[k] = big[j];
- k++;
- }
- delete[] big;
- big = new char[temp_size];
- for(int z=0; z<temp_size; z++)
- big[z]=tmp.big[z];
- big_size=temp_size;
- }
- }
- BigInt operator+(long c)
- {
- BigInt B(c);
- return *this+B;
- }
- bool operator== (BigInt B)
- {
- if(big_size!=B.big_size)
- return 0;
- else
- {
- int temp_size;
- char *tmp_ar;
- char *tmp_ar2;
- if(big_size>B.big_size)
- {
- temp_size = big_size+1;
- int war = B.big_size;
- int roz = big_size - B.big_size;
- tmp_ar= new char[big_size];
- for(int i=1; i<B.big_size; i++)
- {
- tmp_ar[i+roz] = B.big[i];
- war = war-1;
- if(war==1)
- break;
- }
- for(int i=0; i<=roz; i++)
- tmp_ar[i]='0';
- tmp_ar[0]='+';
- tmp_ar2=new char[big_size];
- for(int i=0; i<big_size; i++)
- tmp_ar2[i] = big[i];
- }
- else
- {
- temp_size = B.big_size+1;
- int war = big_size;
- int roz = B.big_size - big_size;
- tmp_ar= new char[B.big_size];
- for(int i=1; i<big_size; i++)
- {
- tmp_ar[i+roz] = big[i];
- war = war-1;
- if(war==1)
- break;
- }
- for(int i=0; i<=roz; i++)
- tmp_ar[i]='0';
- tmp_ar[0]='+';
- tmp_ar2=new char[B.big_size];
- for(int i=0; i<B.big_size; i++)
- tmp_ar2[i] = B.big[i];
- }
- for(int i=0; i<temp_size-2; i++)
- {
- if(tmp_ar[i]!=tmp_ar2[i])
- return 0;
- }
- }
- return 1;
- }
- bool operator!= (BigInt B)
- {
- if(big_size!=B.big_size)
- return 1;
- else
- {
- int temp_size;
- char *tmp_ar;
- char *tmp_ar2;
- if(big_size>B.big_size)
- {
- temp_size = big_size+1;
- int war = B.big_size;
- int roz = big_size - B.big_size;
- tmp_ar= new char[big_size];
- for(int i=1; i<B.big_size; i++)
- {
- tmp_ar[i+roz] = B.big[i];
- war = war-1;
- if(war==1)
- break;
- }
- for(int i=0; i<=roz; i++)
- tmp_ar[i]='0';
- tmp_ar[0]='+';
- tmp_ar2=new char[big_size];
- for(int i=0; i<big_size; i++)
- tmp_ar2[i] = big[i];
- }
- else
- {
- temp_size = B.big_size+1;
- int war = big_size;
- int roz = B.big_size - big_size;
- tmp_ar= new char[B.big_size];
- for(int i=1; i<big_size; i++)
- {
- tmp_ar[i+roz] = big[i];
- war = war-1;
- if(war==1)
- break;
- }
- for(int i=0; i<=roz; i++)
- tmp_ar[i]='0';
- tmp_ar[0]='+';
- tmp_ar2=new char[B.big_size];
- for(int i=0; i<B.big_size; i++)
- tmp_ar2[i] = B.big[i];
- }
- for(int i=0; i<temp_size-2; i++)
- {
- if(tmp_ar[i]!=tmp_ar2[i])
- return 1;
- }
- }
- return 0;
- }
- bool operator< (BigInt B)
- {
- if(big[0]=='-' && B.big[0]=='+')
- return 1;
- else if(big[0]=='+' && B.big[0]=='-')
- return 0;
- else if(big[0]=='+' && B.big[0]=='+')
- {
- if(big_size<B.big_size)
- return 1;
- else if(big_size>B.big_size)
- return 0;
- else
- {
- for(int i=1; i<big_size; i++)
- if(big[i]>B.big[i])
- return 0;
- }
- }
- else
- {
- if(big_size<B.big_size)
- return 0;
- else if(big_size>B.big_size)
- return 1;
- else
- {
- for(int i=1; i<big_size; i++)
- if(big[i]<B.big[i])
- return 0;
- }
- }
- return 1;
- }
- bool operator> (BigInt B)
- {
- if(big[0]=='-' && B.big[0]=='+')
- return 0;
- else if(big[0]=='+' && B.big[0]=='-')
- return 1;
- else if(big[0]=='+' && B.big[0]=='+')
- {
- if(big_size<B.big_size)
- return 0;
- else if(big_size>B.big_size)
- return 1;
- else
- {
- for(int i=0; i<big_size; i++)
- if(big[i]<B.big[i])
- return 0;
- }
- }
- else
- {
- if(big_size<B.big_size)
- return 1;
- else if(big_size>B.big_size)
- return 0;
- else
- {
- for(int i=1; i<big_size; i++)
- if(big[i]<B.big[i])
- return 0;
- }
- }
- return 1;
- }
- bool operator>= (BigInt B)
- {
- int flag;
- if(big_size == B.big_size)
- for(int i=0; i<big_size; i++)
- {
- if(big[i]==B.big[i])
- flag=1;
- else
- flag=0;
- }
- if(flag==1)
- return 1;
- if(big[0]=='-' && B.big[0]=='+')
- return 0;
- else if(big[0]=='+' && B.big[0]=='-')
- return 1;
- else if(big[0]=='+' && B.big[0]=='+')
- {
- if(big_size<B.big_size)
- return 0;
- else if(big_size>B.big_size)
- return 1;
- else
- {
- for(int i=0; i<big_size; i++)
- if(big[i]<B.big[i])
- return 0;
- }
- }
- else
- {
- if(big_size<B.big_size)
- return 1;
- else if(big_size>B.big_size)
- return 0;
- else
- {
- for(int i=1; i<big_size; i++)
- if(big[i]<B.big[i])
- return 0;
- }
- }
- return 1;
- }
- bool operator<= (BigInt B)
- {
- int flag;
- if(big_size == B.big_size)
- for(int i=0; i<big_size; i++)
- {
- if(big[i]==B.big[i])
- flag=1;
- else
- flag=0;
- }
- if(flag==1)
- return 1;
- if(big[0]=='-' && B.big[0]=='+')
- return 1;
- else if(big[0]=='+' && B.big[0]=='-')
- return 0;
- else if(big[0]=='+' && B.big[0]=='+')
- {
- if(big_size<B.big_size)
- return 1;
- else if(big_size>B.big_size)
- return 0;
- else
- {
- for(int i=1; i<big_size; i++)
- if(big[i]>B.big[i])
- return 0;
- }
- }
- else
- {
- if(big_size<B.big_size)
- return 0;
- else if(big_size>B.big_size)
- return 1;
- else
- {
- for(int i=1; i<big_size; i++)
- if(big[i]<B.big[i])
- return 0;
- }
- }
- return 1;
- }
- const BigInt operator++(int x)
- {
- BigInt tmp("+1");
- return *this+tmp;
- }
- BigInt operator++()
- {
- BigInt tmp("+1");
- BigInt retval = *this;
- *this = *this+tmp;
- return retval;
- }
- BigInt operator--(int x)
- {
- BigInt tmp("+1");
- return *this-tmp;
- }
- BigInt operator--()
- {
- BigInt tmp("+1");
- BigInt retval = *this;
- *this = *this-tmp;
- return retval;
- }
- BigInt& operator-()
- {
- if(big[0]=='+')
- big[0]='-';
- else
- big[0]='+';
- return *this;
- }
- BigInt& operator=(const BigInt B)
- {
- if (this == &B)
- return *this;
- delete [] big;
- big_size = B.big_size;
- big = new char[big_size];
- for(int i=0; i<B.big_size; i++)
- {
- big[i]=B.big[i];
- }
- return *this;
- }
- char& operator[](int i)
- {
- int j=1;
- int flag=0;
- for(j=1; j<big_size; j++)
- if(big[i]>48 && big[j]<57)
- flag=i;
- else
- break;
- return big[flag];
- }
- BigInt operator/(BigInt B)
- {
- if(big_size<B.big_size || *this<B){
- BigInt zeros("+0");
- return zeros;
- }
- char counter = 0;
- int full_counter = B.big_size;
- BigInt iloraz(big_size, 1);
- BigInt tmp(B.big_size+1, 1);
- for(int i=0; i<big_size; i++)
- tmp[i] = big[i];
- if(tmp<B) {
- tmp[big_size+1]=big[big_size+1];
- full_counter++;
- }
- while(tmp>B)
- {
- counter++;
- tmp=tmp-B;
- }
- iloraz[1] = counter+48;
- full_counter++;
- if(full_counter==big_size)
- return iloraz;
- }
- BigInt(int, int);
- };
- std::ostream& operator<< (std::ostream& os, const BigInt& B)
- {
- for(int i=0; i<B.big_size; i++)
- os<< B.big[i];
- return os;
- }
- std::istream& operator>> (std::istream& is, BigInt& B)
- {
- for(int i=1; i<B.big_size; i++)
- is>>B.big[i];
- return is;
- }
- int NumDigits(int x)
- {
- x = abs(x);
- return (x < 10 ? 1 :
- (x < 100 ? 2 :
- (x < 1000 ? 3 :
- (x < 10000 ? 4 :
- (x < 100000 ? 5 :
- (x < 1000000 ? 6 :
- (x < 10000000 ? 7 :
- (x < 100000000 ? 8 :
- (x < 1000000000 ? 9 :
- 10)))))))));
- }
- BigInt::BigInt(int size, int flag)
- {
- big = new char[size];
- big_size = size;
- }
- BigInt::BigInt(long a)
- {
- int numberOfDigits = NumDigits(a)+1;
- big = new char[numberOfDigits];
- big_size = numberOfDigits;
- if(a<0)
- big[0] = '-';
- else
- big[0] = '+';
- int temp = a;
- int rest;
- a=abs(a);
- for(int i=numberOfDigits-1; i>0; i--)
- {
- rest = temp%10;
- temp = (temp-rest)/10;
- rest = rest + 48;
- big[i] = rest;
- }
- }
- BigInt::BigInt(std::string str)
- {
- int size;
- if(str.at(0) == '+' || str.at(0) == '-')
- size = str.length();
- else
- size = str.length()+1;
- big = new char[size];
- if(str.at(0) == '+' || str.at(0) == '-')
- {
- big[0] = str.at(0);
- for(int i=size-1; i>0; i--)
- {
- big[i] = str.at(i);
- }
- }
- else
- {
- big[0] = '+';
- for(int i=size-1; i>0; i--)
- {
- big[i] = str.at(i-1);
- }
- }
- big_size = size;
- }
- BigInt::BigInt()
- {
- // big_size = 0;
- // big = new char[2];
- // big[0] = '+';
- // big[1] = '0';
- }
- int main(void)
- {
- BigInt bi("+1");
- BigInt bi2("-112");
- BigInt s1("22");
- BigInt s2("32");
- BigInt s3("42");
- BigInt s4("52");
- BigInt s5("62");
- BigInt b_silnia = s1*s2*s3*s4*s5;
- std::cout<<"Mnozenie: " << b_silnia << std::endl;
- std::cout<<"test: "<<bi<<std::endl;
- std::cout<<"test: "<<bi2<<std::endl;
- std::cout<<"Dodawanie longa: " <<bi+999999998<<std::endl;
- BigInt b8 = bi-bi2;
- std::cout<<"Odejmowanie: "<<b8<<std::endl;
- -b8;
- std::cout<<"Odejmowanie: "<<b8<<std::endl;
- BigInt bi3("+122");
- BigInt bi4("+144");
- BigInt bi5 = bi3/bi4;
- std::cout<<"dzielenie: "<<bi5<<std::endl;
- std::cout<<"dzielenie: "<<bi5<<std::endl;
- BigInt size_test1("+125");
- BigInt size_test2("-15");
- size_test1=size_test1+size_test2;
- std::cout<<size_test1<<", size: "<<size_test1.big_size<<std::endl;
- size_test1=size_test1+size_test2;
- std::cout<<size_test1<<", size: "<<size_test1.big_size<<std::endl;
- size_test1=size_test1+size_test2;
- std::cout<<size_test1<<", size: "<<size_test1.big_size<<std::endl;
- size_test1=size_test1-size_test2;
- std::cout<<size_test1<<", size: "<<size_test1.big_size<<std::endl;
- size_test1=size_test1-size_test2;
- std::cout<<size_test1<<", size: "<<size_test1.big_size<<std::endl;
- size_test1=size_test1-size_test2;
- std::cout<<size_test1<<", size: "<<size_test1.big_size<<std::endl;
- size_test1=size_test1-size_test2;
- std::cout<<size_test1<<", size: "<<size_test1.big_size<<std::endl;
- size_test1=size_test1-size_test2;
- std::cout<<size_test1<<", size: "<<size_test1.big_size<<std::endl;
- size_test1=size_test1-size_test2;
- std::cout<<size_test1<<", size: "<<size_test1.big_size<<std::endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement