Aracılığıyla paylaş


complex Sınıfı

Sınıf şablonu, biri karmaşık bir sayının gerçek kısmını, diğeri de hayali bölümü temsil eden türünde Typeiki nesne depolayan bir nesneyi açıklar.

Sözdizimi

template <class Type>
class complex

Açıklamalar

sınıfının Typenesnesi:

  • Genel bir varsayılan oluşturucu, yıkıcı, kopya oluşturucu ve geleneksel davranışa sahip atama işlecine sahiptir.

  • Tamsayı veya kayan nokta değerleri atanabilir ya da geleneksel davranışla bu tür değerlere tür atanabilir.

  • Gerektiğinde, geleneksel davranışa sahip kayan nokta türleri için tanımlanan aritmetik işleçleri ve matematik işlevlerini tanımlar.

Özellikle, kopya yapımı ile varsayılan yapı arasında küçük farklar olmayabilir ve bunu atama takip edebilir. Sınıfın Type nesneleri üzerindeki işlemlerin hiçbiri özel durumlar oluşturamayabilir.

Üç kayan nokta türü için sınıf şablonunun complex açık özelleştirmeleri vardır. Bu uygulamada, gerçek hesaplamalar için başka herhangi bir türdeki Type değer türüne double yayınlanır ve double sonuç türündeki Typedepolanan nesneye geri atanır.

Üyeler

Oluşturucular

Ad Tanım
complex Belirtilen gerçek ve sanal parçalarla veya başka bir karmaşık sayının kopyası olarak karmaşık bir sayı oluşturur.

Tür tanımları

Ad Tanım
value_type Karmaşık bir sayının gerçek ve sanal bölümlerini temsil etmek için kullanılan veri türünü temsil eden bir tür.

İşlevler

Ad Tanım
imag Karmaşık bir sayının sanal bileşenini ayıklar.
real Karmaşık bir sayının gerçek bileşenini ayıklar.

İşleçler

Ad Tanım
operator*= Bir hedef karmaşık sayıyı, karmaşık veya karmaşık sayının gerçek ve sanal parçalarıyla aynı türde olabilecek bir faktörle çarpar.
operator+= Eklenen sayının karmaşık olabileceği veya eklendiği karmaşık sayının gerçek ve sanal bölümleriyle aynı türde olabileceği hedef karmaşık sayıya bir sayı ekler.
operator-= Bir sayıyı, çıkarılan sayının karmaşık olabileceği veya eklendiği karmaşık sayının gerçek ve sanal bölümleriyle aynı türde olabileceği hedef karmaşık sayıdan çıkarır.
operator/= Bir hedef karmaşık sayıyı, karmaşık veya karmaşık sayının gerçek ve hayali bölümleriyle aynı türde olabilecek bölenlere böler.
operator= Bir sayıyı, atanan sayının karmaşık olabileceği veya atandığı karmaşık sayının gerçek ve sanal bölümleriyle aynı türde olabileceği hedef karmaşık bir sayıya atar.

complex

Belirtilen gerçek ve sanal parçalarla veya başka bir karmaşık sayının kopyası olarak karmaşık bir sayı oluşturur.

constexpr complex(
    const T& _RealVal = 0,
    const T& _ImagVal = 0);

template <class Other>
constexpr complex(
    const complex<Other>& complexNum);

Parametreler

_RealVal
Oluşturmakta olan karmaşık sayıyı başlatmak için kullanılan gerçek parçanın değeri.

_ImagVal
Oluşturmakta olan karmaşık sayıyı başlatmak için kullanılan sanal bölümün değeri.

complexNum
Gerçek ve sanal parçaları, derlenen karmaşık sayıyı başlatmak için kullanılan karmaşık sayı.

Açıklamalar

İlk oluşturucu, depolanan gerçek bölümü ve _RealVal depolanan sanal bölümü olarak _Imagvalbaşlatır. İkinci oluşturucu, için depolanan gerçek bölümü complexNum.real() ve depolanan sanal bölümü olarak complexNum.imag()başlatır.

Bu uygulamada, bir çevirici üye şablonu işlevlerini desteklemiyorsa, şablon:

template <class Other>
complex(const complex<Other>& right);

şununla değiştirilir:

complex(const complex& right);

