用结构体表示一个复数,编写实现复数的加法、
1个回答
展开全部
#include
"stdio.h"
struct
complex_num
{
double
real;
double
image;
};
complex_num
add(complex_num
a,complex_num
b)
{
complex_num
c;
c.real=a.real+b.real;
c.image=a.image+b.image;
return
c;
}
complex_num
cheng(complex_num
a,complex_num
b)
{
complex_num
c;
c.real=a.real*b.real-a.image*b.image;
c.image=a.real*b.image+a.image*b.real;
return
c;
}
void
main()
{
complex_num
a,b,c,d;
printf("请输入复数a的实部和虚部:");
scanf("%lf
%lf",&a.real,&a.image);
printf("请输入复数b的实部和虚部:");
scanf("%lf
%lf",&b.real,&b.image);
c=cheng(a,b);
d=add(a,b);
printf("实数a加上实数b后新的实数的实部虚部为:%d
,%d\n",d.real,d.image);
printf("实数a乘以实数b后新的实数的实部虚部为:%d
,%d\n",c.real,c.image);
}
"stdio.h"
struct
complex_num
{
double
real;
double
image;
};
complex_num
add(complex_num
a,complex_num
b)
{
complex_num
c;
c.real=a.real+b.real;
c.image=a.image+b.image;
return
c;
}
complex_num
cheng(complex_num
a,complex_num
b)
{
complex_num
c;
c.real=a.real*b.real-a.image*b.image;
c.image=a.real*b.image+a.image*b.real;
return
c;
}
void
main()
{
complex_num
a,b,c,d;
printf("请输入复数a的实部和虚部:");
scanf("%lf
%lf",&a.real,&a.image);
printf("请输入复数b的实部和虚部:");
scanf("%lf
%lf",&b.real,&b.image);
c=cheng(a,b);
d=add(a,b);
printf("实数a加上实数b后新的实数的实部虚部为:%d
,%d\n",d.real,d.image);
printf("实数a乘以实数b后新的实数的实部虚部为:%d
,%d\n",c.real,c.image);
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询