
现代科学在很大程度上依赖于复数的概念,这一概念最初是通过Girolamo Cardano在16世纪引入的在17世纪初建立。复数的公式是 a + ib,其中 a 保留html代码并且b是实数。一个复数被认为有两个部分:实部和虚部()。i或iota的值为√-1。C++中的复数类是一个用于表示复数的类。C++中的complex类可以表示并控制几个复数操作。我们来看一下如何表示和控制显示复数。
imag()成员函数
如前所述,复数由实部和虚部两部分组成。
显示实部我们使用real()函数,使用imag()函数来显示给定复数的虚部。在下一个例子中,我们取一个复数number object, 初始化它,然后显示数字的实部和虚部分别。
语法
//displaying the imaginary part onlycomplex c;cout << imag(c) << endl;
算法
拿一个新的复数对象。
使用’a. + ib’表示法为对象赋值。
使用imag()函数显示复数值的虚部。
立即学习“C++免费学习笔记(深入)”;
示例
#include #include using namespace std;//displays the imaginary part of the complex numbervoid display(complex c){ cout << "The imaginary part of the complex number is: "; cout << imag(c) << endl;}//initializing the complex numbercomplex solve( double real, double img ){ complex cno(real, img); return cno;}int main(){ complex 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+11iThe imaginary part of the complex number is: 11
需要注意的是,imag()函数只接受复数作为输入,并且
返回所提供复数的虚部。输出值为
复数对象的模板参数。
结论
对于科学领域广泛的许多不同操作,复杂的
numbers are required. An interface for representing complex numbers is provided by theC++复数类,它是头文件的一部分。所有复数运算,包括加法、减法、乘法、共轭、范数等等,通过复数类支持。可以使用复数类轻松创建复数传统数值,正如我们在本文中刚刚讨论的。保持这些数值的重要性请注意,在展示复数时,实部和虚部的number must both be taken into consideration. Otherwise, several issues could arise. The程序员必须确保在和()>之间不犯错误imag()函数或其他情况下,值将被反转。
以上就是获取给定复数的虚部的C++程序的详细内容,更多请关注创想鸟其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1444517.html
微信扫一扫
支付宝扫一扫