-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfreddy.cpp
More file actions
1418 lines (1202 loc) · 49.9 KB
/
freddy.cpp
File metadata and controls
1418 lines (1202 loc) · 49.9 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
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
include <iostream>
#include <cmath>
#include <ctime>
#include <iomanip>
#include <stdexcept>
#include <vector>
#include <algorithm>
#include <sstream>
#include <functional>
#include <map>
#include <limits>
#include <thread>
#include <chrono>
#include <random>
using namespace std;
// Constants
const double PI = 3.14159265358979323846;
const double EPSILON = 1e-10; // For numerical precision
// Currency structure and exchange rates
struct Currency {
string code;
string name;
string symbol;
double toUSD; // Exchange rate to USD
time_t lastUpdated;
};
// Global currency database
map<string, Currency> currencies = {
{"USD", {"USD", "US Dollar", "$", 1.0, time(0)}},
{"EUR", {"EUR", "Euro", "€", 0.85, time(0)}},
{"GBP", {"GBP", "British Pound", "£", 0.73, time(0)}},
{"JPY", {"JPY", "Japanese Yen", "¥", 110.25, time(0)}},
{"CAD", {"CAD", "Canadian Dollar", "C$", 1.25, time(0)}},
{"AUD", {"AUD", "Australian Dollar", "A$", 1.35, time(0)}},
{"CHF", {"CHF", "Swiss Franc", "CHF", 0.92, time(0)}},
{"CNY", {"CNY", "Chinese Yuan", "¥", 6.45, time(0)}},
{"INR", {"INR", "Indian Rupee", "₹", 74.50, time(0)}},
{"BRL", {"BRL", "Brazilian Real", "R$", 5.20, time(0)}},
{"RUB", {"RUB", "Russian Ruble", "₽", 73.80, time(0)}},
{"MXN", {"MXN", "Mexican Peso", "MX$", 20.15, time(0)}},
{"KRW", {"KRW", "South Korean Won", "₩", 1180.0, time(0)}},
{"SGD", {"SGD", "Singapore Dollar", "S$", 1.35, time(0)}},
{"NZD", {"NZD", "New Zealand Dollar", "NZ$", 1.45, time(0)}}
};
// Function prototypes
void displayMenu();
double add(double a, double b);
double subtract(double a, double b);
double multiply(double a, double b);
double divide(double a, double b);
double modulo(double a, double b);
double square(double x);
double squareRoot(double x);
double power(double base, double exponent);
void displayCurrentDate();
int calculateAge(int birthYear);
double sine(double x);
double cosine(double x);
double tangent(double x);
double logarithm(double x);
double factorial(int n);
vector<double> quadraticEquation(double a, double b, double c);
double standardDeviation(const vector<double>& data);
void statisticsCalculator();
void matrixOperations();
void scientificCalculator();
void conversionCalculator();
void printMatrix(const vector<vector<double>>& matrix);
void printHistory(const vector<string>& history);
// New function prototypes for differentiation and integration
double numericalDerivative(const function<double(double)>& f, double x, double h = 1e-5);
double numericalIntegration(const function<double(double)>& f, double a, double b, int n = 1000);
void calculusCalculator();
double evaluateExpression(const string& expression, double x);
void functionAnalyzer();
// Currency conversion prototypes
void currencyConverter();
double convertCurrency(double amount, const string& from, const string& to);
void displayAllCurrencies();
void updateExchangeRates(bool forceUpdate = false);
void simulateMarketFluctuations();
void addCustomCurrency();
void manageCurrencies();
string getCurrencySymbol(const string& code);
bool currencyExists(const string& code);
// Enhanced menu and utility functions
void clearScreen();
void pauseScreen();
string formatNumber(double num, int precision = 6);
string formatCurrency(double amount, const string& currencyCode);
// Memory function prototypes
void memoryOperations();
double memoryRecall();
void memoryStore(double value);
void memoryClear();
// Global variables for memory
double memory = 0.0;
bool memoryStored = false;
// Global variable for auto-update
bool autoUpdateEnabled = false;
const int AUTO_UPDATE_INTERVAL = 300; // 5 minutes in seconds
int main() {
vector<string> calculationHistory;
int choice;
double num1, num2, result;
bool running = true;
// Initialize currency rates
updateExchangeRates();
cout << "=== ADVANCED MATHEMATICAL CALCULATOR ===" << endl;
cout << "Created with C++" << endl;
cout << "Includes Real-Time Currency Conversion" << endl << endl;
while (running) {
displayMenu();
cout << "Enter your choice: ";
cin >> choice;
try {
switch (choice) {
case 1: // Addition
cout << "Enter two numbers: ";
cin >> num1 >> num2;
result = add(num1, num2);
cout << "Result: " << num1 << " + " << num2 << " = " << formatNumber(result) << endl;
calculationHistory.push_back(to_string(num1) + " + " + to_string(num2) + " = " + to_string(result));
break;
case 2: // Subtraction
cout << "Enter two numbers: ";
cin >> num1 >> num2;
result = subtract(num1, num2);
cout << "Result: " << num1 << " - " << num2 << " = " << formatNumber(result) << endl;
calculationHistory.push_back(to_string(num1) + " - " + to_string(num2) + " = " + to_string(result));
break;
case 3: // Multiplication
cout << "Enter two numbers: ";
cin >> num1 >> num2;
result = multiply(num1, num2);
cout << "Result: " << num1 << " * " << num2 << " = " << formatNumber(result) << endl;
calculationHistory.push_back(to_string(num1) + " * " + to_string(num2) + " = " + to_string(result));
break;
case 4: // Division
cout << "Enter two numbers: ";
cin >> num1 >> num2;
if (abs(num2) < EPSILON) throw runtime_error("Error: Division by zero!");
result = divide(num1, num2);
cout << "Result: " << num1 << " / " << num2 << " = " << formatNumber(result) << endl;
calculationHistory.push_back(to_string(num1) + " / " + to_string(num2) + " = " + to_string(result));
break;
case 5: // Modulo
cout << "Enter two numbers: ";
cin >> num1 >> num2;
if (abs(num2) < EPSILON) throw runtime_error("Error: Division by zero in modulo!");
result = modulo(num1, num2);
cout << "Result: " << num1 << " % " << num2 << " = " << formatNumber(result) << endl;
calculationHistory.push_back(to_string(num1) + " % " + to_string(num2) + " = " + to_string(result));
break;
case 6: // Square
cout << "Enter a number: ";
cin >> num1;
result = square(num1);
cout << "Result: " << num1 << "² = " << formatNumber(result) << endl;
calculationHistory.push_back(to_string(num1) + "² = " + to_string(result));
break;
case 7: // Square Root
cout << "Enter a number: ";
cin >> num1;
if (num1 < 0) throw runtime_error("Error: Square root of negative number!");
result = squareRoot(num1);
cout << "Result: √" << num1 << " = " << formatNumber(result) << endl;
calculationHistory.push_back("√" + to_string(num1) + " = " + to_string(result));
break;
case 8: // Power
cout << "Enter base and exponent: ";
cin >> num1 >> num2;
result = power(num1, num2);
cout << "Result: " << num1 << "^" << num2 << " = " << formatNumber(result) << endl;
calculationHistory.push_back(to_string(num1) + "^" + to_string(num2) + " = " + to_string(result));
break;
case 9: // Current Date
displayCurrentDate();
break;
case 10: // Age Calculator
int birthYear;
cout << "Enter your birth year: ";
cin >> birthYear;
cout << "Your age is: " << calculateAge(birthYear) << " years" << endl;
calculationHistory.push_back("Age calculation for birth year " + to_string(birthYear));
break;
case 11: // Scientific Calculator
scientificCalculator();
break;
case 12: // Quadratic Equation
double a, b, c;
cout << "Enter coefficients a, b, c: ";
cin >> a >> b >> c;
{
vector<double> roots = quadraticEquation(a, b, c);
if (roots.empty()) {
cout << "No real roots" << endl;
calculationHistory.push_back("Quadratic equation " + to_string(a) + "x² + " +
to_string(b) + "x + " + to_string(c) + " has no real roots");
} else if (roots.size() == 1) {
cout << "Double root: x = " << formatNumber(roots[0]) << endl;
calculationHistory.push_back("Quadratic equation " + to_string(a) + "x² + " +
to_string(b) + "x + " + to_string(c) + " has double root x = " +
to_string(roots[0]));
} else {
cout << "Roots: x1 = " << formatNumber(roots[0]) << ", x2 = " << formatNumber(roots[1]) << endl;
calculationHistory.push_back("Quadratic equation " + to_string(a) + "x² + " +
to_string(b) + "x + " + to_string(c) + " has roots x1 = " +
to_string(roots[0]) + ", x2 = " + to_string(roots[1]));
}
}
break;
case 13: // Statistics
statisticsCalculator();
break;
case 14: // Matrix Operations
matrixOperations();
break;
case 15: // Unit Conversions
conversionCalculator();
break;
case 16: // Factorial
int n;
cout << "Enter a number: ";
cin >> n;
result = factorial(n);
cout << "Result: Factorial(" << n << ") = " << formatNumber(result) << endl;
calculationHistory.push_back("Factorial(" + to_string(n) + ") = " + to_string(result));
break;
case 17: // Calculus Operations
calculusCalculator();
break;
case 18: // Function Analyzer
functionAnalyzer();
break;
case 19: // Currency Converter
currencyConverter();
break;
case 20: // Memory Operations
memoryOperations();
break;
case 21: // View History
printHistory(calculationHistory);
break;
case 22: // Clear Screen
clearScreen();
break;
case 23: // Manage Currencies
manageCurrencies();
break;
case 24: // Update Exchange Rates
updateExchangeRates(true);
cout << "Exchange rates updated successfully!" << endl;
break;
case 0: // Exit
running = false;
cout << "Exiting calculator. Goodbye!" << endl;
break;
default:
cout << "Invalid choice. Please try again." << endl;
}
} catch (const exception& e) {
cerr << e.what() << endl;
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
if (choice != 0 && choice != 22) {
pauseScreen();
}
}
return 0;
}
void displayMenu() {
cout << "\n===== MAIN MENU =====" << endl;
cout << "MATHEMATICAL OPERATIONS:" << endl;
cout << " 1. Addition" << endl;
cout << " 2. Subtraction" << endl;
cout << " 3. Multiplication" << endl;
cout << " 4. Division" << endl;
cout << " 5. Modulo" << endl;
cout << " 6. Square" << endl;
cout << " 7. Square Root" << endl;
cout << " 8. Power" << endl;
cout << " 9. Current Date" << endl;
cout << "10. Age Calculator" << endl;
cout << "11. Scientific Calculator" << endl;
cout << "12. Quadratic Equation Solver" << endl;
cout << "13. Statistics Calculator" << endl;
cout << "14. Matrix Operations" << endl;
cout << "15. Unit Conversions" << endl;
cout << "16. Factorial" << endl;
cout << "17. Calculus Operations" << endl;
cout << "18. Function Analyzer" << endl;
cout << "\nCURRENCY & FINANCE:" << endl;
cout << "19. Currency Converter" << endl;
cout << "23. Manage Currencies" << endl;
cout << "24. Update Exchange Rates" << endl;
cout << "\nUTILITIES:" << endl;
cout << "20. Memory Operations" << endl;
cout << "21. View Calculation History" << endl;
cout << "22. Clear Screen" << endl;
cout << " 0. Exit" << endl;
}
// Basic arithmetic operations
double add(double a, double b) { return a + b; }
double subtract(double a, double b) { return a - b; }
double multiply(double a, double b) { return a * b; }
double divide(double a, double b) { return a / b; }
double modulo(double a, double b) { return fmod(a, b); }
double square(double x) { return x * x; }
double squareRoot(double x) { return sqrt(x); }
double power(double base, double exponent) { return pow(base, exponent); }
// Date and time functions
void displayCurrentDate() {
time_t now = time(0);
tm* localTime = localtime(&now);
cout << "Current date: "
<< 1900 + localTime->tm_year << "-"
<< 1 + localTime->tm_mon << "-"
<< localTime->tm_mday << endl;
}
int calculateAge(int birthYear) {
time_t now = time(0);
tm* localTime = localtime(&now);
int currentYear = 1900 + localTime->tm_year;
return currentYear - birthYear;
}
// Trigonometric functions (accept degrees)
double sine(double x) { return sin(x * PI / 180.0); }
double cosine(double x) { return cos(x * PI / 180.0); }
double tangent(double x) { return tan(x * PI / 180.0); }
// Logarithmic function
double logarithm(double x) {
if (x <= 0) throw runtime_error("Error: Logarithm of non-positive number!");
return log10(x);
}
// Factorial function
double factorial(int n) {
if (n < 0) throw runtime_error("Error: Factorial of negative number!");
if (n == 0 || n == 1) return 1;
// Use gamma function for larger values to avoid overflow
if (n > 20) {
return tgamma(n + 1);
}
double result = 1;
for (int i = 2; i <= n; ++i) {
result *= i;
}
return result;
}
// Quadratic equation solver
vector<double> quadraticEquation(double a, double b, double c) {
vector<double> roots;
if (abs(a) < EPSILON) {
if (abs(b) < EPSILON) {
if (abs(c) < EPSILON) {
roots.push_back(0); // Infinite solutions, return 0 as representative
}
// Otherwise no solution
} else {
roots.push_back(-c / b); // Linear equation
}
return roots;
}
double discriminant = b * b - 4 * a * c;
if (discriminant > 0) {
roots.push_back((-b + sqrt(discriminant)) / (2 * a));
roots.push_back((-b - sqrt(discriminant)) / (2 * a));
} else if (abs(discriminant) < EPSILON) {
roots.push_back(-b / (2 * a));
}
return roots;
}
// Statistics functions
double standardDeviation(const vector<double>& data) {
if (data.empty()) return 0;
double sum = 0;
for (double num : data) {
sum += num;
}
double mean = sum / data.size();
double variance = 0;
for (double num : data) {
variance += pow(num - mean, 2);
}
variance /= data.size();
return sqrt(variance);
}
void statisticsCalculator() {
vector<double> data;
double num;
string input;
cout << "=== STATISTICS CALCULATOR ===" << endl;
cout << "Enter data points (enter 'done' when finished):" << endl;
while (true) {
cout << "Enter number: ";
cin >> input;
if (input == "done" || input == "DONE") break;
try {
num = stod(input);
data.push_back(num);
} catch (const exception& e) {
cout << "Invalid input. Please enter a number or 'done' to finish." << endl;
cin.clear();
}
}
if (data.empty()) {
cout << "No data entered." << endl;
return;
}
// Calculate statistics
double sum = 0, minVal = data[0], maxVal = data[0];
for (double num : data) {
sum += num;
if (num < minVal) minVal = num;
if (num > maxVal) maxVal = num;
}
double mean = sum / data.size();
double median = 0;
// Calculate median
vector<double> sortedData = data;
sort(sortedData.begin(), sortedData.end());
if (sortedData.size() % 2 == 1) {
median = sortedData[sortedData.size() / 2];
} else {
median = (sortedData[sortedData.size() / 2 - 1] + sortedData[sortedData.size() / 2]) / 2;
}
double stdDev = standardDeviation(data);
// Calculate mode
map<double, int> frequency;
for (double num : sortedData) {
frequency[num]++;
}
double mode = sortedData[0];
int maxCount = 1;
for (const auto& pair : frequency) {
if (pair.second > maxCount) {
maxCount = pair.second;
mode = pair.first;
}
}
// Display results
cout << "\n=== STATISTICS RESULTS ===" << endl;
cout << "Count: " << data.size() << endl;
cout << "Sum: " << formatNumber(sum) << endl;
cout << "Mean: " << formatNumber(mean) << endl;
cout << "Median: " << formatNumber(median) << endl;
cout << "Mode: " << formatNumber(mode) << " (appears " << maxCount << " times)" << endl;
cout << "Minimum: " << formatNumber(minVal) << endl;
cout << "Maximum: " << formatNumber(maxVal) << endl;
cout << "Standard Deviation: " << formatNumber(stdDev) << endl;
}
// Matrix operations
void matrixOperations() {
int rows, cols;
cout << "=== MATRIX OPERATIONS ===" << endl;
cout << "Enter number of rows: ";
cin >> rows;
cout << "Enter number of columns: ";
cin >> cols;
vector<vector<double>> matrix1(rows, vector<double>(cols));
vector<vector<double>> matrix2(rows, vector<double>(cols));
// Input matrix 1
cout << "Enter elements of matrix 1:" << endl;
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
cout << "Element [" << i << "][" << j << "]: ";
cin >> matrix1[i][j];
}
}
// Input matrix 2
cout << "Enter elements of matrix 2:" << endl;
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
cout << "Element [" << i << "][" << j << "]: ";
cin >> matrix2[i][j];
}
}
// Perform operations
vector<vector<double>> sum(rows, vector<double>(cols));
vector<vector<double>> difference(rows, vector<double>(cols));
vector<vector<double>> product(rows, vector<double>(cols));
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
sum[i][j] = matrix1[i][j] + matrix2[i][j];
difference[i][j] = matrix1[i][j] - matrix2[i][j];
product[i][j] = matrix1[i][j] * matrix2[i][j];
}
}
// Display results
cout << "\n=== MATRIX RESULTS ===" << endl;
cout << "Matrix 1:" << endl;
printMatrix(matrix1);
cout << "Matrix 2:" << endl;
printMatrix(matrix2);
cout << "Sum:" << endl;
printMatrix(sum);
cout << "Difference:" << endl;
printMatrix(difference);
cout << "Element-wise Product:" << endl;
printMatrix(product);
}
void printMatrix(const vector<vector<double>>& matrix) {
for (const auto& row : matrix) {
for (double num : row) {
cout << setw(12) << formatNumber(num, 4);
}
cout << endl;
}
cout << endl;
}
// Scientific calculator functions
void scientificCalculator() {
int sciChoice;
double num, result;
bool sciRunning = true;
while (sciRunning) {
cout << "\n=== SCIENTIFIC CALCULATOR ===" << endl;
cout << "1. Sine (degrees)" << endl;
cout << "2. Cosine (degrees)" << endl;
cout << "3. Tangent (degrees)" << endl;
cout << "4. Logarithm (base 10)" << endl;
cout << "5. Natural Logarithm (base e)" << endl;
cout << "6. Factorial" << endl;
cout << "7. Degrees to Radians" << endl;
cout << "8. Radians to Degrees" << endl;
cout << "9. Exponential (e^x)" << endl;
cout << "10. Absolute Value" << endl;
cout << "0. Back to Main Menu" << endl;
cout << "Enter your choice: ";
cin >> sciChoice;
try {
switch (sciChoice) {
case 1:
cout << "Enter angle in degrees: ";
cin >> num;
result = sine(num);
cout << "sin(" << num << "°) = " << formatNumber(result) << endl;
break;
case 2:
cout << "Enter angle in degrees: ";
cin >> num;
result = cosine(num);
cout << "cos(" << num << "°) = " << formatNumber(result) << endl;
break;
case 3:
cout << "Enter angle in degrees: ";
cin >> num;
result = tangent(num);
cout << "tan(" << num << "°) = " << formatNumber(result) << endl;
break;
case 4:
cout << "Enter number: ";
cin >> num;
result = logarithm(num);
cout << "log10(" << num << ") = " << formatNumber(result) << endl;
break;
case 5:
cout << "Enter number: ";
cin >> num;
if (num <= 0) throw runtime_error("Error: Logarithm of non-positive number!");
result = log(num);
cout << "ln(" << num << ") = " << formatNumber(result) << endl;
break;
case 6:
int n;
cout << "Enter integer: ";
cin >> n;
result = factorial(n);
cout << n << "! = " << formatNumber(result) << endl;
break;
case 7:
cout << "Enter angle in degrees: ";
cin >> num;
result = num * PI / 180.0;
cout << num << "° = " << formatNumber(result) << " radians" << endl;
break;
case 8:
cout << "Enter angle in radians: ";
cin >> num;
result = num * 180.0 / PI;
cout << num << " radians = " << formatNumber(result) << "°" << endl;
break;
case 9:
cout << "Enter exponent: ";
cin >> num;
result = exp(num);
cout << "e^" << num << " = " << formatNumber(result) << endl;
break;
case 10:
cout << "Enter number: ";
cin >> num;
result = abs(num);
cout << "|" << num << "| = " << formatNumber(result) << endl;
break;
case 0:
sciRunning = false;
break;
default:
cout << "Invalid choice. Try again." << endl;
}
} catch (const exception& e) {
cerr << e.what() << endl;
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
if (sciChoice != 0) {
pauseScreen();
}
}
}
// Unit conversion functions
void conversionCalculator() {
int convChoice;
double value, result;
bool convRunning = true;
while (convRunning) {
cout << "\n=== UNIT CONVERSION CALCULATOR ===" << endl;
cout << "1. Celsius to Fahrenheit" << endl;
cout << "2. Fahrenheit to Celsius" << endl;
cout << "3. Kilometers to Miles" << endl;
cout << "4. Miles to Kilometers" << endl;
cout << "5. Kilograms to Pounds" << endl;
cout << "6. Pounds to Kilograms" << endl;
cout << "7. Meters to Feet" << endl;
cout << "8. Feet to Meters" << endl;
cout << "9. Liters to Gallons" << endl;
cout << "10. Gallons to Liters" << endl;
cout << "0. Back to Main Menu" << endl;
cout << "Enter your choice: ";
cin >> convChoice;
switch (convChoice) {
case 1:
cout << "Enter temperature in Celsius: ";
cin >> value;
result = (value * 9/5) + 32;
cout << value << "°C = " << formatNumber(result) << "°F" << endl;
break;
case 2:
cout << "Enter temperature in Fahrenheit: ";
cin >> value;
result = (value - 32) * 5/9;
cout << value << "°F = " << formatNumber(result) << "°C" << endl;
break;
case 3:
cout << "Enter distance in kilometers: ";
cin >> value;
result = value * 0.621371;
cout << value << " km = " << formatNumber(result) << " miles" << endl;
break;
case 4:
cout << "Enter distance in miles: ";
cin >> value;
result = value * 1.60934;
cout << value << " miles = " << formatNumber(result) << " km" << endl;
break;
case 5:
cout << "Enter weight in kilograms: ";
cin >> value;
result = value * 2.20462;
cout << value << " kg = " << formatNumber(result) << " lbs" << endl;
break;
case 6:
cout << "Enter weight in pounds: ";
cin >> value;
result = value * 0.453592;
cout << value << " lbs = " << formatNumber(result) << " kg" << endl;
break;
case 7:
cout << "Enter length in meters: ";
cin >> value;
result = value * 3.28084;
cout << value << " m = " << formatNumber(result) << " ft" << endl;
break;
case 8:
cout << "Enter length in feet: ";
cin >> value;
result = value * 0.3048;
cout << value << " ft = " << formatNumber(result) << " m" << endl;
break;
case 9:
cout << "Enter volume in liters: ";
cin >> value;
result = value * 0.264172;
cout << value << " L = " << formatNumber(result) << " gallons" << endl;
break;
case 10:
cout << "Enter volume in gallons: ";
cin >> value;
result = value * 3.78541;
cout << value << " gallons = " << formatNumber(result) << " L" << endl;
break;
case 0:
convRunning = false;
break;
default:
cout << "Invalid choice. Try again." << endl;
}
if (convChoice != 0) {
pauseScreen();
}
}
}
// Print calculation history
void printHistory(const vector<string>& history) {
if (history.empty()) {
cout << "No calculations in history." << endl;
return;
}
cout << "=== CALCULATION HISTORY ===" << endl;
for (size_t i = 0; i < history.size(); ++i) {
cout << i+1 << ". " << history[i] << endl;
}
}
// Calculus functions
double numericalDerivative(const function<double(double)>& f, double x, double h) {
// Using central difference for better accuracy
return (f(x + h) - f(x - h)) / (2 * h);
}
double numericalIntegration(const function<double(double)>& f, double a, double b, int n) {
// Simpson's rule for numerical integration
if (n % 2 != 0) n++; // Ensure n is even
double h = (b - a) / n;
double sum = f(a) + f(b);
for (int i = 1; i < n; i++) {
double x = a + i * h;
if (i % 2 == 0) {
sum += 2 * f(x);
} else {
sum += 4 * f(x);
}
}
return sum * h / 3;
}
void calculusCalculator() {
int calcChoice;
double a, b, x, result;
bool calcRunning = true;
// Predefined functions
map<int, string> functions = {
{1, "sin(x)"},
{2, "cos(x)"},
{3, "x^2"},
{4, "x^3"},
{5, "e^x"},
{6, "ln(x)"},
{7, "1/x"}
};
while (calcRunning) {
cout << "\n=== CALCULUS CALCULATOR ===" << endl;
cout << "1. Numerical Differentiation" << endl;
cout << "2. Numerical Integration" << endl;
cout << "0. Back to Main Menu" << endl;
cout << "Enter your choice: ";
cin >> calcChoice;
try {
switch (calcChoice) {
case 1: {
cout << "\n=== NUMERICAL DIFFERENTIATION ===" << endl;
cout << "Select a function:" << endl;
for (const auto& func : functions) {
cout << func.first << ". f(x) = " << func.second << endl;
}
cout << "Enter function choice: ";
int funcChoice;
cin >> funcChoice;
if (functions.find(funcChoice) == functions.end()) {
throw runtime_error("Invalid function choice!");
}
cout << "Enter point x to calculate derivative: ";
cin >> x;
function<double(double)> f;
switch (funcChoice) {
case 1: f = [](double x) { return sin(x); }; break;
case 2: f = [](double x) { return cos(x); }; break;
case 3: f = [](double x) { return x * x; }; break;
case 4: f = [](double x) { return x * x * x; }; break;
case 5: f = [](double x) { return exp(x); }; break;
case 6:
if (x <= 0) throw runtime_error("ln(x) undefined for x <= 0");
f = [](double x) { return log(x); };
break;
case 7:
if (abs(x) < EPSILON) throw runtime_error("1/x undefined at x = 0");
f = [](double x) { return 1.0 / x; };
break;
}
result = numericalDerivative(f, x);
cout << "f'(x) at x = " << x << " is approximately " << formatNumber(result) << endl;
break;
}
case 2: {
cout << "\n=== NUMERICAL INTEGRATION ===" << endl;
cout << "Select a function:" << endl;
for (const auto& func : functions) {
cout << func.first << ". f(x) = " << func.second << endl;
}
cout << "Enter function choice: ";
int funcChoice;
cin >> funcChoice;
if (functions.find(funcChoice) == functions.end()) {
throw runtime_error("Invalid function choice!");
}
cout << "Enter lower limit a: ";
cin >> a;
cout << "Enter upper limit b: ";
cin >> b;
if (a > b) {
swap(a, b);
cout << "Swapped limits (a must be <= b)" << endl;
}
function<double(double)> f;
switch (funcChoice) {
case 1: f = [](double x) { return sin(x); }; break;
case 2: f = [](double x) { return cos(x); }; break;
case 3: f = [](double x) { return x * x; }; break;
case 4: f = [](double x) { return x * x * x; }; break;
case 5: f = [](double x) { return exp(x); }; break;
case 6:
if (a <= 0) throw runtime_error("ln(x) undefined for x <= 0");
f = [](double x) { return log(x); };
break;
case 7:
if (a <= 0 && b >= 0) throw runtime_error("1/x undefined at x = 0");
f = [](double x) { return 1.0 / x; };
break;
}
result = numericalIntegration(f, a, b);
cout << "∫f(x)dx from " << a << " to " << b << " ≈ " << formatNumber(result) << endl;
break;
}
case 0:
calcRunning = false;
break;
default:
cout << "Invalid choice. Try again." << endl;
}
} catch (const exception& e) {
cerr << e.what() << endl;
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
if (calcChoice != 0) {
pauseScreen();
}
}
}
// Function analyzer
void functionAnalyzer() {
cout << "\n=== FUNCTION ANALYZER ===" << endl;
cout << "This feature analyzes basic polynomial functions." << endl;
double a, b, c;
cout << "Enter coefficients for quadratic function f(x) = ax² + bx + c:" << endl;
cout << "a: "; cin >> a;
cout << "b: "; cin >> b;
cout << "c: "; cin >> c;
function<double(double)> f = [a, b, c](double x) { return a*x*x + b*x + c; };