-
Notifications
You must be signed in to change notification settings - Fork 35
/
complex-data-fetch
61 lines (61 loc) · 1.68 KB
/
complex-data-fetch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#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();
}