Advertisement
Gireada

Adunare/Scadere in Binar

Apr 5th, 2018
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.29 KB | None | 0 0
  1. #include <iostream>
  2. #include <string.h>
  3.  
  4. using namespace std;
  5.  
  6. int ai[10], bi[10];
  7.  
  8.  
  9. void binartoint_suma(char a[10], char b[10])
  10. {
  11.     char ak[10];
  12.     int t = 0;int j = 0;
  13.     for(int i = strlen(a); i >=0 ; i--)
  14.     {
  15.  
  16.         if(a[i] == '0' && b[i] == '0')
  17.         {
  18.             if(t == 1)
  19.             {
  20.                 ak[j] = '1'; t = 0;
  21.             }
  22.             else
  23.             {
  24.                 ak[j] = '0'; t = 0;
  25.             }
  26.         }
  27.  
  28.         if(a[i] == '0' && b[i] == '1')
  29.         {
  30.             if(t == 1)
  31.             {
  32.                 ak[j] = '0'; t = 1;
  33.             }
  34.             else
  35.             {
  36.                 ak[j] = '1'; t = 0;
  37.             }
  38.  
  39.         }
  40.  
  41.         if(a[i] == '1' && b[i] == '0')
  42.         {
  43.             if(t == 1)
  44.             {
  45.                 ak[j] = '0'; t = 1;
  46.             }
  47.             else
  48.             {
  49.                 ak[j] = '1'; t = 0;
  50.             }
  51.         }
  52.  
  53.         if(a[i] == '1' && b[i] == '1')
  54.         {
  55.             if(t == 1)
  56.             {
  57.                 ak[j] = '1'; t = 1;
  58.             }
  59.             else
  60.             {
  61.                 ak[j] = '0'; t = 1;
  62.                 if(i == 0) ak[j+1] = '1';
  63.             }
  64.         }
  65.         j++;
  66.     }
  67.     for(int i = strlen(ak)-1; i >= 0;  i--)
  68.     {
  69.         cout<<ak[i]<<" ";
  70.     }
  71. }
  72.  
  73. bool imprumuta(char a[10], char b[10], int pozitie)
  74. {
  75.     bool result = false;
  76.     for(int i = pozitie+1; i <=strlen(a); i++)
  77.     {
  78.         if(a[i] == '1')
  79.         {
  80.             a[i] = '0';
  81.             result = true;
  82.             break;
  83.         }
  84.     }
  85.     return result;
  86. }
  87.  
  88. void binartoint_scadere(char a[10], char b[10])
  89. {
  90.     int t = 0;
  91.     cout<<"SCADERE\n\n";
  92.     for(int i = 0; i <= 10; i++)
  93.     {
  94.         if(a[i] == '0' && b[i] == '0')
  95.         {
  96.             if(t == 1)
  97.             {
  98.                 if(imprumuta(a, b, i) == true)
  99.                 {
  100.                     cout<<"1"; t = 0;
  101.                 }
  102.             }
  103.             else
  104.             {
  105.                 cout<<"0"; t = 0;
  106.             }
  107.         }
  108.         if(a[i] == '0' && b[i] == '1')
  109.         {
  110.             if(t == 1)
  111.             {
  112.                 if(imprumuta(a, b, i) == true)
  113.                 {
  114.                     cout<<"0"; t = 0;
  115.                 }
  116.             }
  117.             else
  118.             {
  119.                 cout<<"1"; t = 1;
  120.             }
  121.  
  122.         }
  123.         if(a[i] == '1' && b[i] == '0')
  124.         {
  125.             if(t == 1)
  126.             {
  127.                 if(imprumuta(a, b, i) == true)
  128.                 {
  129.                     cout<<"0"; t = 0;
  130.                 }
  131.             }
  132.             else
  133.             {
  134.                 cout<<"1"; t = 0;
  135.             }
  136.         }
  137.         if(a[i] == '1' && b[i] == '1')
  138.         {
  139.             if(t == 1)
  140.             {
  141.                 if(imprumuta(a, b, i) == true)
  142.                 {
  143.                     cout<<"1"; t = 1;
  144.                 }
  145.             }
  146.             else
  147.             {
  148.                 cout<<"0"; t = 0;
  149.             }
  150.         }
  151.     }
  152. }
  153.  
  154. int main()
  155. {
  156.     int op;
  157.     char a[10],b[10];
  158.     cout<<"Introduceti numerele: ";cin>>a>>b;
  159.     cout<<"Alegeti operatia: \n1.Adunare\n2.Scadere\n\n";cin>>op;
  160.     if(op == 1)
  161.         binartoint_suma(a, b);
  162.     else
  163.         binartoint_scadere(a, b);
  164.  
  165.  
  166.     return 0;
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement