Sabtu, 26 September 2009

Program Probabilitas dengan menggunakan C/C++

#include

#include

#include

int n,k;

class probabilitas

{

public:

void input();

float fact(int bil);

void menu();

int permutasi(int x,int y);

int kombinasi(int x,int y);

};

void probabilitas :: input()

{

clrscr();

gotoxy(20,5);

cout<<”PROGRAM INPUT NILAI”;

gotoxy(20,7);

cout<<”MASUKKAN NILAI N :”;

cin>>n;

do

{

gotoxy(20,8);

cout<<”MASUKKAN NILAI K :”;

gotoxy(38,8);

cin>>k;

if (k>n||k<=0)

{

gotoxy(39,8);

}

}while (k>n||k<=0);

}

float probabilitas :: fact(int bil)

{

if (bil<=1)

{

return 1;

}

else

{

return bil*fact(bil-1);

}

}

int probabilitas :: permutasi(int x,int y)

{

float perm;

clrscr();

gotoxy(20,5);

cout<<”MENCARINILAI PERMUTASI”;

perm=fact(n)/(fact(n-k));

gotoxy(20,7);

cout<<”P(”<<<”,”<<<”)=”<<<”!/(”<<<”-”<<<”)!=”<

getch();

return perm;

}

int probabilitas :: kombinasi(int x,int y)

{

float komb;

clrscr();

gotoxy(20,5);

cout<<”MENCARI NILAI KOMBINASI”;

komb=fact(n)/(fact(k)*fact(n-k));

gotoxy(20,7);

cout<<”C(”<<<”,”<<<”)=”<<<”!/”<<<”!(”<<<”-”<<<”)!=”<

getch();

return komb;

}

void probabilitas :: menu()

{

int pil;

while (pil<=4||pil!=0)

{

clrscr();

gotoxy(20,4);cout<<”———————-”;

gotoxy(20,5);cout<<”|PROGRAM PROBABILITAS|”;

gotoxy(20,6);cout<<”———————-”;

gotoxy(20,7);cout<<”1. INPUT BILANGAN |”;

gotoxy(20,8);cout<<”2. HITUNG PERMUTASI |”;

gotoxy(20,9);cout<<”3. HITUNG KOMBINASI |”;

gotoxy(20,10);cout<<”4. KELUAR |”;

gotoxy(20,11);cout<<”———————-”;

gotoxy(20,12);cout<<”| SELAMAT MENCOBA |”;

gotoxy(20,13);cout<<”———————-”;

gotoxy(20,15);cout<<”MASUKKAN PILIHAN ANDA :”;cin>>pil;

switch(pil)

{

case 1: input();

break;

case 2: permutasi(n,k);

break;

case 3: kombinasi(n,k);

break;

case 4: exit(0);

}

}

}

void main()

{

probabilitas obj;

obj.menu();

getch();

}

saya hanya ingin share ilmu yg saya dapat, kalau masih ada yg salah maaf, maklum masih awam neh

Tidak ada komentar:

Posting Komentar