7-36 复数四则运算(15 分)

4 篇文章 0 订阅

7-36 复数四则运算(15 分)
本题要求编写程序,计算2个复数的和、差、积、商。

输入格式:

输入在一行中按照a1 b1 a2 b2的格式给出2个复数C1=a1+b1i和C2=a2+b2i的实部和虚部。题目保证C2不为0。

输出格式:

分别在4行中按照(a1+b1i) 运算符 (a2+b2i) = 结果的格式顺序输出2个复数的和、差、积、商,数字精确到小数点后1位。如果结果的实部或者虚部为0,则不输出。如果结果为0,则输出0.0。

输入样例1:

2 3.08 -2.04 5.06
输出样例1:

(2.0+3.1i) + (-2.0+5.1i) = 8.1i
(2.0+3.1i) - (-2.0+5.1i) = 4.0-2.0i
(2.0+3.1i) * (-2.0+5.1i) = -19.7+3.8i
(2.0+3.1i) / (-2.0+5.1i) = 0.4-0.6i
输入样例2:

1 1 -1 -1.01
输出样例2:

(1.0+1.0i) + (-1.0-1.0i) = 0.0
(1.0+1.0i) - (-1.0-1.0i) = 2.0+2.0i
(1.0+1.0i) * (-1.0-1.0i) = -2.0i
(1.0+1.0i) / (-1.0-1.0i) = -1.0

DD–头一次开头评语,下面代码需要脑洞,可读性的提高一时做不到,望谅解,我的做法希望可以给有疑惑的小伙伴提供帮助!
(基础编程暂时告一段落,要开始更新别的内容了)

 import java.util.Scanner;
