(C语言用结构体实现复数初始化加减法)程序怎样改才对
#include<stdio.h>typedefstruct{floatreal;floatimage;}complex;voidinitcomplex(complex&...
#include<stdio.h>
typedef struct
{
float real;
float image;
}complex ;
void initcomplex(complex &l)
{
l.real=0;
l.image=0;
}
complex getcomplex(float a,float b,complex &l)
{
l.real=a;
l.image=b;
return l;
}
complex addcomplex(complex &s,complex t)
{
s.real=s.real+t.real;
s.image=s.image+t.image;
return s;
}
complex subtractcomplex(complex s,complex t)
{
s.real=s.real-t.real;
s.image=s.image-t.image;
return s;
}
complex multiplycomplex(complex s,complex t)
{
s.real=s.real*t.real;
s.image=s.image*t.image;
return s;
}
complex dividecomplex(complex s,complex t)
{
s.real=s.real/t.real;
s.image=s.image/t.image;
return s;
}
void printcomplex(complex l)
{
printf("%g%gi\n",l.real,l.image);
}
int main()
{
float a,b;
complex l,m;
printf("输入复数的实部和虚部\n");
scanf("%g%g\n",&a,&b);
initcomplex(&l);
getcomplex(a,b,&l);
printcomplex(l);
printf("输入另一个复数的实部和虚部:\n");
scanf("%g%g\n",&m.real,&m.image);
addcomplex(&m,l);
printcomplex(m);
return 0;
} 展开
typedef struct
{
float real;
float image;
}complex ;
void initcomplex(complex &l)
{
l.real=0;
l.image=0;
}
complex getcomplex(float a,float b,complex &l)
{
l.real=a;
l.image=b;
return l;
}
complex addcomplex(complex &s,complex t)
{
s.real=s.real+t.real;
s.image=s.image+t.image;
return s;
}
complex subtractcomplex(complex s,complex t)
{
s.real=s.real-t.real;
s.image=s.image-t.image;
return s;
}
complex multiplycomplex(complex s,complex t)
{
s.real=s.real*t.real;
s.image=s.image*t.image;
return s;
}
complex dividecomplex(complex s,complex t)
{
s.real=s.real/t.real;
s.image=s.image/t.image;
return s;
}
void printcomplex(complex l)
{
printf("%g%gi\n",l.real,l.image);
}
int main()
{
float a,b;
complex l,m;
printf("输入复数的实部和虚部\n");
scanf("%g%g\n",&a,&b);
initcomplex(&l);
getcomplex(a,b,&l);
printcomplex(l);
printf("输入另一个复数的实部和虚部:\n");
scanf("%g%g\n",&m.real,&m.image);
addcomplex(&m,l);
printcomplex(m);
return 0;
} 展开
1个回答
展开全部
#include<stdio.h>
typedef struct
{ float real;
float image;
} complex ;
void initcomplex(complex &l)
{ l.real=0;
l.image=0;
}
complex getcomplex(float a,float b,complex &l)
{ l.real=a;
l.image=b;
return l;
}
complex addcomplex(complex &s,complex t)
{ s.real=s.real+t.real;
s.image=s.image+t.image;
return s;
}
complex subtractcomplex(complex s,complex t)
{ s.real=s.real-t.real;
s.image=s.image-t.image;
return s;
}
complex multiplycomplex(complex s,complex t)
{ s.real=s.real*t.real;
s.image=s.image*t.image;
return s;
}
complex dividecomplex(complex s,complex t)
{ s.real=s.real/t.real;
s.image=s.image/t.image;
return s;
}
void printcomplex(complex l)
{ printf("%g%+-gi\n",l.real,l.image);
}
int main()
{ float a,b;
complex l,m;
printf("输入复数的实部和虚部\n");
scanf("%g%g",&a,&b);
// initcomplex(l);
getcomplex(a,b,l);
printcomplex(l);
printf("输入另一个复数的实部和虚部:\n");
scanf("%g%g",&m.real,&m.image);
addcomplex(m,l);
printcomplex(m);
return 0;
}
TableDI
2024-07-18 广告
2024-07-18 广告
仅需3步!不写公式自动完成Excel vlookup表格匹配!Excel在线免,vlookup工具,点击16步自动完成表格匹配,无需手写公式,免费使用!...
点击进入详情页
本回答由TableDI提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询