java复数类源代码 编写复数类-成都快上网建站

java复数类源代码 编写复数类

JAVA编程 实现一个复数类 急!!

import java.io.*;

成都创新互联公司专注于霍山网站建设服务及定制,我们拥有丰富的企业做网站经验。 热诚为您提供霍山营销型网站建设,霍山网站制作、霍山网页设计、霍山网站官网定制、微信小程序服务,打造霍山网络公司原创品牌,更为您提供霍山网站排名全网营销落地服务。

class complex{

//复数类

public float realPart;

public float imagPart;

}

public class Main extends complex{

public static String readString(){

//从键盘读取字符串

BufferedReader br=new BufferedReader(new InputStreamReader(System.in),1);

String s1="";

try{

s1=br.readLine();

}catch(IOException ex){

System.out.println(ex);

}

return s1;

}

public static float readFloat(){

//字符串转换为浮点数

return Float.parseFloat(readString());

}

public static char plus(float r1,float r2,float i1,float i2){

//实现复数相加并输出

complex sum=new complex();

sum.imagPart=i1+i2;

sum.realPart=r1+r2;

if(sum.imagPart0){

//解决虚部为负时同时显示+ -号问题

sum.imagPart=-sum.imagPart;

System.out.println("两数相加="+sum.realPart+"-"+sum.imagPart+"i");

return 'f';

}

System.out.println("两数相加="+sum.realPart+"+"+sum.imagPart+"i");

return 'z';

}

public static char minus(float r1,float r2,float i1,float i2){

//实现复数相减并输出

complex cha=new complex();

cha.imagPart=i1-i2;

cha.realPart=r1-r2;

if(cha.imagPart0){

//解决虚部为负时同时显示+ -号问题

cha.imagPart=-cha.imagPart;

System.out.println("两数相减="+cha.realPart+"-"+cha.imagPart+"i");

return 'f';

}

System.out.println("两数相减="+cha.realPart+"+"+cha.imagPart+"i");

return 'z';

}

public static void main(String[] args) {

//主方法

complex a=new complex();

complex b=new complex();

System.out.println("输入第一个复数...");

System.out.print("\n实部=");

a.realPart=Main.readFloat();

System.out.print("虚部=");

a.imagPart=Main.readFloat();

System.out.println("\n输入第二个复数...");

System.out.print("\n实部=");

b.realPart=Main.readFloat();

System.out.print("虚部=");

b.imagPart=Main.readFloat();

System.out.println("\n");

Main.plus(a.realPart,b.realPart,a.imagPart,b.imagPart);

Main.minus(a.realPart,b.realPart,a.imagPart,b.imagPart);

}

}

以前做的 可以实现复数加减法。因为浮点数精度问题,输出可能会有点误差

用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”

import java.util.*;

public class ComplexTest{

static class ComplexNumber{

private double real,image;

public ComplexNumber(){

this(0.0,0.0);}

public ComplexNumber(double a,double b){

real=a;image=b;

}

public ComplexNumber add(ComplexNumber x){

return new ComplexNumber(real+x.real,image+x.image);}

public ComplexNumber sub(ComplexNumber x){

return new ComplexNumber(real-x.real,image-x.image); }

public ComplexNumber mul(ComplexNumber x){

return new ComplexNumber(real*x.real-image*x.image,real*x.image+image*x.real);}

public ComplexNumber div(ComplexNumber x){

if(x.real==0x.image==0){

System.out.println("无法进行除法!");

return new ComplexNumber();}

else return new ComplexNumber((real*x.real+image*x.image)/(x.real*x.real+x.image*x.image)

,(image*x.real-real*x.image)/(x.real*x.real+x.image*x.image));}

public double getReal (){return real;}

public double getImage (){return image;}

public void show(){System.out.println(this.toString());}

public String toString(){

if(image0)return ""+real+image+"i";

else return ""+real+"+"+image+"i";

}

}

static class Test{

public Test(){

Scanner sr=new Scanner(System.in);

ComplexNumber a,b,c;

try{System.out.println("请输入第一个实部和虚部:");

a=new ComplexNumber(sr.nextDouble(),sr.nextDouble());

System.out.println("请输入第二个实部和虚部:");

b=new ComplexNumber(sr.nextDouble(),sr.nextDouble());

System.out.print("第一个复数:");a.show();

System.out.print("第二个复数:");b.show();

c=a.add(b);

System.out.print("两个复数的和:");c.show();

c=a.sub(b);

System.out.print("两个复数的差:");c.show();

c=a.mul(b);

System.out.print("两个复数的积:");c.show();

c=a.div(b);

System.out.print("两个复数的商:");c.show();

}

catch(InputMismatchException e){

System.out.println("输入有误!");}

}

}

public static void main(String[] args){

new Test();

}

}

用JAVA实现复数的混合运算的源程序

public class Complex

{

private double realPart;

private double imaginaryPart;

public Complex(double a, double b)

{

this.realPart = a;

this.imaginaryPart = b;

}

public Complex add(Complex a)

{

Complex result = new Complex(this.realPart + a.realPart, this.imaginaryPart + a.imaginaryPart);//(why?)

return result;

}

public Complex decrease(Complex a)

{

Complex result = new Complex(this.realPart - a.realPart, this.imaginaryPart - a.imaginaryPart);//(why?)

return result;

}

public Complex multiply(Complex a)

{

double newReal = this.realPart*a.realPart - this.imaginaryPart * a.imaginaryPart;

double newImaginary = this.realPart*a.imaginaryPart + this.imaginaryPart * a.realPart;

Complex result = new Complex(newReal, newImaginary);

return result;

}

public Complex divide(Complex a)

{

Complex conjugate = new Complex(this.realPart, -this.imaginaryPart);

Complex multiplication = conjugate.multiply(a);

multiplication.realPart /= this.realPart*this.realPart + this.imaginaryPart * this.imaginaryPart;

multiplication.imaginaryPart /= this.realPart*this.realPart + this.imaginaryPart * this.imaginaryPart;

return multiplication;

}

public String toString()

{

String show = this.realPart + " + " + this.imaginaryPart + "i";

return show;

}

public static void main(String [] args)

{

Complex a = new Complex (2, 3);

Complex b = new Complex (1,1);

System.out.println((a.add(b)).toString());

System.out.println((a.decrease(b)).toString());

System.out.println((a.multiply(b)).toString());

System.out.println((a.divide(b)).toString());

}

}


网站名称:java复数类源代码 编写复数类
网页地址:http://kswjz.com/article/hiddpp.html
扫二维码与项目经理沟通

我们在微信上24小时期待你的声音

解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流