#include // A complex number with integer real and imaginary // components has the form // // m + nj, // // where 'm' and 'n' are integers. // // For example: // // 3 + 5j // 2 - 7j // -4 + j // -3 - j // 6 // -2j // // A straightforward attempt to print such a number might // be // // std::cout << m << " + " << n << "j" // << std::endl; // // Unfortunately, this can produce awkward output: // // m = 0, n = -3 -> 0 + -3j // m = 0, n = 0 -> 0 + 0j // m = -3, n = -1 -> -3 + -1j // m = 5, n = 1 -> 5 + 1j // // Instead, we want the complex number to be printed using // conventional mathematical notation: // // m = 0, n = -3 -> -3j // m = 0, n = 0 -> 0 // m = -3, n = -1 -> -3 - j // m = 5, n = 1 -> 5 + j // // More generally, use the following rules: // // If n = 0: // Print m. // // Otherwise, if m = 0: // If n = 1, print j. // If n = -1, print -j. // Otherwise, print nj. // // Otherwise, both m and n are nonzero: // If n <= -2, print m - (-n)j. // If n = -1, print m - j. // If n = 1, print m + j. // Otherwise, print m + nj. // // For example: // // m n Output // // 0 0 0 // 7 0 7 // -7 0 -7 // 0 1 j // 0 -1 -j // 0 4 4j // 0 -4 -4j // 3 5 3 + 5j // 3 -5 3 - 5j // -3 5 -3 + 5j // -3 -5 -3 - 5j // 3 1 3 + j // 3 -1 3 - j // // Write a program that asks the user for 'm' and 'n' // and prints the corresponding complex number using // these rules. // Function declarations int main(); // Function definitions int main() { int m{}; std::cout << "Enter the real component: "; std::cin >> m; int n{}; std::cout << "Enter the imaginary component: "; std::cin >> n; // Print the complex number here. return 0; } // Hint: // // This problem requires a combination of cascading and // nested conditional statements. // // Begin with the cases that eliminate the largest number // of possibilities: // // Is n equal to zero? // // If not, is m equal to zero? // // If neither is zero, what possibilities remain for n? // Translate the rules directly into conditional // statements: // // if n = 0 // print m // // else if m = 0 // if n = 1 // print j // else if n = -1 // print -j // else // print nj // // else // if n <= -2 // print m - (-n)j // else if n = -1 // print m - j // else if n = 1 // print m + j // else // print m + nj // // Notice that by the time the final 'else' is reached, // we already know that // // m != 0 // // and // // n != 0. // These rules give the following structure: /* if ( n == 0 ) { // Print m. } else if ( m == 0 ) { if ( n == 1 ) { // Print j. } else if ( n == -1 ) { // Print -j. } else { // Print nj. } } else { if ( n <= -2 ) { // Print m - (-n)j. } else if ( n == -1 ) { // Print m - j. } else if ( n == 1 ) { // Print m + j. } else { // Print m + nj. } } */ // Here is one solution: /* int main() { int m{}; std::cout << "Enter the real component: "; std::cin >> m; int n{}; std::cout << "Enter the imaginary component: "; std::cin >> n; if ( n == 0 ) { // The imaginary component is zero, so print // only the real component. std::cout << m << std::endl; } else if ( m == 0 ) { // The real component is zero, so print only // the imaginary component. if ( n == 1 ) { std::cout << "j" << std::endl; } else if ( n == -1 ) { std::cout << "-j" << std::endl; } else { std::cout << n << "j" << std::endl; } } else { // At this point, neither 'm' nor 'n' is zero. if ( n <= -2 ) { // For a negative imaginary component, print // subtraction followed by its magnitude. std::cout << m << " - " << -n << "j" << std::endl; } else if ( n == -1 ) { // Do not print the coefficient 1. std::cout << m << " - j" << std::endl; } else if ( n == 1 ) { // Do not print the coefficient 1. std::cout << m << " + j" << std::endl; } else { // Since n is nonzero and is not negative, // -1 or 1, we know that n >= 2. std::cout << m << " + " << n << "j" << std::endl; } } return 0; } */