对复数做抽象数据类型定义
做复数的加减运算
#include<stdio.h>
typedef struct complex
{
float re;
float im;
}Complex;
Complex set(float r, float i)
{
struct complex t;
t.re = r;
t.im = i;
return t;
}
Complex add(Complex a, Complex b)
{
struct complex c;
c.re = a.re + b.re;
c.im = a.im + b.im;
return c;
}
int main()
{
Complex x, y, z;
x = set(1, 4);
y = set(2, 5);
z = add(x, y);
printf("z=%.1f+%.1fi\n", z.re, z.im);
return 0;
}

本文介绍了如何在C语言中使用结构体定义复数类型,并提供了复数加法的实现,通过`set`和`add`函数展示了复数的创建与相加操作。

被折叠的 条评论
为什么被折叠?



