-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathcomplex.cpp
96 lines (78 loc) · 2 KB
/
complex.cpp
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// Define a class called complex that represents complex numbers.
// The class should contain data-members that stores real and imaginary parts moreover it should contain member functions
// that can implement the elementary operations(addition, subtraction, multiplication and division) of two complex numbers.
// The class should contain print member function that prints complex numbers and the result of operations on the screen in the form of a+ib.
#include <iostream>
using namespace std;
class complex
{
float a,b;
public:
complex(){a=0;b=0;}
complex(float x, float y)
{
a = x;
b = y;
}
void display()
{
cout << a << " + i" << b << endl;
}
friend complex addition(complex &o1, complex &o2);
friend complex subtraction(complex &o1, complex &o2);
friend complex multiplication(complex &o1, complex &o2);
friend complex division(complex &o1, complex &o2);
};
complex addition(complex &o1, complex &o2)
{
complex result;
result.a = o1.a + o2.a;
result.b = o1.b + o2.b;
return result;
}
complex subtraction(complex &o1, complex &o2)
{
complex result;
result.a = o1.a - o2.a;
result.b = o1.b - o2.b;
return result;
}
complex multiplication(complex &o1, complex &o2)
{
complex result;
float p,q,r,s;
p = o1.a * o2.a;
q = -(o1.b * o2.b);
r = o1.a * o2.b;
s = o1.b * o2.a;
result.a = p + q;
result.b = r + s;
return result;
}
complex division(complex &o1, complex &o2)
{
complex result, p, q, r;
p = o2;
p.b = -(p.b);
q = multiplication(o1,p);
r = multiplication(o2,p);
result.a = q.a/r.a;
result.b = q.b/r.a;
return result;
}
int main()
{
float a,b,c,d;
complex result;
cin >> a >> b >> c >> d;
complex x(a,b), y(c,d);
result = addition(x,y);
result.display();
result = subtraction(x,y);
result.display();
result = multiplication(x,y);
result.display();
result = division(x,y);
result.display();
return 0;
}