Advertisement
rootuss

euklides

Dec 5th, 2016
306
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.68 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <conio.h>
  4.  
  5. using namespace std;
  6.  
  7. int nwdresztait  (int a, int b)
  8. {
  9.     int r;
  10.     while(b>0)
  11.     {
  12.         r=a%b;
  13.         a=b;
  14.         b=r;
  15.     }
  16.  
  17.     return a;
  18. }
  19.  
  20. int nwdresztarek  (int a, int b)
  21. {
  22.     if (b==0) return a;
  23.     return nwdresztarek(b,a%b);
  24. }
  25.  
  26. int nwdodejit  (int a, int b)
  27. {
  28.     while(a!=b)
  29.     if(a>b) a-=b;
  30.     else b-=a;
  31.     return a;
  32. }
  33.  
  34. int nwdodejrek (int a, int b)
  35. {
  36.     if (a==b) return a;
  37.     if (a>b) return nwdodejrek(a-b,b);
  38.  
  39.     return nwdodejrek(a,b-a);
  40. }
  41.  
  42. int NWW (int a, int b)
  43. {
  44.     return a*b/nwdresztait(a,b);
  45. }
  46.  
  47. int main()
  48. {
  49.     char jeszcze='t';
  50.     do
  51.         {
  52.  
  53.  
  54.  
  55.  
  56.     cout<<"MENU"<<endl;
  57.     cout<<"1. NWD- reszta z dzielnia- rekurencyjnie"<<endl;
  58.     cout<<"2. NWD- reszta z dzielnia- iteracyjnie"<<endl;
  59.     cout<<"3. NWD- odejmowanie- rekurencyjnie"<<endl;
  60.     cout<<"4. NWD- odejmowanie- iteracyjnie"<<endl;
  61.     cout<<"5. KONIEC"<<endl;
  62.  
  63.     char warunek;
  64.     warunek=getch();
  65.  
  66.     int a1, b1;
  67.  
  68.  
  69.     switch (warunek)
  70.     {
  71.  
  72.  
  73.         case '1':
  74.             {
  75.                 cout << "Podaj a: ";
  76.                 cin>>a1;
  77.                 cout << "Podaj b: ";
  78.                 cin>>b1;
  79.                 cout<<"\nNWD to: "<<nwdresztarek(a1,b1)<<endl;
  80.                 cout<<"\nNWW to: "<<NWW(a1,b1)<<endl;
  81.                 break;
  82.             }
  83.  
  84.         case '2':
  85.         {
  86.  
  87.                 cout << "Podaj a: ";
  88.                 cin>>a1;
  89.                 cout << "Podaj b: ";
  90.                 cin>>b1;
  91.                 cout<<"\nNWD to: "<<nwdresztait(a1,b1)<<endl;
  92.                 cout<<"\nNWW to: "<<NWW(a1,b1)<<endl;
  93.                 break;
  94.         }
  95.  
  96.             case '3':
  97.         {
  98.  
  99.                 cout << "Podaj a: ";
  100.                 cin>>a1;
  101.                 cout << "Podaj b: ";
  102.                 cin>>b1;
  103.                 cout<<"\nNWD to: "<<nwdodejrek(a1,b1)<<endl;
  104.                 cout<<"\nNWW to: "<<NWW(a1,b1)<<endl;
  105.                 break;
  106.         }
  107.  
  108.             case '4':
  109.         {
  110.  
  111.                 cout << "Podaj a: ";
  112.                 cin>>a1;
  113.                 cout << "Podaj b: ";
  114.                 cin>>b1;
  115.                 cout<<"\nNWD to: "<<nwdodejit(a1,b1)<<endl;
  116.                 cout<<"\nNWW to: "<<NWW(a1,b1)<<endl;
  117.                 break;
  118.         }
  119.         case '5':
  120.             {
  121.                 cout<<"KONIEC"<<endl;
  122.                 exit(0);
  123.                 break;
  124.             }
  125.         default:
  126.             cout<<"Brak opcji"<<endl;
  127.             break;
  128.     }
  129.     cout<<"\nCzy chcesz jeszcze raz t/n"<<endl;
  130.     jeszcze=getch();
  131.     system("cls");
  132. }
  133. while (jeszcze=='t');
  134. cout<<"KONIEC"<<endl;
  135.  
  136.  
  137.  
  138.  
  139.     return 0;
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement