本文主要是介绍java:设计一个名为Complex的复数类来表示复数以及完成复数的运算,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
预览如下:
Complex类:
package num;public class Complex {double a;double b;public Complex(double x,double y){a=x;b=y;}public static Complex add(Complex x,Complex y){Complex ans = new Complex(x.a+y.a,x.b+y.b);return ans;}public static Complex sub(Complex x,Complex y){Complex ans = new Complex(x.a-y.a,x.b-y.b);return ans;}public static Complex mul(Complex x,Complex y){Complex ans = new Complex(x.a*y.a-x.b*y.b,x.a*y.b+x.b*y.a);return ans;}public static Complex div(Complex x,Complex y){Complex ans = new Complex((x.a*y.a+x.b*y.b)/(y.a*y.a+x.b*y.b),(x.a*y.b-x.b*y.a)/(y.a*y.a+x.b*y.b));return ans;}public static void print(Complex ans){if(ans.b>=0)System.out.println(ans.a+"+"+ans.b+"i");elseSystem.out.println(ans.a+""+ans.b+"i");}
}
主类:
package num;import java.util.Scanner;public class Complexmain {public static void main(String[] args) {// TODO Auto-generated method stubScanner scan = new Scanner(System.in);Complex x = new Complex(scan.nextDouble(),scan.nextDouble());Complex y = new Complex(scan.nextDouble(),scan.nextDouble());Complex z = Complex.add(x,y);System.out.print("加法运算:");Complex.print(z);Complex z1 = Complex.sub(x,y);System.out.print("减法运算:");Complex.print(z1);Complex z2 = Complex.mul(x,y);System.out.print("乘法运算:");Complex.print(z2);Complex z3 = Complex.div(x,y);System.out.print("除法运算:");Complex.print(z3);}}
小记
假如可以,最好是携一人终老,结婚而定。那个绅士说的:这就够了。希望可以一直提醒着我,这就够了。
即使明天是世界末日,我们也要穿着得体,这是一种生活态度。
这篇关于java:设计一个名为Complex的复数类来表示复数以及完成复数的运算的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!