#include <stdio.h>

typedef struct { float real; float imag; } Complex;

void Create(float x, float 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, float *e) { *e = z.real; }

void Get_ImagPart(Complex z, float *e) { *e = z.imag; }

int main() { Complex z1, z2, sum, difference, product; float real_part, imag_part;

Create(2.5, 3.7, &z1);
Create(1.8, -2.1, &z2);

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

Get_RealPart(sum, &real_part);
Get_ImagPart(sum, &imag_part);
printf("The sum is: %f + %fi\n", real_part, imag_part);

Get_RealPart(difference, &real_part);
Get_ImagPart(difference, &imag_part);
printf("The difference is: %f + %fi\n", real_part, imag_part);

Get_RealPart(product, &real_part);
Get_ImagPart(product, &imag_part);
printf("The product is: %f + %fi\n", real_part, imag_part);

return 0;

}

C语言编程实现下列抽象数据类型并实际应用数据对象:D=c1c2c1∈Rc2∈R 数据关系:R=<c1c2>c1∈Rc2∈R 基本操作:Createxy&z生成一个复数操作结果:c1c2分别被参数xyx∈Ry∈R 赋值用z返回生成的复数x+yi。Addz1z2&sum复数求和。操作结果:按照复数加法原则对复数z1z2求和用sum返回求和结果x1+x2+ y1+y2i。Substractz1z2&di

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

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