复数的基本运算C语言
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
typedefstructfushu//抽象数据类型定义
{
floatreal;//数据对象
floatimage;
}fushu;
fushuComplexNumberInput(floata,floatb)//构造二元组{
fushuc;
c.real=a;//实部
c.image=b;//虚部
return(c);
}
fushuComplexNumberAdd(fushuc1,fushuc2)//求和运算{
fushusum;
sum.real=c1.real+c2.real;
sum.image=c1.image+c2.image;
return(sum);
}
fushuComplexNumberSub(fushuc1,fushuc2)//求差运算{
fushusub;
sub.real=c1.real-c2.real;
sub.image=c1.image-c2.image;
return(sub);
}
fushuComplexNumberMul(fushuc1,fushuc2)//求积运算{
fushuMul;
Mul.real=c1.real*c2.real-c1.image*c2.image;
Mul.image=c1.real*c2.image+c1.image*c2.real;
return(Mul);
}
fushuComplexNumberDiv(fushuc1,fushuc2)//求商运算{
fushudiv;
floatd1,d2,d3,d4;
d1=c1.real*c2.real+c1.image*c2.image;
d2=c2.real*c2.real+c2.image*c2.image;
d3=c1.image*c2.real-c1.real*c2.image;
d4=c2.real*c2.real+c2.image*c2.image;
if(d2!=0&&d4!=0)
{
div.real=d1/d2;
div.image=d3/d4;
return(div);
}
else
{
div.real=0;
div.image=0;
return(div);
}
}
voidComplexNumberOutput(fushuc)//输出运算结果
{
if(c.real==0.0&&c.image==0.0)printf("0\n");
if(c.real==0.0&&c.image!=0.0)printf("%fi\n",c.image);
if(c.real!=0.0&&c.image==0.0)printf("%f\n",c.real);
if(c.real!=0.0&&c.image!=0.0)printf("%f+(%fi)\n",c.real,c.image); }
voidmain()//主函数
{
intchoice;
intk;
floata1,a2,b1,b2;
structfushuc1,c2,sum,sub,mult,div;
printf("欢迎进行复数的基本代数运算\n");
printf("1:复数加法运算\n");
printf("2:复数减法运算\n");
printf("3:复数乘法运算\n");
printf("4:复数除法运算\n");
printf("0:推出系统\n");
printf("请分别输入第一个复数的实部和虚部:");
scanf("%f%f",&a1,&b1);
c1=ComplexNumberInput(a1,b1);
printf("第一个复数为:");
ComplexNumberOutput(c1);
printf("请分别输入第二个复数的实部和虚部:");
scanf("%f%f",&a2,&b2);
c2=ComplexNumberInput(a2,b2);
printf("第二个复数为:"); ComplexNumberOutput(c2);
for(;;)
{
printf("请输入你选择的功能:");
scanf("%d",&choice);
switch(choice)
{
case0:exit(0);
break;
case1:sum=ComplexNumberAdd(c1,c2);
printf("求和后结果为:");
ComplexNumberOutput(sum);
break;
case2:sub=ComplexNumberSub(c1,c2);
printf("求差后结果为:");
ComplexNumberOutput(sub);
break;
case3:mult=ComplexNumberMul(c1,c2);
printf("求积后结果为:");
ComplexNumberOutput(mult);
break;
case4:div=ComplexNumberDiv(c1,c2); if(div.real!=0&&div.image!=0)
{
printf("求商后结果:"); ComplexNumberOutput(div);
break;
}
else
{