复数java
‘壹’ java创建一个复数类
packagetable;
publicclassComplex
{
doublereal;
doubleimaginary;
publicstaticfinalComplexZERO=newComplex(0,0);
publicstaticfinalComplexONE=newComplex(1,0);
publicstaticfinalComplexI=newComplex(0,1);
publicComplex(doublereal,doubleimaginary)
{
this.real=real;
this.imaginary=imaginary;
}
publicdoublemagnitude()
{
returnMath.sqrt(this.real*this.real+this.imaginary*this.imaginary);
}
publicComplexnegative()
{
returnnewComplex(-real,-imaginary);
}
publicdoublevalueOf()
{
returnthis.real;
}
publicComplexadd(Complexa,Complexb)
{
returnnewComplex(a.real+b.real,a.imaginary+b.imaginary);
}
publicComplexsubtract(Complexa,Complexb)
{
returnnewComplex(a.real-b.real,a.imaginary-b.imaginary);
}
publicComplexmultiply(Complexa,Complexb)
{
returnnewComplex(a.real*b.real-a.imaginary*b.imaginary,a.real*b.imaginary+a.imaginary*b.real);
}
@Override
publicStringtoString()
{
StringBuilderbuilder=newStringBuilder();
builder.append("Complex[real=").append(real).append(",imaginary=").append(imaginary).append("]");
returnbuilder.toString();
}
}
‘贰’ java里怎样表示复数啊
ic class Complex
{
private double real,im; //实部,虚部
public Complex(double real, double im) //构造方法
{
this.real = real;
this.im = im;
}
public Complex(double real) //构造方法重载
{
this(real,0);
}
public Complex()
{
this(0,0);
}
public Complex(Complex c) //拷贝构造方法
{
this(c.real,c.im);
}
public boolean equals(Complex c) //比较两个对象是否相等
{
return this.real==c.real && this.im==c.im;
}
public String toString()
{
return "("+this.real+"+"+this.im+"i)";
}
public void add(Complex c) //两个对象相加
{ //改变当前对象,没有返回新对象
this.real += c.real;
this.im += c.im;
}
public Complex plus(Complex c) //两个对象相加,与add()方法参数一样不能重载
{ //返回新创建对象,没有改变当前对象
return new Complex(this.real+c.real, this.im+c.im);
}
public void subtract(Complex c) //两个对象相减
{ //改变当前对象,没有返回新对象
this.real -= c.real;
this.im -= c.im;
}
public Complex minus(Complex c) //两个对象相减,与subtract()方法参数一样不能重载
{ //返回新创建的对象,没有改变当前对象
return new Complex(this.real-c.real, this.im-c.im);
}
}
class Complex__ex
{
public static void main(String args[])
{
Complex a = new Complex(1,2);
Complex b = new Complex(3,5);
Complex c = a.plus(b); //返回新创建对象
System.out.println(a+" + "+b+" = "+c);
}
}
/*
程序运行结果如下:
(1.0+2.0i) + (3.0+5.0i) = (40.0+7.0i)
*/
‘叁’ 用java 编写一个复数类
public class Complex {
protected int a;
protected int b;
public Complex(int a, int b) {
this.a = a;
this.b = b;
}
public String toString() {
return this.a + " + " + this.b + "i";
}
public static Complex addition(Complex complex1, Complex complex2) {
int a = complex1.a + complex2.a;
int b = complex1.b + complex2.b;
return new Complex(a, b);
}
public static Complex subtract(Complex complex1, Complex complex2) {
int a = complex1.a - complex2.a;
int b = complex1.b - complex2.b;
return new Complex(a, b);
}
public static Complex multiplication(Complex complex1, Complex complex2) {
int a = complex1.a * complex2.a - complex1.b * complex2.b;
int b = complex1.b * complex2.a + complex1.a * complex2.b;
return new Complex(a, b);
}
public static Complex division(Complex complex1, Complex complex2) throws Exception {
if (complex2.a == 0) {
throw new Exception("complex2.a is 0");
}
if (complex2.b == 0) {
throw new Exception("complex2.b is 0");
}
int a = (complex1.a * complex2.a + complex1.b * complex2.b) / (complex2.a * complex2.a + complex2.b * complex2.b);
int b = (complex1.b * complex2.a - complex1.a * complex2.b) / (complex2.a * complex2.a + complex2.b * complex2.b);
return new Complex(a, b);
}
}
//测试用的类
public class ComplexTest {
public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub
Complex complex1 = new Complex(1, 2);
Complex complex2 = new Complex(3, 4);
System.out.println(complex1 + " + " + complex2 + " = " + Complex.addition(complex1, complex2));
System.out.println(complex1 + " - " + complex2 + " = " + Complex.subtract(complex1, complex2));
System.out.println(complex1 + " x " + complex2 + " = " + Complex.multiplication(complex1, complex2));
System.out.println(complex1 + " / " + complex2 + " = " + Complex.division(complex1, complex2));
}
}
‘肆’ 在java中复数的结构包括实部和虚部什么是实部什么是虚部
复数是形如
a+bi
的数,
其中
a,
b
是实数,a称为实部,b称为虚部,i是虚数单位,
i^2
=
-1.
实数可以用一条直线(数轴)上的点表示,
类似地,
复数可以用一个平面(称为复平面)上的点表示.
在复平面上,a+bi
用点Z(a,b)
表示。Z与原点的距离r称为Z的模|Z|=√(a^2+b^2)
复数a+bi,b=0为实数,b不等于0为虚数,
a=0且b不等于0时为纯虚数。
复数的三角形式是
Z=r(cosx+isinx)
中x,r是实数,rcosx称为实部,rsinx称为虚部,i是虚数单位。Z与原点的距离r称为Z的模,x称为辐角。
‘伍’ java 定义一个复数类
//余下的自己完成
import java.util.Scanner;
public class ComplexOperation {
static Scanner s = new Scanner(System.in);
public Complex option(Complex c1, Complex c2, String opch) {
Complex r = new Complex();
if("+".equals(opch)) {
r.setReaPart(c1.getReaPart() + c2.getReaPart());
r.setVirPart(c1.getVirPart() + c2.getVirPart());
} else if("-".equals(opch)) {
r.setReaPart(c1.getReaPart() - c2.getReaPart());
r.setVirPart(c1.getVirPart() - c2.getVirPart());
}
return r;
}
public Complex read(String info) {
System.out.println(info);
Complex c = new Complex();
System.out.print("实部: ");
c.setReaPart(s.nextInt());
System.out.print("虚部: ");
c.setVirPart(s.nextInt());
return c;
}
public static void main(String[] args) {
// ComplexOperation co = new ComplexOperation();
// Complex c1 = co.read("输入复数一");
// Complex c2 = co.read("输入复数二");
// System.out.print("输入运算符: ");
// String opch = s.next();
// System.out.print("结果是: " + co.option(c1, c2, opch));
// double d = 2.36;
// int len = 1;
// String format = "%" + len + ".2f";
// System.out.printf(format, d);
}
}
class Complex{
private int reaPart;
private int virPart;
public Complex() {
}
public Complex(int r, int v) {
this.reaPart = r;
this.virPart = v;
}
public String toString() {
int tag = this.getVirPart();
if(tag == 0) {
return getReaPart() + "";
} else if(tag > 0) {
return getReaPart() + "+" + getVirPart() + "i";
} else {
return getReaPart() + "-" + -getVirPart() + "i";
}
}
public int getReaPart() {
return reaPart;
}
public void setReaPart(int reaPart) {
this.reaPart = reaPart;
}
public int getVirPart() {
return virPart;
}
public void setVirPart(int virPart) {
this.virPart = virPart;
}
}
‘陆’ java 中实现复数的加减
(1):具体代码(附注释)
复数类:
publicclassComplex{
privatefloatshibu;
privatefloatxubu;
Complex()
{this(0,0);
}
Complex(floatshibu,floatxubu){
this.shibu=shibu;
this.xubu=xubu;
}
publicvoidAdd(Complexp)
{
Complexresult=newComplex();
result.shibu=this.shibu+p.shibu;
result.xubu=this.xubu+p.xubu;
System.out.print("加法结果为:"+result.shibu+"+"+result.xubu+"i");
}
publicvoidSub(Complexp)
{
Complexresult=newComplex();
result.shibu=this.shibu-p.shibu;
result.xubu=this.xubu-p.xubu;
System.out.print("加法结果为:"+result.shibu+"+"+result.xubu+"i");
}
publicvoidMul(Complexp)
{
Complexresult=newComplex();
result.shibu=this.shibu*p.shibu-this.xubu*p.xubu;
result.xubu=this.shibu*p.xubu+p.shibu*this.xubu;
System.out.print("乘法结果为:"+result.shibu+"+"+result.xubu+"i");
}
publicstaticvoidmain(String[]args){
Complexfushu1=newComplex(1,2);
Complexfushu2=newComplex(3,4);
fushu1.Add(fushu2);
fushu1.Sub(fushu2);
fushu1.Mul(fushu2);
}
}
(2):提供一个例子:
源代码:
importjava.io.*;
publicclassBook{
doublesb;
doublexb;
Book(doublex,doubley){
this.sb=x;
this.xb=y;
}
Book(){
}
publicstaticvoidmain(Stringargs[]){
System.out.println("请输入数据:");
doublea=0;
doubleb=0;
doublec=0;
doubled=0;
Strings;
BufferedReaderin=newBufferedReader(newInputStreamReader(System.in));
System.out.println("请输入第一个复述的实部:");
try{
s=in.readLine();
a=Double.parseDouble(s);
}
catch(IOExceptione)
{System.out.println("抛掷异常");}
System.out.println("请输入第一个复述的虚部:");
try{
s=in.readLine();
b=Double.parseDouble(s);
}
catch(IOExceptione)
{System.out.println("抛掷异常");}
System.out.println("请输入第二个复述的实部:");
try{
s=in.readLine();
c=Double.parseDouble(s);
}
catch(IOExceptione)
{System.out.println("抛掷异常");}
System.out.println("请输入第二个复述的虚部:");
try{
s=in.readLine();
d=Double.parseDouble(s);
}
catch(IOExceptione)
{System.out.println("抛掷异常");}
Bookh;
h=newBook(a,b);
Bookj;
j=newBook(c,d);
System.out.println("您输入的一个数为:");
toString(h);
System.out.println("您输入的二个数为:");
toString(j);
Bookk;
k=newBook();
charz='y';
do{
System.out.println("请选择您要进行的计算:");
System.out.println("1:进行加法运算");
System.out.println("2:进行减法运算");
System.out.println("3:进行修改");
System.out.println("4:进行乘法运算");
System.out.println("5:进行除法运算");
System.out.println("6:查看修改结果");
inti=0;
try{
i=Integer.parseInt(in.readLine());
}
catch(IOExceptione)
{System.out.println("抛掷异常");}
switch(i)
{
case1:
k.sb=jia(h.sb,j.sb);
k.xb=jia(h.xb,j.xb);
System.out.println("计算结果的实部为:"+k.sb);
System.out.println("计算结果的虚部为:"+k.xb);
toString(k);
break;
case2:
k.sb=jian(h.sb,j.sb);
k.xb=jian(h.xb,j.xb);
System.out.println("计算结果的实部为:"+k.sb);
System.out.println("计算结果的虚部为:"+k.xb);
toString(k);
break;
case3:
System.out.println("请输入您要修改哪个实数:");
intl=0;
try{
l=Integer.parseInt(in.readLine());
}
catch(IOExceptione)
{System.out.println("抛掷异常");}
if(l==1)
{
h.xiugais(h);
h.xiugaix(h);
}
else
{
xiugais(j);
xiugaix(j);
}
break;
case4:
doublef=0;
doublee=0;
f=cheng(h.sb,j.sb)+cheng(h.xb,j.xb);
e=cheng(h.sb,j.xb)+cheng(h.xb,j.sb);
k.sb=(double)(Math.round(f*100)/100.0);
k.xb=(double)(Math.round(e*100)/100.0);
System.out.println("计算结果的实部为:"+k.sb);
System.out.println("计算结果的虚部为:"+k.xb);
toString(k);
break;
case5:
doublechushu=cheng(j.sb,j.sb)-cheng(j.xb,-j.xb);
doublebeichushus=jian(cheng(h.sb,j.sb),cheng(h.xb,-j.xb));
doublebeichushux=jia(cheng(h.sb,-j.xb),cheng(h.xb,j.sb));
k.sb=chu(beichushus,chushu);
k.xb=chu(beichushux,chushu);
System.out.println("计算结果的实部为:"+k.sb);
System.out.println("计算结果的虚部为:"+k.xb);
toString(k);
break;
case6:
System.out.println("修改后的结果为:");
System.out.println("第一个复数:"+toString(h));
System.out.println("第二个复数:"+toString(j));
break;
}
System.out.println("请问您是否还要继续y/n:");
try{
z=(char)System.in.read();
System.in.skip(2);//忽略回车换行
}
catch(IOExceptione){}
}while(z=='y');
}
publicstaticdoublegets(Booka){
returna.sb;
}
publicstaticdoublegetx(Bookb){
returnb.xb;
}
publicstaticdoublexiugais(Booka)
{
BufferedReaderin=newBufferedReader(newInputStreamReader(System.in));
System.out.println("请输入您要修改的实部:");
doublem=0;
try{
m=Double.parseDouble(in.readLine());
}
catch(IOExceptione)
{System.out.println("抛掷异常");}
a.sb=m;
System.out.println("修改成功:");
return0;
}
publicstaticdoublexiugaix(Bookb)
{
BufferedReaderin=newBufferedReader(newInputStreamReader(System.in));
System.out.println("请输入您要修改的虚部:");
doublen=0;
try{
n=Double.parseDouble(in.readLine());
}
catch(IOExceptione)
{System.out.println("抛掷异常");}
b.xb=n;
System.out.println("修改成功:");
return0;
}
publicstaticdoublejia(doublea,doubleb)//
{
doublec=0;
c=a+b;
System.out.println("加法成功:");
returnc;
}
publicstaticdoublejian(doublea,doubleb)
{
doublec=0;
c=a-b;
System.out.println("减法成功:");
returnc;
}
publicstaticdoublecheng(doublea,doubleb)
{
doublec=0;
c=a*b;
System.out.println("乘法成功:");
returnc;
}
publicstaticdoublechu(doublea,doubleb)
{
doubled=0;
doublec=0;
d=a/b;
c=(double)(Math.round(d*100)/100.0);
System.out.println("除法成功:");
returnc;
}
publicstaticdoubletoString(Booka){
System.out.println("结果为:"+a.sb+"+"+a.xb+"*i");
return0;
}
}
(3)测试结果截图:
‘柒’ java中怎么表示复数
public class Complex { /**成员属性定义实部和虚部 *@param real_part 实部 *@param imagin_part 虚部 */ private double real_part; private double imagin_part; //构造方法,初始化 public Complex() { this(0.0,0.0); } //构造方法的重载
‘捌’ java编写复数
public class Complex {
/**成员属性定义实部和虚部
*@param real_part 实部
*@param imagin_part 虚部
*/
private double real_part;
private double imagin_part;
//构造方法,初始化
public Complex() {
this(0.0,0.0);
}
//构造方法的重载
public Complex(double real_part, double imagin_part) {
this.real_part = real_part;
this.imagin_part = imagin_part;
}
//取得实部
public void getReal_part(double real_part) {
this.real_part = real_part;
}
//取得虚部
public void getImagin_part() {
this.imagin_part = imagin_part;
}
//复数自加
public Complex addComplex(Complex c) {
this.real_part += c.real_part;
this.imagin_part += c.imagin_part;
return this;
}
//复数自减
public Complex decComplex(Complex c) {
this.real_part -= c.real_part;
this.imagin_part -= c.imagin_part;
return this;
}
//定义输出格式
public String toString() {
//按照虚部正负,不同显示
if(this.imagin_part >=0)
return this.real_part + "+" + this.imagin_part + "i";
return this.real_part + "-" + this.imagin_part + "i";
}
}
//测试类
class TestComplex {
public static void main(String[] args) {
//一个c1复数对象
Complex c1 = new Complex(1.0,1.0);
//输出自加结果
c1.addComplex(c1);
System.out.println(c1.toString());
//输出自减结果
c1.decComplex(c1);
System.out.println(c1.toString());
}
}
‘玖’ 用JAVA定义个复数类
public class Complex {
private double x;//实部
private double y;//虚部
public Complex(){}
/**构造函数
* @param x 实数部分
* @param y 虚数部分
*/
public Complex(double x,double y){
super();
this.x = x;
this.y = y;
}
/**求模
* @return 该复数的模
*/
public double mod(){
return x * x + y * y;
}
/**复数间加法
* @param complex 加数
* @return 计算结果
*/
public Complex add(Complex complex){
double x = this.x + complex.x;
double y = this.y + complex.y;
return new Complex(x,y);
}
/**复数与实数的加法
* @param a 加数
* @return 计算结果
*/
public Complex add(double a){
return this.add(new Complex(a,0));
}
/**复数间减法
* @param complex 减数
* @return 计算结果
*/
public Complex subtract(Complex complex){
double x = this.x - complex.x;
double y = this.y - complex.y;
return new Complex(x,y);
}
/**复数与实数的减法
* @param a 减数
* @return 计算结果
*/
public Complex subtract(double a){
return subtract(new Complex(a,0));
}
/**复数间乘法
* @param complex 乘数
* @return 计算结果
*/
public Complex multiply(Complex complex){
double x = this.x * complex.x - this.y * complex.y;
double y = this.y * complex.x + this.x * complex.y;
return new Complex(x,y);
}
/**复数间除法
* @param complex 除数
* @return 计算结果
*/
public Complex divide(Complex complex){
double x = (this.x * complex.x + this.y * complex.y) / (complex.mod());
double y = (this.y * complex.x - this.x * complex.y) / (complex.mod());
return new Complex(x,y);
}
public String toString(){
StringBuffer sb = new StringBuffer();
if(x != 0){
sb.append(x);
if(y > 0){
sb.append("+" + y + "i");
}else if(y < 0){
sb.append(y + "i");
}
}else{
if(y != 0){
sb.append(y + "i");
}
}
if(x == 0 && y == 0){
return "0";
}
return sb.toString();
}
public double getX() {
return x;
}
public void setX(double x) {
this.x = x;
}
public double getY() {
return y;
}
public void setY(double y) {
this.y = y;
}
public static void main(String[] args) {
Complex a = new Complex(2,0.5);
Complex b = new Complex(0.5,2);
System.out.println("(" + a + ")+(" + b + ")=" + a.add(b));
System.out.println("(" + a + ")+" + 2 + "=" + a.add(2));
System.out.println("(" + a + ")-(" + b + ")=" + a.subtract(b));
System.out.println("(" + a + ")-" + 2 + "=" + a.subtract(2));
System.out.println("(" + a + ")*(" + b + ")=" + a.multiply(b));
System.out.println("(" + a + ")/(" + b + ")=" + a.divide(b));
}
}