-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomplex_numbers.cpp
106 lines (99 loc) · 2.02 KB
/
complex_numbers.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
97
98
99
100
101
102
103
104
105
#include<iostream>
#include<math.h>
using namespace std;
class Complex
{
float real;
float imag;
public:
void input()
{
cout<<"Enter the real part: "<<endl;
cin>>real;
cout<<"Enter the imaginary part: "<<endl;
cin>>imag;
cout<<"\nThe complex number is: "<<endl;
if(imag>0)
{
cout<<"("<<real<<")"<<"+"<<"("<<imag<<"i"<<")"<<endl;
}
else if(imag<0)
{
int temp=imag;
temp=-temp;
cout<<"("<<real<<")"<<"+"<<"("<<imag<<"i"<<")"<<endl;
}
else
{
cout<<"("<<real<<")"<<"+"<<"("<<imag<<"i"<<")"<<endl;
}
}
void add(Complex c1, Complex c2)
{
real=c1.real+c2.real;
imag=c1.imag+c2.imag;
}
void sub(Complex c1, Complex c2)
{
real=c1.real-c2.real;
imag=c1.imag-c2.imag;
}
void mult(Complex c1, Complex c2)
{
real=((c1.real*c2.real)-(c1.imag*c2.imag));
imag=((c1.imag*c2.real)+(c1.real*c2.imag));
}
void div(Complex c1, Complex c2)
{
real=(((c1.real)*(c2.real))+((c1.imag)*(c2.imag)))/(pow(c2.real,2)+pow(c2.imag,2));
imag=(((c2.real)*(c1.imag))-((c1.real)*(c2.imag)))/(pow(c2.real,2)+pow(c2.imag,2));
}
void display()
{
if(imag>0)
{
cout<<"("<<real<<")"<<"+"<<"("<<imag<<"i"<<")"<<endl;
}
else if(imag<0)
{
imag=-imag;
cout<<"("<<real<<")"<<"-"<<"("<<imag<<"i"<<")"<<endl;
}
else
{
cout<<"("<<real<<")"<<"-"<<"("<<imag<<"i"<<")"<<endl;
}
}
Complex()
{
real=1;
imag=1;
}
};
int main()
{
Complex c1, c2, c3, c4, c5, c6;
Complex *ptr1, *ptr2, *ptr3, *ptr4, *ptr5, *ptr6;
cout<<"Enter first complex number.\n "<<endl;
ptr1=&c1;
ptr1->input();
cout<<"\nEnter second complex number. "<<endl;
ptr2=&c2;
ptr2->input();
cout<<"\nThe addition is: "<<endl;
ptr3=&c3;
ptr3->add(c1,c2);
ptr3->display();
cout<<"\nThe subtraction is: "<<endl;
ptr4=&c4;
ptr4->sub(c1,c2);
ptr4->display();
cout<<"\nThe multiplication is: "<<endl;
ptr5=&c5;
ptr5->mult(c1,c2);
ptr5->display();
cout<<"\nThe division is:"<<endl;
ptr6=&c6;
ptr6->div(c1,c2);
ptr6->display();
}