Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string.h>
- #include <stdio.h>
- #include <math.h>
- char binar[6];
- char octal[6];
- using namespace std;
- void IntToBinar(int n)
- {
- for(int i = 0; i <= 4; i++)
- {
- binar[i] = '0';
- }
- int j = 3;
- while(n>0)
- {
- if(n%2 == 0)
- binar[j] = '0';
- else
- binar[j] = '1';
- n = n / 2;
- j--;
- }
- for(int i = 0; i <= 3; i++)
- {
- cout<<binar[i];
- }
- }
- int BinarToInt()
- {
- int numar = 0;
- int i = strlen(binar)-1;
- int j = 0;
- while(i>=0)
- {
- if(binar[i] == '1')
- {
- numar += pow(2,j);
- }
- i--;
- j++;
- }
- return numar;
- }
- int IntToOcta(int n)
- {
- int octa;
- if(n / 8 > 0)
- {
- octa = 1;
- octa *= 10;
- octa = octa + (n % 8);
- n = n/8;
- }
- else
- {
- octa = 0;
- octa += (n % 8);
- n = n/8;
- }
- return octa;
- }
- void IntToHex(int n)
- {
- int rest = 0;
- while (n > 0) {
- rest = n % 16;
- if (rest > 9) {
- switch (rest) {
- case 10: cout << "A"; break;
- case 11: cout << "B"; break;
- case 12: cout << "C"; break;
- case 13: cout << "D"; break;
- case 14: cout << "E"; break;
- case 15: cout << "F"; break;
- }
- }
- else {
- cout << rest;
- }
- n = n / 16;
- }
- }
- int OctalToInt(int numaroctal)
- {
- int numar = 0, i = 0;
- while(numaroctal != 0)
- {
- numar += (numaroctal%10) * pow(8,i);
- ++i;
- numaroctal/=10;
- }
- i = 1;
- return numar;
- }
- int hexadecimalToDecimal(char hex[])
- {
- int lungime = strlen(hex);
- int baza = 1;
- int zecimal = 0;
- for (int i=lungime-1; i>=0; i--)
- {
- if (hex[i]>='0' && hex[i]<='9')
- {
- zecimal += (hex[i] - 48)*baza;
- baza = baza * 16;
- }
- else if (hex[i]>='A' && hex[i]<='F')
- {
- zecimal += (hex[i] - 55)*baza;
- baza = baza*16;
- }
- }
- return zecimal;
- }
- int main()
- {
- int baza;
- cout<<"Introdu baza:\n1.Zecimal\n2.Binar\n3.Octal\n4.Hexa\n\n";
- cin>>baza;
- switch(baza)
- {
- case 1: //Conversie din zecimal
- {
- int numar;
- cout<<"Introdu numarul Zecimal";cin>>numar;
- cout<<"Conversie din Zecial:\n\n";
- cout<<"Binar ";IntToBinar(numar);cout<<"\n";
- cout<<"Octal "<<IntToOcta(numar)<<"\n";
- cout<<"Hexa ";IntToHex(numar);cout<< "\n"; break;
- }
- case 2: //Conversie din binar
- {
- cout<<"Introdu numarul Binar";cin>>binar;
- cout<<"Conversie din Binar:\n\n";
- int numar = BinarToInt();
- cout<<"Zecimal "<<numar;cout<<"\n";
- cout<<"Octal "<<IntToOcta(numar)<<"\n";
- cout<<"Hexa ";IntToHex(numar);cout<< "\n"; break;
- }
- case 3: //Conversie din octal
- {
- int numaroctal;
- cout<<"Introdu numarul Octal";cin>>numaroctal;
- cout<<"Conversie din Octal:\n\n";
- int numar = OctalToInt(numaroctal);
- cout<<"Zecimal "<<numar;cout<<"\n";
- cout<<"Binar ";IntToBinar(numar);cout<<"\n";
- cout<<"Hexa ";IntToHex(numar);cout<< "\n"; break;
- }
- case 4: //Conversie din HEXA
- {
- char numarhex[20];
- cout<<"Introdu numarul HEXA";cin>>numarhex;
- cout<<"Conversie din HEXA:\n\n";
- int numar = hexadecimalToDecimal(numarhex);
- cout<<"Zecimal "<<numar;cout<<"\n";
- cout<<"Binar ";IntToBinar(numar);cout<<"\n";
- cout<<"Octa "<<IntToOcta(numar);cout<< "\n"; break;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement