/* Complex Numbers (15 points) Write a complete C program that asks the user to enter the real and the imaginary parts of two complex numbers. The program must then calculate and print on the screen the result of adding, subtracting, multiplying, and dividing the two complex numbers. Each of the three operations must be performed by a separate function. The result of each operation must be printed by each function as we don't know how to make a function return two values yet (e.g., the four functions can call a separate printComplex function to help them print the number). Math refresher: Given two complex numbers Z1 = a + bi and Z2 = c + di, the four operations are defined as: Addition: (a + bi) + (c + di) = (a+c) + (b+d)i Subtraction: (a + bi) - (c + di) = (a-c) + (b-d)i Multiplication: (a + bi) * (c + di) = (ac - bd) + (bc + ad)i (a + bi) (ac + bd) (bc - ad) Division: -------- = --------- + --------- i (c + di) (cc + dd) (cc + dd) SAMPLE RUN: =========== Z1 - Enter real part: 1 Z1 - Enter imaginary part: 2 Z2 - Enter real part: 3 Z2 - Enter imaginary part: 4 Z1 = ( 1.0000) + ( 2.0000)i Z2 = ( 3.0000) + ( 4.0000)i Addition: ( 4.0000) + ( 6.0000)i Subtraction: ( -2.0000) + ( -2.0000)i Multiplication: ( -5.0000) + ( 10.0000)i Division: ( 0.4400) + ( 0.0800)i */ #include #include #include void printComplex(double a, double b) { printf("(% 8.4f) + (% 8.4f)i\n\n", a, b); } void addComplex(double a, double b, double c, double d) { double x, y; x = (a+c); y = (b+d); printf("Addition: "); printComplex(x,y); } void subtractComplex(double a, double b, double c, double d) { double x, y; x = (a-c); y = (b-d); printf("Subtraction: "); printComplex(x,y); } void multiplyComplex(double a, double b, double c, double d) { double x, y; x = (a*c - b*d); y = (b*c + a*d); printf("Multiplication: "); printComplex(x,y); } void divideComplex(double a, double b, double c, double d) { double x, y; x = (a*c + b*d)/ (c*c + d*d); y = (b*c - a*d)/ (c*c + d*d); printf("Division: "); printComplex(x,y); } int main() { double a, b, c, d; printf("Z1 - Enter real part: "); scanf("%lf", &a); printf("Z1 - Enter imaginary part: "); scanf("%lf", &b); printf("\n"); printf("Z2 - Enter real part: "); scanf("%lf", &c); printf("Z2 - Enter imaginary part: "); scanf("%lf", &d); printf("\n"); printf("Z1 = "); printComplex(a, b); printf("Z2 = "); printComplex(c, d); addComplex (a, b, c, d); subtractComplex(a, b, c, d); multiplyComplex(a, b, c, d); divideComplex (a, b, c, d); system("pause"); }