Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- #include <string>
- using namespace std;
- /**
- * Gives the greatest common divisor of two integers or returns 0 if the
- * gcd is undefined. Uses the Euclidean GCD algorithm
- * @param a an integer
- * @param b an integer
- * @return the gcd of the specified parameter or 0 if both a and b are 0
- */
- /**
- * Computes the sum of two fractions
- * @param n1 the numerator of the first fraction
- * @param d1 the denominator of the first fraction
- * @param n2 the numerator of the second fraction
- * @param d2 the denominator of the second fraction
- * @param num the numerator of the sum
- * @param den the denominator of the sum
- */
- void add(int n1, int d1, int n2, int d2, int& num, int& den)
- {
- num = (n1 * d2 + n2 * d1) / gCD(n1 * d2 + n2 * d1, d1 * d2);
- den = (d1 * d2) / gCD(n1 * d2 + n2 * d1, d1 * d2);
- }
- /**
- * Computes the difference of two fractions
- * @param n1 the numerator of the minuend
- * @param d1 the denominator of the minuend
- * @param n2 the numerator of the subtrahend
- * @param d2 the denominator of the subtrahend
- * @param num the numerator of the dufferebce
- * @param den the denominator of the difference
- */
- void sub(int n1, int d1, int n2, int d2, int& num, int& den)
- {
- num = (n1 * d2 - n2 * d1) / gCD(n1 * d2 - n2 * d1, d1 * d2);
- den = (d1 * d2) / gCD(n1 * d2 + n2 * d1, d1 * d2);
- }
- /**
- * Computes the product of two fractions
- * @param n1 the numerator of the multiplier
- * @param d1 the denominator of the multiplier
- * @param n2 the numerator of the multiplicand
- * @param d2 the denominator of the multiplicand
- * @param num the numerator of the product
- * @param den the denominator of the product
- */
- void mult(int n1, int d1, int n2, int d2, int& num, int& den)
- {
- num = (n1 * n2 / gCD(n1 * n2, d2 * d1));
- den = (d1 * d2 / gCD(n1 * n2, d1 * d2));
- }
- /**
- * Computes the quotient of two fractions, if defined
- * @param n1 the numerator of the dividend
- * @param d1 the denominator of the dividend
- * @param n2 the numerator of the divisor
- * @param d2 the denominator of the divisor
- * @param num the numerator of the quotient
- * @param den the numerator of the quotient
- * or 0 if the quotient is undefinded
- */
- void divd(int n1, int d1, int n2, int d2, int& num, int& den)
- {
- num = (n1 * d2) / gCD(n1 * d2, n2 * d1);
- den = (n2 * d1) / gCD(n1 * d2, n2 * d1);
- }
- /**
- * Gives the string representation of a fraction
- * @param n the numerator of the fraction
- * @param d the denominator of the fraction
- * @param normalize gives a string represnetation
- * of a fraction in normalized form where the
- * numerator and denominator are relatively prime
- * and n/d when normalized is false.
- * @return n/d when normalized is false; otherwise,
- * 1. d = 0 -> nan
- * 2. n = 0 -> 0
- * 3. d = 1 -> n
- * 4. d = -1 -> -n
- * 5. n and d have the same sign -> |n|/|d|
- * 6. n and d have different signs -> -|n|/|d|
- */
- string str(int n, int d, bool normalize)
- {
- if (normalize == false)
- return to_string(n) + "/" + to_string(d);
- else if (d == 0)
- return "nan";
- else if (n == 0)
- return "0";
- else if (d == 1)
- return to_string(n);
- else if (d == -1)
- return "-" + to_string(n);
- else if ((d < 0 && n < 0) || (d > 0 && n > 0))
- return to_string(abs(n)) + "/" + to_string(abs(d));
- else if ((d < 0 && n > 0) || (d > 0 && n < 0))
- return "-" + to_string(abs(n)) + "/" + to_string(abs(d));
- }
- int gCD(int a, int b)
- {
- a = abs(a);
- b = abs(b);
- if (a == 0 && b == 0)
- return 0;
- else if (a == 0 || b == 0)
- return abs(a + b);
- else
- {
- int r = a % b;
- while (r != 0)
- {
- a = b;
- b = r;
- r = a % b;
- }
- return b;
- }
- }
- int main()
- {
- int f1n, f1d, f2n, f2d, f3n, f3d, numerator, denominator, addCacheNum, addCacheDen, multiplyCacheNum, multiplyCacheDen, divideCacheNum, divideCacheDen;
- cout << "Enter the numerator and denominator of fraction 1 -> ";
- cin >> f1n >> f1d;
- cout << "Enter the numerator and denominator of fraction 2 -> ";
- cin >> f2n >> f2d;
- cout << "Enter the numerator and denominator of fractoin 3 -> ";
- cin >> f3n >> f3d;
- if (f1d == 0 || f2d == 0 || f3d == 0)
- cout << endl << "ERROR: The denominator of a fraction cannot be equal to 0." << endl;
- else
- {
- cout << endl << "F1 = " << str(f1n, f1d, false) << endl;
- cout << "F2 = " << str(f2n, f2d, false) << endl;
- cout << "F3 = " << str(f3n, f3d, false) << endl << endl;
- add(f1n, f1d, f2n, f2d, numerator, denominator);
- cout << "F1 + F2 = " << str(numerator, denominator, true) << endl;
- sub(f1n, f1d, f2n, f2d, numerator, denominator);
- cout << "F1 - F2 = " << str(numerator, denominator, true) << endl;
- mult(f1n, f1d, f2n, f2d, numerator, denominator);
- cout << "F1 x F2 = " << str(numerator, denominator, true) << endl;
- if (gCD(f1n * f2d, f1d * f2n) == 0)
- cout << "F1 / F2 = nan" << endl;
- else
- {
- divd(f1n, f1d, f2n, f2d, numerator, denominator);
- cout << "F1 / F2 = " << str(numerator, denominator, true) << endl;
- }
- if (gCD(f1n * f2d, f1d * f2n) == 0)
- cout << "F1 x (F2 + F3) / (F1 + F3) = nan";
- else
- {
- add(f2n, f2d, f3n, f3d, addCacheNum, addCacheDen);
- mult(f1n, f1d, addCacheNum, addCacheDen, multiplyCacheNum, multiplyCacheDen);
- add(f1n, f1d, f3n, f3d, addCacheNum, addCacheDen);
- divd(multiplyCacheNum, multiplyCacheDen, addCacheNum, addCacheDen, divideCacheNum, divideCacheDen);
- cout << "F1 x (F2 + F3) / (F1 + F3) = " << str(divideCacheNum, divideCacheDen, true) << endl;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement