C++ 中的 real( ) 函数


下面是 C++ 中 real( ) 函数的工作任务。

此函数用于查找复数的实部。此函数返回复数的实部,并将实部设置为值。而real 返回实数分量。real( ) 函数是 <complex> 头文件函数。

在复数 a+Bi 中,Bi 是虚部,A 是复数的实部。

返回 − 它返回指定复数的实部。

语法

Template<class Y> Y
Real(constant complex<Y>& X);

参数 − 参数 X,表示给定的复数。

示例

输出 – 复数 = (14.7, 6.7)

复数的实部 = 14.7

输出 – 复数 = (12, 12)

复数的实部 = 12

可遵循的方法

  • 首先定义复数。

  • 然后打印复数。

  • 然后打印复数的实部。

使用以上方法,我们可以得到所需的输出。real 函数将打印复数的实部。此函数返回与调用以下内容相同的值:X.real( )。

算法

Start-
STEP 1 – Create the main ( ) function and defines the complex number.
   complex<double> realpart(3.6, 2.4)
END
STEP 2 - Then print the real part using the real function.
   cout<< “ Real part of Complex number “<< real(realpart) << endl;
END
Stop

示例

// C++ code to demonstrate the working of real( ) function
#include<iostream.h>
#include<complex.h>
Using namespace std;
int main( ){
   //defines the complex number
   Complex<defines> real_part(17.6, 4.5) ;
   cout << “ Complex Number = “<<real_part << endl;
   // prints the real part using the real function
   cout<< “ Real part of complex number =”<< real(real_part) <<endl;
   return 0;
}

输出

如果运行以上代码,它将生成以下输出

Output – Complex Number = (17.6, 4.5)
   Real part of complex number = 17.6
Output – Complex Number = (14, 17)
   Real part of complex number = 14

更新于: 2020 年 2 月 28 日

997 次观看

开启您的 职业生涯

完成课程,获得认证

入门
广告
© . All rights reserved.