本文主要是介绍7-36 | 复数四则运算,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
题目:
解答:
个人认为需要注意的地方是对负数的四舍五入;
#include "stdio.h"
#include "math.h"void rounding(float *f){int flag=*f<0?-1:1; int i=abs((int)(*f*100));if(i%10<5){i=i/10; }else{i=i/10+1;}*f=flag==1?(float)i/10.0:-(float)i/10.0;
}void print(float a1,float b1,float a2,float b2,float ret1,float ret2,char ch){printf("(%.1f%+.1fi) %c (%.1f%+.1fi) = ",a1,b1,ch,a2,b2);rounding(&ret1);rounding(&ret2);if(ret1==0.0&&ret2==0.0){printf("0.0\n");}else if(ret1==0.0){printf("%.1fi\n",ret2);}else if(ret2==0.0){printf("%.1f\n",ret1);}else{printf("%.1f%+.1fi\n",ret1,ret2);}
}int main(){float a1,b1,a2,b2;scanf("%f %f %f %f",&a1,&b1,&a2,&b2);print(a1,b1,a2,b2,a1+a2,b1+b2,'+');print(a1,b1,a2,b2,a1-a2,b1-b2,'-');print(a1,b1,a2,b2,a1*a2-b1*b2,a1*b2+a2*b1,'*');print(a1,b1,a2,b2,(a1*a2+b1*b2)/(a2*a2+b2*b2),(b1*a2-a1*b2)/(a2*a2+b2*b2),'/');return 0;
}
这篇关于7-36 | 复数四则运算的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!