问答文章1 问答文章501 问答文章1001 问答文章1501 问答文章2001 问答文章2501 问答文章3001 问答文章3501 问答文章4001 问答文章4501 问答文章5001 问答文章5501 问答文章6001 问答文章6501 问答文章7001 问答文章7501 问答文章8001 问答文章8501 问答文章9001 问答文章9501

帮忙做个JAVA程序

发布网友 发布时间:2024-05-01 22:11

我来回答

4个回答

热心网友 时间:2024-10-19 23:42

/*
* 文件名:Complex.java
* 作者 :wangdan
* 主页 :http://blog.csdn.net/wd22420103
*/

public class Complex
{
double real,img; //实部和虚部

public Complex() //默认构造方法
{
this.real=0;
this.img =0;
}
public Complex(double real,double img) //带参数的构造方法
{
this.real=real;
this.img =img;
}
public double getReal(){//得到实部
return this.real;
}
public double getImage(){//得到虚部
return this.img;
}
public double getReal(Complex c){//得到复数c的实部,这两个函数看起来好像有点多余,但在特殊的情况下会有用
return c.real;
}
public double getImage(Complex c){ //得到复数c的虚部
return c.img;
}
public void setReal(double real){//设置实部
this.real=real;
}
public void setImage(double img){//设置虚部
this.img =img;
}

public Complex addComplex(Complex a,Complex b) //两个复数相加,结果返回
{
Complex temp =new Complex();
temp.real=a.real+b.real;
temp.img =a.img +b.img;
return temp;
}

public Complex decComplex(Complex a,Complex b) //两个复数相减,结果返回
{
Complex temp = new Complex();
temp.real = a.real - b.real;
temp.img = a.img - b.img;
return temp;
}
public Complex mulComplex(Complex a,Complex b) //两个复数相乘,结果返回
{
Complex temp = new Complex();
temp.real = a.real*b.real-a.img*b.img;
temp.img = a.real*b.img+a.img*b.real;
return temp;
}
public Complex divComplex(Complex a,Complex b) //两个复数相除,结果返回
{
Complex temp = new Complex();
temp.real=(a.real*b.real+a.img*b.img)/(b.real*b.real+b.img*b.img);
temp.img =(a.img*b.real-a.real*b.img)/(b.real*b.real+b.img*b.img);
return temp;
}

public void addComplex(Complex cplx) //加上一个复数
{
this.real=this.real+cplx.real;
this.img =this.img +cplx.img;
}
public void decComplex(Complex cplx) //减去一个复数
{
this.real=this.real-cplx.real;
this.img =this.img -cplx.img;
}
public void mulComplex(Complex cplx) //乘与一个复数
{
double temp=this.real; //下一行代码会改变this.real的值,先用一个临时变量存起来
this.real=this.real*cplx.real-this.img*cplx.img;
this.img =temp*cplx.img+this.img*cplx.real;
}
public void divComplex(Complex cplx) //除去一个复数
{
double temp=this.real; //下一行代码会改变this.real的值,先用一个临时变量存起来
this.real=(this.real*cplx.real+this.img*cplx.img)/(cplx.real*cplx.real+cplx.img*cplx.img);
this.img =(this.img*cplx.real-temp*cplx.img)/(cplx.real*cplx.real+cplx.img*cplx.img);
}

/** *//****以上是这个复数类的所有函数,下面是一些测试的代码****/

public void printComplex() //在console端输出这个复数,测试用
{
System.out.println(""+this.real+"+"+this.img+"i");//这里可以填加一点代码以判断虚部的正负,这个工作我没有做
}

public static void main(String[] args) //测试代码
{
Complex cc=new Complex(4,8);
cc.printComplex();
Complex dd=new Complex(2,2);
dd.printComplex();
System.out.println("-----------------");
Complex ff=new Complex();

ff=ff.addComplex(cc,dd);
ff.printComplex();
ff=ff.decComplex(cc,dd);
ff.printComplex();
ff=ff.mulComplex(cc,dd);
ff.printComplex();
ff=ff.divComplex(cc,dd);
ff.printComplex();
System.out.println("-----------------");
cc.addComplex(dd);
cc.printComplex();
cc=new Complex(4,8);
cc.decComplex(dd);
cc.printComplex();
cc=new Complex(4,8);
cc.mulComplex(dd);
cc.printComplex();
cc=new Complex(4,8);
cc.divComplex(dd);
cc.printComplex();
System.out.println("-----------------");
cc.setReal(123);
cc.setImage(456);
cc.printComplex();
System.out.println(""+cc.getReal()+"+"+cc.getImage()+"i");
System.out.println("-----------------");
}
}

直接可以用

热心网友 时间:2024-10-19 23:42

