
使用类构造函数进行初始化
要使用复数类,我们必须导入复数库,可以通过导入或者
使用#include
语法
double value1 =; double value2 = ; complex cno(value1, value2);
算法
在两个数值变量中接受输入。
将这两个变量传递给复数的构造函数。
显示复数。
Example
的翻译为:示例
#include#include using namespace std; //displays the complex number supplied void display(complex c){ cout << "The complex number is: "; cout << real(c) << '+' << imag(c) << 'i' << endl; } //initializing the complex number complex 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 is complexcno = 15.0 + 6i;
算法
获取一个新的复数对象。
使用'a. + ib'表示法为对象分配一个值。
显示复数的值。
Example
的翻译为:示例
#include#include using namespace std; //displays the complex number supplied void 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 format complexc; cout << real(c) << '+' << imag(c) << 'i' << endl;
算法
获取一个新的复数对象。
使用'a. + ib'表示法将值分配给对象。
显示复数的值。
Example
的翻译为:示例
#include#include using namespace std; //displays the complex number supplied void display(complex c){ cout << "The complex number is: "; cout << real(c) << '+' << imag(c) << 'i' << endl; } //initializing the complex number complex 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++复数类,包含在头文件