kopyalama oluşturucusunun adıdır.

Örnek

// complex_complex.cpp
// compile with: /EHsc
#include <complex>
#include <iostream>

int main( )
{
    using namespace std;
    double pi = 3.14159265359;

    // The first constructor specifies real & imaginary parts
    complex<double> c1( 4.0 , 5.0 );
    cout << "Specifying initial real & imaginary parts,"
        << "c1 = " << c1 << endl;

    // The second constructor initializes values of the real &
    // imaginary parts using those of another complex number
    complex<double> c2( c1 );
    cout << "Initializing with the real and imaginary parts of c1,"
        << " c2 = " << c2 << endl;

    // Complex numbers can be initialized in polar form
    // but will be stored in Cartesian form
    complex<double> c3( polar( sqrt( (double)8 ) , pi / 4 ) );
    cout << "c3 = polar( sqrt( 8 ) , pi / 4 ) = " << c3 << endl;

    // The modulus and argument of a complex number can be recovered
    double absc3 = abs( c3 );
    double argc3 = arg( c3 );
    cout << "The modulus of c3 is recovered from c3 using: abs( c3 ) = "
        << absc3 << endl;
    cout << "Argument of c3 is recovered from c3 using:\n arg( c3 ) = "
        << argc3 << " radians, which is " << argc3 * 180 / pi
        << " degrees." << endl;
}

imag

Karmaşık bir sayının sanal bileşenini ayıklar.

T imag() const;

T imag(const T& right);

Parametreler

right
Sanal değeri ayıklanacak karmaşık bir sayı.

İade Değeri

Karmaşık sayının sanal kısmı.

Açıklamalar

Karmaşık bir sayı a + bi için, hayali parça veya bileşen Im(a + bi) = b'dir.

Örnek

// complex_imag.cpp
// compile with: /EHsc
#include <complex>
#include <iostream>

int main( )
{
    using namespace std;

    complex<double> c1( 4.0 , 3.0 );
    cout << "The complex number c1 = " << c1 << endl;

    double dr1 = c1.real();
    cout << "The real part of c1 is c1.real() = "
        << dr1 << "." << endl;

    double di1 = c1.imag();
    cout << "The imaginary part of c1 is c1.imag() = "
        << di1 << "." << endl;
}
The complex number c1 = (4,3)
The real part of c1 is c1.real() = 4.
The imaginary part of c1 is c1.imag() = 3.

operator*=

Bir hedef karmaşık sayıyı, karmaşık veya karmaşık sayının gerçek ve sanal parçalarıyla aynı türde olabilecek bir faktörle çarpar.

template <class Other>
complex& operator*=(const complex<Other>& right);

complex<Type>& operator*=(const Type& right);

complex<Type>& operator*=(const complex<Type>& right);

Parametreler

right
Karmaşık bir sayı veya hedef karmaşık sayının parametresiyle aynı türde bir sayı.

İade Değeri

Parametre olarak belirtilen sayı ile çarpılmış karmaşık bir sayı.

Açıklamalar

Basit aritmetik işlemlerin verilerin belirli bir biçime dönüştürülmeden yürütülebilmesi için işlem aşırı yüklenir.

Örnek

// complex_op_me.cpp
// compile with: /EHsc
#include <complex>
#include <iostream>

int main()
{
    using namespace std;
    double pi = 3.14159265359;

    // Example of the first member function
    // type complex<double> multiplied by type complex<double>
    complex<double> cl1( polar ( 3.0 , pi / 6 ) );
    complex<double> cr1( polar ( 2.0 , pi / 3 ) );
    cout << "The left-side complex number is cl1 = " << cl1 << endl;
    cout << "The right-side complex number is cr1 = " << cr1 << endl;

    complex<double> cs1 = cl1 * cr1;
    cout << "Quotient of two complex numbers is: cs1 = cl1 * cr1 = "
        << cs1 << endl;

    // This is equivalent to the following operation
    cl1 *= cr1;
    cout << "Quotient of two complex numbers is also: cl1 *= cr1 = "
        << cl1 << endl;

    double abscl1 = abs ( cl1 );
    double argcl1 = arg ( cl1 );
    cout << "The modulus of cl1 is: " << abscl1 << endl;
    cout << "The argument of cl1 is: "<< argcl1 << " radians, which is "
        << argcl1 * 180 / pi << " degrees." << endl << endl;

    // Example of the second member function
    // type complex<double> multiplied by type double
    complex<double> cl2 ( polar ( 3.0 , pi / 6 ) );
    double cr2 = 5.0;
    cout << "The left-side complex number is cl2 = " << cl2 << endl;
    cout << "The right-side complex number is cr2 = " << cr2 << endl;

    complex<double> cs2 = cl2 * cr2;
    cout << "Quotient of two complex numbers is: cs2 = cl2 * cr2 = "
        << cs2 << endl;

    // This is equivalent to the following operation
    cl2 *= cr2;
    cout << "Quotient of two complex numbers is also: cl2 *= cr2 = "
        << cl2 << endl;

    double abscl2 = abs ( cl2 );
    double argcl2 = arg ( cl2 );
    cout << "The modulus of cl2 is: " << abscl2 << endl;
    cout << "The argument of cl2 is: "<< argcl2 << " radians, which is "
        << argcl2 * 180 / pi << " degrees." << endl;
}

operator+=

Eklenen sayının karmaşık olabileceği veya eklendiği karmaşık sayının gerçek ve sanal bölümleriyle aynı türde olabileceği hedef karmaşık sayıya bir sayı ekler.

template <class Other>
complex<Type>& operator+=(const complex<Other>& right);

complex<Type>& operator+=(const Type& right);

complex<Type>& operator+=(const complex<Type>& right);

Parametreler

right
Karmaşık bir sayı veya hedef karmaşık sayının parametresiyle aynı türde bir sayı.

İade Değeri

Parametre olarak belirtilen sayı eklenmiş karmaşık bir sayı.

Açıklamalar

Basit aritmetik işlemlerin verilerin belirli bir biçime dönüştürülmeden yürütülebilmesi için işlem aşırı yüklenir.

Örnek

// complex_op_pe.cpp
// compile with: /EHsc
#include <complex>
#include <iostream>

int main( )
{
   using namespace std;
   double pi = 3.14159265359;

   // Example of the first member function
   // type complex<double> added to type complex<double>
   complex<double> cl1( 3.0 , 4.0 );
   complex<double> cr1( 2.0 , -1.0 );
   cout << "The left-side complex number is cl1 = " << cl1 << endl;
   cout << "The right-side complex number is cr1 = " << cr1 << endl;

   complex<double> cs1 = cl1 + cr1;
   cout << "The sum of the two complex numbers is: cs1 = cl1 + cr1 = "
        << cs1 << endl;

   // This is equivalent to the following operation
   cl1 += cr1;
   cout << "The complex number cr1 added to the complex number cl1 is:"
        << "\n cl1 += cr1 = " << cl1 << endl;

   double abscl1 = abs( cl1 );
   double argcl1 = arg( cl1 );
   cout << "The modulus of cl1 is: " << abscl1 << endl;
   cout << "The argument of cl1 is: "<< argcl1 << " radians, which is "
        << argcl1 * 180 / pi << " degrees." << endl << endl;

   // Example of the second member function
   // type double added to type complex<double>
   complex<double> cl2( -2 , 4 );
   double cr2 =5.0;
   cout << "The left-side complex number is cl2 = " << cl2 << endl;
   cout << "The right-side complex number is cr2 = " << cr2 << endl;

   complex<double> cs2 = cl2 + cr2;
   cout << "The sum of the two complex numbers is: cs2 = cl2 + cr2 = "
        << cs2 << endl;

   // This is equivalent to the following operation
   cl2 += cr2;
   cout << "The complex number cr2 added to the complex number cl2 is:"
        << "\n cl2 += cr2 = " << cl2 << endl;

   double abscl2 = abs( cl2 );
   double argcl2 = arg( cl2 );
   cout << "The modulus of cl2 is: " << abscl2 << endl;
   cout << "The argument of cl2 is: "<< argcl2 << " radians, which is "
        << argcl2 * 180 / pi << " degrees." << endl << endl;
}
The left-side complex number is cl1 = (3,4)
The right-side complex number is cr1 = (2,-1)
The sum of the two complex numbers is: cs1 = cl1 + cr1 = (5,3)
The complex number cr1 added to the complex number cl1 is:
cl1 += cr1 = (5,3)
The modulus of cl1 is: 5.83095
The argument of cl1 is: 0.54042 radians, which is 30.9638 degrees.

The left-side complex number is cl2 = (-2,4)
The right-side complex number is cr2 = 5
The sum of the two complex numbers is: cs2 = cl2 + cr2 = (3,4)
The complex number cr2 added to the complex number cl2 is:
cl2 += cr2 = (3,4)
The modulus of cl2 is: 5
The argument of cl2 is: 0.927295 radians, which is 53.1301 degrees.

operator-=

Bir sayıyı, çıkarılan sayının karmaşık olabileceği veya eklendiği karmaşık sayının gerçek ve sanal bölümleriyle aynı türde olabileceği hedef karmaşık sayıdan çıkarır.

template <class Other>
complex<Type>& operator-=(const complex<Other>& complexNum);

complex<Type>& operator-=(const Type& _RealPart);

complex<Type>& operator-=(const complex<Type>& complexNum);

Parametreler

complexNum
Hedef karmaşık sayıdan çıkarılacak karmaşık sayı.

_RealPart
Hedef karmaşık sayıdan çıkarılacak gerçek sayı.

İade Değeri

Parametre olarak belirtilen sayıyı ondan çıkarılmış karmaşık bir sayıdır.

Açıklamalar

Basit aritmetik işlemlerin verilerin belirli bir biçime dönüştürülmeden yürütülebilmesi için işlem aşırı yüklenir.

Örnek

// complex_op_se.cpp
// compile with: /EHsc
#include <complex>
#include <iostream>

int main( )
{
   using namespace std;
   double pi = 3.14159265359;

   // Example of the first member function
   // type complex<double> subtracted from type complex<double>
   complex<double> cl1( 3.0 , 4.0 );
   complex<double> cr1( 2.0 , -1.0 );
   cout << "The left-side complex number is cl1 = " << cl1 << endl;
   cout << "The right-side complex number is cr1 = " << cr1 << endl;

   complex<double> cs1 = cl1 - cr1;
   cout << "The difference between the two complex numbers is:"
        << "\n cs1 = cl1 - cr1 = " << cs1 << endl;

   // This is equivalent to the following operation
   cl1 -= cr1;
   cout << "Complex number cr1 subtracted from complex number cl1 is:"
        << "\n cl1 -= cr1 = " << cl1 << endl;

   double abscl1 = abs( cl1 );
   double argcl1 = arg( cl1 );
   cout << "The modulus of cl1 is: " << abscl1 << endl;
   cout << "The argument of cl1 is: "<< argcl1 << " radians, which is "
        << argcl1 * 180 / pi << " degrees." << endl << endl;

   // Example of the second member function
   // type double subtracted from type complex<double>
   complex<double> cl2( 2.0 , 4.0 );
   double cr2 = 5.0;
   cout << "The left-side complex number is cl2 = " << cl2 << endl;
   cout << "The right-side complex number is cr2 = " << cr2 << endl;

   complex<double> cs2 = cl2 - cr2;
   cout << "The difference between the two complex numbers is:"
        << "\n cs2 = cl2 - cr2 = " << cs2 << endl;

   // This is equivalent to the following operation
   cl2  -= cr2;
   cout << "Complex number cr2 subtracted from complex number cl2 is:"
        << "\n cl2 -= cr2 = " << cl2 << endl;

   double abscl2 = abs( cl2 );
   double argcl2 = arg( cl2 );
   cout << "The modulus of cl2 is: " << abscl2 << endl;
   cout << "The argument of cl2 is: "<< argcl2 << " radians, which is "
        << argcl2 * 180 / pi << " degrees." << endl << endl;
}
The left-side complex number is cl1 = (3,4)
The right-side complex number is cr1 = (2,-1)
The difference between the two complex numbers is:
cs1 = cl1 - cr1 = (1,5)
Complex number cr1 subtracted from complex number cl1 is:
cl1 -= cr1 = (1,5)
The modulus of cl1 is: 5.09902
The argument of cl1 is: 1.3734 radians, which is 78.6901 degrees.

The left-side complex number is cl2 = (2,4)
The right-side complex number is cr2 = 5
The difference between the two complex numbers is:
cs2 = cl2 - cr2 = (-3,4)
Complex number cr2 subtracted from complex number cl2 is:
cl2 -= cr2 = (-3,4)
The modulus of cl2 is: 5
The argument of cl2 is: 2.2143 radians, which is 126.87 degrees.

operator/=

Bir hedef karmaşık sayıyı, karmaşık veya karmaşık sayının gerçek ve hayali bölümleriyle aynı türde olabilecek bölenlere böler.

template <class Other>
complex<Type>& operator/=(const complex<Other>& complexNum);

complex<Type>& operator/=(const Type& _RealPart);

complex<Type>& operator/=(const complex<Type>& complexNum);

Parametreler

complexNum
Hedef karmaşık sayıdan çıkarılacak karmaşık sayı.

_RealPart
Hedef karmaşık sayıdan çıkarılacak gerçek sayı.

İade Değeri

Parametre olarak belirtilen sayıya bölünen karmaşık bir sayı.

Açıklamalar

Basit aritmetik işlemlerin verilerin belirli bir biçime dönüştürülmeden yürütülebilmesi için işlem aşırı yüklenir.

Örnek

// complex_op_de.cpp
// compile with: /EHsc
#include <complex>
#include <iostream>

int main( )
{
   using namespace std;
   double pi = 3.14159265359;

   // Example of the first member function
   // type complex<double> divided by type complex<double>
   complex<double> cl1( polar (3.0 , pi / 6 ) );
   complex<double> cr1( polar (2.0 , pi / 3 ) );
   cout << "The left-side complex number is cl1 = " << cl1 << endl;
   cout << "The right-side complex number is cr1 = " << cr1 << endl;

   complex<double> cs1 = cl1 / cr1;
   cout << "The quotient of the two complex numbers is: cs1 = cl1 /cr1 = "
        << cs1 << endl;

   // This is equivalent to the following operation
   cl1 /= cr1;
   cout << "Quotient of two complex numbers is also: cl1 /= cr1 = "
        << cl1 << endl;

   double abscl1 = abs( cl1 );
   double argcl1 = arg( cl1 );
   cout << "The modulus of cl1 is: " << abscl1 << endl;
   cout << "The argument of cl1 is: "<< argcl1 << " radians, which is "
        << argcl1 * 180 / pi << " degrees." << endl << endl;

   // Example of the second member function
   // type complex<double> divided by type double
   complex<double> cl2( polar(3.0 , pi / 6 ) );
   double cr2 =5;
   cout << "The left-side complex number is cl2 = " << cl2 << endl;
   cout << "The right-side complex number is cr2 = " << cr2 << endl;

   complex<double> cs2 = cl2 / cr2;
   cout << "The quotient of the two complex numbers is: cs2 /= cl2 cr2 = "
        << cs2 << endl;

   // This is equivalent to the following operation
   cl2 /= cr2;
   cout << "Quotient of two complex numbers is also: cl2 = /cr2 = "
        << cl2 << endl;

   double abscl2 = abs( cl2 );
   double argcl2 = arg( cl2 );
   cout << "The modulus of cl2 is: " << abscl2 << endl;
   cout << "The argument of cl2 is: "<< argcl2 << " radians, which is "
        << argcl2 * 180 / pi << " degrees." << endl << endl;
}
The left-side complex number is cl1 = (2.59808,1.5)
The right-side complex number is cr1 = (1,1.73205)
The quotient of the two complex numbers is: cs1 = cl1 /cr1 = (1.29904,-0.75)
Quotient of two complex numbers is also: cl1 /= cr1 = (1.29904,-0.75)
The modulus of cl1 is: 1.5
The argument of cl1 is: -0.523599 radians, which is -30 degrees.

The left-side complex number is cl2 = (2.59808,1.5)
The right-side complex number is cr2 = 5
The quotient of the two complex numbers is: cs2 /= cl2 cr2 = (0.519615,0.3)
Quotient of two complex numbers is also: cl2 = /cr2 = (0.519615,0.3)
The modulus of cl2 is: 0.6
The argument of cl2 is: 0.523599 radians, which is 30 degrees.

operator=

Bir sayıyı, atanan sayının karmaşık olabileceği veya atandığı karmaşık sayının gerçek ve sanal bölümleriyle aynı türde olabileceği hedef karmaşık bir sayıya atar.

