C语言编程实现下列抽象数据类型并实际应用ADT COMPLEX数据对象:D=c1021ClERCZER数据关系:R=C1c21ClERCZER基本操作:Createx 了 &z1生成一个复数操作结果:c1c2分别被参数xyxERyER賦值用2返回生成的复数xtyiAddZ122 &Sumy复数求和。操作结果:按照复数加法原则对复数2122求和用sum返回求和结果x1+x2+ y1+y2ioSubs
#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; }
// 取复数的实部 float GetRealPart(Complex z) { return z.real; }
// 取复数的虚部 float GetImagPart(Complex z) { return z.imag; }
int main() { Complex z1, z2, sum, difference, product; float realPart, imagPart;
// 生成复数
Create(1, 2, &z1);
Create(3, 4, &z2);
// 复数求和
Add(z1, z2, &sum);
printf("Sum: %.2f + %.2fi\n", sum.real, sum.imag);
// 复数求差
Subtract(z1, z2, &difference);
printf("Difference: %.2f + %.2fi\n", difference.real, difference.imag);
// 复数求积
Multiply(z1, z2, &product);
printf("Product: %.2f + %.2fi\n", product.real, product.imag);
// 取复数的实部和虚部
realPart = GetRealPart(z1);
imagPart = GetImagPart(z1);
printf("Real Part: %.2f\n", realPart);
printf("Imaginary Part: %.2f\n", imagPart);
return 0;
}
原文地址: https://www.cveoy.top/t/topic/i4uJ 著作权归作者所有。请勿转载和采集!