关于java编程复数运算的问题
答案:2 悬赏:10
解决时间 2021-02-23 10:06
- 提问者网友:兔牙战士
- 2021-02-22 16:40
关于java编程复数运算的问题
最佳答案
- 二级知识专家网友:千杯敬自由
- 2021-02-22 17:19
// 复数, 因为是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 );
}
// 其他略
}
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 );
}
// 其他略
}
全部回答
- 1楼网友:深街酒徒
- 2021-02-22 18:34
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
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
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