Repository navigation
Expand file tree
/
Copy pathcomplex.cpp
More file actions
77 lines (72 loc) · 2.15 KB
/
Copy pathcomplex.cpp
File metadata and controls
77 lines (72 loc) · 2.15 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
#include<iostream>
#include "fraction.h"
using namespace std;
class ComplexNumber
{
private:
Fraction real;
Fraction imaginary;
public:
ComplexNumber(Fraction = 0.0, Fraction = 0.0);
void display();
ComplexNumber operator+(const ComplexNumber & ref);
ComplexNumber operator-(const ComplexNumber & ref);
ComplexNumber operator*(const ComplexNumber & ref);
ComplexNumber operator/( ComplexNumber & ref);
};
ComplexNumber :: ComplexNumber(Fraction real, Fraction imaginary): real(real), imaginary(imaginary){}
void ComplexNumber :: display(){
cout<<real<<" + "<<imaginary<<" i"<<endl;
}
ComplexNumber ComplexNumber :: operator+(const ComplexNumber & ref){
ComplexNumber s;
s.real=(this->real+ref.real);
s.imaginary=(this->imaginary+ ref.imaginary);
return s;
}
ComplexNumber ComplexNumber :: operator-(const ComplexNumber & ref){
ComplexNumber S;
S.real=(this->real-ref.real);
S.imaginary=(this->imaginary- ref.imaginary);
return S;
}
ComplexNumber ComplexNumber ::operator*(const ComplexNumber & ref){
ComplexNumber c;
c.real= (this->real*ref.real)-(this->imaginary*ref.imaginary);
c.imaginary=(this->imaginary*ref.real)+(this->real*ref.imaginary);
return c;
}
ComplexNumber ComplexNumber :: operator/( ComplexNumber & ref){
ComplexNumber C;
Fraction denom=(ref.real*ref.real)+(ref.imaginary*ref.imaginary);
C.real= ((this->real*ref.real)+(this->imaginary*(ref.imaginary)))/denom;
C.imaginary=((this->imaginary*ref.real)-(this->real*(ref.imaginary)))/denom;
return C;
}
int main(){
Fraction c1(2,3);
Fraction c2(3,4);
cout<<" c1 is: "<<endl;
c1.display();
cout<<endl;
cout<<" c2 is: "<<endl;
c2.display();
cout<<endl;
Fraction c3=c1+c2;
cout<<"after addition of c1+c2 ,c3 is: "<<endl;
c3.display();
cout<<endl;
Fraction c4=c3-c2;
cout<<"after subtraction of c3-c2 ,c4 is: "<<endl;
c4.display();
cout<<endl;
Fraction c5=c1*c2;
cout<<"after multiplication of c1*c2, c5 is: "<<endl;
c5.display();
cout<<endl;
Fraction c8=c1/c2;
cout<<"after division of c1/c2, c8 is: "<<endl;
c8.display();
cout<<endl;
return 0;
}