-
Notifications
You must be signed in to change notification settings - Fork 0
/
EEcalc.cpp
351 lines (308 loc) · 13.7 KB
/
EEcalc.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
#include <iostream>
#include <cmath>
#include <unordered_map>
#include <complex>
#include <vector>
using namespace std;
const double PI = 3.14159265358979323846;
// Function to calculate the Fourier series coefficients
vector<complex<double>> calculateFourierCoefficients(const vector<double>& signal) {
int N = signal.size();
vector<complex<double>> coefficients;
for (int k = 0; k < N; ++k) {
complex<double> sum = 0.0;
for (int n = 0; n < N; ++n) {
double angle = 2.0 * PI * k * n / N;
sum += signal[n] * polar(1.0, -angle);
}
coefficients.push_back(sum/static_cast<double>(N));
}
return coefficients;
}
class ElectricalCalculator {
public:
// Ohm's Law Calculator
static double calculateVoltage(double current, double resistance) {
return current * resistance;
}
static double calculateCurrent(double voltage, double resistance) {
return voltage / resistance;
}
static double calculateResistance(double voltage, double current) {
return voltage / current;
}
// Power Calculator
static double calculatePower(double voltage, double current) {
return voltage * current;
}
static double calculatePower2(double current, double resistance) {
return pow(current, 2) * resistance;
}
// Capacitance and Inductance Calculator
static double calculateImpedance(double resistance, double reactance) {
return sqrt(pow(resistance, 2) + pow(reactance, 2));
}
static double calculateReactance(double impedance, double resistance) {
return sqrt(pow(impedance, 2) - pow(resistance, 2));
}
static double calculatePhaseAngle(double resistance, double reactance) {
return atan(reactance / resistance);
}
// Resistor Color Code Decoder
static double decodeResistorColorCode(string color1, string color2, string color3) {
unordered_map<string, int> colorMap = {
{"black", 0}, {"brown", 1}, {"red", 2}, {"orange", 3}, {"yellow", 4},
{"green", 5}, {"blue", 6}, {"violet", 7}, {"gray", 8}, {"white", 9}
};
int value = colorMap[color1] * 10 + colorMap[color2];
double multiplier = pow(10, colorMap[color3]);
return value * multiplier;
}
// Series and Parallel Resistor Calculator
static double calculateEquivalentResistanceSeries(double resistances[], int count) {
double totalResistance = 0;
for (int i = 0; i < count; ++i) {
totalResistance += resistances[i];
}
return totalResistance;
}
static double calculateEquivalentResistanceParallel(double resistances[], int count) {
double totalInverseResistance = 0;
for (int i = 0; i < count; ++i) {
totalInverseResistance += 1.0 / resistances[i];
}
return 1.0 / totalInverseResistance;
}
// Time Constant Calculator
static double calculateTimeConstantRC(double resistance, double capacitance) {
return resistance * capacitance;
}
static double calculateTimeConstantRL(double resistance, double inductance) {
return resistance * inductance;
}
// Frequency and Wavelength Calculator
static double calculateFrequency(double speedOfLight, double wavelength) {
return speedOfLight / wavelength;
}
static double calculateWavelength(double speedOfLight, double frequency) {
return speedOfLight / frequency;
}
// Power Factor Calculator
static double calculatePowerFactor(double realPower, double apparentPower) {
return realPower / apparentPower;
}
// Three-Phase Power Calculator
static double calculateThreePhasePower(double voltage, double current, double powerFactor, int numberOfPhases) {
return sqrt(3.0) * voltage * current * powerFactor * numberOfPhases;
}
// Voltage Divider Calculator
static double calculateVoltageDivider(double sourceVoltage, double resistance1, double resistance2) {
return sourceVoltage * (resistance2 / (resistance1 + resistance2));
}
// Current Divider Calculator
static double calculateCurrentDivider(double sourceCurrent, double conductance1, double conductance2) {
return sourceCurrent * (conductance2 / (conductance1 + conductance2));
}
// Fourier Series Calculator
static void calculateFourierSeries(const vector<double>& signal, int numHarmonics) {
int N = signal.size();
vector<complex<double>> coefficients = calculateFourierCoefficients(signal);
// Display the Fourier series components
cout << "Fourier Series Components:\n";
for (int k = 0; k <= numHarmonics; ++k) {
complex<double> coeff = coefficients[k];
double magnitude = abs(coeff);
double phase = arg(coeff);
cout << "Harmonic " << k << ": Magnitude = " << magnitude << ", Phase = " << phase << " radians\n";
}
}
// Function to display menu and get user choice
static int getUserChoice() {
int choice;
cout << "\nChoose a calculation:\n";
cout << "1. Ohm's Law Calculator\n";
cout << "2. Power Calculator\n";
cout << "3. Capacitance and Inductance Calculator\n";
cout << "4. Resistor Color Code Decoder\n";
cout << "5. Series and Parallel Resistor Calculator\n";
cout << "6. Time Constant Calculator\n";
cout << "7. Frequency and Wavelength Calculator\n";
cout << "8. Power Factor Calculator\n";
cout << "9. Three-Phase Power Calculator\n";
cout << "10. Voltage Divider Calculator\n";
cout << "11. Current Divider Calculator\n";
cout << "12. Fourier Series Calculator\n";
cout << "0. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
return choice;
}
};
int main() {
int choice;
do {
choice = ElectricalCalculator::getUserChoice();
switch (choice) {
case 1: {
// Ohm's Law Calculator
double current, resistance, voltage;
cout << "Enter current (A): ";
cin >> current;
cout << "Enter resistance (Ohms): ";
cin >> resistance;
voltage = ElectricalCalculator::calculateVoltage(current, resistance);
cout << "Voltage (V): " << voltage << endl;
break;
}
case 2: {
// Power Calculator
double voltage, current;
cout << "Enter voltage (V): ";
cin >> voltage;
cout << "Enter current (A): ";
cin >> current;
double powerIV = ElectricalCalculator::calculatePower(voltage, current);
double powerI2R = ElectricalCalculator::calculatePower(current, 1.0); // Assuming R = 1 Ohm
cout << "Power (IV): " << powerIV << " watts" << endl;
cout << "Power (I^2R): " << powerI2R << " watts" << endl;
break;
}
case 3: {
// Capacitance and Inductance Calculator
double resistance, reactance, impedance, phaseAngle;
cout << "Enter resistance (Ohms): ";
cin >> resistance;
cout << "Enter reactance (Ohms): ";
cin >> reactance;
impedance = ElectricalCalculator::calculateImpedance(resistance, reactance);
cout << "Impedance (Ohms): " << impedance << endl;
phaseAngle = ElectricalCalculator::calculatePhaseAngle(resistance, reactance);
cout << "Phase Angle (radians): " << phaseAngle << endl;
break;
}
case 4: {
// Resistor Color Code Decoder
string color1, color2, color3;
cout << "Enter color bands (e.g., black, red, green, brown, orange, blue, violet, gray, white, yellow): ";
cin >> color1 >> color2 >> color3;
double resistance = ElectricalCalculator::decodeResistorColorCode(color1, color2, color3);
cout << "Decoded Resistance (Ohms): " << resistance << endl;
break;
}
case 5: {
// Series and Parallel Resistor Calculator
int count;
cout << "Enter the number of resistors: ";
cin >> count;
double resistances[count];
for (int i = 0; i < count; ++i) {
cout << "Enter resistance " << i + 1 << " (Ohms): ";
cin >> resistances[i];
}
cout << "Equivalent Resistance (Series): " << ElectricalCalculator::calculateEquivalentResistanceSeries(resistances, count) << " Ohms" << endl;
cout << "Equivalent Resistance (Parallel): " << ElectricalCalculator::calculateEquivalentResistanceParallel(resistances, count) << " Ohms" << endl;
break;
}
case 6: {
// Time Constant Calculator
double resistance, capacitance, inductance;
cout << "Enter resistance (Ohms): ";
cin >> resistance;
cout << "Enter capacitance (Farads): ";
cin >> capacitance;
cout << "Time Constant (RC): " << ElectricalCalculator::calculateTimeConstantRC(resistance, capacitance) << " seconds" << endl;
cout << "Enter resistance (Ohms): ";
cin >> resistance;
cout << "Enter inductance (Henrys): ";
cin >> inductance;
cout << "Time Constant (RL): " << ElectricalCalculator::calculateTimeConstantRL(resistance, inductance) << " seconds" << endl;
break;
}
case 7: {
// Frequency and Wavelength Calculator
double speedOfLight, frequency, wavelength;
cout << "Enter speed of light (m/s): ";
cin >> speedOfLight;
cout << "Enter frequency (Hz): ";
cin >> frequency;
cout << "Wavelength (meters): " << ElectricalCalculator::calculateWavelength(speedOfLight, frequency) << endl;
cout << "Enter wavelength (meters): ";
cin >> wavelength;
cout << "Frequency (Hz): " << ElectricalCalculator::calculateFrequency(speedOfLight, wavelength) << endl;
break;
}
case 8: {
// Power Factor Calculator
double realPower, apparentPower;
cout << "Enter real power (watts): ";
cin >> realPower;
cout << "Enter apparent power (VA): ";
cin >> apparentPower;
cout << "Power Factor: " << ElectricalCalculator::calculatePowerFactor(realPower, apparentPower) << endl;
break;
}
case 9: {
// Three-Phase Power Calculator
double voltage, current, powerFactor;
int numberOfPhases;
cout << "Enter line voltage (V): ";
cin >> voltage;
cout << "Enter line current (A): ";
cin >> current;
cout << "Enter power factor: ";
cin >> powerFactor;
cout << "Enter number of phases: ";
cin >> numberOfPhases;
cout << "Three-Phase Power: " << ElectricalCalculator::calculateThreePhasePower(voltage, current, powerFactor, numberOfPhases) << " VA" << endl;
break;
}
case 10: {
// Voltage Divider Calculator
double sourceVoltage, resistance1, resistance2;
cout << "Enter source voltage (V): ";
cin >> sourceVoltage;
cout << "Enter resistance 1 (Ohms): ";
cin >> resistance1;
cout << "Enter resistance 2 (Ohms): ";
cin >> resistance2;
cout << "Voltage Across Resistance 2: " << ElectricalCalculator::calculateVoltageDivider(sourceVoltage, resistance1, resistance2) << " V" << endl;
break;
}
case 11: {
// Current Divider Calculator
double sourceCurrent, conductance1, conductance2;
cout << "Enter source current (A): ";
cin >> sourceCurrent;
cout << "Enter conductance 1 (Siemens): ";
cin >> conductance1;
cout << "Enter conductance 2 (Siemens): ";
cin >> conductance2;
cout << "Current Through Conductance 2: " << ElectricalCalculator::calculateCurrentDivider(sourceCurrent, conductance1, conductance2) << " A" << endl;
break;
}
case 12: {
// Fourier Series Calculator
int numHarmonics;
cout << "Enter the number of harmonics to compute: ";
cin >> numHarmonics;
// Get the signal from the user
vector<double> signal;
cout << "Enter the signal values (space-separated): ";
for (int i = 0; i < numHarmonics; ++i) {
double value;
cin >> value;
signal.push_back(value);
}
// Perform Fourier series calculation
ElectricalCalculator::calculateFourierSeries(signal, numHarmonics);
break;
}
case 0:
cout << "Exiting the calculator. Goodbye!\n";
break;
default:
cout << "Invalid choice. Please try again.\n";
}
} while (choice != 0);
return 0;
}