java代码复数加减乘除,复数的加法运算java
java 中实现复数的加减
(1):具体代码(附注释)
创新互联建站服务项目包括望城网站建设、望城网站制作、望城网页制作以及望城网络营销策划等。多年来,我们专注于互联网行业,利用自身积累的技术优势、行业经验、深度合作伙伴关系等,向广大中小型企业、政府机构等提供互联网行业的解决方案,望城网站推广取得了明显的社会效益与经济效益。目前,我们服务的客户以成都为中心已经辐射到望城省份的部分城市,未来相信会继续扩大服务区域并继续获得客户的支持与信任!
复数类:
public class Complex {
private float shibu;
private float xubu;
Complex()
{this(0,0);
}
Complex(float shibu,float xubu){
this.shibu=shibu;
this.xubu=xubu;
}
public void Add(Complex p)
{
Complex result=new Complex();
result.shibu=this.shibu+p.shibu;
result.xubu=this.xubu+p.xubu;
System.out.print("加法结果为:"+result.shibu+"+"+result.xubu+"i");
}
public void Sub(Complex p)
{
Complex result=new Complex();
result.shibu=this.shibu-p.shibu;
result.xubu=this.xubu-p.xubu;
System.out.print("加法结果为:"+result.shibu+"+"+result.xubu+"i");
}
public void Mul(Complex p)
{
Complex result=new Complex();
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");
}
public static void main(String[] args) {
Complex fushu1=new Complex(1,2);
Complex fushu2=new Complex(3,4);
fushu1.Add(fushu2);
fushu1.Sub(fushu2);
fushu1.Mul(fushu2);
}
}
(2):提供一个例子:
源代码:
import java.io.*;
public class Book{
double sb;
double xb;
Book(double x,double y){
this.sb=x;
this.xb=y;
}
Book(){
}
public static void main(String args[]){
System.out.println("请输入数据:");
double a=0;
double b=0;
double c=0;
double d=0;
String s;
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.println("请输入第一个复述的实部:");
try{
s = in.readLine();
a=Double.parseDouble(s);
}
catch(IOException e)
{ System.out.println("抛掷异常");}
System.out.println("请输入第一个复述的虚部:");
try{
s = in.readLine();
b =Double.parseDouble(s);
}
catch(IOException e)
{ System.out.println("抛掷异常");}
System.out.println("请输入第二个复述的实部:");
try{
s = in.readLine();
c =Double.parseDouble(s);
}
catch(IOException e)
{ System.out.println("抛掷异常");}
System.out.println("请输入第二个复述的虚部:");
try{
s = in.readLine();
d =Double.parseDouble(s);
}
catch(IOException e)
{ System.out.println("抛掷异常");}
Book h;
h=new Book(a,b);
Book j;
j=new Book(c,d);
System.out.println("您输入的一个数为:");
toString(h);
System.out.println("您输入的二个数为:");
toString(j);
Book k;
k=new Book();
char z='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 :查看修改结果");
int i=0;
try{
i= Integer.parseInt(in.readLine());
}
catch(IOException e)
{ System.out.println("抛掷异常");}
switch(i)
{
case 1:
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 ;
case 2:
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 ;
case 3:
System.out.println("请输入您要修改哪个实数:");
int l=0;
try{
l= Integer.parseInt(in.readLine());
}
catch(IOException e)
{ System.out.println("抛掷异常");}
if(l==1)
{
h.xiugais(h);
h.xiugaix(h);
}
else
{
xiugais(j);
xiugaix(j);
}
break ;
case 4:
double f=0;
double e=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 ;
case 5:
double chushu=cheng(j.sb,j.sb)-cheng(j.xb,-j.xb);
double beichushus=jian(cheng(h.sb,j.sb),cheng(h.xb,-j.xb));
double beichushux=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 ;
case 6:
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(IOException e){}
} while(z=='y');
}
public static double gets(Book a){
return a.sb;
}
public static double getx(Book b){
return b.xb;
}
public static double xiugais(Book a)
{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.println("请输入您要修改的实部:");
double m=0;
try{
m= Double.parseDouble(in.readLine());
}
catch(IOException e)
{ System.out.println("抛掷异常");}
a.sb=m;
System.out.println("修改成功:");
return 0;
}
public static double xiugaix(Book b)
{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.println("请输入您要修改的虚部:");
double n=0;
try{
n= Double.parseDouble(in.readLine());
}
catch(IOException e)
{ System.out.println("抛掷异常");}
b.xb=n;
System.out.println("修改成功:");
return 0;
}
public static double jia(double a,double b)//
{
double c=0;
c=a+b;
System.out.println("加法成功:");
return c ;
}
public static double jian(double a,double b)
{
double c=0;
c=a-b;
System.out.println("减法成功:");
return c;
}
public static double cheng(double a,double b)
{
double c=0;
c=a*b;
System.out.println("乘法成功:");
return c;
}
public static double chu(double a,double b)
{
double d=0;
double c=0;
d=a/b;
c=(double)(Math.round(d*100)/100.0);
System.out.println("除法成功:");
return c ;
}
public static double toString(Book a){
System.out.println("结果为:"+a.sb+"+"+a.xb+"*i");
return 0;
}
}
(3)测试结果截图:
1.运用java 编写一个复数类,有实部和虚部,并实现复数的加减乘除运算?
public class ComplexNumber {
/**
* @param args
*/
int shi,xu;//复数的实部和虚部
public ComplexNumber(int n,int ni){
shi = n;
xu = ni;
}
public void ComplexShow(){
String output = "";
output+=shi;
if(xu=0){
output+="+";
}
output+=xu;
output+="i";
System.out.println(output);
}
public void ComplexShow1(){//不要换行
String output = "";
output+=shi;
if(xu=0){
output+="+";
}
output+=xu;
output+="i";
System.out.print(output);
}
public static void ComplexAdd(ComplexNumber x1,ComplexNumber x2){//实现两个复数相加
ComplexNumber cn = new ComplexNumber(0, 0);//将两个复数相加等于cn
cn.shi = x1.shi + x2.shi;
cn.xu = x1.xu + x2.xu;
cn.ComplexShow();
}
public static void ComplexMinus(ComplexNumber x1,ComplexNumber x2){//实现两个复数相减,第一个数减第二个数
ComplexNumber cn = new ComplexNumber(0, 0);//将两个复数相加等于cn
cn.shi = x1.shi - x2.shi;
cn.xu = x1.xu - x2.xu;
cn.ComplexShow();
}
public static void ComplexMultiply(ComplexNumber x1,ComplexNumber x2){//实现两个复数相乘
ComplexNumber cn = new ComplexNumber(0, 0);//将两个复数相加等于cn
cn.shi = x1.shi * x2.shi - x1.xu * x2.xu;
cn.xu = x1.xu * x2.shi + x2.xu * x1.shi;
cn.ComplexShow();
}
public static void ComplexDivide(ComplexNumber x1,ComplexNumber x2){//实现两个复数相除,第一个数除以第二个数
ComplexNumber x2_gong = new ComplexNumber(x2.shi,0-x2.xu);//求被除数的共轭复数
ComplexNumber cn = new ComplexNumber(0, 0);//将两个复数相加等于cn
cn.shi = x1.shi * x2_gong.shi - x1.xu * x2_gong.xu;//x1/x2,求分子实部
cn.xu = x1.xu * x2_gong.shi + x2_gong.xu * x1.shi;//x1/x2,求分子虚部
int fenMu = x2.shi * x2.shi + x2.xu * x2.xu;
if(fenMu!=0){
System.out.print("(");
cn.ComplexShow1();
System.out.print(")");
System.out.println("/"+fenMu);
}
else
System.out.println("分母为零,无法相除");
}
public static void main(String[] args) {
// TODO Auto-generated method stub
ComplexNumber cn = new ComplexNumber(-1, -1);//初始化复数
cn.ComplexShow();//显示复数
ComplexNumber c1 = new ComplexNumber(-1, -1);
ComplexNumber c2 = new ComplexNumber(1, 1);
System.out.print("加:");
ComplexAdd(c1, c2);
System.out.print("减:");
ComplexMinus(c1, c2);
System.out.print("乘:");
ComplexMultiply(c1, c2);
System.out.print("除:");
ComplexDivide(c1, c2);//自己化简
}
}
Java编程,计算复数的加减乘除和比较大小
首先复数是不能比较大小的。
以下是一个复数类,希望能帮助你
public class Complex
{
private final double re; // the real part
private final double im; // the imaginary part
// create a new object with the given real and imaginary parts
public Complex(double real, double imag)
{
re = real;
im = imag;
}
// return a string representation of the invoking Complex object
public String toString() {
if (im == 0) return re + "";
if (re == 0) return im + "i";
if (im 0) return re + " - " + (-im) + "i";
return re + " + " + im + "i";
}
// return abs/modulus/magnitude and angle/phase/argument
public double abs() { return Math.hypot(re, im); } // Math.sqrt(re*re + im*im)
public double phase() { return Math.atan2(im, re); } // between -pi and pi
// return a new Complex object whose value is (this + b)
public Complex plus(Complex b) {
Complex a = this; // invoking object
double real = a.re + b.re;
double imag = a.im + b.im;
return new Complex(real, imag);
}
/**
* return a new Complex object whose value is (this - b)
* 减法
* @param b
* @return
*/
public Complex minus(Complex b) {
Complex a = this;
double real = a.re - b.re;
double imag = a.im - b.im;
return new Complex(real, imag);
}
/**
* return a new Complex object whose value is (this * b)
* 乘以一个复数
* @param b 被乘的复数
* @return
*/
public Complex times(Complex b) {
Complex a = this;
double real = a.re * b.re - a.im * b.im;
double imag = a.re * b.im + a.im * b.re;
return new Complex(real, imag);
}
/**
*scalar multiplication
* return a new object whose value is (this * alpha)/br
* 乘以一个实数
*/
public Complex times(double alpha) {
return new Complex(alpha * re, alpha * im);
}
/**
* return a new Complex object whose value is the conjugate of this/br
* 共轭复数
* @return
*/
public Complex conjugate() { return new Complex(re, -im); }
/**
* return a new Complex object whose value is the reciprocal of this/br
* 倒数 a +bi 的倒数 /br
* a -bi/br
* —————————————/br
* a^2 + b ^2/br
*/
public Complex reciprocal()
{
double scale = re*re + im*im;
return new Complex(re / scale, -im / scale);
}
/**
* return the real part
* @return
*/
public double re() { return re; }
/**
* imaginary part
* @return
*/
public double im() { return im; }
/**
* return a / b
*/
public Complex divides(Complex b)
{
Complex a = this;
return a.times(b.reciprocal());
}
// return a new Complex object whose value is the complex exponential of this
public Complex exp()
{
return new Complex(Math.exp(re) * Math.cos(im), Math.exp(re) * Math.sin(im));
}
// return a new Complex object whose value is the complex sine of this
public Complex sin()
{
return new Complex(Math.sin(re) * Math.cosh(im), Math.cos(re) * Math.sinh(im));
}
// return a new Complex object whose value is the complex cosine of this
public Complex cos()
{
return new Complex(Math.cos(re) * Math.cosh(im), -Math.sin(re) * Math.sinh(im));
}
// return a new Complex object whose value is the complex tangent of this
public Complex tan()
{
return sin().divides(cos());
}
// a static version of plus
public static Complex plus(Complex a, Complex b)
{
double real = a.re + b.re;
double imag = a.im + b.im;
Complex sum = new Complex(real, imag);
return sum;
}
// sample client for testing
public static void main(String[] args) {
Complex a = new Complex(5.0, 6.0);
Complex b = new Complex(-3.0, 4.0);
System.out.println("a = " + a);
System.out.println("b = " + b);
System.out.println("Re(a) = " + a.re());
System.out.println("Im(a) = " + a.im());
System.out.println("b + a = " + b.plus(a));
System.out.println("a - b = " + a.minus(b));
System.out.println("a * b = " + a.times(b));
System.out.println("b * a = " + b.times(a));
System.out.println("a / b = " + a.divides(b));
System.out.println("(a / b) * b = " + a.divides(b).times(b));
System.out.println("conj(a) = " + a.conjugate());
System.out.println("|a| = " + a.abs());
System.out.println("tan(a) = " + a.tan());
}
}
java 编写一个可对复数进行加减运算的程序
1、real和image这两个field前面的static去掉。
2、public Complex() 这个构造器去掉,如果要接受输入的话,应该放到main方法里,这样这个类更清晰。
3、静态方法Complex_add和Complex_minus没指定返回值类型,应该返回的是Complex。另外方法名字首字母应小写。
4、参考这个:
如何用java程序编写输入两个复数,然后进行那个加减乘,最后显示结果。
例如:ai+b ci+d
加法:int a,b,c,d;int x,y; x=a+c; y=b+d
结果就为:xi+y 这很简单 再输出的时候要变为字符串型 这样你就可以再x后加个i 形成xi的形式了
减法: int a,b,c,d;int x,y; x=a-c; y=b-d
结果就为:xi+y 这很简单 再输出的时候要变为字符串型 这样你就可以再x后加个i 形成xi的形式了
乘法: int a,b,c,d;int x,y;
x=a*d+b*c y=b*d-a*c
结果就为:xi+y 这很简单 再输出的时候要变为字符串型 这样你就可以再x后加个i 形成xi的形式了
分享标题:java代码复数加减乘除,复数的加法运算java
标题网址:http://pwwzsj.com/article/hsspsj.html