
要使用复数类,我们必须导入复数库,可以通过导入或者
使用#include
double value1 = <double value>; double value2 = <double value>; complex <double> cno(value1, value2);
在两个数值变量中接受输入。
将这两个变量传递给复数的构造函数。
显示复数。
#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
任何数值数据类型都可以用来替代复数变量的当前
立即学习“C++免费学习笔记(深入)”;
类型,为 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'表示法为对象分配一个值。
显示复数的值。
#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'表示法将值分配给对象。
显示复数的值。
#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中文网其它相关文章!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号