public class PluralOpreation {
    public static void main(String[] args){//处理了表达式,其实很简单
        Scanner sc=new Scanner(System.in);
        Operation ope= new Operation();
        System.out.println("请输入两个复数的实部与虚部:");
        double a1 =sc.nextDouble();//实部
        double b1 =sc.nextDouble();//虚部
        double a2 =sc.nextDouble();
        double b2=sc.nextDouble();
        ope.add(a1,b1,a2,b2);
        ope.sub(a1,b1,a2,b2);
        ope.muti(a1,b1,a2,b2);
        ope.div(a1,b1,a2,b2);
    }
}
class Operation{
    public void add( double a1, double b1, double a2, double b2){//1.虚部实部为0判断 2.虚部符号判断
        if(a1+a2 < 0.05 && a1+a2>-0.05 && b1+b2<0.05 && b1+b2>-0.05)//优先考虑最极端情况//由于保留一位,还是四舍五入,所以考虑范围是0.05,判断条件还可以优化,调用Math.abs(绝对值判断范围,代码可读性较好点)
            System.out.printf("(%.1f+%.1fi)+(%.1f+%.1fi)=0.0\n",a1,b1,a2,b2);
        else if(a1+a2<0.05 && a1+a2>-0.05)
       System.out.printf("(%.1f+%.1fi)+(%.1f+%.1fi)=%.1fi\n",a1,b1,a2,b2,b1+b2);
      else if(b1+b2<0.05 && b1+b2>-0.05)
          System.out.printf("(%.1f+%.1fi)+(%.1f+%.1fi)=%.1f\n",a1,b1,a2,b2,a1+a2);

      else if(b1+b2 > -0.05)
            System.out.printf("(%.1f+%.1fi)+(%.1f+%.1fi)=%.1f+%.1fi\n",a1,b1,a2,b2,a1+a2,b1+b2);
      else //虚部小于零不用输出加号
            System.out.printf("(%.1f+%.1fi)+(%.1f+%.1fi)=%.1f%.1fi\n",a1,b1,a2,b2,a1+a2,b1+b2);
    }
    public void sub( double a1, double b1, double a2, double b2) {
           if (a1 - a2 < 0.05 && a1 - a2 > -0.05&& b1 - b2 < 0.05 && b1 - b2 > -0.05)
            System.out.printf("(%.1f+%.1fi)-(%.1f+%.1fi)=0.0\n", a1, b1, a2, b2);
        else if (a1 - a2 < 0.05 && a1 - a2 > -0.05)
            System.out.printf("(%.1f+%.1fi)-(%.1f+%.1fi)=%.1fi\n", a1, b1, a2, b2, b1 - b2);

        else if (b1 - b2 < 0.05 && b1 - b2 > -0.05)
            System.out.printf("(%.1f+%.1fi)-(%.1f+%.1fi)=%.1f\n", a1, b1, a2, b2, a1 -a2);

        else if (b1 - b2 > -0.05)
            System.out.printf("(%.1f+%.1fi)-(%.1f+%.1fi)=%.1f+%.1fi\n", a1, b1, a2, b2, a1 - a2, b1 - b2);
        else //虚部小于零不用输出加号
            System.out.printf("(%.1f+%.1fi)-(%.1f+%.1fi)=%.1f%.1fi\n", a1, b1, a2, b2, a1 - a2, b1 - b2);
    }
    public void muti( double a1, double b1, double a2, double b2){
         if((a1*a2-b1*b2)<0.05 &&(a1*a2-b1*b2)>-0.05 && (a2*b1+a1*b2)<0.05 && (a2*b1+a1*b2)>-0.05)
            System.out.printf("(%.1f+%.1fi)*(%.1f+%.1fi)=0.0\n",a1,b1,a2,b2);
         else if((a1*a2-b1*b2)<0.05 && (a1*a2-b1*b2)>-0.05)//i的平方为-1
            System.out.printf("(%.1f+%.1fi)*(%.1f+%.1fi)=%.1fi\n",a1,b1,a2,b2,(a2*b1+a1*b2));
        else if((a2*b1+a1*b2)<0.05 && (a2*b1+a1*b2)>-0.05)
            System.out.printf("(%.1f+%.1fi)*(%.1f+%.1fi)=%.1f\n",a1,b1,a2,b2,(a1*a2-b1*b2));

        else if((a2*b1+a1*b2)>-0.05)
            System.out.printf("(%.1f+%.1fi)*(%.1f+%.1fi)=%.1f+%.1fi\n",a1,b1,a2,b2,(a1*a2-b1*b2),(a2*b1+a1*b2));
        else //虚部小于零不用输出加号
            System.out.printf("(%.1f+%.1fi)*(%.1f+%.1fi)=%.1f%.1fi\n",a1,b1,a2,b2,(a1*a2-b1*b2),(a2*b1+a1*b2));
    }
    public void div( double a1, double b1, double a2, double b2){//除法麻烦点,要构造平方差使分母为a2和b2的平方和,再简化分子
          if(((a1*a2+b1*b2)/(a2*a2+b2*b2))<0.05 && ((a1*a2+b1*b2)/(a2*a2+b2*b2))>-0.05 && ((b1*a2-a1*b2)/(a2*a2+b2*b2))<0.05&&((b1*a2-a1*b2)/(a2*a2+b2*b2))>-0.05)
            System.out.printf("(%.1f+%.1fi)/(%.1f+%.1fi)=0.0\n",a1,b1,a2,b2);
          else if(((a1*a2+b1*b2)/(a2*a2+b2*b2))<0.05 && ((a1*a2+b1*b2)/(a2*a2+b2*b2))>-0.05)
            System.out.printf("(%.1f+%.1fi)/(%.1f+%.1fi)=%.1fi\n",a1,b1,a2,b2,((b1*a2-a1*b2)/(a2*a2+b2*b2)));
        else if(((b1*a2-a1*b2)/(a2*a2+b2*b2))<0.05 && ((b1*a2-a1*b2)/(a2*a2+b2*b2))>-0.05)
            System.out.printf("(%.1f+%.1fi)/(%.1f+%.1fi)=%.1f\n",a1,b1,a2,b2,((a1*a2+b1*b2)/(a2*a2+b2*b2)));

        else if(((b1*a2-a1*b2)/(a2*a2+b2*b2)) > -0.05)
            System.out.printf("(%.1f+%.1fi)/(%.1f+%.1fi)=%.1f+%.1fi\n",a1,b1,a2,b2,((a1*a2+b1*b2)/(a2*a2+b2*b2)),((b1*a2-a1*b2)/(a2*a2+b2*b2)));
        else //虚部小于零不用输出加号
            System.out.printf("(%.1f+%.1fi)/(%.1f+%.1fi)=%.1f%.1fi\n",a1,b1,a2,b2,((a1*a2+b1*b2)/(a2*a2+b2*b2)),((b1*a2-a1*b2)/(a2*a2+b2*b2)));
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值