首页  >  文章  >  后端开发  >  C++程序来初始化和打印一个复数

C++程序来初始化和打印一个复数

王林
王林转载
2023-09-01 09:41:071340浏览

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删除