//主要是对零的处理,有什么不对的地方欢迎批评指正,一起进步
class complex{
double a,b;
public String toString() {
return("实部:"+a+"虚部:"+b);
}
void add(complex x,complex y) {
double m,n;
m = x.a+y.a;
n = x.b+y.b;
print(m,n);
}
void mult(complex x,complex y) {
double m,n;
n = x.b*y.a+x.a*y.b;
m =x.a*y.a-x.b*y.b;
print(m,n);
}
void dep(complex x,complex y) {
double n = (x.a*y.a-x.b*y.b)/(y.a*y.a+y.b*y.b);
double m = (x.b*y.a+x.a*y.b)/(y.a*y.a+y.b*y.b);
if(y.a==0&&y.b==0)
System.out.println("除法无意义
");
else
{
print(m,n);
}
}
void print(double m,double n) {
if(m!=0)
{
if(n>0) {
System.out.println(m+"+");
System.out.println(n+"i");
}
else if(n<0) {
System.out.print(m);
System.out.println(n+"i");
}
else
System.out.println(m);
}
else {
if(n!=0) {
System.out.println(n+"i");
}
else
System.out.println(m);
}
}
}
public class Javacomplex {
public static void main(String[] args) {
// TODO Auto-generated method stub
complex n1,n2,n;
n1 = new complex();
n2 = new complex();
n = new complex();
n1.a = 1;n1.b=2;
n2.a = -1;n2.b = 1;
System.out.println(n1.toString());
System.out.println(n2.toString());
n.add(n1, n2);
n.mult(n1, n2);
n.dep(n1, n2);
}
}