📜  将两个复数相加的Java程序

📅  最后修改于: 2022-05-13 01:54:41.517000             🧑  作者: Mango

将两个复数相加的Java程序

复数由两部分组成的数字——实和虚复数是更复杂数学(例如代数)的基石。复数的标准格式是 a + bi,实在前,虚在后。

任何复数的一般形式是:

a+ib

Where "a" is real number and "b" is Imaginary number.

复数的构造

为了创建复数,我们将虚数和实数作为构造函数的参数传递。

Java
// Java program to construct the complex number
  
class ComplexNumber {
  
    // variables to hold real and imaginary part of complex
    // number
    int real, image;
  
    // Constructor which will be used while creating complex
    // number
    public ComplexNumber(int r, int i)
    {
        this.real = r;
        this.image = i;
    }
  
    // function to print real number
    public void showC()
    {
        System.out.println(this.real + " +i " + this.image);
    }
  
    // we will implement  this function for addition
    public complex add(ComplexNumber, ComplexNumber);
}


Java
// Java program to add two complex numbers
  
class ComplexNumber {
  
    // variables to hold real and imaginary part of complex
    // number
    int real, image;
  
    // Constructor which will be used while creating complex
    // number
    public ComplexNumber(int r, int i)
    {
        this.real = r;
        this.image = i;
    }
  
    // function to print real number
    public void showC()
    {
        System.out.print(this.real + " +i" + this.image);
    }
  
    // function for addition
    public static ComplexNumber add(ComplexNumber n1,
                                    ComplexNumber n2)
    {
  
        // creating blank complex number
        // to store result
        ComplexNumber res = new ComplexNumber(0, 0);
  
        // adding real parts of both complex numbers
        res.real = n1.real + n2.real;
  
        // adding imaginary parts
        res.image = n1.image + n2.image;
  
        // returning result
        return res;
    }
  
    public static void main(String arg[])
    {
  
        // creating two complex numbers
        ComplexNumber c1 = new ComplexNumber(4, 5);
        ComplexNumber c2 = new ComplexNumber(10, 5);
  
        // printing complex numbers
          System.out.print("first Complex number: ");
        c1.showC();
          
        System.out.print("\nSecond Complex number: ");
        c2.showC();
  
        // calling add() to perform addition
        ComplexNumber res = add(c1, c2);
  
        // displaying addition
        System.out.println("\nAddition is :");
        res.showC();
    }
}



添加函数

  • 基本上,两个复数的加法是通过将第一个复数的实部与第二个复数的实部相加来完成的。
  • 并将第一个复数的虚部与第二个相加,结果为第三个复数。
  • 所以这意味着我们的add()将返回另一个复数。

函数定义:

ComplexNumber add(ComplexNumber n1, ComplexNumber n2){
    
  ComplexNumber res = new ComplexNumber(0,0); //creating blank complex number 
  
  // adding real parts of both complex numbers
  res.real = n1.real + n2.real;
  
  // adding imaginary parts
  res.image = n1.image + n2.image;
  
  // returning result
  return res;

}


代码:

Java

// Java program to add two complex numbers
  
class ComplexNumber {
  
    // variables to hold real and imaginary part of complex
    // number
    int real, image;
  
    // Constructor which will be used while creating complex
    // number
    public ComplexNumber(int r, int i)
    {
        this.real = r;
        this.image = i;
    }
  
    // function to print real number
    public void showC()
    {
        System.out.print(this.real + " +i" + this.image);
    }
  
    // function for addition
    public static ComplexNumber add(ComplexNumber n1,
                                    ComplexNumber n2)
    {
  
        // creating blank complex number
        // to store result
        ComplexNumber res = new ComplexNumber(0, 0);
  
        // adding real parts of both complex numbers
        res.real = n1.real + n2.real;
  
        // adding imaginary parts
        res.image = n1.image + n2.image;
  
        // returning result
        return res;
    }
  
    public static void main(String arg[])
    {
  
        // creating two complex numbers
        ComplexNumber c1 = new ComplexNumber(4, 5);
        ComplexNumber c2 = new ComplexNumber(10, 5);
  
        // printing complex numbers
          System.out.print("first Complex number: ");
        c1.showC();
          
        System.out.print("\nSecond Complex number: ");
        c2.showC();
  
        // calling add() to perform addition
        ComplexNumber res = add(c1, c2);
  
        // displaying addition
        System.out.println("\nAddition is :");
        res.showC();
    }
}
输出
first Complex number: 4 +i5
Second Complex number: 10 +i5
Addition is :
14 +i10