//使用程序的输入格式为:
//* 3+4i 5+8i
//运算符+n个空格+第一个复数+n个空格+第二个复数
/*
*auther starshus
*
*Date 04/11/20
*/
import java.io.BufferedReader;
import java.io.InputStreamReader;
//5.7.10
public class Command
{
private String commandLine;
public Command(String line)//构造方法,得到输入
{
commandLine = line.trim();//.trim()用来去掉两边的空格
}
public String getCommand()//得到+、-、*、/中的一个运算符
{
String cmd = "";
int index = commandLine.indexOf(' ');
if(index >= 0)
cmd = commandLine.substring(0,index);
else cmd = commandLine;
return cmd;//.toUpperCase();
}
//假设输入格式为 + a+bi c+di
//首先提取数字a
public String getA()
{
String a = "";
int index1 = commandLine.indexOf(' ');
if (index1 >= 0)
{
a=commandLine.substring(index1+1,commandLine.length()).trim();
int index2 = a.indexOf(' ');
if (index2 >=0)
a=a.substring(0,index2);
int index3 = a.indexOf('+');
if (index3 >=0)
a=a.substring(0,index3);
}else a ="";
return a;
}
//提取b
public String getB()
{
String b ="";
int index1 = commandLine.indexOf(' ');
if (index1 >= 0)
{
b=commandLine.substring(index1+1,commandLine.length()).trim();
int index2 = b.indexOf(' ');
if (index2 >=0)
b=b.substring(0,index2);
int index3 = b.indexOf('+');
int index4 = b.indexOf('i');
if ((index3+1)==index4)
{
b="1";
return b;
}
else if(index3>=0&&index4>=0)
b=b.substring(index3+1,index4);
else
b="0";
}else b="";
return b;
}
//提取c
public String getC()
{
String c ="";
int index1 = commandLine.indexOf(' ');
if (index1 >= 0)
{
c=commandLine.substring(index1+1,commandLine.length()).trim();
int index2 = c.indexOf(' ');
if (index2>=0)
c=c.substring(index2+1,c.length()).trim();
int index3= c.indexOf('+');
if(index3>=0)
c=c.substring(0,index3);
}else c="";
return c;
}
//提取d
public String getD()
{
String d="";
int index1 = commandLine.indexOf(' ');
if (index1 >= 0)
{
d=commandLine.substring(index1+1,commandLine.length()).trim();
int index2 = d.indexOf(' ');
if (index2>=0)
d=d.substring(index2+1,d.length()).trim();
int index3= d.indexOf('+');
int index4= d.indexOf('i');
if ((index3+1)==index4)
{
d="1";
return d;
}
else if(index3>=0&&index4>=0)
d=d.substring(index3+1,index4);
else
d="0";
}else d="";
return d;
}
public static String add(double a,double b,double c,double d)//加法
{
double realPart,imaginaryPart;
realPart = a+c;
imaginaryPart = b+d;
if(imaginaryPart>0)
return(realPart + "+"+imaginaryPart+"i");
else
return(realPart+""+imaginaryPart+"i");
}
public static String sub(double a,double b,double c,double d)//减法
{
double realPart,imaginaryPart;
realPart = a-c;
imaginaryPart=b-d;
if(imaginaryPart>0)
return(realPart + "+"+imaginaryPart+"i");
else
return(realPart+""+imaginaryPart+"i");
}
public static String mult(double a,double b,double c,double d)//乘法
{
double realPart,imaginaryPart;
realPart = a*c-b*d;
imaginaryPart = a*d+b*c;
if(imaginaryPart>0)
return(realPart + "+"+imaginaryPart+"i");
else
return(realPart+""+imaginaryPart+"i");
}
public static String division(double a,double b,double c,double d)//除法
{
double realPart,imaginaryPart;
realPart = (a*c+b*d)/(c*c+d*d);
imaginaryPart = (b*c-a*d)/(c*c+d*d);
if(imaginaryPart>0)
return(realPart + "+"+imaginaryPart+"i");
else
return(realPart+""+imaginaryPart+"i");
}
public static void main(String[] args)//主方法
{
System.out.println("Welcome!Press 'q' or 'Q' to quit.");
System.out.println("You can use this program like the follow example:");
System.out.println("* 20+30i 15+35i");
System.out.println("+ 20+-30i 15+-35i");
System.out.println("the first character can be '*' or '/' or '+' or '-'.");
for(;;)
{
System.out.print("$ ");
String inputLine;
try{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
inputLine = in.readLine();
}catch (Exception exc){
System.out.println("Your input can't be used.");
continue;
}
Command commander = new Command(inputLine);//用Command类生成对象
String cmd = commander.getCommand();
if (cmd.equals("q")||cmd.equals("Q"))
break;
else if(cmd.equals("+")||cmd.equals("-")||cmd.equals("*")||cmd.equals("/"))
{
double a1,b1,c1,d1;
try{
String a=commander.getA();
a1=Double.valueOf(a).doubleValue();
String b=commander.getB();
b1=Double.valueOf(b).doubleValue();
String c=commander.getC();
c1=Double.valueOf(c).doubleValue();
String d=commander.getD();
d1=Double.valueOf(d).doubleValue();
}catch(Exception exc){
System.out.println("Sorry,your input can't be used.");
System.out.println("It should be looks like : * 1+2i 3+4i");
continue;
}
if(cmd.equals("+"))
{
System.out.println("The result is: "+add(a1,b1,c1,d1));
}
else if(cmd.equals("-"))
{
System.out.println("The result is: "+sub(a1,b1,c1,d1));
}
else if(cmd.equals("*"))
{
System.out.println("The result is: "+mult(a1,b1,c1,d1));
}
else if(cmd.equals("/"))
{
System.out.println("The result is: "+division(a1,b1,c1,d1));
}
}else
System.out.println("Sorry,the first character can be '*' or '/' or '+' or '-'.");
}
}
}