#include // In Section 1-7, there was an assignment question that // asked you to print an improper fraction as a mixed // number. For example, // // 52134/321 = 162 + 132/321 // 91/13 = 7 // // At that point, you were asked to assume that the // numerator was non-negative and the denominator was // positive. // // Now that you have learned conditional statements, // extend that program so that // // - the numerator may be any integer, // - the denominator may be any nonzero integer, // - a denominator of zero is detected, // - the fractional part is not printed when the // remainder is zero, // - a zero integer part is not printed, and // - negative fractions are printed naturally. // // For example, // // 32/5 = 6 + 2/5 // -32/5 = -6 - 2/5 // -4/5 = -4/5 // 4/-5 = -4/5 // -10/-5 = 2 // // You may assume that the user enters valid integers. // 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; std::cout << num << "/" << den << " = " << (num/den) << " + " << (num%den) << "/" << den << std::endl; return 0; } // Hint: // // In C++, integer division truncates toward zero, and the // remainder has the same sign as the numerator. // // For example, what are the values of // // -11/5 // -11%5 // // and what are the values of // // -11/-5 // -11%-5? // // It will be easier to print the answer consistently if // the denominator is always positive. // // If the denominator is negative, how can you use // assignment to change // // num/den // // into an equivalent fraction with a positive // denominator? // If the denominator is negative, multiplying both the // numerator and denominator by -1 does not change the // value of the fraction: // // 11/-5 becomes -11/5 // // -11/-5 becomes 11/5. // // Thus, after checking that the denominator is not zero, // you can use // // if ( den < 0 ) { // num *= -1; // den *= -1; // } // // From that point onward, you only have to consider a // positive denominator. // Try your program with each of the following: // // num den output // // 0 5 0 // 1 5 1/5 // 4 5 4/5 // 5 5 1 // 6 5 1 + 1/5 // 9 5 1 + 4/5 // 10 5 2 // 11 5 2 + 1/5 // // -1 5 -1/5 // -4 5 -4/5 // -5 5 -1 // -6 5 -1 - 1/5 // -9 5 -1 - 4/5 // -10 5 -2 // -11 5 -2 - 1/5 // // 1 -5 -1/5 // 6 -5 -1 - 1/5 // -6 -5 1 + 1/5 // // Also test a denominator of zero. // A 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 *= -1; den *= -1; } if ( num == 0 ) { std::cout << "0" << std::endl; } else if ( num%den == 0 ) { // There is no fractional part. std::cout << (num/den) << std::endl; } else if ( num < 0 ) { // The fraction is negative. if ( num/den != 0 ) { std::cout << (num/den) << " - "; } else { // For a proper negative fraction such as // -4/5, do not print a zero integer part. std::cout << "-"; } std::cout << (-num%den) << "/" << den << std::endl; } else { // The fraction is positive. if ( num/den != 0 ) { std::cout << (num/den) << " + "; } std::cout << (num%den) << "/" << den << std::endl; } return 0; } */