template <class Other>
complex<Type>& operator=(const complex<Other>& right);

complex<Type>& operator=(const Type& right);

Parametreler

right
Karmaşık bir sayı veya hedef karmaşık sayının parametresiyle aynı türde bir sayı.

İade Değeri

Parametre olarak belirtilen sayı atanmış karmaşık bir sayı.

Açıklamalar

Basit aritmetik işlemlerin verilerin belirli bir biçime dönüştürülmeden yürütülebilmesi için işlem aşırı yüklenir.

Örnek

// complex_op_as.cpp
// compile with: /EHsc
#include <complex>
#include <iostream>

int main( )
{
   using namespace std;
   double pi = 3.14159265359;

   // Example of the first member function
   // type complex<double> assigned to type complex<double>
   complex<double> cl1( 3.0 , 4.0 );
   complex<double> cr1( 2.0 , -1.0 );
   cout << "The left-side complex number is cl1 = " << cl1 << endl;
   cout << "The right-side complex number is cr1 = " << cr1 << endl;

   cl1  = cr1;
   cout << "The complex number cr1 assigned to the complex number cl1 is:"
        << "\ncl1 = cr1 = " << cl1 << endl;

   // Example of the second member function
   // type double assigned to type complex<double>
   complex<double> cl2( -2 , 4 );
   double cr2 =5.0;
   cout << "The left-side complex number is cl2 = " << cl2 << endl;
   cout << "The right-side complex number is cr2 = " << cr2 << endl;

   cl2 = cr2;
   cout << "The complex number cr2 assigned to the complex number cl2 is:"
        << "\ncl2 = cr2 = " << cl2 << endl;

   cl2 = complex<double>(3.0, 4.0);
   cout << "The complex number (3, 4) assigned to the complex number cl2 is:"
        << "\ncl2 = " << cl2 << endl;
}
The left-side complex number is cl1 = (3,4)
The right-side complex number is cr1 = (2,-1)
The complex number cr1 assigned to the complex number cl1 is:
cl1 = cr1 = (2,-1)
The left-side complex number is cl2 = (-2,4)
The right-side complex number is cr2 = 5
The complex number cr2 assigned to the complex number cl2 is:
cl2 = cr2 = (5,0)
The complex number (3, 4) assigned to the complex number cl2 is:
cl2 = (3,4)

real

Karmaşık bir sayının real bileşenini alır veya ayarlar.

constexpr T real() const;

T real(const T& right);

Parametreler

right
Değeri ayıklanacak karmaşık bir sayı real .

İade Değeri

real Karmaşık sayının kısmı.

Açıklamalar

Karmaşık bir sayı a + bi için parça real veya bileşen Re(a + bi) = a'dır.

Örnek

// complex_class_real.cpp
// compile with: /EHsc
#include <complex>
#include <iostream>

int main( )
{
   using namespace std;

   complex<double> c1( 4.0 , 3.0 );
   cout << "The complex number c1 = " << c1 << endl;

   double dr1 = c1.real();
   cout << "The real part of c1 is c1.real() = "
        << dr1 << "." << endl;

   double di1 = c1.imag();
   cout << "The imaginary part of c1 is c1.imag() = "
        << di1 << "." << endl;
}
The complex number c1 = (4,3)
The real part of c1 is c1.real() = 4.
The imaginary part of c1 is c1.imag() = 3.

value_type

Karmaşık bir sayının gerçek ve sanal bölümlerini temsil etmek için kullanılan veri türünü temsil eden bir tür.

typedef Type value_type;

Açıklamalar

value_type , sınıf karmaşık Type şablon parametresinin eş anlamlısıdır.

Örnek

// complex_valuetype.cpp
// compile with: /EHsc
#include <complex>
#include <iostream>

int main( )
{
    using namespace std;
    complex<double>::value_type a = 3, b = 4;

    complex<double> c1 ( a , b );
    cout << "Specifying initial real & imaginary parts"
        << "\nof type value_type: "
        << "c1 = " << c1 << "." << endl;
}
Specifying initial real & imaginary parts
of type value_type: c1 = (3,4).

Ayrıca bkz.

C++ Standart Kitaplığında İş Parçacığı Güvenliği