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

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

复数是现代科学中非常基本的概念,这个概念首先被引入到数学中在17世纪早期引入了。复数的形式为a + ib,其中a和b是实数。 a 称为实部,ib 称为 a 的虚部是实数。a被称为实部,ib被称为a的虚部复数。在C++中,有一个表示复数的类,它是复杂类。C++中的复杂类可以表示和操作各种操作关于复数。我们来看一下如何表示、初始化和显示复数数字。

使用类构造函数进行初始化

要使用复数类,我们必须导入复数库,可以通过导入或者使用#include 语句进行包含。声明和初始化一个复杂对象如下所述。在第一个示例中,我们使用构造函数进行赋值将复数的实部和虚部作为初始化的值。

语法

double value1 = ;double value2 = ;complex  cno(value1, value2);

算法

在两个数值变量中接受输入。

将这两个变量传递给复数的构造函数。

显示复数。

Example

的翻译为:

示例

#include #include using namespace std;//displays the complex number suppliedvoid display(complex  c){   cout << "The complex number is: ";   cout << real(c) << '+' << imag(c) << 'i' << endl;}//initializing the complex numbercomplex solve( double real, double img ){   complex 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

任何数值数据类型都可以用来替代复数变量的当前

立即学习“C++免费学习笔记(深入)”;

类型,为 double。

使用赋值运算符进行初始化

我们还可以使用赋值运算符将实部和虚部的值分配给一个复数数字。然而,要做到这一点,我们必须提供一个类型为”a + ib”的数字and “b”都是数值。如果数字是整数,则在空格中放置零在小数点后面。写实数时必须使用小数点“a”部分。例如,10 必须写为 10.0。

语法

//the real and imaginary parts have to be assigned as it iscomplex  cno = 15.0 + 6i;

算法

获取一个新的复数对象。

使用’a. + ib’表示法为对象分配一个值。

显示复数的值。

Example

的翻译为:

示例

#include #include using namespace std;//displays the complex number suppliedvoid display(complex  c){   cout << "The complex number is: ";   cout << real(c) << '+' << imag(c) << 'i' << endl;}int main(){      //initializing a complex number object   complex  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 formatcomplex c;cout << real(c) << '+' << imag(c) << 'i' << endl;

算法

获取一个新的复数对象。

使用’a. + ib’表示法将值分配给对象。

显示复数的值。

Example

的翻译为:

示例

#include #include using namespace std;//displays the complex number suppliedvoid display(complex  c){   cout << "The complex number is: ";   cout << real(c) << '+' << imag(c) << 'i' << endl;}//initializing the complex numbercomplex solve( double real, double img ){   complex 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++程序来初始化和打印一个复数的详细内容,更多请关注创想鸟其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1444192.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2025年12月17日 21:10:09
下一篇 2025年12月10日 05:11:11

发表回复

登录后才能评论
关注微信