-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
133 lines (96 loc) · 2.99 KB
/
Copy pathmain.cpp
File metadata and controls
133 lines (96 loc) · 2.99 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
class Complex{
private:
double img;
double real;
public:
Complex()=default;
Complex(double r, double i){
img = i;
real = r;
}
Complex(const Complex& c){
img = c.img;
real = c.real;
}
void printComplex(){
cout<<real<<" + "<<img<<"i"<<endl;
}
void setImg(double i){ img = i;}
void setReal(double r){ real = r;}
double getImg(){return img;}
double getReal(){ return real; }
friend istream &operator >>(istream &in , Complex &c){
cout<<"Enter the imaginary part: ";
in >>c.img;
cout<<"Enter the real part: ";
in >>c.real;
return in;
}
friend ostream& operator <<(ostream& os, Complex& c){
os<<c.getReal()<<" + "<<c.getImg()<<"i";
return os;
}
Complex operator+(Complex& c){
Complex res;
res.setImg(img+c.getImg());
res.setReal(real+c.getReal());
return res;
}
Complex operator-(Complex& c){
Complex res;
res.setImg(img-c.getImg());
res.setReal(real-c.getReal());
return res;
}
Complex operator*(Complex& c){
double resreal = real*c.getReal() - img*c.getImg();
double resimg = real*c.getImg() + img*c.getReal();
Complex res(resreal,resimg);
return res;
}
Complex operator/(Complex& c){
double resimg;
double resreal;
resreal = (real*c.getReal() + img*getImg())/(pow(c.getImg(),2)+pow(c.getReal(),2));
resimg = (img*c.getReal() - real*c.getImg())/(pow(c.getImg(),2)+pow(c.getReal(),2));
Complex res(resreal,resimg);
return res;
}
void complexModulus(){
double res = sqrt(pow(img,2)+ pow(real,2));
cout<<"|Z|= "<<res<<endl;
}
};
int main()
{
cout<<"==========={CREATED BY ELHIZABRI MAROUANE (AKA Floksy)}==========="<<endl;
Complex c(1,2);
Complex t;
cin>>t;
cout << "The complex object is ";
cout<<t<<endl;
cout<<c<<endl;
c.complexModulus();
Complex cc(2,2);
cout<<"Addition: "<<endl;
Complex c2= cc +c;
cout<<"("<<cc<<") + ("<<c<<") = "<<c2<<endl;
cout<<"division: "<<endl;
Complex c3 = cc/c;
cout<<"("<<cc<<") / ("<<c<<") = "<<c3<<endl;
cout<<"Multiplication: "<<endl;
Complex c4(1,3);
Complex c5(3,-7);
Complex c6 = c5*c4;
cout<<"("<<c4<<") * ("<<c5<<") = "<<c6<<endl;
cout<<"Subtraction: "<<endl;
Complex c7(-5,1);
Complex c8(-4,-6);
Complex c9 = c7 - c8;
cout<<"("<<c7<<") - ("<<c8<<") = "<<c9<<endl;
return 0;
}