Treceți la conținutul principal

big integers cpp clasa

#include <iostream.h>
#include <stdio.h>
#include <conio.h>
#include <string.h>

class BigInt
{
private:
 char semn;
 unsigned char nr[400];
 int dim;
public:
 BigInt();  //constructor cu citire de la tastatura
 BigInt(int); 
 friend ostream& operator<<(ostream&,const BigInt&);
 friend istream& operator>>(istream&, BigInt&);

 int Pozitiv(int x);
 int Max(BigInt);

 BigInt operator+(BigInt);
 template<typename tip> BigInt operator+(tip);
 BigInt operator-(BigInt);
 template<typename tip> BigInt operator-(tip);
 template<typename tip> BigInt operator*(tip);
 template<typename tip> BigInt operator/(tip);
 template<typename tip> BigInt operator%(tip);

 int operator==(BigInt);  //intoarce 1 daca e adev si 0 pt fals
 int operator<(BigInt); 
 int operator<=(BigInt);
 int operator>(BigInt);
 int operator>=(BigInt);
 int operator!=(BigInt);
 int operator!();
 template<typename tip> int operator==(tip);
 template<typename tip> int operator<(tip); 
 template<typename tip> int operator<=(tip);
 template<typename tip> int operator>(tip);
 template<typename tip> int operator>=(tip);
 template<typename tip> int operator!=(tip);
 
};


//#########################################################################

BigInt::BigInt()
{char x;
 signed char t[400];
 int i;
 
 printf("\nNumarul cu semn ");
 do 
 semn=getche();
 while((semn!='+')&&(semn!='-'));
 dim=0;
 do {x=getche();
  t[dim]=x-'0';
  dim++;
  }
 while((x>='0')&&(x<='9'));
 dim--;
 for(i=0;i<dim;i++)
  nr[i]=t[dim-i-1];
}

int BigInt::Pozitiv(int x)
{int a,vb;
 a=0; vb=0;
 while(vb==0)
  if((x+a)==0) vb=1;
  else a=a+1;
 x=a;
return x;
}

BigInt::BigInt(int x) 
{ int i;
 if(x>=0) semn='+';
 else {semn='-'; x=Pozitiv(x); }
 dim=0; i=0;
 while(x!=0)
 {nr[i]=x%10; i++; dim=i; x=x/10;}
}

ostream& operator<<(ostream& out,const BigInt& a)
{int i;
 out<<a.semn;
 if(a.dim==0) out<<0;
 else
  for(i=0;i<a.dim;i++)
   out<<(int)a.nr[a.dim-i-1];
 
return out;

}

istream& operator>>(istream& in, BigInt& a)
{char t[400];
 int i;
 do
 in>>a.semn;
 while((a.semn!='+')&&(a.semn!='-'));
 a.dim=0;
 cin.get(t,40);
 for(i=0;i<strlen(t);i++)
  {if((t[i]>='0')&&(t[i]<='9'))
    {t[i]=t[i]-'0';
    a.dim++;
    }
   else break;
  }
 for(i=0;i<a.dim;i++)
  a.nr[i]=t[a.dim-i-1];
return in;

}

BigInt BigInt::operator+(BigInt b)
{int i,k,max; 
 signed char p;
 BigInt c(1);
 max=(dim>b.dim)?dim:b.dim;
 c.semn=semn;
 k=0;
 for(i=0;i<max;i++)
  {p=(i<dim?nr[i]:0)+(i<b.dim?b.nr[i]:0)+k;
  c.nr[i]=p%10;
  k=p/10;
  }
 if(k!=0) {c.nr[max]=k; c.dim=max+1;}
 else c.dim=max;
return c;
}

template <typename tip> BigInt BigInt::operator+(tip x)
{BigInt c(0),b(x);
 c=*this+b;
return c; 
}

int BigInt::Max(BigInt b)
{int i,m;
 m=dim<b.dim?-1:(dim==b.dim?0:1);
 i=dim;
 while((m==0)&&(i>=0))
  {m=nr[i]-b.nr[i];
   i--;
  }
return m;
}

BigInt BigInt::operator-(BigInt b)
{int i,m,impr;
 BigInt c(0),aux(0);
 m=this->Max(b);
 if(m>0) c.semn=this->semn;
 else
  if(m<0) c.semn=(this->semn=='+')?'-':'+';
  else {m=0; c.dim=0;c.semn='+';}
 if(m!=0)
  {if(m<0) 
   {aux=*this;
    *this=b;
    b=aux;
   }
  impr=0;
  for(i=0;i<this->dim;i++)
   {c.nr[i]=this->nr[i]-(i<b.dim?b.nr[i]:0)-impr;
   if(c.nr[i]<0)
    {c.nr[i]+=10;
    impr=0;
    }
   else impr=0;
   c.dim=this->dim;
   while(c.nr[c.dim-1]==0) c.dim--;
   }
  }
return c;
}

template<typename tip> BigInt BigInt::operator-(tip x)
{BigInt b(x),c(0);
 c=*this-b;
return c;
}

template<typename tip> BigInt BigInt::operator*(tip x)
{int i;
 signed char t,s1;
 BigInt b(0);
 b.semn=semn;
 t=0;
 for(i=0;i<dim;i++)
 {s1=nr[i]*x+t;
  b.nr[i]=s1%10;
  t=s1/10; 
 }
 if(t!=0) {b.nr[n]=t;
     b.dim=dim+1;
    }
 else b.dim=dim;
return b;
}

template<typename tip> BigInt BigInt::operator/(tip x)
{int i,r,k,temp[401];
 BigInt t(0);
 t.semn=semn;
 if(x==0) cout<<"Operatie lipsita de sens!";
 else
 { t.dim=1;
  if(nr[dim-1]>=x)
   {temp[0]=nr[dim-1]/x;
   r=nr[dim-1]%x;
   i=2;
   while(i<dim+2) 
    {t.dim++;
    r=r*10+nr[dim-i];
    temp[i-1]=r/x;
    r=r%x;
    i++;}
   }
  else
   {i=0;
    temp[0]=nr[dim-1];
    while(temp[0]<x)
    {i++;
     temp[0]=temp[0]*10+nr[dim-i-1];}
    r=temp[0]%x;
    temp[0]=temp[0]/x;
    k=1;
    while(i<dim) 
    {t.dim++;
    r=r*10+nr[dim-i-2];
    temp[k]=r/x;
    r=r%x;
    i++;k++;}
   }
 }
 t.dim--;
 for(i=0;i<t.dim;i++)
  t.nr[i]=temp[t.dim-i-1];
return t;
}

template<typename tip> BigInt BigInt::operator%(tip x)
{BigInt r(0); 
 r=(*this)/x;
 r=(*this)-r*x;
return r;
}

int BigInt::operator==(BigInt b)
{int i,vb;
 if((semn==b.semn)&&(dim==b.dim))
  {vb=0; i=0;
   while((vb==0)&&(i<dim))
   {if(nr[i]!=b.nr[i]) vb=1;
    i++;
   }
   if(vb==0) return 1;
  }
return 0;
}

template<typename tip> int BigInt::operator==(tip x)
{BigInt b(x);
 if(*this==b) return 1;
return 0;
}

int BigInt::operator<(BigInt b)
{int i,vb;
 if((semn!=b.semn)&&(semn=='-')) return 1;
 else
  if(semn=='+')
   {if(dim<b.dim) return 1;
    if(dim==b.dim)
    {vb=0; i=dim-1;
     while((vb==0)&&(i!=-1))
     {if(nr[i]<b.nr[i]) vb=1;
      i--;
     }
     if(vb==1) return 1;
    }
   }
  else
   {if(semn=='-')
    {if(dim>b.dim) return 1;
     if(dim==b.dim)
     {vb=0; i=dim-1;
      while((vb==0)&&(i!=-1))
      {if(nr[i]>b.nr[i]) vb=1;
       i--;
      }
      if(vb==1) return 1;
      }
    }   
   }
return 0;
}

template<typename tip> int BigInt::operator<(tip x)
{BigInt b(x);
 if(*this<b) return 1;
return 0;
}

int BigInt::operator<=(BigInt a2)
{
 if(*this<a2) return 1;
 if(*this==a2) return 1;
return 0;
}

template<typename tip> int BigInt::operator<=(tip x)
{BigInt b(x);
 if(*this<b) return 1;
 if(*this==b) return 1;
return 0;
}

int BigInt::operator>(BigInt b)
{
 if(*this<b) return 0;
 if(*this==b) return 0;
return 1;
}

template<typename tip> int BigInt::operator>(tip x)
{BigInt b(x);
 if(*this<b) return 0;
 if(*this==b) return 0;
return 1;
}

int BigInt::operator>=(BigInt b)
{ if(*this>b) return 1;
 if(*this==b) return 1;
return 0;
}

template<typename tip> int BigInt::operator>=(tip x)
{BigInt b(x);
  if(*this>b) return 1;
 if(*this==b) return 1;
return 0;
}

int BigInt::operator!=(BigInt b)
{int i,vb;
 if(semn!=b.semn) return 1;
 else
  if(dim!=b.dim) return 1;
  else 
   {vb=0; i=0;
    while((vb==0)&&(i<dim))
    {if(nr[i]!=b.nr[i]) vb=1;
     i++;
    }
   if(vb==1) return 1;
   }
return 0;
}

template<typename tip> int BigInt::operator!=(tip x)
{BigInt b(x);
 if(*this!=b) return 1;
return 0;
}

int BigInt::operator!()
{
 if((dim==0)&&(nr[dim]==0))return 1;
return 0;
}


//########################################################################
void main()
{ 
 BigInt a(123456);
 int x=12;

 cout<<"Numarul mare: "<<a<<endl;;
 cout<<"Numar oarecare: "<<x<<endl;
 cout<<"a+x= "<<(a+x)<<endl;
 cout<<"a-x= "<<(a-x)<<endl;
 cout<<"a/x= "<<(a/x)<<endl; 
 cout<<"a%x= "<<(a%x)<<endl;
 cout<<"a==x";
 a==x?cout<<"  True\n":cout<<"  False\n";
 cout<<"a<x";
 a<x?cout<<"  True\n":cout<<"  False\n";
 cout<<"a<=x";
 a<=x?cout<<"  True\n":cout<<"  False\n";
 cout<<"a>x";
 a>x?cout<<"  True\n":cout<<"  False\n";
 cout<<"a>=x";
 a>=x?cout<<"  True\n":cout<<"  False\n";
 cout<<"a!=x";
 a!=x?cout<<"  True\n":cout<<"  False\n";
 cout<<"!x";
 !x?cout<<"  True\n":cout<<"  False\n";
 

}

Comentarii

Postări populare de pe acest blog

program principal cpp

#include "clasa.h" #include <stdio.h> #include <conio.h> #include <stdlib.h> #include <string.h> #define DELAY 9000000 void delay() { for(long i=0;i<DELAY;i++); } //constructor cu initializare de la tastatura BigInt::BigInt() {char x; signed char t[400]; int i; printf("\nNumarul cu semn "); do s=getche(); while((s!='+')&&(s!='-')); n=0; do {x=getche(); t[n]=x-'0'; n++; } while((x>='0')&&(x<='9')); n--; for(i=0;i<n;i++) nr[i]=t[n-i-1]; } //constructor cu initializare prin parametri BigInt::BigInt(char semn,signed char numar[],int dim) {int i; s=semn; n=dim; for(i=0;i<n;i++) nr[i]=numar[n-i-1]; } //transform un int negativ in pozitiv int BigInt::Pozitiv(int x) {int a,vb; a=0; vb=0; while(vb==0) if((x+a)==0) vb=1; else a=a+1; x=a; return x; } //constructor dintr-un nr int obisnuit BigInt::BigInt(int x) {int i; if(x>=0) s='+'…

NUMERE PRIME ALGORITM C++

// NUMERE PRIME ALGORITM C++//  reediting from scratch //on this page is just the study for a next algoritm for generating the parime nr series like Fibonnaci or ....if possibile

74111121313417374414124343447 if u know the red part you can generate the orange part
1 0 1 111112222 1 1 23

o aplicatie php localitati romania

//APLICATIA SE REFERA LA BAZA DE DATE SIRUTA

//dragtable.js


/* dragtable v1.0 June 26, 2008 Dan Vanderkam, http://danvk.org/dragtable/ http://code.google.com/p/dragtable/ \Bsortabledraggable\B Instructions: - Download this file - Add <script src="dragtable.js"></script> to your HTML. - Add class="draggable" to any table you might like to reorder. - Drag the headers around to reorder them. This is code was based on: - Stuart Langridge's SortTable (kryogenix.org/code/browser/sorttable) - Mike Hall's draggable class (http://www.brainjar.com/dhtml/drag/) - A discussion of permuting table columns on comp.lang.javascript Licensed under the MIT license. */ // Here's the notice from Mike Hall's draggable script: //***************************************************************************** // Do not remove this notice. // // Copyright 2001 by Mike Hall. // See http…