Senin, 20 Mei 2013
PERMUTASI & KOMBINASI
#include <cstdlib>
#include <iostream>
using namespace std;
class matdis{
friend istream& operator>>(istream&, matdis&);
friend ostream& operator<<(ostream&, matdis&);
public:
matdis();
int permutasi();
int kombinasi();
void pilihan();
void exit();
private:
int n,r,x,y,z,a,p,c;
};
matdis::matdis(){
cout<<"\t==PROGRAM PERMUTASI DAN KOMBINASI=="<<endl;
cout<<"\t+++++++++++DENGAN PROSEDUR"<<endl;
cout<<"\t====SILAHKAN PILIH PILIHAN ANDA===="<<endl;
cout<<"\t+++++++++++1. PERMUTASI +++++++++++"<<endl;
cout<<"\t+++++++++++ 2. COMBINASI +++++++++++"<<endl;
cout<<"\t+++++++++++ 0. EXIT +++++++++=++"<<endl;
cout<<"\t+++++++++++ TERIMA KASIH ++++++++++++"<<endl;
};
int matdis::permutasi(){
cout<<"Program Menghitung Permutasi"<<endl;
cout<<"Masukkan n harus lebih besar dari r :"<<endl;
cout<<"Masukkan n :"; cin>>n;
cout<<"Masukkan r :"; cin>>r;
x=1;
y=1;
for(int i=1; i<=n; i++){
x=x*i;
}
for(int j=1; j<=(n-r); j++){
y=y*j;
}
p=x/y;
cout<<"Nilai n ="<<n<<endl;
cout<<"Nilai r ="<<r<<endl;
cout<<"Nilai n! ="<<x<<endl;
cout<<"Hasil (n-r)! ="<<y<<endl;
cout<<"Hasil Permutasi "<<"("<<n<<","<<r<<") : "<<p<<endl;
pilihan();
return p;
}
int matdis::kombinasi(){
cout<<"Program Menghitung Kombinasi"<<endl;
cout<<"Masukkan n harus lebih besar dari r :"<<endl;
cout<<"Masukkan n :"; cin>>n;
cout<<"Masukkan r :"; cin>>r;
x=1;
y=1;
z=1;
a=(n-r);
for(int i=1; i<=n; i++){
x=x*i;
}
for(int j=1; j<=r; j++){
y=y*j;
}
for(int k=1; k<=a; k++){
z=z*k;
}
c=x/(z*y);
cout<<"Nilai n ="<<n<<endl;
cout<<"Nilai r ="<<r<<endl;
cout<<"Nilai n! ="<<x<<endl;
cout<<"Hasil r! ="<<y<<endl;
cout<<"Nilai (n-r)! ="<<z<<endl;
cout<<"Hasil Kombinasi "<<"("<<n<<","<<r<<") : "<<c<<endl;
pilihan();
return c;
}
void matdis::pilihan(){
int pil;
cout<<endl;
cout<<"masukan pilihan anda = ";
cin>>pil;
switch(pil){
case 1 : permutasi ();break;
case 2 : kombinasi ();break;
case 0 : exit (); break;
default :
cout<<"Maaf anda salah tulis"<<endl;
cout<<"Pilih kembali Pilihan "<<endl;
pilihan();
break;
}}
void matdis::exit(){
cout << "TERIMA KASIH"<<endl;
}
int main(int argc, char *argv[])
{
matdis x;
x.pilihan();
system("PAUSE");
return EXIT_SUCCESS;
}
Langganan:
Posting Komentar (Atom)
Tidak ada komentar:
Posting Komentar