Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <limits>
- #include <cmath>
- using namespace std;
- int pilihan, angka;
- void askPilihan(), askNumber(int max);
- int getNearestBelowNumber(int number);
- int getNumbersBetweenNumbers(int min, int max);
- class Count {
- public:
- virtual void startCount() {
- cout<<"Virtual for startCount"<<endl;
- }
- };
- class Factorial : public Count{
- private:
- int hasil;
- public:
- void startCount() {
- hasil = 1;
- for(int i=angka; i>0; --i){
- hasil *= i;
- }
- cout<<"The Result of Factorial "<<angka<<" :"<<endl;
- cout<<hasil<<endl;
- }
- };
- class Fibonacci : public Count{
- private:
- int t1, t2, nextTerm;
- public:
- void startCount() {
- t2 = 1;
- t1 = 0;
- nextTerm = 0;
- cout<<"The Result of Fibonacci "<<angka<<" :"<<endl;
- for (int i = 1; i <= angka; ++i){
- // Prints the first two terms.
- if(i == 1)
- {
- cout << t1 << " ";
- continue;
- }
- if(i == 2)
- {
- cout << t2 << " ";
- continue;
- }
- nextTerm = t1 + t2;
- t1 = t2;
- t2 = nextTerm;
- cout << nextTerm << " ";
- }
- }
- };
- class Binary : public Count{
- private:
- int hasil;
- public:
- void startCount() {
- hasil=0;
- cout<<"The Result of Binary Number "<<angka<<" :"<<endl;
- cout<<"0 ";
- while (angka>0){
- if(hasil>0){
- for(int i=0; i<getNumbersBetweenNumbers(getNearestBelowNumber(angka), hasil); i++){
- cout<<"0 ";
- }
- }
- hasil = getNearestBelowNumber(angka);
- cout<<"1 ";
- angka -= pow(2,hasil);
- }
- for(int i=0; i<getNumbersBetweenNumbers(getNearestBelowNumber(angka), hasil); i++){
- cout<<"0 ";
- }
- }
- };
- int getNumbersBetweenNumbers(int min, int max){
- return max-(min+1);
- }
- int getNearestBelowNumber(int number){
- int hasil = 1, i=0;
- while(hasil<number){
- hasil *= 2;
- ++i;
- }
- if(hasil>number) return i-1;
- else return i;
- }
- void welcome(){
- cout<<"Factorial, Fibonacci, and Binary Calculation"<<endl;
- cout<<"============================================\n"<<endl;
- cout<<"1. Factorial"<<endl;
- cout<<"2. Fibonacci"<<endl;
- cout<<"3. Binary Number"<<endl;
- cout<<"4. Exit"<<endl;
- askPilihan();
- }
- void askNumber(int max){
- cout<<"Input the number[1-"<<max<<"]: ";
- cin>>angka;
- if(!cin || angka<1 || angka>max){
- cin.clear();
- cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
- askNumber(max);
- }
- }
- void hitung(Count *count) { count->startCount(); }
- void askPilihan(){
- cout<<"Choose: ";
- cin>>pilihan;
- if(!cin){
- cin.clear();
- cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
- askPilihan();
- }
- Factorial *faktorial = new Factorial;
- Fibonacci *fibonaci = new Fibonacci;
- Binary *binari = new Binary;
- switch(pilihan){
- case 1:
- askNumber(20);
- hitung(faktorial);
- break;
- case 2:
- askNumber(20);
- hitung(fibonaci);
- break;
- case 3:
- askNumber(100);
- hitung(binari);
- break;
- case 4:
- exit(0);
- break;
- default:
- cout<<"Pilihan yang dimasukkan salah"<<endl;
- cin.clear();
- cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
- askPilihan();
- }
- }
- main(){
- welcome();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement