Advertisement
rootuss

funkcje fib i silnia

Nov 18th, 2016
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.49 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <conio.h>
  4. using namespace std;
  5.  
  6. double silnia_rek(int s)
  7. {
  8.     if (s==0)
  9.     {
  10.         return 1;
  11.     }
  12.     else
  13.         return silnia_rek(s-1)*s;
  14. }
  15.  
  16. double fib_rek(int s)
  17. {
  18.     if (s<=2)
  19.     {
  20.         return 1;
  21.     }
  22.     else
  23.         return fib_rek(s-1)+fib_rek(s-2);
  24. }
  25.  
  26. double fib_it(int s)
  27. {
  28.     if (s<=2)
  29.     {
  30.         return 1;
  31.     }
  32.     else
  33.     {
  34.         int f1=1;
  35.         int f2=1;
  36.         int i=3;
  37.         int f;
  38.  
  39.         do
  40.         {
  41.             f=f1+f2;
  42.             f1=f2;
  43.             f2=f;
  44.             i++;
  45.         }
  46.         while (i<=s);
  47.  
  48.         return f;
  49.     }
  50.  
  51. }
  52.  
  53. double silnia_it(int s)
  54. {
  55.     int silnia=1;
  56.     int licznik=1;
  57.     if (s==0)
  58.     {
  59.         return 1;
  60.     }
  61.     else
  62.  
  63.     {
  64.         while(licznik<=s)
  65.         {
  66.             silnia=silnia*licznik;
  67.             licznik++;
  68.         }
  69.     }
  70.  
  71.         return silnia;
  72. }
  73.  
  74. int main()
  75. {
  76. char kolejne='t';
  77. do
  78. {
  79.     int liczba;
  80.  
  81.     cout<<"Podaj liczbe do oblicznia: ";
  82.     cin>>liczba;
  83.  
  84.     while (liczba<0)
  85.     {
  86.     cout<<"Podaj poprawna liczbe do oblicznia: ";
  87.     cin>>liczba;
  88.     }
  89.  
  90.     cout<<endl;
  91.     cout<<"-------MENU--GLOWNE-------"<<endl;
  92.     cout<<"--------------------------"<<endl;
  93.     cout<<"1. Silnia rekurencyjnie"<<endl;
  94.     cout<<"2. Silnia iteracyjnie"<<endl;
  95.     cout<<"3. Fibonacci rekurencyjnie"<<endl;
  96.     cout<<"4. Fibonacci iteracyjnie"<<endl;
  97.     cout<<"5. Wyjscie"<<endl;
  98.  
  99.     char wybor;
  100.     wybor=getch();
  101.     cout<<"Wybrano opcje: "<<wybor<<endl<<endl;
  102.  
  103.     cout<<endl;
  104.  
  105.     switch (wybor)
  106.     {
  107.         case '1':
  108.         {
  109.             cout<<"Silnia rekurencyjnie wynosi: "<<silnia_rek(liczba)<<endl;
  110.             break;
  111.         }
  112.  
  113.         case '2':
  114.         {
  115.             cout<<"Silnia iteracyjnie wynosi: "<<silnia_it(liczba)<<endl;
  116.             break;
  117.         }
  118.  
  119.         case '3':
  120.         {
  121.             cout<<"Fibonacci rekurencyjnie wynosi: "<<fib_rek(liczba)<<endl;
  122.             break;
  123.         }
  124.  
  125.         case '4':
  126.         {
  127.             cout<<"Fibonacci iteracyjnie wynosi: "<<fib_it(liczba)<<endl;
  128.  
  129.             break;
  130.         }
  131.  
  132.         case '5':
  133.         {
  134.             cout<<"KONIEC"<<endl<<endl;
  135.             exit(0);
  136.         }
  137.         default:
  138.             cout<<"Brak opcji w menu"<<endl;
  139.     }
  140.  
  141.  
  142.     cout<<"\n \nCzy chesz jeszcze raz?";
  143.     kolejne=getch();
  144.     system("cls");
  145.  
  146. }
  147. while (kolejne=='t');
  148.  
  149. cout<<"KONIEC"<<endl<<endl;
  150.  
  151.  
  152. return 0;
  153.  
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement