-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkaran3.cpp
118 lines (108 loc) · 4.62 KB
/
karan3.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
106
107
108
109
110
111
112
113
114
115
116
117
118
#include<iostream>
#include<cmath>
using namespace std;
class ComplexCalculator {
public:
float real;
float imaginary;
void calculation() {
int choice;
while (1) {
cout << "Options:" << endl;
cout << "Click 1. To Add two complex numbers" << endl;
cout << "Click 2. To Subtract two complex numbers" << endl;
cout << "Click 3. To Multiply two complex numbers" << endl;
cout << "Click 4. To Divide two complex numbers" << endl;
cout << "Click 5. To Calculate modulus of a complex number" << endl;
cout << "Click 6. To Calculate argument (phase) of a complex number" << endl;
cout << "Click 7. To Calculate square root of a complex number" << endl;
cout << "Click 0. To Exit the program" << endl;
cout << "Enter your choice" << endl;
cin >> choice;
switch (choice) {
case 1:
performAddition();
break;
case 2:
performSubtraction();
break;
case 3:
performMultiplication();
break;
case 4:
performDivision();
break;
case 5:
calculateModulus();
break;
case 6:
calculateArgument();
break;
case 7:
calculateSquareRoot();
break;
case 0:
return;
default:
cout << "Invalid choice!" << endl;
}
}
}
void performAddition() {
cout << "Enter the real and imaginary parts of the first complex number: ";
cin >> real >> imaginary;
cout << "Enter the real and imaginary parts of the second complex number: ";
float real2, imaginary2;
cin >> real2 >> imaginary2;
cout << "Addition of two complex numbers is: " << real + real2 << " + " << imaginary + imaginary2 << "i" << endl;
}
void performSubtraction() {
cout << "Enter the real and imaginary parts of the first complex number: ";
cin >> real >> imaginary;
cout << "Enter the real and imaginary parts of the second complex number: ";
float real2, imaginary2;
cin >> real2 >> imaginary2;
cout << "Subtraction of two complex numbers is: " << real - real2 << " + " << imaginary - imaginary2 << "i" << endl;
}
void performMultiplication() {
cout << "Enter the real and imaginary parts of the first complex number: ";
cin >> real >> imaginary;
cout << "Enter the real and imaginary parts of the second complex number: ";
float real2, imaginary2;
cin >> real2 >> imaginary2;
cout << "Multiplication of two complex numbers is: " << (real * real2 - imaginary * imaginary2) << " + " << (real * imaginary2 + imaginary * real2) << "i" << endl;
}
void performDivision() {
cout << "Enter the real and imaginary parts of the first complex number: ";
cin >> real >> imaginary;
cout << "Enter the real and imaginary parts of the second complex number: ";
float real2, imaginary2;
cin >> real2 >> imaginary2;
cout << "Division of two complex numbers is: " << ((real * real2 + imaginary * imaginary2) / (real2 * real2 + imaginary2 * imaginary2)) << " + " << ((imaginary * real2 - real * imaginary2) / (real2 * real2 + imaginary2 * imaginary2)) << "i" << endl;
}
void calculateModulus() {
cout << "Enter the real and imaginary parts of the complex number: ";
cin >> real >> imaginary;
cout << "Modulus of the complex number is: " << sqrt(real * real + imaginary * imaginary) << endl;
}
void calculateArgument() {
cout << "Enter the real and imaginary parts of the complex number: ";
cin >> real >> imaginary;
double argument = atan2(imaginary, real); // atan2 returns the principal value of the argument in radians
cout << "Argument (phase) of the complex number is: " << argument << " radians" << endl;
}
void calculateSquareRoot() {
cout << "Enter the real and imaginary parts of the complex number: ";
cin >> real >> imaginary;
double mod = sqrt(real * real + imaginary * imaginary);
double arg = atan2(imaginary, real);
double sqrtMod = sqrt(mod);
double sqrtArg = arg / 2.0;
cout << "Square root of the complex number is: " << sqrtMod * cos(sqrtArg) << " + " << sqrtMod * sin(sqrtArg) << "i" << endl;
}
};
int main() {
ComplexCalculator cc;
cc.calculation();
return 0;
}