复数的运算(类和对象)

时间:2021-05-18 15:37:44

Problem Description
设计一个类Complex,用于封装对复数的下列操作:
成员变量:实部real,虚部image,均为整数变量;
构造方法:无参构造方法、有参构造方法(参数2个)
成员方法:含两个复数的加、减、乘操作。
复数相加举例: (1+2i)+(3+4i)= 4 + 6i
复数相减举例: (1+2i)-(3+4i)= -2 - 2i
复数相乘举例: (1+2i)*(3+4i)= -5 + 10i
要求:对复数进行连环运算。
Input
输入有多行。
第一行有两个整数,代表复数X的实部和虚部。
后续各行的第一个和第二个数表示复数Y的实部和虚部,第三个数表示操作符op: 1——复数X和Y相加;2——复数X和Y相减;3——复数X和Y相乘。

Output
计算数据输出其简化复数形式,如:-2-2i、-4、-3i、1+2i、0。

Sample Input
1 1
3 4 2
5 2 1
2 -1 3
0 2 2
Sample Output
5-7i
Hint
输入与输出形式示例:
如果输入:
2 3
-2 1 1
则输出:4i
如果输入:
1 2
-1 -2 1
则输出:0

复数的输出形式示例:
实部 虚部 输出形式
0 0 0
-4 0 -4
0 4 4i
3 2 3+2i
3 -2 3-2i

package pp;
import java.util.*;
class Fs{
int x;
int y;
public Fs(int x, int y) {
super();
this.x = x;
this.y = y;
}
public Fs add(Fs a) {
x += a.x;
y += a.y;
return new Fs(x, y);
}
public Fs jian(Fs a) {
x -= a.x;
y -= a.y;
return new Fs(x, y);
}
public Fs cheng(Fs a) {
int m = x*a.x - y*a.y;
int n = x*a.y + y*a.x;
return new Fs(m, n);
}

/* public String show() {
if(x != 0 && y == 1) {
return x + “+i”;
}
else if(x != 0 && y == -1) {
return x + “-i”;
}
else if(x != 0 && y > 0) {
return x + “+” + y +”i”;
}
else if(x!= 0 && y < 0){//
return x + y + “i”;
}

    else if(x == 0 && y == -1) {
        return "-i";
    }
    else if(x == 0 && y == 1) {
        return "i";
    }

    else if(x != 0 && y == 0) {
        return x +"";//将整数变为字符串
    }
    else if(x ==0 && y == 0) {
        return "0";
    }
    return null;
}*/

}
public class Main {

public static void main(String[] args){
    Scanner sc = new Scanner(System.in);
    Fs a = new Fs(sc.nextInt(), sc.nextInt());//连环运算
    while(sc.hasNext()) {
        Fs b = new Fs(sc.nextInt(), sc.nextInt());
        int opo = sc.nextInt();
        if(opo == 1) {
            a = a.add(b);
        }
        else if(opo == 2) {
            a = a.jian(b);
        }
        else if(opo == 3) {
            a = a.cheng(b);
        }

    }
    /*String s = a.show();
    System.out.println(s);*/
     if ( a.x == 0 && a.y == 1 ){  
            System.out.println("i");  
        }  
        else if ( a.x == 0 && a.y == -1 ){  
            System.out.println("-i");  
        }  
        else if ( a.x == 0 && a.y != 0 ){  
            System.out.println(a.y+"i");  
        }  
        else if ( a.x == 0 && a.y == 0 ){  
            System.out.println("0");  
        }  
        else if ( a.x != 0&&a.y == 0 ){  
            System.out.println(a.x);  
        }  
        else if ( a.x != 0 && a.y == 1 ){  
            System.out.println(a.x+"+i");  
        }  
        else if ( a.x != 0 && a.y == -1 ){  
            System.out.println(a.x+"-i");  
        }  
        else if ( a.x != 0 && a.y > 0 ){  
            System.out.println(a.x+"+"+a.y+"i");  
        }  
        else if ( a.x != 0 && a.y < 0 ){  
            System.out.println(a.x+""+a.y+"i");  
        }  
    sc.close(); 
}

}

1:  
完全不知道哪里错。。。。
package pp;
import java.util.*;
class Fs{
    int x;
    int y;
    public Fs(int x, int y) {
        super();
        this.x = x;
        this.y = y;
    }
    public Fs add(Fs a) {
        x += a.x;
        y += a.y;
        return new Fs(x, y);
    }
    public Fs jian(Fs a) {
        x -= a.x;
        y -= a.y;
        return new Fs(x, y);
    }
    public Fs cheng(Fs a) {
        int m = x*a.x - y*a.y;
        int n = x*a.y + y*a.x;
        return new Fs(m, n);
    }

    public String show() {
        if(x != 0 && y == 1) {
            return x + "+i";
        }
        else if(x != 0 && y == -1) {
            return x + "-i";
        }
        else if(x != 0 && y > 0) {
            return x + "+" + y +"i";
        }
        else if(x!= 0 && y < 0){//
            return x + y + "i";
        }

        else if(x == 0 && y == -1) {
            return "-i";
        }
        else if(x == 0 && y == 1) {
            return "i";
        }

        else if(x != 0 && y == 0) {
            return x +"";//将整数变为字符串
        }
        else if(x ==0 && y == 0) {
            return "0";
        }
        return null;
    }
}
public class Main {

    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        Fs a = new Fs(sc.nextInt(), sc.nextInt());//连环运算
        while(sc.hasNext()) {
            Fs b = new Fs(sc.nextInt(), sc.nextInt());
            int opo = sc.nextInt();
            if(opo == 1) {
                a = a.add(b);
            }
            else if(opo == 2) {
                a = a.jian(b);
            }
            else if(opo == 3) {
                a = a.cheng(b);
            }

        }
        String s = a.show();
        System.out.println(s);
        sc.close(); 
    }
}