#include // Recall how we add and subtract polynomials: // // (a + bx) + (c + dx) = (a + c) + (b + d)x // // and // // (a + bx) - (c + dx) = (a - c) + (b - d)x. // // For example, // // (3 + 4x) + (5 - 6x) = 8 - 2x // // and // // (3 + 4x) - (5 - 6x) = -2 + 10x. // // Adding and subtracting complex numbers works in exactly // the same way. We add or subtract the corresponding real // and imaginary components: // // (a + bj) + (c + dj) = (a + c) + (b + d)j // // and // // (a + bj) - (c + dj) = (a - c) + (b - d)j. // // For example, // // (3 + 4j) + (5 - 6j) = 8 - 2j // // and // // (3 + 4j) - (5 - 6j) = -2 + 10j. // // Multiplication requires a little more work. // // Using the distributive law when multiplying two // polynomials gives // // (a + bx)(c + dx) // // = ac + adx + bcx + bdx^2 // // = ac + (ad + bc)x + bdx^2. // // Multiplication of complex numbers begins in exactly the // same way: // // (a + bj)(c + dj) // // = ac + adj + bcj + bdj^2. // // However, the imaginary unit satisfies // // j^2 = -1. // // Therefore, // // (a + bj)(c + dj) // // = ac + (ad + bc)j + bd(-1) // // = (ac - bd) + (ad + bc)j. // // Thus, if // // z1 = a + bj // // and // // z2 = c + dj, // // then // // z1 + z2 = (a + c) + (b + d)j, // // z1 - z2 = (a - c) + (b - d)j, // // and // // z1*z2 = (ac - bd) + (ad + bc)j. // // Write a program that asks the user to enter the real and // imaginary components of two complex numbers. // // Then ask the user to select one of the following // operations: // // 1. Addition // 2. Subtraction // 3. Multiplication // // Calculate the real and imaginary components of the // result. // // The code for printing the resulting complex number has // already been provided. This is the same logic used in // the previous exercise on printing complex numbers. // Function declarations int main(); // Function definitions int main() { int re1{}; std::cout << "Enter the first real component: "; std::cin >> re1; int im1{}; std::cout << "Enter the first imaginary component: "; std::cin >> im1; int re2{}; std::cout << "Enter the second real component: "; std::cin >> re2; int im2{}; std::cout << "Enter the second imaginary component: "; std::cin >> im2; int choice{}; std::cout << "Enter 1 for addition," << std::endl; std::cout << " 2 for subtraction, and" << std::endl; std::cout << " 3 for multiplication: "; std::cin >> choice; int re_result{}; int im_result{}; // Calculate the real and imaginary components of // the result. if ( choice == 1 ) { } else if ( choice == 2 ) { } else if ( choice == 3 ) { } else { std::cout << "Invalid operation: " << choice << std::endl; return 0; } // Print the resulting complex number. if ( im_result == 0 ) { std::cout << re_result << std::endl; } else if ( re_result == 0 ) { if ( im_result == 1 ) { std::cout << "j" << std::endl; } else if ( im_result == -1 ) { std::cout << "-j" << std::endl; } else { std::cout << im_result << "j" << std::endl; } } else { // At this point, neither 're_result' nor // 'im_result' is zero. if ( im_result <= -2 ) { std::cout << re_result << " - " << -im_result << "j" << std::endl; } else if ( im_result == -1 ) { std::cout << re_result << " - j" << std::endl; } else if ( im_result == 1 ) { std::cout << re_result << " + j" << std::endl; } else { std::cout << re_result << " + " << im_result << "j" << std::endl; } } return 0; } // Hint: // // For each operation, calculate two values: // // re_result // im_result // // For addition and subtraction, treat the real and // imaginary components independently. // // For multiplication, use // // (a + bj)(c + dj) // // = (ac - bd) + (ad + bc)j. // // In terms of the variables in this program, // // a = re1 // b = im1 // c = re2 // d = im2. // Translating the formulas directly into the variable // names used in the program gives: // // Addition: // // re_result = re1 + re2 // im_result = im1 + im2 // // Subtraction: // // re_result = re1 - re2 // im_result = im1 - im2 // // Multiplication: // // re_result = re1*re2 - im1*im2 // im_result = re1*im2 + im1*re2 // Here is one solution: /* if ( choice == 1 ) { re_result = re1 + re2; im_result = im1 + im2; } else if ( choice == 2 ) { re_result = re1 - re2; im_result = im1 - im2; } else if ( choice == 3 ) { re_result = re1*re2 - im1*im2; im_result = re1*im2 + im1*re2; } else { std::cout << "Invalid operation: " << choice << std::endl; return 0; } */