package haha;
import java.util.Scanner;
class Complex {
private double real;
private double image;
Complex(){
Scanner scanner = new Scanner(System.in);
double real = scanner.nextDouble();
double image = scanner.nextDouble();
this.real=real;
this.image=image;
}
Complex(double real,double image){
this.real = real;
this.image = image;
}
public double getReal() {
return real;
}
public void setReal(double real) {
this.real = real;
}
public double getImage() {
return image;
}
public void setImage(double image) {
this.image = image;
}
Complex add(Complex a){
double r = a.getReal();
double i = a.getImage();
double newReal = real + r;
double newImage = image + i;
Complex result = new Complex(newReal,newImage);
return result;
}
Complex sub(Complex a){
double r = a.getReal();
double i = a.getImage();
double newReal = real - r;
double newImage = image - i;
Complex result = new Complex(newReal,newImage);
return result;
}
Complex mul(Complex a){
double r = a.getReal();
double i = a.getImage();
double newReal = real*r - image*i;
double newImage = image*r + real*i;
Complex result = new Complex(newReal,newImage);
return result;
}
Complex div(Complex a){
double r = a.getReal();
double i = a.getImage();
double newReal = (real*r + image*i)/(r*r + i*i);
double newImage = (image*r - real*i)/(r*r + i*i);
Complex result = new Complex(newReal,newImage);
return result;
}
public void print(){
if(image > 0){
System.out.println(real + " + " + image + "i");
}else if(image < 0){
System.out.println(real + "" + image + "i");
}else{
System.out.println(real);
}
}
}
public class haha {
public static void main(String[] args) {
System.out.println("请输入第一个复数的实部和虚部:");
Complex d1=new Complex();
System.out.println("请输入第二个复数的实部和虚部:");
Complex d2=new Complex();
System.out.print("加法结果为:");
Complex result_add=d1.add(d2);
result_add.print();
System.out.print("减法结果为:");
Complex result_sub=d1.sub(d2);
result_sub.print();
System.out.print("乘法结果为:");
Complex result_mul=d1.mul(d2);
result_mul.print();
System.out.print("除法结果为:");
Complex result_div=d1.div(d2);
result_div.print();
}
}
java-复数加减乘除
最新推荐文章于 2022-05-03 16:25:33 发布