-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComplex_library.cpp
More file actions
79 lines (69 loc) · 1.68 KB
/
Complex_library.cpp
File metadata and controls
79 lines (69 loc) · 1.68 KB
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
#include<iostream>
#include<cmath>
using namespace std;
#define PI 3.14159265
class Complex
{
private:
float a, b;
public:
Complex(int x, int y)
{
a = x; b = y;
}
float modulus()
{
float mod = sqrt(pow(a, 2) + pow(b, 2));
return mod;
}
void print_number()
{
if (b<0)
{
cout<<a<<"-"<<-b<<'i'<<endl;;
}
else
cout<<a<<"+"<<b<<'i'<<endl;
}
Complex operator+(Complex c1)
{
return Complex(a+c1.a, b+c1.b);
}
Complex operator*(Complex c1)
{
return Complex(a*c1.a - b*c1.b, a*c1.b + b*c1.a);
}
Complex operator/(Complex c1)
{
if (c1.a == 0 && c1.b == 0)
{
cout<<"Division by zero error";
return Complex(0, 0);
}
Complex temp = Complex(a, b)*c1;
temp.a = temp.a/pow(c1.modulus(), 2);
temp.b = temp.b/pow(c1.modulus(), 2);
return temp;
}
float arg()
{
// the argument value of the number
float angle = atan(b/a);
angle = angle*180/PI;
return angle;
}
};
int main()
{
Complex c1(3, 4), c2(12, 7);
Complex c3 = c1+c2;
c3.print_number();
Complex ctest(1, 0);
Complex c4 = c1 * ctest;
c4.print_number();
Complex c5 = ctest/c1;
c5.print_number();
cout<<c1.arg();
Complex c6 = (c1 + c2) + c3;
c6.print_number();
}