📜  复数的模数

📅  最后修改于: 2021-05-07 10:00:02             🧑  作者: Mango

给定复数z ,任务是确定该复数的模数。

注意:给定复数z = a + ib ,模量由| z |表示并定义为\left | z \right | = \sqrt{a^{2}+b^{2}}

例子:

方法:对于给定的复数z = x + iy

  1. 分别找到x和y的实部和虚部。
    If z = x +iy
    
    Real part = x
    Imaginary part = y
    
  2. 分别找到x和y的平方。
    Square of Real part = x2
    Square of Imaginary part = y2
    
  3. 求出计算平方的总和。
    Sum = Square of Real part 
          + Square of Imaginary part
        = x2 + y2
    
  4. 找出计算出的总和的平方根。这将是给定复数的模数

    \left | z \right | = \sqrt{x^{2}+y^{2}}

下面是上述方法的实现:

C++
// C++ program to find the
// Modulus of a Complex Number
  
#include 
using namespace std;
  
// Function to find modulus
// of a complex number
void findModulo(string s)
{
    int l = s.length();
    int i, modulus = 0;
  
    // Storing the index of '+'
    if (s.find('+') < l) {
        i = s.find('+');
    }
    // Storing the index of '-'
    else {
        i = s.find('-');
    }
  
    // Finding the real part
    // of the complex number
    string real = s.substr(0, i);
  
    // Finding the imaginary part
    // of the complex number
    string imaginary = s.substr(i + 1, l - 1);
  
    int x = stoi(real);
    int y = stoi(imaginary);
  
    cout << sqrt(x * x + y * y) << "\n";
}
  
// Driver code
int main()
{
    string s = "3+4i";
  
    findModulo(s);
  
    return 0;
}


Java
// Java program to find the
// Modulus of a Complex Number
import java.util.*;
  
class GFG{
   
// Function to find modulus
// of a complex number
static void findModulo(String s)
{
    int l = s.length();
    int i, modulus = 0;
   
    // Storing the index of '+'
    if (s.contains("+")) {
        i = s.indexOf("+");
    }
  
    // Storing the index of '-'
    else {
        i = s.indexOf("-");
    }
   
    // Finding the real part
    // of the complex number
    String real = s.substring(0, i);
   
    // Finding the imaginary part
    // of the complex number
    String imaginary = s.substring(i + 1, l-1);
   
    int x = Integer.parseInt(real);
    int y = Integer.parseInt(imaginary);
   
    System.out.print(Math.sqrt(x * x + y * y)+ "\n");
}
   
// Driver code
public static void main(String[] args)
{
    String s = "3+4i";
   
    findModulo(s);
}
}
  
// This code is contributed by Rajput-Ji


Python 3
# Python 3 program to find the
# Modulus of a Complex Number
from math import sqrt
  
# Function to find modulus
# of a complex number
def findModulo(s):
    l = len(s)
    modulus = 0
  
    # Storing the index of '+'
    if ( '+' in s ):
        i = s.index('+')
  
    # Storing the index of '-'
    else:
        i = s.index('-')
  
    # Finding the real part
    # of the complex number
    real = s[0:i]
  
    # Finding the imaginary part
    # of the complex number
    imaginary = s[i + 1:l - 1]
  
    x = int(real)
    y = int(imaginary)
  
    print(int(sqrt(x * x + y * y)))
  
# Driver code
if __name__ == '__main__':
    s = "3+4i"
  
    findModulo(s)
  
# This code is contributed by Surendra_Gangwar


C#
// C# program to find the
// Modulus of a Complex Number
using System;
  
public class GFG{
    
// Function to find modulus
// of a complex number
static void findModulo(String s)
{
    int l = s.Length;
    int i;
    
    // Storing the index of '+'
    if (s.Contains("+")) {
        i = s.IndexOf("+");
    }
   
    // Storing the index of '-'
    else {
        i = s.IndexOf("-");
    }
    
    // Finding the real part
    // of the complex number
    String real = s.Substring(0, i);
    
    // Finding the imaginary part
    // of the complex number
    String imaginary = s.Substring(i + 1, l-i - 2);
    
    int x = Int32.Parse(real);
    int y = Int32.Parse(imaginary);
    
    Console.Write(Math.Sqrt(x * x + y * y)+ "\n");
}
    
// Driver code
public static void Main(String[] args)
{
    String s = "3+4i";
    
    findModulo(s);
}
}
// This code contributed by sapnasingh4991


输出:
5