首頁  >  文章  >  後端開發  >  取得給定複數的虛部的C++程序

取得給定複數的虛部的C++程序

PHPz
PHPz轉載
2023-09-06 18:05:05687瀏覽

取得給定複數的虛部的C++程序

現代科學在很大程度上依賴複數的概念,這一概念最初是透過Girolamo Cardano在16世紀引入的 在17世紀初建立。複數的公式是 a ib,其中 a 保留html程式碼 且b是實數。一個複數被認為有兩個部分:實部 和虛部()。 i或iota的值為√-1。 C 中的複數類別是一個 用於表示複數的類別。 C 中的complex類別可以表示 並控制幾個複數操作。讓我們來看看如何表示和控制 顯示複數。

imag()成員函數

如前所述,複數由實部和虛部兩部分組成。

顯示實部我們使用real()函數,使用imag()函數來顯示 給定複數的虛部。在下一個例子中,我們取一個複數 number object, 初始化它,然後顯示數字的實部和虛部 分別。

文法

//displaying the imaginary part only
complex<double> c;
cout << imag(c) << endl;

演算法

  • 拿一個新的複數物件。

  • 使用'a. ib'表示法為物件賦值。

  • 使用imag()函數顯示複數值的虛部。

範例

#include <iostream>
#include <complex>
using namespace std;
//displays the imaginary part of the complex number
void display(complex <double> c){
   cout << "The imaginary part of the complex number is: ";
   cout << imag(c) << endl;
}
//initializing the complex number
complex<double> solve( double real, double img ){
   complex<double> cno(real, img);
   return cno;
}
int main(){
   complex<double> cno = 10.0 + 11i;
   //displaying the complex number
   cout << "The complex number is: " << real(cno) << '+' << imag(cno) << 'i' << endl;
   display(cno);
   return 0;
}

輸出

The complex number is: 10+11i
The imaginary part of the complex number is: 11

要注意的是,imag()函數只接受複數作為輸入,並且

返回所提供複數的虛部。輸出值為

複數物件的模板參數。

結論

對於科學領域廣泛的許多不同操作,複雜的

numbers are required. An interface for representing complex numbers is provided by the C 複數類,它是頭檔的一部分。所有複數運算, 包括加法、減法、乘法、共軛、範數等等, 透過複數類支援。可以使用複數類別輕鬆建立複數 傳統數值,正如我們在本文中剛剛討論的。保持這些數值的重要性 請注意,在展示複數時,實部和虛部的 number must both be taken into consideration. Otherwise, several issues could arise. The 程式設計師必須確保在()>之間不犯​​錯 imag()函數或其他情況下,值將會反轉。

以上是取得給定複數的虛部的C++程序的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:tutorialspoint.com。如有侵權,請聯絡admin@php.cn刪除