/**
Complex implements a complex number and defines complex
arithmetic and mathematical functions
Last Updated February 27, 2001
Copyright 1997-2001
@version 1.0
@author Andrew G. Bennett
*/
public class Complex extends Object {

private double x,y;

/**
Constructs the complex number z = u + i*v
@param u Real part
@param v Imaginary part
*/
public Complex(double u,double v) {
x=u;
y=v;
}

/**
Real part of this Complex number
(the x-coordinate in rectangular coordinates).
@return Re[z] where z is this Complex number.
*/
public double real() {
return x;
}

/**
Imaginary part of this Complex number
(the y-coordinate in rectangular coordinates).
@return Im[z] where z is this Complex number.
*/
public double imag() {
return y;
}

/**
Molus of this Complex number
(the distance from the origin in polar coordinates).
@return |z| where z is this Complex number.
*/
public double mod() {
if (x!=0 || y!=0) {
return Math.sqrt(x*x+y*y);
} else {
return 0d;
}
}

/**
Argument of this Complex number
(the angle in radians with the x-axis in polar coordinates).
@return arg(z) where z is this Complex number.
*/
public double arg() {
return Math.atan2(y,x);
}

/**
Complex conjugate of this Complex number
(the conjugate of x+i*y is x-i*y).
@return z-bar where z is this Complex number.
*/
public Complex conj() {
return new Complex(x,-y);
}

/**
Addition of Complex numbers (doesn't change this Complex number).
<br>(x+i*y) + (s+i*t) = (x+s)+i*(y+t).
@param w is the number to add.
@return z+w where z is this Complex number.
*/
public Complex plus(Complex w) {
return new Complex(x+w.real(),y+w.imag());
}

/**
Subtraction of Complex numbers (doesn't change this Complex number).
<br>(x+i*y) - (s+i*t) = (x-s)+i*(y-t).
@param w is the number to subtract.
@return z-w where z is this Complex number.
*/
public Complex minus(Complex w) {
return new Complex(x-w.real(),y-w.imag());
}

/**
Complex multiplication (doesn't change this Complex number).
@param w is the number to multiply by.
@return z*w where z is this Complex number.
*/
public Complex times(Complex w) {
return new Complex(x*w.real()-y*w.imag(),x*w.imag()+y*w.real());
}

/**
Division of Complex numbers (doesn't change this Complex number).
<br>(x+i*y)/(s+i*t) = ((x*s+y*t) + i*(y*s-y*t)) / (s^2+t^2)
@param w is the number to divide by
@return new Complex number z/w where z is this Complex number
*/
public Complex div(Complex w) {
double den=Math.pow(w.mod(),2);
return new Complex((x*w.real()+y*w.imag())/den,(y*w.real()-x*w.imag())/den);
}

/**
Complex exponential (doesn't change this Complex number).
@return exp(z) where z is this Complex number.
*/
public Complex exp() {
return new Complex(Math.exp(x)*Math.cos(y),Math.exp(x)*Math.sin(y));
}

/**
Principal branch of the Complex logarithm of this Complex number.
(doesn't change this Complex number).
The principal branch is the branch with -pi < arg <= pi.
@return log(z) where z is this Complex number.
*/
public Complex log() {
return new Complex(Math.log(this.mod()),this.arg());
}

/**
Complex square root (doesn't change this complex number).
Computes the principal branch of the square root, which
is the value with 0 <= arg < pi.
@return sqrt(z) where z is this Complex number.
*/
public Complex sqrt() {
double r=Math.sqrt(this.mod());
double theta=this.arg()/2;
return new Complex(r*Math.cos(theta),r*Math.sin(theta));
}

// Real cosh function (used to compute complex trig functions)
private double cosh(double theta) {
return (Math.exp(theta)+Math.exp(-theta))/2;
}

// Real sinh function (used to compute complex trig functions)
private double sinh(double theta) {
return (Math.exp(theta)-Math.exp(-theta))/2;
}

/**
Sine of this Complex number (doesn't change this Complex number).
<br>sin(z) = (exp(i*z)-exp(-i*z))/(2*i).
@return sin(z) where z is this Complex number.
*/
public Complex sin() {
return new Complex(cosh(y)*Math.sin(x),sinh(y)*Math.cos(x));
}

/**
Cosine of this Complex number (doesn't change this Complex number).
<br>cos(z) = (exp(i*z)+exp(-i*z))/ 2.
@return cos(z) where z is this Complex number.
*/
public Complex cos() {
return new Complex(cosh(y)*Math.cos(x),-sinh(y)*Math.sin(x));
}

/**
Hyperbolic sine of this Complex number
(doesn't change this Complex number).
<br>sinh(z) = (exp(z)-exp(-z))/2.
@return sinh(z) where z is this Complex number.
*/
public Complex sinh() {
return new Complex(sinh(x)*Math.cos(y),cosh(x)*Math.sin(y));
}

/**
Hyperbolic cosine of this Complex number
(doesn't change this Complex number).
<br>cosh(z) = (exp(z) + exp(-z)) / 2.
@return cosh(z) where z is this Complex number.
*/
public Complex cosh() {
return new Complex(cosh(x)*Math.cos(y),sinh(x)*Math.sin(y));
}

/**
Tangent of this Complex number (doesn't change this Complex number).
<br>tan(z) = sin(z)/cos(z).
@return tan(z) where z is this Complex number.
*/
public Complex tan() {
return (this.sin()).div(this.cos());
}

/**
Negative of this complex number (chs stands for change sign).
This proces a new Complex number and doesn't change
this Complex number.
<br>-(x+i*y) = -x-i*y.
@return -z where z is this Complex number.
*/
public Complex chs() {
return new Complex(-x,-y);
}

/**
String representation of this Complex number.
@return x+i*y, x-i*y, x, or i*y as appropriate.
*/
public String toString() {
if (x!=0 && y>0) {
return x+" + "+y+"i";
}
if (x!=0 && y<0) {
return x+" - "+(-y)+"i";
}
if (y==0) {
return String.valueOf(x);
}
if (x==0) {
return y+"i";
}
// shouldn't get here (unless Inf or NaN)
return x+" + i*"+y;

}
}

