#includestdio.h#includestdlib.htypedefstructfushu//抽象数据类型定义{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{printf(除零错误,请重新输入第二个复数;\n);k=0;exit(k);}}}}