Advertisement
rootuss

test iterac przerabianie

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