Advertisement
Guest User

Untitled

a guest
May 17th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.64 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3. #include <windows.h>
  4. #include <conio.h>
  5.  
  6. using namespace std;
  7.  
  8. class BigInt
  9. {
  10. private:
  11.     int *numar,lungime_sir;
  12. public:
  13.     BigInt();
  14.     BigInt(BigInt &c);
  15.     BigInt(BigInt &c,int v[],int lg);
  16.     ~BigInt();
  17.     BigInt& operator = (const BigInt&a);
  18.     friend istream & operator >>(istream&is,BigInt &r);
  19.     friend ostream & operator <<(ostream&os,BigInt &r);
  20.     friend BigInt operator + (BigInt a,BigInt b);
  21.     friend BigInt operator - (BigInt a,BigInt b);
  22.     friend BigInt operator * (BigInt a,BigInt b);
  23.     friend int compara(BigInt a, BigInt b);
  24.     friend bool operator > (BigInt a,BigInt b);
  25.     friend bool operator < (BigInt a,BigInt b);
  26.     friend bool par(BigInt a);
  27. };
  28.  
  29. BigInt::BigInt()
  30. {
  31.     lungime_sir=0;
  32.     numar=NULL;
  33. }
  34.  
  35. BigInt::BigInt(BigInt &c)
  36. {
  37.     lungime_sir=c.lungime_sir;
  38.     numar=new int[lungime_sir];
  39.     for(int i=0; i<lungime_sir; i++)
  40.         numar[i]=c.numar[i];
  41. }
  42.  
  43. BigInt::BigInt(BigInt &c,int v[],int lg)
  44. {
  45.     c.lungime_sir=lg;
  46.     c.numar=new int[c.lungime_sir];
  47.     for(int i=0; i<lg; i++)
  48.         c.numar[i]=v[i];
  49. }
  50.  
  51. BigInt::~BigInt()
  52. {
  53.     delete numar;
  54. }
  55.  
  56. BigInt& BigInt::operator = (const BigInt&a)
  57. {
  58.     int i;
  59.     lungime_sir=a.lungime_sir;
  60.     numar=new int[lungime_sir];
  61.     for(i=0; i<lungime_sir; i++)
  62.         numar[i]=a.numar[i];
  63. }
  64.  
  65. istream & operator >>(istream & is,BigInt & r)
  66. {
  67.     char sir[256];
  68.     is.getline(sir,256);
  69.     r.lungime_sir=strlen(sir);
  70.     r.numar=new int[r.lungime_sir];
  71.     for(int i=0; i<strlen(sir); i++)
  72.         r.numar[i]=sir[strlen(sir)-1-i]-48;
  73.     return is;
  74. }
  75.  
  76. ostream & operator <<(ostream & os,BigInt & r)
  77. {
  78.     for(int i=r.lungime_sir-1; i>=0; i--)
  79.         os<<r.numar[i];
  80.     return os;
  81. }
  82.  
  83. BigInt operator + (BigInt a,BigInt b)
  84. {
  85.     BigInt c;
  86.     int i, t=0,v[256],lungime_sir;
  87.     lungime_sir=max(a.lungime_sir,b.lungime_sir);
  88.     for(i=0;i<lungime_sir;i++)
  89.     {
  90.         if(a.numar[i]>9 || a.numar[i]<0)
  91.             a.numar[i]=0;
  92.         if(b.numar[i]>9 || b.numar[i]<0)
  93.             b.numar[i]=0;
  94.     }
  95.     for(i=0; i<lungime_sir; ++i)
  96.     {
  97.         v[i]=a.numar[i]+b.numar[i]+t;
  98.         t=v[i]/10;
  99.         v[i]=v[i]%10;
  100.     }
  101.     if(t)
  102.         v[lungime_sir++]=t;
  103.     BigInt(c,v,lungime_sir);
  104.     return c;
  105. }
  106.  
  107. BigInt operator - (BigInt a,BigInt b)
  108. {
  109.     BigInt c;
  110.     int i, t=0,dif[256],lungime_sir;
  111.  
  112.     for(i=0; i<256; i++)
  113.         dif[i]=0;
  114.  
  115.     for(i=0; i<a.lungime_sir; i++)
  116.         dif[i]=a.numar[i];
  117.  
  118.     for(i=0; i<b.lungime_sir; ++i)
  119.     {
  120.         dif[i]=dif[i]-b.numar[i]+t;
  121.         if(dif[i]<0)
  122.         {
  123.             dif[i]=dif[i]+10;
  124.             t=-1;
  125.         }
  126.         else
  127.             t=0;
  128.     }
  129.     i=a.lungime_sir;
  130.     while(i>1 && dif[i]==0)
  131.         i--;
  132.     lungime_sir=i;
  133.     BigInt(c,dif,lungime_sir);
  134.     return c;
  135. }
  136.  
  137. BigInt operator * (BigInt a,BigInt b)
  138. {
  139.     BigInt c;
  140.     int i,j,t,v[256],lungime_sir;
  141.     for(i=0; i<256; i++)
  142.         v[i]=0;
  143.     for(i=0; i<a.lungime_sir; ++i)
  144.     {
  145.         t=0;
  146.         for(j=0; j<b.lungime_sir; ++j)
  147.         {
  148.             v[i+j]=v[i+j]+a.numar[i]*b.numar[j]+t;
  149.             t=v[i+j]/10;
  150.             v[i+j]=v[i+j]%10;
  151.         }
  152.         if(t)
  153.             v[i+j]=t;
  154.     }
  155.     lungime_sir=a.lungime_sir+b.lungime_sir;
  156.     if(v[lungime_sir]==0)
  157.         lungime_sir--;
  158.     BigInt(c,v,lungime_sir);
  159.     return c;
  160. }
  161.  
  162. int compara(BigInt a, BigInt b)
  163. {
  164.     int i;
  165.     if(a.lungime_sir<b.lungime_sir)
  166.         return -1;
  167.     if(b.lungime_sir<a.lungime_sir)
  168.         return 1;
  169.     for(i=0; i<a.lungime_sir; ++i)
  170.         if(a.numar[i]<b.numar[i])
  171.             return -1;
  172.         else if(a.numar[i]>b.numar[i])
  173.             return 1;
  174.     return 0;
  175. }
  176.  
  177. bool operator > (BigInt a,BigInt b)
  178. {
  179.     return compara(a,b)>0;
  180. }
  181.  
  182. bool operator < (BigInt a,BigInt b)
  183. {
  184.     return compara(a,b)<0;
  185. }
  186.  
  187. bool par(BigInt a)
  188. {
  189.     if(a.numar[0]%2)
  190.         return true;
  191.     return false;
  192. }
  193.  
  194. void menu()
  195. {
  196.     system("cls");
  197.     cout<<"\n\n Clasa Numere Mari \n";
  198.     cout<<"\n\t1. Citire numere \n";
  199.     cout<<"\n\t2. Afisare numere \n";
  200.     cout<<"\n\t3. Suma \n";
  201.     cout<<"\n\t4. Diferenta \n";
  202.     cout<<"\n\t5. Inmultire \n";
  203.     cout<<"\n\t6. Paritate \n";
  204.     cout<<"\n\t7. Comparare \n";
  205.     //cout<<"\n\t8. Copiere \n";
  206.     cout<<"\n\t0. Exit\n";
  207.     //getch();
  208. }
  209.  
  210. int main()
  211. {
  212.     BigInt a,b,c;
  213.     int op;
  214.     while(1)
  215.     {
  216.         menu();
  217.         cout<<"\n\tOptiunea Dumneavoastra: ";
  218.         //getch();
  219.         cin>>op;
  220.         cin.get();
  221.         switch(op)
  222.         {
  223.         case 0:
  224.             return 0;
  225.         case 1:
  226.             cin>>a>>b;
  227.             //getch();
  228.             break;
  229.         case 2:
  230.             cout<<'\n';
  231.             cout<<a<<' '<<b;
  232.             getch();
  233.             break;
  234.         case 3:
  235.             c=a+b;
  236.             cout<<'\n';
  237.             cout<<c;
  238.             getch();
  239.             break;
  240.         case 4:
  241.             if(a>b)
  242.             {
  243.                 c=a-b;
  244.                 cout<<'\n';
  245.                 cout<<c;
  246.             }
  247.             else
  248.             {
  249.                 c=b-a;
  250.                 cout<<'\n';
  251.                 cout<<'-'<<c;
  252.             }
  253.             getch();
  254.             break;
  255.         case 5:
  256.             c=a*b;
  257.             cout<<'\n';
  258.             cout<<c;
  259.             getch();
  260.             break;
  261.         case 6:
  262.             cout<<'\n';
  263.             if(!par(a))
  264.                 cout<<"primul numar este par";
  265.             else
  266.                 cout<<"primul numar este impar";
  267.             cout<<'\n';
  268.             if(!par(b))
  269.                 cout<<"al doilea numar numar este par";
  270.             else
  271.                 cout<<"al doilea numar este impar";
  272.             getch();
  273.             break;
  274.         case 7:
  275.             cout<<'\n';
  276.             if(a > b)
  277.                 cout<<"primul numar este mai mare ca al doilea";
  278.             else
  279.             {
  280.                 if(a < b)
  281.                     cout<<"al doilea numar este mai mare ca primul";
  282.                 else
  283.                     cout<<"numerele sunt egale";
  284.             }
  285.             getch();
  286.             break;
  287.         case 8:
  288.             system("cls");
  289.             cout<<" ----    ---- --------  ------------ ----    ----      ------------    ------    -----------     ------    ------------ --------  ----    ----    ------     \n";
  290.             cout<<" *****   **** ********  ************ ****    ****      ************   ********   ************   ********   ************ ********  *****   ****   ********   \n";
  291.             cout<<" ------  ----   ----    ---          ----    ----      ---           ----------  ---      ---  ----------  ------------   ----    ------  ----  ----------  \n";
  292.             cout<<" ************   ****    ***          ****    ****      ***          ****    **** ************ ****    ****     ****       ****    ************ ****    **** \n";
  293.             cout<<" ------------   ----    ---          ----    ----      ---          ------------ -----------  ------------     ----       ----    ------------ ------------ \n";
  294.             cout<<" ****  ******   ****    ***          ************      ***          ************ ****         ************     ****       ****    ****  ****** ************ \n";
  295.             cout<<" ----   ----- --------  ------------ ------------      ------------ ----    ---- ----         ----    ----     ----     --------  ----   ----- ----    ---- \n";
  296.             cout<<" ****    **** ********  ************ ************      ************ ****    **** ****         ****    ****     ****     ********  ****    **** ****    **** \n";
  297.             getch();
  298.         }
  299.     }
  300.     return 0;
  301. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement