Advertisement
Guest User

Capinski ulamki dodawanie/od/mn/dz VS

a guest
May 19th, 2019
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.95 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. //int NWDr(int a, int b) {
  5. //  int r;
  6. //  while (b != 0) {
  7. //      r = a % b;
  8. //      a = b;
  9. //      b = r;
  10. //  }
  11. //  return a;
  12. //}
  13.  
  14. //int NWDrr(int a, int b){
  15. //  if (b == 0) return a;
  16. //  return NWDrr(b, a % b);
  17. // 
  18. //}
  19.  
  20. int NWDo(int a, int b) {
  21.     while (a != b) {
  22.         if (a > b) a -= b;
  23.         else b -= a;
  24.     }
  25.     return a;
  26. }
  27.  
  28. int NWW(int a, int b) {
  29.     return a * b / NWDo(a, b);
  30. }
  31.  
  32. //int NWDor(int a, int b) {
  33. //  if (a == b) return a;
  34. //  if (a > b) return NWDor(a - b, b);
  35. //  return NWDor(a, b - a);
  36. //}
  37.  
  38. int main()
  39. {
  40.     //cout << NWDo(12,18) << endl;
  41.     //cout << NWW(12, 18) << endl;
  42.  
  43.  
  44.     // skracanie ulamkow
  45.     int n1, d1, n2, d2, ns1, ns2, ds1, ds2;
  46.     cout << "Wpisz licznik pierwszego ulamka: ";
  47.     cin >> n1;
  48.     cout << "Wpisz mianownik pierwszego ulamka: ";
  49.     cin >> d1;
  50.  
  51.  
  52.     ns1 = n1 / NWDo(n1, d1);
  53.     ds1 = d1 / NWDo(n1, d1);
  54.     cout << "\nSkrocony zapis ulamka pierwszego to: " << ns1 << "/" << ds1 << endl;
  55.  
  56.     cout << "Wpisz licznik drugiego ulamka: ";
  57.     cin >> n2;
  58.     cout << "Wpisz mianownik drugiego ulamka: ";
  59.     cin >> d2;
  60.     ns2 = n2 / NWDo(n2, d2);
  61.     ds2 = d2 / NWDo(n2, d2);
  62.     cout << "\nSkrocony zapis ulamka pierwszego to: " << ns2 << "/" << ds2 << endl;
  63.  
  64.  
  65.     // wspolny mianownik
  66.     int cd = NWW (ds1,ds2); // - common denominator
  67.    
  68.  
  69.     // dodawanie ulamkow
  70.     cout << "\n______________________________________________________" << endl;
  71.     cout << "Dodawanie ulamkow:" << endl;
  72.     int nv1, nr1, nr2;
  73.     nr1 = cd / ds1 * ns1;
  74.     nr2 = cd / ds2 * ns2;
  75.     nv1 = nr1 + nr2;
  76.  
  77.     cout << "\n\nWynik: " << ns1 << " / " << ds1 << " + " << ns2 << " / " << ds2 << " = " << nv1 << " / " << cd<<endl;
  78.  
  79.     //calosci
  80.     int rn, divn;
  81.     divn = nv1 / cd;
  82.     rn = nv1 % cd;
  83.    
  84.     if (divn != 0) {
  85.         if (rn != 0) cout << "Co jest rowne: " << divn << " i " << rn << " / " << cd << endl;
  86.         else cout << "Co jest rowne: " << divn << endl;
  87.     }
  88.     else {
  89.         cout << "Co jest rowne: " << " i " << rn << " / " << cd << endl;
  90.     }
  91.  
  92.     // odejmowanie ulamkow
  93.     cout << "\n______________________________________________________" << endl;
  94.     cout << "Odejmowanie ulamkow:" << endl;
  95.     int nv2;
  96.     nv2 = nr1 - nr2;
  97.     cout << "\n\nWynik: " << ns1 << " / " << ds1 << " - " << ns2 << " / " << ds2 << " = " << nv2 << " / " << cd << endl;
  98.  
  99.     //calosci
  100.     divn = nv2 / cd;
  101.     rn = nv2 % cd;
  102.     if (divn != 0) {
  103.         if (rn != 0) cout << "Co jest rowne: " << divn << " i " << rn << " / " << cd << endl;
  104.         else cout << "Co jest rowne: " << divn << endl;
  105.     }
  106.     else {
  107.         cout << "Co jest rowne: " << rn << " / " << cd << endl;
  108.     }
  109.  
  110.     // mnozenie ulamkow
  111.     cout << "\n______________________________________________________" << endl;
  112.     cout << "Mnozenie ulamkow:" << endl;
  113.  
  114.     int nv3, nv3s, dv3, dv3s;
  115.     nv3 = ns1 * ns2;
  116.     dv3 = ds1 * ds2;
  117.     // skracanie
  118.     nv3s = nv3 / NWDo(nv3, dv3);
  119.     dv3s = dv3 / NWDo(nv3, dv3);
  120.     cout << "\n\nWynik: " << ns1 << " / " << ds1 << " * " << ns2 << " / " << ds2 << " = " << nv3 << " / " << dv3 << " = " << nv3s << " / " << dv3s << endl;
  121.    
  122.     // calosci
  123.     divn = nv3s / dv3s;
  124.     rn = nv3s % dv3s;
  125.     if (divn != 0) {
  126.         if (rn != 0) cout << "Co jest rowne: " << divn << " i " << rn << " / " << dv3s << endl;
  127.         else cout << "Co jest rowne: " << divn << endl;
  128.     }
  129.     else {
  130.         cout << "Co jest rowne: " << rn << " / " << dv3s << endl;
  131.     }
  132.  
  133.     // dzielenie ulamkow
  134.     cout << "\n______________________________________________________" << endl;
  135.     cout << "Dzielenie ulamkow:" << endl;
  136.  
  137.     int nv4, nv4s, dv4, dv4s;
  138.     nv4 = ns1 * ds2;
  139.     dv4 = ns2 * ds1;
  140.     // skracanie
  141.     nv4s = nv4 / NWDo(nv4, dv4);
  142.     dv4s = dv4 / NWDo(nv4, dv4);
  143.     cout << "\n\nWynik: " << ns1 << " / " << ds1 << " / " << ns2 << " / " << ds2 << " = " << nv4 << " / " << dv4 << " = " << nv4s << " / " << dv4s << endl;
  144.  
  145.     // calosci
  146.     divn = nv4s / dv4s;
  147.     rn = nv4s % dv4s;
  148.     if (divn != 0) {
  149.         if (rn != 0) cout << "Co jest rowne: " << divn << " i " << rn << " / " << dv4s << endl;
  150.         else cout << "Co jest rowne: " << divn << endl;
  151.     }
  152.     else {
  153.         cout << "Co jest rowne: " << rn << " / " << dv4s << endl;
  154.     }
  155.  
  156.     return 0;
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement