📌  相关文章
📜  找出复数的实部和虚部

📅  最后修改于: 2021-05-06 22:44:58             🧑  作者: Mango

给定复数Z ,任务是确定此复数的实部和虚部。

例子:

方法:复数可以表示为Z = x + yi ,其中x是实部, y是虚部。
我们将按照以下步骤将实部和虚部分开

  1. 找出字符串中+运算符的索引
  2. 实际部分将是一个从索引0到长度(运算符的索引– 1)开始的子字符串。
  3. 虚构部分将是一个从索引(运算符的索引+1)(字符串的长度–运算符的索引–2)开始的子字符串

执行:

C++
// C++ program to find the real and
// imaginary parts of a Complex Number
#include 
using namespace std;
  
// Function to find real and imaginary
// parts of a complex number
void findRealAndImag(string s)
{
    // string length stored in variable l
    int l = s.length();
  
    // variable for the index of the separator
    int i;
  
    // Storing the index of '+'
    if (s.find('+') < l) {
        i = s.find('+');
    }
    // else 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 - i - 2);
  
    cout << "Real part: " << real << "\n";
    cout << "Imaginary part: "
         << imaginary << "\n";
}
  
// Driver code
int main()
{
    string s = "3+4i";
  
    findRealAndImag(s);
  
    return 0;
}


Java
// Java program to find the real and
// imaginary parts of a Complex Number 
class GFG
{
    // Function to find real and imaginary
    // parts of a complex number
    static void findRealAndImag(String s)
    {
        // string length stored in variable l
        int l = s.length();
       
        // variable for the index of the separator
        int i;
       
        // Storing the index of '+'
        if (s.indexOf('+') != -1) {
            i = s.indexOf('+');
        }
  
        // else 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);
       
        System.out.println("Real part: " + real );
        System.out.println("Imaginary part: "+
              imaginary);
    }
       
    // Driver code
    public static void main(String []args)
    {
        String s = "3+4i";
       
        findRealAndImag(s);
      
    }
}
  
// This code is contributed by chitranayal


Python3
# Python3 program to find the real and 
# imaginary parts of a Complex Number 
  
# Function to find real and imaginary 
# parts of a complex number 
def findRealAndImag(s) :
  
    # string length stored in variable l 
    l = len(s) 
  
    # variable for the index of the separator 
    i = 0 
  
    # Storing the index of '+' 
    if (s.find('+') != -1): 
        i = s.find('+')
    # else storing the index of '-' 
    else:
        i = s.find('-'); 
  
    # Finding the real part 
    # of the complex number 
    real = s[:i]
  
    # Finding the imaginary part 
    # of the complex number 
    imaginary = s[i + 1:l  - 1]
  
    print("Real part:", real)
    print("Imaginary part:", imaginary)
  
# Driver code 
s = "3+4i"; 
  
findRealAndImag(s); 
  
# This code is contributed by Sanjit_Prasad


C#
// C# program to find the real and
// imaginary parts of a Complex Number 
using System;
  
class GFG
{
    // Function to find real and imaginary
    // parts of a complex number
    static void findRealAndImag(String s)
    {
        // string length stored in variable l
        int l = s.Length;
        
        // variable for the index of the separator
        int i;
        
        // Storing the index of '+'
        if (s.IndexOf('+') != -1) {
            i = s.IndexOf('+');
        }
  
        // else 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);
        
        Console.WriteLine("Real part: " + real );
        Console.WriteLine("Imaginary part: "+
              imaginary);
    }
        
    // Driver code
    public static void Main(String []args)
    {
        String s = "3+4i";
        
        findRealAndImag(s);
       
    }
}
  
// This code is contributed by 29AjayKumar


输出:
Real part: 3
Imaginary part: 4

绩效分析

  • 时间复杂度:在上述方法中,由于我们正在执行恒定数量的操作,而与字符串的长度无关,所以时间复杂度为O(1)
  • 辅助空间复杂度:在上述方法中,除了一些变量之外,我们没有使用任何额外的空间。因此辅助空间复杂度为O(1)