Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- using namespace std;
- long EuclidRecursive(long a, long b) {
- if (a == b) {
- return a;
- }
- if (a > b) {
- long tmp = a;
- a = b;
- b = tmp;
- }
- return EuclidRecursive(a, b - a);
- }
- long EuclidIterated(long a, long b) {
- long tmp;
- while (a != b) {
- if (a > b) {
- tmp = a;
- a = b;
- b = tmp;
- }
- b = b - a;
- }
- return a;
- }
- void reduceFraction(long& a, long& b, long nod = 0) {
- if (!nod)
- nod = EuclidIterated(a, b);
- a = a / nod;
- b = b / nod;
- }
- long nok(long a, long b, long nod = 0) {
- if (!nod)
- nod = EuclidIterated(a, b);
- return a / nod * b;
- }
- void solveFractionPlus(long& a, long& b, long c, long d) {
- long nodBC = EuclidIterated(b, c);
- long nokBC = nok(b, c, nodBC);
- long mult1 = nokBC / b, mult2 = nokBC / c;
- a = a * mult1 + d * mult2;
- b = nokBC;
- reduceFraction(a, b);
- }
- int main() {
- cout << endl << " a d ";
- cout << endl << "--- + ---";
- cout << endl << " b c ";
- cout << endl << "Enter values a, b, c, d: ";
- long a, b, c, d;
- cin >> a >> b >> c >> d;
- solveFractionPlus(a, b, c, d);
- cout << " " << a << endl;
- cout << "-----" << endl;
- cout << " " << b;
- }
Add Comment
Please, Sign In to add comment