实验
0
预备实验
实验题目:
用
c
语言的结构类型定义表示复数
(
1
)用浮点型
(
2
)实行
+-*
、
/
运算
实验代码:
#include
typedef struct{
int e1,e2;}Complex;
Complex InitComplex(int v1,int v2)
{Complex z;
z.e1=v1;
z.e2=v2;
return z;}
int Getreal(Complex z)
{
return z.e1;}
int Getimagetpart(Complex z)
{
return z.e2;
}
Complex Add (Complex z1,Complex z2)
{z1.e1=z1.e1+z2.e1;
z1.e2=z1.e2+z2.e2;
return z1;}
Complex Sub (Complex z1,Complex z2)
{z1.e1=z1.e1-z2.e1;
z2.e2=z1.e2-z2.e2;
return z1;}
Complex Mul (Complex z1,Complex z2)
{z1.e1=z1.e1*z2.e1+z1.e2+z2.e2;
z1.e2=z1.e1*z2.e2+z1.e2+z2.e1;
return z1;}
Complex Div (Complex z1,Complex z2)
{z1.e1=(z1.e1*z2.e1+z1.e2+(-z2.e2))/(z2.e1*z2.e1-z2.e2*z2.e2);
z1.e2=(z1.e1*(-z2.e2)+z1.e2+z2.e1)/(z2.e1*z2.e1-z2.e2*z2.e2);