Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- //int NWDr(int a, int b) {
- // int r;
- // while (b != 0) {
- // r = a % b;
- // a = b;
- // b = r;
- // }
- // return a;
- //}
- //int NWDrr(int a, int b){
- // if (b == 0) return a;
- // return NWDrr(b, a % b);
- //
- //}
- int NWDo(int a, int b) {
- while (a != b) {
- if (a > b) a -= b;
- else b -= a;
- }
- return a;
- }
- int NWW(int a, int b) {
- return a * b / NWDo(a, b);
- }
- //int NWDor(int a, int b) {
- // if (a == b) return a;
- // if (a > b) return NWDor(a - b, b);
- // return NWDor(a, b - a);
- //}
- int main()
- {
- //cout << NWDo(12,18) << endl;
- //cout << NWW(12, 18) << endl;
- // skracanie ulamkow
- int n1, d1, n2, d2, ns1, ns2, ds1, ds2;
- cout << "Wpisz licznik pierwszego ulamka: ";
- cin >> n1;
- cout << "Wpisz mianownik pierwszego ulamka: ";
- cin >> d1;
- ns1 = n1 / NWDo(n1, d1);
- ds1 = d1 / NWDo(n1, d1);
- cout << "\nSkrocony zapis ulamka pierwszego to: " << ns1 << "/" << ds1 << endl;
- cout << "Wpisz licznik drugiego ulamka: ";
- cin >> n2;
- cout << "Wpisz mianownik drugiego ulamka: ";
- cin >> d2;
- ns2 = n2 / NWDo(n2, d2);
- ds2 = d2 / NWDo(n2, d2);
- cout << "\nSkrocony zapis ulamka pierwszego to: " << ns2 << "/" << ds2 << endl;
- // wspolny mianownik
- int cd = NWW (ds1,ds2); // - common denominator
- // dodawanie ulamkow
- cout << "\n______________________________________________________" << endl;
- cout << "Dodawanie ulamkow:" << endl;
- int nv1, nr1, nr2;
- nr1 = cd / ds1 * ns1;
- nr2 = cd / ds2 * ns2;
- nv1 = nr1 + nr2;
- cout << "\n\nWynik: " << ns1 << " / " << ds1 << " + " << ns2 << " / " << ds2 << " = " << nv1 << " / " << cd<<endl;
- //calosci
- int rn, divn;
- divn = nv1 / cd;
- rn = nv1 % cd;
- if (divn != 0) {
- if (rn != 0) cout << "Co jest rowne: " << divn << " i " << rn << " / " << cd << endl;
- else cout << "Co jest rowne: " << divn << endl;
- }
- else {
- cout << "Co jest rowne: " << " i " << rn << " / " << cd << endl;
- }
- // odejmowanie ulamkow
- cout << "\n______________________________________________________" << endl;
- cout << "Odejmowanie ulamkow:" << endl;
- int nv2;
- nv2 = nr1 - nr2;
- cout << "\n\nWynik: " << ns1 << " / " << ds1 << " - " << ns2 << " / " << ds2 << " = " << nv2 << " / " << cd << endl;
- //calosci
- divn = nv2 / cd;
- rn = nv2 % cd;
- if (divn != 0) {
- if (rn != 0) cout << "Co jest rowne: " << divn << " i " << rn << " / " << cd << endl;
- else cout << "Co jest rowne: " << divn << endl;
- }
- else {
- cout << "Co jest rowne: " << rn << " / " << cd << endl;
- }
- // mnozenie ulamkow
- cout << "\n______________________________________________________" << endl;
- cout << "Mnozenie ulamkow:" << endl;
- int nv3, nv3s, dv3, dv3s;
- nv3 = ns1 * ns2;
- dv3 = ds1 * ds2;
- // skracanie
- nv3s = nv3 / NWDo(nv3, dv3);
- dv3s = dv3 / NWDo(nv3, dv3);
- cout << "\n\nWynik: " << ns1 << " / " << ds1 << " * " << ns2 << " / " << ds2 << " = " << nv3 << " / " << dv3 << " = " << nv3s << " / " << dv3s << endl;
- // calosci
- divn = nv3s / dv3s;
- rn = nv3s % dv3s;
- if (divn != 0) {
- if (rn != 0) cout << "Co jest rowne: " << divn << " i " << rn << " / " << dv3s << endl;
- else cout << "Co jest rowne: " << divn << endl;
- }
- else {
- cout << "Co jest rowne: " << rn << " / " << dv3s << endl;
- }
- // dzielenie ulamkow
- cout << "\n______________________________________________________" << endl;
- cout << "Dzielenie ulamkow:" << endl;
- int nv4, nv4s, dv4, dv4s;
- nv4 = ns1 * ds2;
- dv4 = ns2 * ds1;
- // skracanie
- nv4s = nv4 / NWDo(nv4, dv4);
- dv4s = dv4 / NWDo(nv4, dv4);
- cout << "\n\nWynik: " << ns1 << " / " << ds1 << " / " << ns2 << " / " << ds2 << " = " << nv4 << " / " << dv4 << " = " << nv4s << " / " << dv4s << endl;
- // calosci
- divn = nv4s / dv4s;
- rn = nv4s % dv4s;
- if (divn != 0) {
- if (rn != 0) cout << "Co jest rowne: " << divn << " i " << rn << " / " << dv4s << endl;
- else cout << "Co jest rowne: " << divn << endl;
- }
- else {
- cout << "Co jest rowne: " << rn << " / " << dv4s << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement