/* 文件 complex.c */

#include <stdio.h>
#include "complex.h"

/* 常量定义 */
const Complex Complex0 = {0.0, 0.0};
const Complex Complex1 = {1.0, 0.0};
const Complex ComplexI = {0.0, 1.0};

/* 构造函数 */
Complex mkComplex(double r, double i) {
    Complex c;
    c.re = r;
    c.im = i;
    return c;
}

Complex d2Complex(double d)  {
    Complex c;
    c.re = d;
    c.im = 0;
    return c;
}

Complex n2Complex(int n)  {
    Complex c;
    c.re = n;
    c.im = 0;
    return c;
}

/* 运算函数 */
Complex addComplex(Complex x, Complex y) {
    Complex c;
    c.re = x.re + y.re;
    c.im = x.im + y.im;
    return c;
}

/* 函数需要定义 
Complex subComplex(Complex x, Complex y) { ...... }
Complex tmsComplex(Complex x, Complex y) { ...... }
*/

Complex divComplex(Complex x, Complex y) {
    Complex c;
double den = y.re * y.re + y.im * y.im;

    if (den == 0.0) {
        fprintf(stderr, "Complex error: divid 0.\n");
        c.re = 1; c.im = 0;
    }
    else {
        c.re = (x.re * y.re + x.im * y.im) / den;
        c.im = (x.im * y.re - x.re * y.im) / den;
    }
    return c;
}

/* 其他运算函数的定义 ... ...
*/

/* 比较函数 */
/* 函数需要定义
int eqComplex(Complex x, Complex y) { ...... }
*/

/* 输入输出函数 */
/* 函数需要定义
int readComplex(FILE *fp, Complex *xp) { ...... }
*/

void prtComplex(FILE *fp, Complex x) {
    fprintf(fp, "(%f, %f)", x.re, x.im);
}


