//import java.awt.*;
import java.util.*;
//import java.lang.Object;
class ComplexNumber{
private double m_dRealPart;
private double m_dImaginPart;
ComplexNumber(){}
ComplexNumber(double r,double i){
m_dRealPart=r;
m_dImaginPart=i;
}
double getRealPart(){
return m_dRealPart;
}
double getImaginPart(){
return m_dImaginPart;
}
public void setRealPart(double d){
m_dRealPart=d;
}
public void setImaginPart(double d){
m_dImaginPart=d;
}
public void complexAdd(ComplexNumber c){
this.m_dRealPart+=c.m_dRealPart;
this.m_dImaginPart+=c.m_dImaginPart;
}
public void complexAdd(double c){
this.m_dRealPart+=c;
//this.m_dImaginPart=this.m_dImaginPart;
}
public void complexMinus(ComplexNumber c){
this.m_dRealPart=this.m_dRealPart-c.m_dRealPart;
this.m_dImaginPart=this.m_dImaginPart-c.m_dImaginPart;
}
public void complexMinus(double c){
this.m_dRealPart=this.m_dRealPart-c;
//this.m_dImaginPart=this.m_dImaginPart;
}
public void complexMulti(ComplexNumber c){
//ComplexNumber b;
double Newm_dRealPart = (c.m_dRealPart*this.m_dRealPart)-(c.m_dImaginPart*this.m_dImaginPart);
this.m_dImaginPart = (c.m_dRealPart*this.m_dImaginPart)+(c.m_dImaginPart*this.m_dRealPart);
this.m_dRealPart=Newm_dRealPart;
}
public void complexMulti(double c){
//ComplexNumber b;
this.m_dRealPart = this.m_dRealPart*c;
this.m_dImaginPart=this.m_dImaginPart*c;
}
public void ToString(){
String p=null;
if(this.m_dRealPart==0)
p=this.m_dImaginPart+"i";
else{
if(this.m_dImaginPart>0)
p=this.m_dRealPart+"+"+this.m_dImaginPart+"i";
if(this.m_dImaginPart==0)
p=Double.toString(this.m_dRealPart);
if(this.m_dImaginPart<0){
p=this.m_dImaginPart+"i";
p=this.m_dRealPart+p;}
}
System.out.println(p);
}
}
public class Plural{
public static void main(String args[]){
Scanner reader=new Scanner(System.in);
System.out.print("输入第一个复数的实部:");
double a=reader.nextDouble();
System.out.print("输入第一个复数的虚部:");
double b=reader.nextDouble();
System.out.print("输入第二个复数的实部:");
double d=reader.nextDouble();
System.out.print("输入第二个复数的虚部:");
double e=reader.nextDouble();
ComplexNumber n=new ComplexNumber(a,b);
System.out.println("第一个复数的实部:"+n.getRealPart()+" "+"第一个复数的虚部:"+n.getImaginPart());
//n.getRealPart();
//n.getImaginPart();
ComplexNumber m=new ComplexNumber();
System.out.println("选择要进行的复数四则运算:");
System.out.println("1.1——复数间的加法,1.2——复数与实数间的加法;");
System.out.println("2.1——复数间的减法,1.2——复数与实数间的减法;");
System.out.println("3.1——复数间的乘法,3.2——复数与实数间的乘法;");
System.out.println("0——结束符号。");
int tag=1;
while(true){
//Scanner tag=new Scanner(System.in);
m.setRealPart(d);
m.setImaginPart(e);
if(tag==1){
System.out.print("第一个复数:");
n.ToString();
System.out.print("第二个复数:");
m.ToString();
tag=0;}
double t=reader.nextDouble();
if(t!=1.1&&t!=1.2&&t!=2.1&&t!=2.2&&t!=3.1&&t!=3.2&&t!=0){
System.out.println("输入错误,请重新选择运算:");
continue;}
if(t==1.1){
m.complexAdd(n);
System.out.print("复数间的加法结果:");
m.ToString();continue;}
if(t==1.2){
System.out.print("输入实数:");
double c=reader.nextDouble();
m.complexAdd(c);
System.out.print("第二个复数与实数间的加法结果:");
m.ToString();continue;}
if(t==2.1){
m.complexMinus(n);
System.out.print("复数间的减法结果:");
m.ToString();continue;}
if(t==2.2){
System.out.print("输入实数:");
double c=reader.nextDouble();
m.complexMinus(c);
System.out.print("第二个复数与实数间的减法结果:");
m.ToString();continue;}
if(t==3.1){
m.complexMulti(n);
System.out.println("复数间的乘法结果:");
m.ToString();continue;}
if(t==3.2){
System.out.print("输入实数:");
double c=reader.nextDouble();
m.complexMulti(c);
System.out.print("第二个复数与实数间的乘法结果:");
m.ToString();continue;}
if(t==0)
break;
}
}
}