2 Star 0 Fork 0

20155207wxc/besti-java-20155207wxc

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
complex.java 1.45 KB
一键复制 编辑 原始数据 按行查看 历史
lnaswxc 提交于 2017-06-07 09:22 . 第十六周课上
import static java.lang.Math.*;
/**
* Created by suhuang on 17-4-19.
*/
public class complex {
public static void main (String[] args) {
Complex b = new Complex(2,5);
Complex c = new Complex(3,-4);
System.out.println(b + "+" + c + "=" + b.ComplexAdd(c));
System.out.println(b + "-" + c + "=" + b.ComplexSub(c));
System.out.println(b + "*" + c + "=" + b.ComplexMulit(c));
System.out.println(b + "/" + c + "=" + b.ComplexDiv(c));
}
}
class Complex {
private double R;
private double I;
public Complex (double R, double I){
this.R=R;
this.I=I;
}
public Complex ComplexAdd(Complex a){
return new Complex(R + a.R, I + a.I);
}
public Complex ComplexSub(Complex a){
return new Complex (R - a.R, I - a.I);
}
public Complex ComplexMulit(Complex a){
return new Complex (R*a.R-I*a.I, R*a.I + I*a.R);
}
public Complex ComplexDiv(Complex a){
double d = sqrt(a.R*a.R)+ sqrt(a.I*a.I);
double e = (R * a.I - I * a.R);
return new Complex((R * a.R + I * a.I) / d, round(e / d));
}
public String toString(){
String rtr_str ="";
if(I>0)
rtr_str = "("+R+"+"+I+"i"+")";
if (I==0) {
rtr_str = "(" + R + ")";
}
if (I<0){
rtr_str = "(" + R + I + "i" + ")";
}
return rtr_str;
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Java
1
https://gitee.com/lnaswxc/besti-java-20155207wxc.git
git@gitee.com:lnaswxc/besti-java-20155207wxc.git
lnaswxc
besti-java-20155207wxc
besti-java-20155207wxc
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385