首頁  >  文章  >  後端開發  >  C++程式來初始化並列印一個複數

C++程式來初始化並列印一個複數

王林
王林轉載
2023-09-01 09:41:071341瀏覽

C++程式來初始化並列印一個複數

複數是現代科學中非常基本的概念,這個概念首先被引入數學中 在17世紀早期引進了。複數的形式為a ib,其中a和b 是實數。 a 稱為實部,ib 稱為 a 的虛部 是實數。 a稱為實部,ib被稱為a的虛部 複數。在C 中,有一個表示複數的類,它是 複雜類。 C 中的複雜類別可以表示和操作各種操作 關於複數。讓我們來看看如何表示、初始化和顯示複數 數字。

使用類別建構函式進行初始化

要使用複數類,我們必須導入複數庫,可以透過導入或 使用#include 語句進行包含。聲明和初始化一個 複雜對像如下所述。在第一個範例中,我們使用建構函式進行賦值 將複數的實部和虛部作為初始化的值。

文法

double value1 = <double value>;
double value2 = <double value>;
complex <double> cno(value1, value2);

演算法

  • 在兩個數值變數中接受輸入。

  • 將這兩個變數傳遞給複數的建構子。

  • 顯示複數。

Example

的翻譯為:

範例

#include <iostream>
#include <complex>
using namespace std;

//displays the complex number supplied
void display(complex <double> c){
   cout << "The complex number is: ";
   cout << real(c) << '+' << imag(c) << 'i' << endl;
}

//initializing the complex number
complex<double> solve( double real, double img ){
   complex<double> cno(real, img);
   return cno;
}
int main(){

   //the real and the imaginary values are represented as double values
   double v1 = 10;
   double v2 = 7;

   //creating and displaying the complex number
   display(solve(v1, v2));
   return 0;
}

輸出

The complex number is: 10+7i

任何數值資料型態都可以用來取代複數變數的目前

類型,為 double。

使用賦值運算子進行初始化

我們也可以使用賦值運算子將實部和虛部的值分配給一個複數 數字。然而,要做到這一點,我們必須提供一個類型為"a ib"的數字 and "b"都是數值。如果數字是整數,則在空格中放置零 在小數點後面。寫實數時必須使用小數點 “a”部分。例如,10 必須寫為 10.0。

文法

//the real and imaginary parts have to be assigned as it is
complex <double> cno = 15.0 + 6i;

演算法

  • 取得一個新的複數物件。

  • 使用'a. ib'表示法為物件指派一個值。

  • 顯示複數的值。

Example

的翻譯為:

範例

#include <iostream>
#include <complex>
using namespace std;

//displays the complex number supplied
void display(complex <double> c){
   cout << "The complex number is: ";
   cout << real(c) << '+' << imag(c) << 'i' << endl;
}
int main(){
   
   //initializing a complex number object
   complex <double> cno = 15. + 6i;
   
   //displaying the complex number
   display(cno);
   return 0;
}

輸出

The complex number is: 15+6i

顯示複數

使用"real()"和"imag()"函數,可以得到複數整數的實部和虛部。

被單獨顯示。 "real()"函數顯示複數的實部, 其中 "imag()" 函數顯示了複數的虛部。這裡是一個範例

樣本。

文法

//displaying in the a + ib format
complex<double> c;
cout << real(c) << '+' << imag(c) << 'i' << endl;

演算法

  • 取得一個新的複數物件。

  • 使用'a. ib'表示法將值指派給物件。

  • 顯示複數的值。

Example

的翻譯為:

範例

#include <iostream>
#include <complex>
using namespace std;
//displays the complex number supplied
void display(complex <double> c){
   cout << "The complex number is: ";
   cout << real(c) << '+' << imag(c) << 'i' << endl;
}
//initializing the complex number
complex<double> solve( double real, double img ){
   complex<double> cno(real, img);
   return cno;
}
int main(){
//the real and the imaginary values are represented as double values
   double v1 = 3;
   double v2 = 4;
//creating and displaying the complex number
   display(solve(v1, v2));
   return 0;
}

輸出

The complex number is: 3+4i

結論

複數在廣泛的範圍內需要用於許多不同的程式。

科學學科。 C 複數類,包含在頭檔中,提供了 表示複數的介面。複數類別支援所有操作 複數,包括加法、減法、乘法、共軛、範數和 大量的更多操作。正如我們在本文中已經討論過的那樣,這並不太困難 將常規的數值轉換為複數。我們必須注意,雖然 顯示複數時,我們需要注意實部和虛部 數字,否則可能會在程式中產生多個錯誤。

以上是C++程式來初始化並列印一個複數的詳細內容。更多資訊請關注PHP中文網其他相關文章!

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