C++ 中的复数
在本节中,我们将了解如何在 C++ 中创建和使用复数。我们可以在 C++ 中创建复数类,其中可以将复数的实部和虚部作为成员元素保留下来。有些成员函数用于处理该类。
在此示例中,我们创建了一个特定类型的复数类,有一个函数可以将复数显示为正确的格式。还有两种附加的方法可用于添加和减去两个复数等。
范例
#include<iostream>
using namespace std;
class complex {
int real, img;
public:
complex() {
//default constructor to initialize complex number to 0+0i
real = 0; img = 0;
}
complex(int r, int i) {
//parameterized constructor to initialize complex number.
real = r; img = i;
}
void set();
void get();
void display();
friend complex add(complex, complex);
friend complex sub(complex, complex);
};
void complex::set() {
cout << "Enter Real part: ";
cin >> real;
cout << "Enter Imaginary Part: ";
cin >> img;
}
void complex::get() {
cout << "The complex number is: "<< real << "+" << img << "i" << endl;
}
void complex::display() {
if(img < 0)
if(img == -1)
cout << "The complex number is: "<< real << "-i" << endl;
else
cout << "The complex number is: "<< real << img << "i" << endl;
else
if(img == 1)
cout << "The complex number is: "<< real << " + i"<< endl;
else
cout << "The complex number is: "<< real << " + " << img << "i" <<
endl;
}
complex add(complex c1, complex c2) {
complex res;
res.real = c1.real + c2.real;//addition for real part
res.img = c1.img + c2.img;//addition for imaginary part
return res;//the result after addition
}
complex sub(complex c1, complex c2) {
complex res;
res.real = c1.real - c2.real;//subtraction for real part
res.img = c1.img - c2.img;//subtraction for imaginary part
return res;//the result after subtraction
}
main() {
complex n1(3, 2), n2(4, -3);
complex result;
result = add(n1,n2);
result.display();
result = sub(n1,n2);
result.display();
}输出
The complex number is: 7-i The complex number is: -1 + 5i
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP