设计复数库,实现基本的复数加减乘除运算。
输入时只需分别键入实部和虚部,以空格分割,两个复数之间用运算符分隔;输出时按a+bi的格式在屏幕上打印结果。参加样例输入和样例输出。
注意考虑特殊情况,无法计算时输出字符串”error”。
样例输入
2 4 * -3 2
样例输出
-14-8i
样例输入
3 -2 + -1 3
样例输出
2+1i
方法一:
方法二:
代码如下:
package ADV_120;
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner cin=new Scanner(System.in);
double a1=cin.nextDouble();
double b1=cin.nextDouble();
char str=cin.next().charAt(0);
double a2=cin.nextDouble();
double b2=cin.nextDouble();
double a=0;
double b=0;
boolean flag=true;
if(str=='+'){
a=a1+a2;
b=b1+b2;
}else if(str=='-'){
a=a1-a2;
b=b1-b2;
}else if(str=='*'){
if((a1==a2)&&(b1==-b2)){
System.out.println(a1*a1+b1*b1);
flag=false;
} else {
a=a1*a2-b1*b2;
b=b1*a2+a1*b2;
}
}else if(str=='/'){
if (a2==0&&b2==0){
System.out.println("error");//无法计算时输出字符串"error"
flag = false;
} else {
double c=a2*a2+b2*b2;
a=(a1*a2+b1*b2)/c;
b=(b1*a2-a1*b2)/c;
}
}
if(flag){
if (b<0){//虚部为负
if((a-Math.round(a))==0){//如果a=3.0
System.out.print(Math.round(a));//只输出整数部分3
}else{
System.out.print(a);////如果a=3.1,则输出3.1
}
if((b-Math.round(b))==0){
System.out.print(Math.round(b));
}else{
System.out.print(b);
}
}else{//虚部为正
if((a-Math.round(a))==0){
System.out.print(Math.round(a));
}else{
System.out.print(a);
}
System.out.print("+");
if((b-Math.round(b))==0){
System.out.print(Math.round(b));
}else{
System.out.print(b);
}
}
System.out.println("i");
}
}
}
/*
import java.util.Scanner;
public class Main {
public double real;
public double image;
public Main(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;
}
public Main() {}
public Main sub(Main m){
double r=real-m.real;
double i=image-m.image;
return new Main(r,i);
}
public Main add(Main m){
double r=real+m.real;
double i=image+m.image;
return new Main(r,i);
}
public Main mul(Main m){
double r=real*m.real-image*m.image;
double i=image*m.real+real*m.image;
return new Main(r,i);
}
public Main div(Main m){
double r=(real*m.real+image*m.image)/(m.image*m.image+m.real*m.real);
double i=(image*m.real-real*m.image)/(m.image*m.image+m.real*m.real);
return new Main(r,i);
}
public void print(char c){
if(c!='/'){
if(image>=0){
System.out.print(Math.round(real)+"+"+Math.round(image)+"i");
}else if(image<0){
System.out.print(Math.round(real)+""+Math.round(image)+"i");
}
}else{
if(image>=0){
System.out.print(real+"+"+image+"i");
}else if(image<0){
System.out.print(real+""+image+"i");
}
}
}
public static void main(String[] args) {
Scanner cin=new Scanner(System.in);
double a1=cin.nextDouble();
double b1=cin.nextDouble();
char c=cin.next().charAt(0);
double a2=cin.nextDouble();
double b2=cin.nextDouble();
if(a2==0&&b2==0){
System.out.print("error");
System.exit(0);
}
Main main1=new Main(a1,b1);
Main main2=new Main(a2,b2);
Main main3=new Main();
switch(c){
case '+':
main3=main1.add(main2);
break;
case '-':
main3=main1.sub(main2);
break;
case '*':
main3=main1.mul(main2);
break;
case '/':
main3=main1.div(main2);
break;
}
main3.print(c);
}
}
*/