没时间更优化了,(终极版)声明复数类,成员变量包括实部和虚部,成员方法包括实现由字符串构造复数、复数加法、减法,字符串描述、比较
实名感谢尚梦川大佬教会我正则表达式!!!!!!!!!!!!!!!!!!!!!!!!!!
输入的必须是正确答案,主要是尝试正则表达式。 看不懂的童邪慎用。优化也不是特别好,但是看懂了比较舒服。
有错误欢迎指正
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Complextest {
public float real,vir;
public static int count=0;
public static String Input()
{
Scanner in = new Scanner(System.in);
count++;
System.out.println("请输入第"+count+"个复数:");
String res= in.next();
return res;
}
public Complextest (String str)
{
if(str.equals("i")||str.equals("+i"))
{
this.real=0;
this.vir =1;
}
else if(str.equals("-i"))
{
this.real=0;
this.vir =-1;
}
else{
String a = "^([-+]?)(\\d+|\\d+\\.\\d+)$";//只有实部的匹配
String bi = "^([-+]?)(\\d+|\\d+\\.\\d+)?(i+)$";//只虚部的匹配
String a_bi = "^([-+]?)(\\d+|\\d+\\.\\d+)([-+]+)(\\d+|\\d+\\.\\d+)(i+)$";//都有
String a_i = "^([-+]?)(\\d+|\\d+\\.\\d+)([-+]+)(i+)$";
Pattern fora = Pattern.compile(a);
Pattern forbi = Pattern.compile(bi);
Pattern fora_bi = Pattern.compile(a_bi);
Pattern fora_i = Pattern.compile(a_i);
Matcher toa= fora.matcher(str);
Matcher tobi= forbi.matcher(str);
Matcher toa_bi= fora_bi.matcher(str);
Matcher toa_i= fora_i.matcher(str);
if(toa_i.find())//([-+]?)(\\\\d+|\\\\d+\\\\.\\\\d+)([-+]+)(i+)
{
this.real = Float.parseFloat(toa_i.group(1)+toa_i.group(2));
this.vir = 1;
if(toa_i.group(3).equals("-"))
{
this.vir*=-1;
}
}
if(toa.find())//只匹配实部
{
this.real = Float.parseFloat(toa.group(0));
this.vir = 0;
}else if(tobi.find())//只匹配虚部"([-+]?)(\\d+|\\d+\\.\\d+)(i+)";
{
this.real=0;
this.vir=Float.parseFloat(tobi.group(1)+tobi.group(2));
}
if(toa_bi.find())//([-+]?)(\\d+|\\d+\\.\\d+)([-+]+)(\\d+|\\d+\\.\\d+)(i+)
{
this.real = Float.parseFloat(toa_bi.group(1)+toa_bi.group(2));
if(toa_bi.group(4).length()==0)
{
this.vir=1;
if(toa_bi.group(3)=="+")
{
this.vir*=-1;
}
}else {
this.vir=Float.parseFloat(toa_bi.group(3)+toa_bi.group(4));
}
}
}
System.out.print("你输入的复数:");
sayResult(this.real,this.vir);
}
public void add(Complextest other)//加法
{
float sum_Real,sum_Vir;
sum_Real=this.real+other.real;
sum_Vir=this.vir+other.vir;
System.out.print("和:");
sayResult(sum_Real,sum_Vir);
}
public void lenth(Complextest other)//长度
{
float x_len,y_len;
x_len=this.real-other.real;
y_len=this.vir-other.vir;
System.out.println("长度是"+String.format("%.2f", Math.sqrt(x_len*x_len+y_len*y_len)));
}
public boolean equls(Complextest other)
{
System.out.print("是否相等:");
return this==other||Math.abs(this.real-other.real)<0.001&&Math.abs(this.vir-other.vir)<0.001;
}
public void diff(Complextest other)//减法
{
float diff_Real,diff_Vir;
diff_Real=this.real-other.real;
diff_Vir=this.vir-other.vir;
System.out.print("差:");
sayResult(diff_Real,diff_Vir);
}
public void sayResult(float Real_,float Vir_)
{
StringBuffer put = new StringBuffer();
String str=String.valueOf(Real_);
if(Real_!=0)//实部存在
{
put.append(str);
if(Vir_!=0)//等于0就不管
{
if(Vir_>0)
{
if(Vir_==1)//虚部为1
{
put.append("+i");
}else {
put.append("+"+Vir_+"i");
}
} else if(Vir_<0)
{
if(Vir_==-1)//虚部为-1
{
put.append("-i");
}else {
put.append(Vir_+"i");
}
}
}
}else//不存在实部
{
if(Vir_!=0)//只有虚部
{
if(Vir_==-1)
{
put.append("-i");
}else if(Vir_==1)
{
put.append("i");
}else
{
put.append(Vir_+"i");
}
}else//虚部都没有
{
put.append("0");
}
}
System.out.println(put);
}
public static void main(String[] args) {
String str;
str = Input();
Complextest first = new Complextest(str);
str = Input();
Complextest second = new Complextest(str);
first.add(second);
first.diff(second);
first.lenth(second);
System.out.println(first.equls(second));
}
}
- 点赞
- 收藏
- 关注作者
评论(0)