定义一个复数(z=x+iy)类Complex
发布网友
发布时间:2022-05-02 18:41
我来回答
共2个回答
热心网友
时间:2022-06-21 06:49
public class Complex {
int x,y;
Complex(){
x=0;
y=0;
}
int getx(){
return x;
}
int gety(){
return y;
}
void showcomp(){
if(x==0){
System.out.println(y+"i");
}
else if(y>0)
System.out.println(x+"+"+y+"i");
else if(x==0&&y==0){
System.out.println("0");
}
else
System.out.println(x+""+y+"i");
}
Complex(int s,int z){
x=s;
y=z;
}
static Complex addcomp(Complex c1,Complex c2){
Complex b= new Complex(c1.getx()+c2.getx(),c2.gety()+c1.gety());
return b;
}
static Complex subcomp(Complex c1,Complex c2){
Complex b = new Complex(c1.getx()-c2.getx(),c1.gety()-c2.gety());
return b;
}
static Complex mulitcomp(Complex c1,Complex c2){
Complex b = new Complex(c1.getx()*c2.getx()-c1.gety()*c2.gety(),c1.getx()*c2.gety()+c2.getx()*c1.gety());
return b;
}
public static boolean equalComp(Complex c1,Complex c2){
boolean b;
if(c1.x==c2.x&&c1.y==c2.y)
b=true;
else
b=false;
return b;
}
}
import java.util.*;
public class Mainclass {
public static void main(String args[]){
Scanner reader = new Scanner(System.in);
int x=reader.nextInt();
int y=reader.nextInt();
int x2=reader.nextInt();
int y2=reader.nextInt();
Complex c1,c2,c3;
boolean c4;
c1 = new Complex(x,y);
c2 = new Complex(x2,y2);
c3 = new Complex();
System.out.println("计算二个复数的加法");
c3=Complex.addcomp(c1, c2);
c3.showcomp();
System.out.println("计算二个复数的减法");
c3=Complex.subcomp(c1, c2);
c3.showcomp();
System.out.println("计算二个复数的乘法");
c3=Complex.mulitcomp(c1, c2);
c3.showcomp();
System.out.println("判断二个复数是否相等");
c4=Complex.equalComp(c1, c2);
if(c4){
System.out.println("二个复数是相等");
}
else
System.out.println("二个复数不相等");
}
}
热心网友
时间:2022-06-21 06:50
public class Complex{
private int x;
private int y;
public Complex(){
this.x = 0;
this.y=0;
}
public int getX(){
return this.x;
}
public int getY(){
return this.y;
}
public Complex(int x,int y){
this.x = x;
this.y = y;
}
public void showComp(){
System.out.println(x+""+y+"i");
}
public Complex addComp(Complex C1,Complex C2){
return new Complex(C1.getX() + C2.getX(), C1.getY() + C2.getY());
}
public Complex subComp(Complex C1,Complex C2){
return new Complex(C1.getX() - C2.getX(), C1.getY() - C2.getY());
}
// 乘和除类似
public static void main(String[] args){
//获得测试用例的参数x,y
//获得方法,我不清楚你说的意思,我自定义一个,不是的话自己修改下
int x1=1;
int y1=2;
int x2 =5;
int y2=9;
Complex a1 = new Complex(x1,y1);
Complex a2 = new Complex(x2,y2);
Complex result1 = addComp(a1,a2);
showComp(result1);
Complex result2 = subComp(a1,a2);
showComp(result2);
//乘法除法类似
}
}