设计Java复杂计算机的代码
时间: 2024-03-23 13:38:39 浏览: 26
以下是一个简单的Java复杂计算器的代码,可以进行加、减、乘、除和求幂等基本运算:
```java
import java.util.Scanner;
public class ComplexCalculator {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the first complex number (a+bi): ");
ComplexNumber c1 = new ComplexNumber(input.nextLine());
System.out.print("Enter the second complex number (a+bi): ");
ComplexNumber c2 = new ComplexNumber(input.nextLine());
System.out.println(c1 + " + " + c2 + " = " + c1.add(c2));
System.out.println(c1 + " - " + c2 + " = " + c1.subtract(c2));
System.out.println(c1 + " * " + c2 + " = " + c1.multiply(c2));
System.out.println(c1 + " / " + c2 + " = " + c1.divide(c2));
System.out.println(c1 + " ^ 2 = " + c1.power(2));
}
}
class ComplexNumber {
private double real;
private double imaginary;
public ComplexNumber(double real, double imaginary) {
this.real = real;
this.imaginary = imaginary;
}
public ComplexNumber(String complexNumber) {
String[] parts = complexNumber.split("\\+");
this.real = Double.parseDouble(parts[0]);
this.imaginary = Double.parseDouble(parts[1].replace("i", ""));
}
public double getReal() {
return real;
}
public double getImaginary() {
return imaginary;
}
public ComplexNumber add(ComplexNumber other) {
return new ComplexNumber(this.real + other.real, this.imaginary + other.imaginary);
}
public ComplexNumber subtract(ComplexNumber other) {
return new ComplexNumber(this.real - other.real, this.imaginary - other.imaginary);
}
public ComplexNumber multiply(ComplexNumber other) {
double real = this.real * other.real - this.imaginary * other.imaginary;
double imaginary = this.real * other.imaginary + this.imaginary * other.real;
return new ComplexNumber(real, imaginary);
}
public ComplexNumber divide(ComplexNumber other) {
double denominator = other.real * other.real + other.imaginary * other.imaginary;
double real = (this.real * other.real + this.imaginary * other.imaginary) / denominator;
double imaginary = (this.imaginary * other.real - this.real * other.imaginary) / denominator;
return new ComplexNumber(real, imaginary);
}
public ComplexNumber power(int n) {
double r = Math.pow(Math.sqrt(real * real + imaginary * imaginary), n);
double theta = Math.atan2(imaginary, real);
double real = r * Math.cos(n * theta);
double imaginary = r * Math.sin(n * theta);
return new ComplexNumber(real, imaginary);
}
@Override
public String toString() {
return real + "+" + imaginary + "i";
}
}
```
该程序使用了一个ComplexNumber类来表示复数,其中包含了复数的实部和虚部,以及各种基本运算的方法,如加、减、乘、除和求幂等。
在主函数中,该程序首先通过输入流获得用户输入的两个复数,然后使用ComplexNumber类的各种方法来进行基本运算,并输出结果。
该程序是一个简单的复杂计算器,可以帮助我们更好地理解Java面向对象编程的思想,以及如何使用类来实现面向对象编程的基本概念。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)