Programming Assignment #6: Complex Number Library

In this programming assignment, you won't be writing a program. Instead, you will be writing a library of useful functions to manipulate complex numbers.

Imagine that you are working on a large programming project. Your team leader hands you the following .h file (also found in /mit/thomasc/Public/prog after you add thomasc):

/* complex.h */

typedef struct
  {
  double a,b;
  } complex;

extern int complexError;

complex makec(double a, double b);  /* return complex number with real part
                                       a and imaginary part b */

double real(complex z); 		    /* return real part of z */
double imag(complex z); 		    /* return imaginary part of z */

complex addc(complex c1,complex c2);		/* return c1 + c2 */
complex subc(complex c1,complex c2 );		/* return c1 - c2 */
complex multc(complex c1,complex c2);		/* return c1 * c2 */
complex divc(complex c1,complex c2);		/* return c1 / c2
					set complexError to 1 if c2 = 0 */

double magnc(complex z);		/* return sqrt( a*a + b*b ) */

complex stoc(const char *s);	/* convert string to complex number */
void printc(complex z);		/* print z to stdout */

char *ctos( complex );	/* convert complex number to string
			   memory for string should be got by malloc
			   calling program is responsible for freeing it. */

Your boss says: "Go implement complex.c".

For full credit, you must implement all of the routines in complex.h. You should copy complex.h into your working directory, but you shouldn't change it. To help you test your library, we have provided a program called ctest.c; it can also be found in the /mit/thomasc/Public/prog/ directory.