中易网

关于java编程复数运算的问题

答案:2  悬赏:10  
解决时间 2021-02-23 10:06
关于java编程复数运算的问题
最佳答案
// 复数, 因为是demo, 其精度就按double的精度来设计
public class Complex {
double real = 0;

double imaginary = 0;

public Complex(double real, double imaginary) {
this.real = real;
this.imaginary = imaginary;
}


public double getAbs() {
return Math.sqrt(real * real + imaginary * imaginary);
}


public double getAngle() {
return Math.atan2(imaginary, real);
}


public double getRealpart() {
return real;
}


public double getImaginarypart() {
return imaginary;
}

// 复数相加
public Complex add(Complex c){
return new Complex( this.real + c.real, this.imaginary + c.imaginary);
}
// 相反数
public Complex negtive(){
return new Complex( -this.real , -this.imaginary );
}

// 其他略
}
全部回答
package tutorial;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import com.sun.java_cup.internal.internal_error;
import com.sun.jndi.url.dns.dnsURLContext;
public class Test_1 extends JFrame {
private double realPartDou1, imaginarypartDou1, realPartDou2,
imaginarypartDou2;
private String realPart1, imaginarypart1, realPart2, imaginarypart2;
public Test_1() {

init();
}
public void init() {
initRealPart1();
initimaginarypart1();
System.out.println("您输入第一个复数是:" + realPartDou1
+( imaginarypartDou1>0?"+"+imaginarypartDou1:"-"+(-imaginarypartDou1) )+ "i");
initRealPart2();
initimaginarypart2();
System.out
.println("您输入第二个的复数是:" + realPartDou2 +( imaginarypartDou2 > 0 ? "+"
+ imaginarypartDou2
: "-" + (0 - imaginarypartDou2)) + "i");
System.out.println("相加之后的结果是:"
+ add(realPartDou1, realPartDou2)
+ (add(imaginarypartDou1, imaginarypartDou2) > 0 ? "+"
+ add(imaginarypartDou1, imaginarypartDou2) : "-"
+ (0 - add(imaginarypartDou1, imaginarypartDou2)))
+ "i");
////5+6i 4+3i
double real=add(cheng(realPartDou1, realPartDou2), -cheng(imaginarypartDou1, imaginarypartDou2));
double imaginary=add(cheng(realPartDou1, imaginarypartDou2), cheng(realPartDou2, imaginarypartDou1));
System.out.println("相乘之后的结果是:"+real+(imaginary>0?"+"+imaginary:"-"+(-imaginary)));
}
public void initRealPart1() {
realPart1 = JOptionPane.showInputDialog("请键入第 1 个实数部分:(必须是数字)");
try {
realPartDou1 = Double.parseDouble(realPart1);
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
JOptionPane.showMessageDialog(null, "键入非法数字,请重新填写", "Message",
JOptionPane.INFORMATION_MESSAGE);
initRealPart1();
}
}
public void initimaginarypart1() {
imaginarypart1 = JOptionPane.showInputDialog("请键入第 1 个虚数部分:(必须是数字)");
try {
imaginarypartDou1 = Double.parseDouble(imaginarypart1);
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
JOptionPane.showMessageDialog(null, "键入非法数字,请重新填写", "Message",
JOptionPane.INFORMATION_MESSAGE);
initimaginarypart1();
}
}
public void initRealPart2() {
realPart2 = JOptionPane.showInputDialog("请键入第 2 个实数部分:(必须是数字)");
try {
realPartDou2 = Double.parseDouble(realPart2);
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
JOptionPane.showMessageDialog(null, "键入非法数字,请重新填写", "Message",
JOptionPane.INFORMATION_MESSAGE);
initRealPart2();
}
}
public void initimaginarypart2() {
imaginarypart2 = JOptionPane.showInputDialog("请键入第 2 个虚数部分:(必须是数字)");
try {
imaginarypartDou2 = Double.parseDouble(imaginarypart2);
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
JOptionPane.showMessageDialog(null, "键入非法数字,请重新填写", "Message",
JOptionPane.INFORMATION_MESSAGE);
initimaginarypart2();
}
}
public double add(double frist, double second) {
double result = frist + second;
return result;
}
public double cheng(double first,double second) {
double result=first*second;
return result;
}
public static void main(String[] args) {
Test_1 test_1 = new Test_1();
test_1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
//===========================
结果演示:
您输入第一个复数是:5.0-5.0i
您输入第二个的复数是:9.0-4.0i
相加之后的结果是:14.0-9.0i
相乘之后的结果是:25.0-65.0
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
我爸的坟头长了颗药草,有什么讲究?
长虹电器维修中心在什么地方啊,我要过去处理
时空猎人帐号有数字字母的是什么版本,帐号中
学校安全管理遵循的方针是什么?
自己无证驾驶!会处罚车里有证的朋友吗
汤尚路/秦上路(路口)怎么去啊,有知道地址的
中国工商银行ATM(浦东机场综合保税区支行)地
有一木块,把它轻轻放在盛满水的溢水杯中时,
索尼Z5P怎样将在QQ音乐里下载的歌曲放到手机w
今天从新疆乌鲁木齐到西安的火车几点到
高兴新股票最低的时候是多少?
杀清是什么意思
我为歌狂插曲 满天的繁星……我点点的秘密 歌
首先我家电脑是windows10系统。我想从u盘上安
有谁玩过Caanoo这个掌机吗??
推荐资讯
计算机二级excel 用不同的函数行不行
频数和组距带单位吗?
一个等腰三角形的底角和顶角度数之比是1比2,
请教如何可以将铁器或铜器,变成古懂出土一样
佰草集化妆水好用吗
中国电信东海曲阳支局这个地址在什么地方,我
杜鹃道/群芳路(路口)地址有知道的么?有点事
和男友做的时候总是那么几个动作,还有什么别
为什么很多人讨厌死亡笔记里的Near.....漫画
支付宝为什么认证不了
小海星幼儿园地址在哪,我要去那里办事
宏翔跆拳道馆(崧厦店)在哪里啊,我有事要去这
手机登qq时,显示手机磁盘不足,清理后重新登
刺客的套装怎么选啊?