biner dalam c++

#include <cstdlib>

#include <iostream>

#include <conio.h>

#include <math.h>

#include <string.h>

using namespace std;

class konversi{

public:

void desimal();

void oktal();

void heksa();

private:

char binary[100];

int mag2,mag8,leng2;

int i,m,n;

};

int hasil[100]={0};

void konversi::desimal(){

cout<<“\nKonversi ke desimal”;

cout<<endl;

cout<<“\nMasukkan bilangan biner : ” ;

cin>>binary;

leng2=strlen(binary);

for (i=leng2-1;i>=0;i–)

{

m=leng2-1-i;

mag2=(int)pow(2.0,m);

n=m/100;

mag8=(int)pow(1.0,n);

hasil[n]+=((binary[i]-48)*mag2)/mag8;

}

cout<<endl;

cout<< “Nilai ” <<binary<< ” dalam desimal adalah : ” ;

for(i=n;i>=0;i–)

cout<<hasil[i];

cout<<endl;

}

void konversi::oktal(){

cout<<“\nKonversi ke oktal”;

cout<<endl;

cout<<“\nMasukkan bilangan biner : ” ;

cin>>binary;

leng2=strlen(binary);

for (i=leng2-1;i>=0;i–)

{

m=leng2-1-i;

mag2=(int)pow(2.0,m);

n=m/3;

mag8=(int)pow(8.0,n);

hasil[n]+=((binary[i]-48)*mag2)/mag8;

}

cout<<endl;

cout<< “Nilai ” <<binary<< ” dalam oktal adalah : ” ;

for(i=n;i>=0;i–)cout<<hasil[i];

cout<<endl;

}

void konversi::heksa(){

cout<<“\nKonversi ke heksadesimal”;

cout<<endl;

cout<<“\nMasukkan bilangan biner : ” ;

cin>>binary;

leng2=strlen(binary);

for (i=leng2-1;i>=0;i–)

{

m=leng2-1-i;

mag2=(int)pow(2.0,m);

n=m/4;

mag8=(int)pow(16.0,n);

hasil[n]+=((binary[i]-48)*mag2)/mag8;

}

cout<<endl;

cout<< “Nilai ” <<binary<< ” dalam heksadesimal adalah : ” ;

for(i=n;i>=0;i–)

if(hasil[i]<9)cout<<hasil[i];

else if(hasil[i]==10)cout<<“A”;

else if(hasil[i]==11)cout<<“B”;

else if(hasil[i]==12)cout<<“C”;

else if(hasil[i]==13)cout<<“D”;

else if(hasil[i]==14)cout<<“E”;

else if(hasil[i]==15)cout<<“F”;

cout<<endl;

}

int main(int argc, char *argv[])

{

int d;

konversi x;

cout<<“\t\tKONVERSI BILANGAN BINER SEDERHANA\n”;

cout<<“menu:\n”;

cout<<“\t(1)konversi ke desimal\n”;

cout<<“\t(2)konversi ke oktal\n”;

cout<<“\t(3)konversi ke heksa desimal\n”;

cout<<endl;

cout<<“pilih menu yang akan anda pilih (1-3) :  “;

cin>>d;

if(d==1){x.desimal();}

else if(d==2){x.oktal();}

else if(d==3){x.heksa();}

else cout<<“anda kurang tepat memasukan pilihan”;

cout<<endl;

system(“PAUSE”);

return EXIT_SUCCESS;

}


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s