复数的基本运算(C语言)
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
#include
#include
typedef struct fushu//抽象数据类型定义
{
float real;//数据对象
float image;
}fushu;
fushu ComplexNumberInput(float a,float b)//构造二元组
{
fushu c;
c、real=a;//实部
c、image=b;//虚部
return(c);
}
fushu ComplexNumberAdd(fushu c1,fushu c2)//求与运算{
fushu sum;
sum、real=c1、real+c2、real;
sum、image=c1、image+c2、image;
return (sum);
}
fushu ComplexNumberSub(fushu c1,fushu c2)//求差运算{
fushu sub;
sub、real=c1、real-c2、real;
sub、image=c1、image-c2、image;
return (sub);
}
fushu ComplexNumberMul(fushu c1,fushu c2)//求积运算{
fushu Mul;
Mul、real=c1、real*c2、real-c1、image*c2、image;
Mul、image=c1、real*c2、image+c1、image*c2、real;
return (Mul);
}
fushu ComplexNumberDiv(fushu c1,fushu c2)//求商运算{
fushu div;
float d1,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);
}
}
void ComplexNumberOutput(fushu c)//输出运算结果
{
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); }
void main()//主函数
{
int choice;
int k;
float a1,a2,b1,b2;
struct fushu c1,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)
{
case 0:exit(0);
break;
case 1:sum = ComplexNumberAdd(c1,c2);
printf("求与后结果为: ");
ComplexNumberOutput(sum);
break;
case 2:sub = ComplexNumberSub(c1,c2);
printf("求差后结果为: ");
ComplexNumberOutput(sub);
break;
case 3:mult = ComplexNumberMul(c1,c2);
printf("求积后结果为: ");
ComplexNumberOutput(mult );
break;
case 4:div=ComplexNumberDiv(c1,c2);
if(div、real!=0&&div、image!=0)
{
printf("求商后结果: ");
ComplexNumberOutput(div);
break;
}
else
{
printf("除零错误,请重新输入第二个复数;\n");
k=0;
exit(k);
}
}
}
}