#include // THIS IS MORE CHALLENGING // // Write a program that implements a simple calculator // for rational numbers. // // A rational number can be written as // // a // -, // b // // where 'a' and 'b' are integers and b != 0. // // For this exercise, represent each rational number using // two integers: a numerator and a denominator. // // Ask the user to enter two rational numbers and then // select one of the following operations: // // 1. Addition // 2. Subtraction // 3. Multiplication // 4. Division // // If the user enters any integer other than 1, 2, 3 or 4, // print a message indicating that the selection is invalid. // // If the two rational numbers are // // a c // - and -, // b d // // then calculate the result using // // a c a*d + b*c // - + - = ---------, // b d b*d // // a c a*d - b*c // - - - = ---------, // b d b*d // // a c a*c // - x - = -----, // b d b*d // // and // // a c a*d // - / - = -----. // b d b*c // // Print the resulting numerator and denominator with a // '/' between them. // // Do not reduce the result to lowest terms. For example, // // 1/2 + 1/2 // // should be printed as // // 4/4 // // rather than // // 1/1. // // If the resulting denominator is negative, negate both // the numerator and denominator so that the denominator // is positive. For example, // // 3/-5 // // should be printed as // // -3/5. // // Remember: // // 1. The denominator of a rational number cannot // be zero. // // 2. Division by a rational number with numerator // zero is undefined. // // If either input denominator is zero, or if the user // attempts to divide by a rational number whose numerator // is zero, print an appropriate error message and // terminate the program. // Function declarations int main(); // Function definitions int main() { return 0; } // Hint: // // First read the numerator and denominator of each // rational number. // // Before performing any arithmetic, make sure that neither // denominator is zero. // // Next, use a cascading conditional statement to determine // which operation the user selected. // // Store the numerator and denominator of the answer in // // num // den // // respectively. // // For division, there is one additional issue: // // a c // - / - // b d // // requires multiplying by the reciprocal of c/d: // // a d a*d // - x - = -----. // b c b*c // // What happens if c = 0? // After calculating the numerator and denominator, there // is one final issue to consider. // // We want the denominator to be positive. // // If // // den < 0, // // negate both 'num' and 'den'. This does not change the // value of the rational number because // // num -num // --- = ----. // den -den // Here is one solution: /* int main() { int num_m{}; std::cout << "Enter the left-hand numerator: "; std::cin >> num_m; int den_m{}; std::cout << "Enter the left-hand denominator: "; std::cin >> den_m; // A rational number cannot have a denominator of zero. if ( den_m == 0 ) { std::cout << "A denominator cannot be zero." << std::endl; return 0; } int num_n{}; std::cout << "Enter the right-hand numerator: "; std::cin >> num_n; int den_n{}; std::cout << "Enter the right-hand denominator: "; std::cin >> den_n; // Again, the denominator cannot be zero. if ( den_n == 0 ) { std::cout << "A denominator cannot be zero." << std::endl; return 0; } int choice{}; std::cout << "Enter 1 for addition," << std::endl; std::cout << " 2 for subtraction," << std::endl; std::cout << " 3 for multiplication, and" << std::endl; std::cout << " 4 for division: "; std::cin >> choice; int num{}; int den{}; if ( choice == 1 ) { num = num_m*den_n + num_n*den_m; den = den_m*den_n; } else if ( choice == 2 ) { num = num_m*den_n - num_n*den_m; den = den_m*den_n; } else if ( choice == 3 ) { num = num_m*num_n; den = den_m*den_n; } else if ( choice == 4 ) { // Dividing by zero is undefined. The rational // number num_n/den_n is zero exactly when its // numerator is zero. if ( num_n == 0 ) { std::cout << "Division by zero is undefined." << std::endl; return 0; } num = num_m*den_n; den = den_m*num_n; } else { std::cout << "Invalid operation: " << choice << std::endl; return 0; } // Make the denominator positive by moving any // negative sign to the numerator. if ( den < 0 ) { num = -num; den = -den; } std::cout << num << "/" << den << std::endl; return 0; } */