C语言 *有关复数的四则算法*程 (在线等答案!速度,只求答案正确不追求先答与否)
任务一、 试用C语言的结构类型定义表示复数的抽象数据类型。
(1) 在复数内部用浮点数定义其实部和虚部;
(2) 设计实现复数的+、-、*、/等运算函数:
Add(Complex A, Complex B),
Subtract(Complex A, Complex B),
Multiply(Complex A, Complex B),
Divide(Complex A, Complex B)。
帮忙编程一下,最好解释一下喔..越详细越好,我定以加分!!!
参考答案:#include <stdio.h>
typedef struct {
double a;
double b;
} COMPLEX;
COMPLEX add (COMPLEX a, COMPLEX b) {
a.a += b.a;
a.b += b.b;
return a;
}
COMPLEX subtract (COMPLEX a, COMPLEX b) {
a.a -= b.a;
a.b -= b.b;
return a;
}
COMPLEX multiply (COMPLEX a, COMPLEX b) {
COMPLEX c;
c.a = a.a*b.a - a.b*b.b;
c.b = a.a*b.b + a.b*b.a;
return c;
}
COMPLEX divide (COMPLEX a, COMPLEX b) {
COMPLEX c;
double divisor = b.a*b.a + b.b*b.b;
if (divisor - 0 == 0)
exit (0);
c.a = (a.a*b.a + a.b*b.b) / divisor;
c.b = (a.b*b.a - a.a*b.b) / divisor;
return c;
}