#include // In Section 1-10, there was an assignment question // that asked you to write, for example, // // -52134/321 = -162 - 132/321 // 91/-13 = -7 // // Notice, however, that the fractional part in the // first example is not in lowest terms: // // 132/321 = 44/107. // // Thus, the desired output is // // -52134/321 = -162 - 44/107. // // Modify your program, or the program below, so that // any fractional part is always printed in lowest // terms. // // You have already written a program that calculates // the greatest common divisor of two integers. You // can use the same algorithm here. // Function declarations int main(); // Function definitions int main() { int num{}; std::cout << "Enter an integer numerator: "; std::cin >> num; int den{}; std::cout << "Enter a nonzero integer denominator: "; std::cin >> den; if ( den == 0 ) { std::cout << "The denominator cannot be zero." << std::endl; return 0; } std::cout << num << "/" << den << " = "; // Make the denominator positive. if ( den < 0 ) { num = -num; den = -den; } if ( num == 0 ) { std::cout << "0" << std::endl; } else if ( num%den == 0 ) { std::cout << num/den << std::endl; } else if ( num < 0 ) { if ( num/den != 0 ) { std::cout << num/den << " - "; } else { std::cout << "-"; } std::cout << (-num%den) << "/" << den << std::endl; } else { if ( num/den != 0 ) { std::cout << num/den << " + "; } std::cout << num%den << "/" << den << std::endl; } return 0; } // Hint: // // First determine the numerator of the fractional // part. For example, // // -52134/321 = -162 - 132/321, // // so the numerator of the fractional part is 132. // // You must then find // // gcd(132, 321) // // and divide both 132 and 321 by this greatest // common divisor. // // You have already implemented the Euclidean // algorithm for finding a greatest common divisor. // Hint: // // Once you have dealt with the integer part and the // sign, you can make 'num' positive and then execute // // num %= den; // // At this point, 'num' is the numerator of the // fractional part. // // For example, with the original fraction // // -52134/321, // // 'num' will now be // // 132. // // Find gcd(num, den), and then divide both 'num' // and 'den' by this value. // Here is one solution: /* int main() { int num{}; std::cout << "Enter an integer numerator: "; std::cin >> num; int den{}; std::cout << "Enter a nonzero integer denominator: "; std::cin >> den; if ( den == 0 ) { std::cout << "The denominator cannot be zero." << std::endl; return 0; } std::cout << num << "/" << den << " = "; // Make the denominator positive. if ( den < 0 ) { num = -num; den = -den; } if ( num == 0 ) { std::cout << "0" << std::endl; } else if ( num%den == 0 ) { std::cout << num/den << std::endl; } else { // Print the integer part and the sign of the // fractional part. if ( num < 0 ) { if ( num/den != 0 ) { std::cout << num/den << " - "; } else { std::cout << "-"; } } else if ( num/den != 0 ) { std::cout << num/den << " + "; } // We now only need the positive numerator of // the fractional part. if ( num < 0 ) { num = -num; } num %= den; // Find gcd(num, den) using the Euclidean // algorithm. int first{ den }; int gcd{ num }; while ( first%gcd != 0 ) { int rem{ first%gcd }; first = gcd; gcd = rem; } // Reduce the fractional part to lowest terms. num /= gcd; den /= gcd; std::cout << num << "/" << den << std::endl; } return 0; } */