试用C语言的结构类型定义表示复数的抽象数据类型
来源:学生作业帮 编辑:神马作文网作业帮 分类:综合作业 时间:2024/11/23 22:25:29
试用C语言的结构类型定义表示复数的抽象数据类型
(1)在复数内部用浮点数定义其实部与虚部; (2)设计实现复数的+、-、×、÷等运算的函数.要求运行界面如下:please input first complex number :a + bi the other one :c+ di please choice operation:+,-,* or /:+ (a + bi)+ (c+ di)=……
(1)在复数内部用浮点数定义其实部与虚部; (2)设计实现复数的+、-、×、÷等运算的函数.要求运行界面如下:please input first complex number :a + bi the other one :c+ di please choice operation:+,-,* or /:+ (a + bi)+ (c+ di)=……
#include
//结构体
typedef struct
{
double a;
double b;
} COMPLEX;
//加
COMPLEX add (COMPLEX a,COMPLEX b)
{
a.a += b.a;
a.b += b.b;
return a;
}
//减
COMPLEX subtract (COMPLEX a,COMPLEX b)
{
a.a -= b.a;
a.b -= b.b;
return a;
}
//乘
COMPLEX multiply (COMPLEX a,COMPLEX b)
{
COMPLEX c;
c.a = a.a*b.a - a.b*b.b;
c.b = a.a*b.b + a.b*b.a;
return c;
}
//除
COMPLEX divide (COMPLEX a,COMPLEX b) {
COMPLEX c;
double divisor = b.a*b.a + b.b*b.b;
if (divisor - 0 == 0)
exit (0);
c.a = (a.a*b.a + a.b*b.b) / divisor;
c.b = (a.b*b.a - a.a*b.b) / divisor;
return c;
}
然后读入数据,判断运算符号就行了.
//结构体
typedef struct
{
double a;
double b;
} COMPLEX;
//加
COMPLEX add (COMPLEX a,COMPLEX b)
{
a.a += b.a;
a.b += b.b;
return a;
}
//减
COMPLEX subtract (COMPLEX a,COMPLEX b)
{
a.a -= b.a;
a.b -= b.b;
return a;
}
//乘
COMPLEX multiply (COMPLEX a,COMPLEX b)
{
COMPLEX c;
c.a = a.a*b.a - a.b*b.b;
c.b = a.a*b.b + a.b*b.a;
return c;
}
//除
COMPLEX divide (COMPLEX a,COMPLEX b) {
COMPLEX c;
double divisor = b.a*b.a + b.b*b.b;
if (divisor - 0 == 0)
exit (0);
c.a = (a.a*b.a + a.b*b.b) / divisor;
c.b = (a.b*b.a - a.a*b.b) / divisor;
return c;
}
然后读入数据,判断运算符号就行了.