/* June 29, 1998 */ /* Operators to process complex numbers */ #include#include /* User-defined complex number type */ typedef struct { double real, imag; } complex_t; /* Function Prototypes */ int scan_complex (complex_t *c); void print_complex (complex_t c); void add_complex (complex_t c1, complex_t c2, complex_t *c3); void subtract_complex (complex_t c1, complex_t c2, complex_t *c3); void multiply_complex (complex_t c1, complex_t c2, complex_t *c3); void divide_complex (complex_t c1, complex_t c2, complex_t *c3); complex_t abs_complex (complex_t c); /* Driver */ main () { complex_t com1, com2, csum, cdiff, cprod, cdiv; /* Gets two complex numbers */ printf ("Enter the real and imaginary parts of a complex number\n"); printf ("separated by a space> "); scan_complex (&com1); printf ("Enter a second complex number> "); scan_complex (&com2); /* Forms and displays the sum */ printf ("\n"); print_complex(com1); printf (" + "); print_complex(com2); printf (" = "); add_complex(com1, com2, &csum); print_complex(csum); /* Forms and displays the difference */ printf ("\n\n"); print_complex(com1); printf (" = "); print_complex(com2); printf (" = "); subtract_complex(com1, com2, &cdiff); print_complex(cdiff); /* Forms and displays the product */ printf ("\n\n"); print_complex(com1); printf (" * "); print_complex(com2); printf (" = "); multiply_complex(com1, com2, &cprod); print_complex(cprod); /* Forms and displays the division */ printf ("\n\n"); print_complex(com1); printf (" / "); print_complex(com2); printf (" = "); divide_complex(com1, com2, &cdiv); print_complex(cdiv); /* Forms and displays the absolute value of the first number */ printf ("\n\n|"); print_complex(com1); printf ("| = "); print_complex(abs_complex(com1)); printf ("\n"); } /* Complex number input function returns standard scanning error code */ /* 1 => valid scan, 0 => error, negative EOF value => end of file */ int scan_complex(complex_t *c) /* output - address of complex variable to fill */ { int status; status = scanf ("%lf%lf", &c->real, &c->imag); if (status ==2) status = 1; else if (status != EOF) status = 0; return (status); } /* Complex output function displays value as (a + bi) or (a - bi), */ /* dropping a or b if they round to 0 unless both round to 0 */ void print_complex(complex_t c) /* input - complex number to display */ { double a, b; char sign; a = c.real; b = c.imag; printf("("); if (fabs(a) < .005 && fabs(b) < .005) { printf("%.2f", 0.0); } else if (fabs(b) < .005) { printf("%.2f", a); } else if (fabs(a) < .005) { printf("%.2fi", b); } else { if (b < 0) sign = '-'; else sign = '+'; printf("%.2f %c %.2fi", a, sign, fabs(b)); } printf(")"); } /* * Returns sum of complex values c1 and c2 */ void add_complex(complex_t c1, complex_t c2, complex_t *c3) /* input - values to add */ { c3 -> real = c1.real + c2.real; c3 -> imag = c1.imag + c2.imag; } /* * Returns difference c1 - c2 */ void subtract_complex(complex_t c1, complex_t c2, complex_t *c3) /* input parameters */ { c3 -> real = c1.real - c2.real; c3 -> imag = c1.imag - c2.imag; } /* * Returns product of complex values c1 and c2 */ void multiply_complex(complex_t c1, complex_t c2, complex_t *c3) /* input parameters */ { c3 -> real = ((c1.real * c2.real) - (c1.imag * c2.imag)); c3 -> imag = ((c1.real * c2.imag) + (c1.imag * c2.real)); } /* * Returns quotient of complex values (c1 / c2) */ void divide_complex(complex_t c1, complex_t c2, complex_t *c3) /* input parameters */ { c3 -> real = ((c1.real * c2.real) + (c1.imag * c2.imag)) / ((c2.real * c2.real) + (c2.imag * c2.imag)); c3 -> imag = ((c1.imag * c2.real) - (c1.real * c2.imag)) / ((c2.real * c2.real) + (c2.imag * c2.imag)); } /* * Returns absolute value of complex number c */ complex_t abs_complex(complex_t c) /* input parameter */ { complex_t cabs; cabs.real = sqrt(c.real * c.real + c.imag * c.imag); cabs.imag = 0; return (cabs); }