复数加减求绝对值
#include<stdio.h>#include<math.h>
typedef struct
{
float real,image;
}fushu;
fushu getFuShu();
void printFuShu(fushu c);
float juedui(float a);
fushu jiaFuShu(fushu c1,fushu c2);
fushu jianFuShu(fushu c1,fushu c2);
fushu chengFuShu(fushu c1,fushu c2);
double jueduizhi(fushu c1);
void main(void)
{
fushu fu1,fu2;
fushu add;
fushu sub;
fushu ji;
float jue1,jue2;
printf("次程序将要实现复数的运算\n");
printf("获取第一个复数\n");
fu1=getFuShu();
printFuShu(fu1);
printf("\n");
printf("获取第二个复数\n");
fu2=getFuShu();
printFuShu(fu2);
printf("\n");
printf("两个数相加运算\n");
add=jiaFuShu(fu1,fu2);
printFuShu(add);
printf("\n");
printf("两个复数相减运算\n");
sub=jianFuShu(fu1,fu2);
printFuShu(sub);
printf("\n");
printf("两个复数相乘运算\n");
ji=chengFuShu(fu1,fu2);
printFuShu(ji);
printf("\n");
jue1=(float)jueduizhi(fu1);
printf("第一个复数的绝对值: %.2f\n",jue1);
jue2=(float)jueduizhi(fu2);
printf("第二个复数的绝对值: %.2f\n",jue2);
}
fushu getFuShu()
{
fushu a;
printf("输入一个数作为实部\n");
scanf("%f",(a.real));
printf("输入一个数作为虚部\n");
scanf("%f",(a.image));
printf("\n");
}
void printFuShu(fushu c)
{
float a,b,k;
a=c.real;
b=c.image;
printf("结果是\n");
printf("(");
printf("%0.2f",a);
if(b<0)
{
printf("%c",'-');
}
else
{
printf("%c",'+');
}
k=juedui(b);
printf("%.2f",k);
printf("i)");
printf("\n");
}
float juedui(float a)
{
if(a>=0)
return a;
else
return -a;
}
fushu jiaFuShu(fushu c1,fushu c2)
{
fushu c;
c.real=c1.real+c2.real;
c.image=c1.image+c2.image;
return c;
}
fushu jianFuShu(fushu c1,fushu c2)
{
fushu c;
c.real=c1.real-c2.real;
c.image=c1.image-c2.image;
return c;
}
fushu chengFuShu(fushu c1,fushu c2)
{
fushu c;
c.real=c1.real*c2.real-c1.image*c2.image;
c.image=c1.real*c2.image+c1.image*c2.real;
return c;
}
double jueduizhi(fushu c1)
{
double c;
c=sqrt(c1.real*c1.real+c1.image*c1.image);
return c;
}
这个程序可以运行也可以编译,就是当输出一个数时按回车键时就立马停止运行了。