Advertisement
fjraito

Code 5 - Factorial

Apr 21st, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.48 KB | None | 0 0
  1. #include <iostream>
  2. #include <limits>
  3. #include <cmath>
  4. using namespace std;
  5.  
  6. int pilihan, angka;
  7.  
  8. void askPilihan(), askNumber(int max);
  9. int getNearestBelowNumber(int number);
  10. int getNumbersBetweenNumbers(int min, int max);
  11.  
  12. class Count {
  13.     public:
  14.         virtual void startCount() {
  15.             cout<<"Virtual for startCount"<<endl;
  16.         }
  17. };
  18.  
  19. class Factorial : public Count{
  20.     private:
  21.         int hasil;
  22.        
  23.     public:
  24.         void startCount() {
  25.            
  26.             hasil = 1;
  27.             for(int i=angka; i>0; --i){
  28.                 hasil *= i;
  29.             }
  30.            
  31.             cout<<"The Result of Factorial "<<angka<<" :"<<endl;
  32.             cout<<hasil<<endl;
  33.         }
  34. };
  35.  
  36. class Fibonacci : public Count{
  37.    
  38.     private:
  39.         int t1, t2, nextTerm;
  40.        
  41.     public:
  42.         void startCount() {
  43.        
  44.             t2 = 1;
  45.             t1 = 0;
  46.             nextTerm = 0;
  47.             cout<<"The Result of Fibonacci "<<angka<<" :"<<endl;
  48.             for (int i = 1; i <= angka; ++i){
  49.                
  50.                 // Prints the first two terms.
  51.                 if(i == 1)
  52.                 {
  53.                     cout << t1 << " ";
  54.                     continue;
  55.                 }
  56.                 if(i == 2)
  57.                 {
  58.                     cout << t2 << " ";
  59.                     continue;
  60.                 }
  61.                 nextTerm = t1 + t2;
  62.                 t1 = t2;
  63.                 t2 = nextTerm;
  64.                
  65.                 cout << nextTerm << " ";
  66.             }
  67.         }
  68. };
  69.  
  70. class Binary : public Count{
  71.    
  72.     private:
  73.         int hasil; 
  74.        
  75.     public:
  76.         void startCount() {
  77.            
  78.             hasil=0;
  79.             cout<<"The Result of Binary Number "<<angka<<" :"<<endl;
  80.             cout<<"0 ";
  81.             while (angka>0){
  82.                 if(hasil>0){
  83.                     for(int i=0; i<getNumbersBetweenNumbers(getNearestBelowNumber(angka), hasil); i++){
  84.                         cout<<"0 ";
  85.                     }  
  86.                 }
  87.                 hasil = getNearestBelowNumber(angka);
  88.                 cout<<"1 ";
  89.                 angka -= pow(2,hasil);
  90.             }
  91.             for(int i=0; i<getNumbersBetweenNumbers(getNearestBelowNumber(angka), hasil); i++){
  92.                 cout<<"0 ";
  93.             }  
  94.         }
  95. };
  96.  
  97. int getNumbersBetweenNumbers(int min, int max){
  98.     return max-(min+1);
  99. }
  100.  
  101. int getNearestBelowNumber(int number){
  102.    
  103.     int hasil = 1, i=0;
  104.     while(hasil<number){
  105.         hasil *= 2;
  106.         ++i;
  107.     }
  108.     if(hasil>number) return i-1;
  109.     else return i;
  110. }
  111.  
  112. void welcome(){
  113.     cout<<"Factorial, Fibonacci, and Binary Calculation"<<endl;
  114.     cout<<"============================================\n"<<endl;
  115.    
  116.     cout<<"1. Factorial"<<endl;
  117.     cout<<"2. Fibonacci"<<endl;
  118.     cout<<"3. Binary Number"<<endl;
  119.     cout<<"4. Exit"<<endl;
  120.    
  121.     askPilihan();
  122. }
  123.  
  124. void askNumber(int max){
  125.     cout<<"Input the number[1-"<<max<<"]: ";
  126.     cin>>angka;
  127.     if(!cin || angka<1 || angka>max){
  128.         cin.clear();
  129.         cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
  130.         askNumber(max);
  131.     }
  132. }
  133.  
  134. void hitung(Count *count) { count->startCount(); }
  135.  
  136. void askPilihan(){
  137.    
  138.     cout<<"Choose: ";
  139.     cin>>pilihan;
  140.     if(!cin){
  141.         cin.clear();
  142.         cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
  143.         askPilihan();
  144.     }
  145.    
  146.     Factorial *faktorial = new Factorial;
  147.     Fibonacci *fibonaci = new Fibonacci;
  148.     Binary *binari = new Binary;
  149.     switch(pilihan){
  150.         case 1:
  151.             askNumber(20);
  152.             hitung(faktorial);
  153.             break;
  154.            
  155.         case 2:
  156.             askNumber(20);
  157.             hitung(fibonaci);
  158.             break;
  159.            
  160.         case 3:
  161.             askNumber(100);
  162.             hitung(binari);
  163.             break;
  164.            
  165.         case 4:
  166.             exit(0);
  167.             break;
  168.            
  169.         default:
  170.             cout<<"Pilihan yang dimasukkan salah"<<endl;
  171.             cin.clear();
  172.             cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
  173.             askPilihan();
  174.     }
  175. }
  176.  
  177. main(){
  178.     welcome();
  179. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement