Advertisement
Gireada

Convertire ZECIMAL,BINAR,OCTAL,HEXA

Mar 8th, 2018
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.89 KB | None | 0 0
  1. #include <iostream>
  2. #include <string.h>
  3. #include <stdio.h>
  4. #include <math.h>
  5.  
  6. char binar[6];
  7. char octal[6];
  8.  
  9.  
  10. using namespace std;
  11.  
  12. void IntToBinar(int n)
  13. {
  14.     for(int i = 0; i <= 4; i++)
  15.     {
  16.         binar[i] = '0';
  17.     }
  18.     int j = 3;
  19.     while(n>0)
  20.     {
  21.         if(n%2 == 0)
  22.             binar[j] = '0';
  23.         else
  24.             binar[j] = '1';
  25.         n = n / 2;
  26.         j--;
  27.     }
  28.     for(int i = 0; i <= 3; i++)
  29.     {
  30.         cout<<binar[i];
  31.     }
  32. }
  33.  
  34. int BinarToInt()
  35. {
  36.    int numar = 0;
  37.    int i = strlen(binar)-1;
  38.    int j = 0;
  39.    while(i>=0)
  40.    {
  41.        if(binar[i] == '1')
  42.        {
  43.            numar += pow(2,j);
  44.        }
  45.        i--;
  46.        j++;
  47.    }
  48.    return numar;
  49. }
  50.  
  51. int IntToOcta(int n)
  52. {
  53.    int octa;
  54.    if(n / 8 > 0)
  55.    {
  56.        octa = 1;
  57.        octa *= 10;
  58.        octa = octa + (n % 8);
  59.        n = n/8;
  60.    }
  61.    else
  62.    {
  63.        octa = 0;
  64.        octa += (n % 8);
  65.        n = n/8;
  66.    }
  67.    return octa;
  68. }
  69.  
  70. void IntToHex(int n)
  71. {
  72.      int rest = 0;
  73.      while (n > 0) {
  74.         rest = n % 16;
  75.         if (rest > 9) {
  76.             switch (rest) {
  77.                case 10: cout << "A"; break;
  78.                case 11: cout << "B"; break;
  79.                case 12: cout << "C"; break;
  80.                case 13: cout << "D"; break;
  81.                case 14: cout << "E"; break;
  82.                case 15: cout << "F"; break;
  83.             }
  84.         }
  85.         else {
  86.             cout << rest;
  87.         }
  88.         n = n / 16;
  89.     }
  90. }
  91.  
  92. int OctalToInt(int numaroctal)
  93. {
  94.     int numar = 0, i = 0;
  95.  
  96.     while(numaroctal != 0)
  97.     {
  98.         numar += (numaroctal%10) * pow(8,i);
  99.         ++i;
  100.         numaroctal/=10;
  101.     }
  102.  
  103.     i = 1;
  104.  
  105.     return numar;
  106. }
  107.  
  108. int hexadecimalToDecimal(char hex[])
  109. {
  110.     int lungime = strlen(hex);
  111.     int baza = 1;
  112.  
  113.     int zecimal = 0;
  114.  
  115.     for (int i=lungime-1; i>=0; i--)
  116.     {
  117.  
  118.         if (hex[i]>='0' && hex[i]<='9')
  119.         {
  120.             zecimal += (hex[i] - 48)*baza;
  121.  
  122.             baza = baza * 16;
  123.         }
  124.  
  125.         else if (hex[i]>='A' && hex[i]<='F')
  126.         {
  127.             zecimal += (hex[i] - 55)*baza;
  128.             baza = baza*16;
  129.         }
  130.     }
  131.  
  132.     return zecimal;
  133. }
  134.  
  135.  
  136. int main()
  137. {
  138.     int baza;
  139.     cout<<"Introdu baza:\n1.Zecimal\n2.Binar\n3.Octal\n4.Hexa\n\n";
  140.     cin>>baza;
  141.  
  142.     switch(baza)
  143.     {
  144.         case 1: //Conversie din zecimal
  145.         {
  146.             int numar;
  147.             cout<<"Introdu numarul Zecimal";cin>>numar;
  148.  
  149.             cout<<"Conversie din Zecial:\n\n";
  150.             cout<<"Binar ";IntToBinar(numar);cout<<"\n";
  151.             cout<<"Octal "<<IntToOcta(numar)<<"\n";
  152.             cout<<"Hexa ";IntToHex(numar);cout<< "\n"; break;
  153.         }
  154.         case 2: //Conversie din binar
  155.         {
  156.             cout<<"Introdu numarul Binar";cin>>binar;
  157.  
  158.             cout<<"Conversie din Binar:\n\n";
  159.             int numar = BinarToInt();
  160.             cout<<"Zecimal "<<numar;cout<<"\n";
  161.             cout<<"Octal "<<IntToOcta(numar)<<"\n";
  162.             cout<<"Hexa ";IntToHex(numar);cout<< "\n"; break;
  163.         }
  164.         case 3: //Conversie din octal
  165.         {
  166.             int numaroctal;
  167.             cout<<"Introdu numarul Octal";cin>>numaroctal;
  168.  
  169.             cout<<"Conversie din Octal:\n\n";
  170.             int numar = OctalToInt(numaroctal);
  171.             cout<<"Zecimal "<<numar;cout<<"\n";
  172.             cout<<"Binar ";IntToBinar(numar);cout<<"\n";
  173.             cout<<"Hexa ";IntToHex(numar);cout<< "\n"; break;
  174.         }
  175.         case 4: //Conversie din HEXA
  176.         {
  177.             char numarhex[20];
  178.             cout<<"Introdu numarul HEXA";cin>>numarhex;
  179.  
  180.             cout<<"Conversie din HEXA:\n\n";
  181.             int numar = hexadecimalToDecimal(numarhex);
  182.             cout<<"Zecimal "<<numar;cout<<"\n";
  183.             cout<<"Binar ";IntToBinar(numar);cout<<"\n";
  184.             cout<<"Octa "<<IntToOcta(numar);cout<< "\n"; break;
  185.         }
  186.     }
  187.  
  188.  
  189.     return 0;
  190. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement