#include<stdio.h>

typedef struct{ double real; // 实部 double imag; // 虚部 }COMPLEX;

void Create(double x, double y, COMPLEX *z){ z->real = x; z->imag = y; }

void Add(COMPLEX z1, COMPLEX z2, COMPLEX *sum){ sum->real = z1.real + z2.real; sum->imag = z1.imag + z2.imag; }

void Subtract(COMPLEX z1, COMPLEX z2, COMPLEX *difference){ difference->real = z1.real - z2.real; difference->imag = z1.imag - z2.imag; }

void Multiply(COMPLEX z1, COMPLEX z2, COMPLEX *product){ product->real = z1.real * z2.real - z1.imag * z2.imag; product->imag = z1.real * z2.imag + z2.real * z1.imag; }

void Get_RealPart(COMPLEX z, double *e){ *e = z.real; }

void Get_ImagPart(COMPLEX z, double *e){ *e = z.imag; }

int main(){ COMPLEX z1, z2, sum, difference, product; double real, imag;

Create(1, 2, &z1);
Create(3, 4, &z2);

Add(z1, z2, &sum);
Subtract(z1, z2, &difference);
Multiply(z1, z2, &product);

Get_RealPart(sum, &real);
Get_ImagPart(sum, &imag);

printf('Sum: %.2f + %.2fi\n', real, imag);

Get_RealPart(difference, &real);
Get_ImagPart(difference, &imag);

printf('Difference: %.2f + %.2fi\n', real, imag);

Get_RealPart(product, &real);
Get_ImagPart(product, &imag);

printf('Product: %.2f + %.2fi\n', real, imag);

return 0;

}

C语言实现复数抽象数据类型(ADT COMPLEX)及应用

原文地址: https://www.cveoy.top/t/topic/mYXk 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录