参考资料:http://www.math.ksu.e/~bennett/jomacg/c.html

热心网友 时间:2024-10-19 23:43

可以免费睇片啊种程序啊,帮忙做个绐我吧!

热心网友 时间:2024-10-19 23:43

要什么功能的 程序?
声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com
社会消费品零售总额反映什么问题社会消费品零售总额反映什么 我的头发发质很软 不想每天做头发 是烫发根还是怎么 就能让头发蓬起来... ...的时候如果想让软化完头顶的头发能蓬起来一些,应该可以做到吧?那... 我拿建设银行卡在邮政夸行转给农业银行可不可以 ...银行卡在自助取款机上可不可以跨行转账到农业银行卡上。可以的话手 ... 邮政银行卡转农业银行卡帐号怎么转(在自动取款上转) 邮政卡自助取款机内给外省农业银行卡转账需要多久到账 请问广州D1857在广州哪个站 薄壁不锈钢管什么型号的氩弧焊机能焊 韶关常年平均温度是多少 用java定义一个复数类,能实现加减,获得实部和虚部,将复数转换成字符串这... 我是男的,我兄弟见不得我和其他的人关系好,有时动手动脚,还很无理取... 关于和我一个好兄弟的相处,有时候对我动手动脚的我很且烦 怎么办_百度... 贵州黔运交通集团贵阳黔通汽车快捷客货运输有限公司怎么样? 驱出俄外交官的都有哪几个国家 black用什么提问 请帮我设计一个以水如何影响植物发芽为题的实验,急救~~~! 成都有没有回收布娃娃的地方,大小都有的,在娃娃机抓的,有的还有商标呢... ...成都有没有回收布娃娃的地方,大小都有的,在娃娃机抓的,有的还有商标... 哪家的机器人价格更优惠? 什么是虚拟现实,它如何改变娱乐和教育方式? 睢宁姚集镇民政可以办结婚证吗 机床用铜镶条是什么材质 怎么在QQ上与暗恋已久的人聊天 关于国画中山水、花鸟、人物的寓意,你知道多少?(花卉篇) 买房“定金”超过这个数,将不受法律保护,大家一定要小心! 为什么有的时候总是会遇到麻烦? 学校里的勤工俭学:收入、锻炼与潜在的风险 在江西财经职业学院生活多少生活费合适 职业学校可以办培训班赚收入吗 (2011?邵阳)如图所示,餐厅服务员手托茶盘,沿水平方向走向顾客以地面为参... 急!急!急!JAVA明天作业拜托帮忙 铝托盘可以找哪家? 《HUNTERxHUNTER梦》彼时否悔的txt全集下载地址 麻醉穿刺脊神经损伤,右胫神胫、右胫总神胫严重损害属医疗失误事件吗... 硬膜外麻醉打L2.3会不会损伤脊髓 中电投协鑫滨海发电有限公司怎么样? 中电投协鑫电厂在滨海建了一个大电厂请问在滨海那个位置 加工中心单单复位Z轴到机械原点是什么指令?不是回工件的原点,请教 从山顶到山脚的植物种群分布 从山顶到山脚的植物种群怎么分布 长在山脚的植物有哪些? 小孩肚子里为什么会有虫啊? u盘和移动硬盘哪个好用? 海尔洗衣机故障显示FC是什么意思? 老公生日祝福语幽默2023 老公生日祝福语幽默简短 连云港教育局电话号码多少哦 和 灌云教育局号码 江苏省连云港市新浦区教育局地址及联系方式 邯郸市北环到肥乡田寨路线 服用美金刚后出现嗜睡、便秘应如何处置 80岁老年痴呆吃福丽康欣盐酸美金刚药效果好吗?