-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScientificCalculator.cpp
More file actions
181 lines (163 loc) · 4.27 KB
/
Copy pathScientificCalculator.cpp
File metadata and controls
181 lines (163 loc) · 4.27 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
#include <iostream>
#include <cmath>
using namespace std;
class ScientificCalculator
{
public:
// 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)
{
if (b == 0)
{
cout << "Error: Division by zero!" << endl;
return NAN; // Not a Number
}
return a / b;
}
// Trigonometric functions
double sine(double angle_degrees)
{
return sin(degreesToRadians(angle_degrees));
}
double cosine(double angle_degrees)
{
return cos(degreesToRadians(angle_degrees));
}
double tangent(double angle_degrees)
{
return tan(degreesToRadians(angle_degrees));
}
// Exponentiation
double power(double base, double exponent)
{
return pow(base, exponent);
}
// Square root
double squareRoot(double number)
{
if (number < 0)
{
cout << "Error: Cannot calculate square root of a negative number!" << endl;
return NAN;
}
return sqrt(number);
}
// Logarithms
double logarithm(double number)
{
if (number <= 0)
{
cout << "Error: Cannot calculate logarithm of a non-positive number!" << endl;
return NAN;
}
return log(number);
}
double logarithm(double number, double base)
{
if (number <= 0 || base <= 0 || base == 1)
{
cout << "Error: Invalid input for logarithm calculation!" << endl;
return NAN;
}
return log(number) / log(base);
}
private:
// Helper function to convert degrees to radians
double degreesToRadians(double angle_degrees)
{
return angle_degrees * M_PI / 180.0;
}
};
int main()
{
ScientificCalculator calc;
double a, b;
int choice;
cout << "Scientific Calculator" << endl;
cout << "1. Add" << endl;
cout << "2. Subtract" << endl;
cout << "3. Multiply" << endl;
cout << "4. Divide" << endl;
cout << "5. Sine" << endl;
cout << "6. Cosine" << endl;
cout << "7. Tangent" << endl;
cout << "8. Power" << endl;
cout << "9. Square Root" << endl;
cout << "10. Natural Logarithm" << endl;
cout << "11. Logarithm (Custom Base)" << endl;
cout << "Enter your choice: ";
cin >> choice;
switch (choice)
{
case 1:
cout << "Enter two numbers to add: ";
cin >> a >> b;
cout << "Result: " << calc.add(a, b) << endl;
break;
case 2:
cout << "Enter two numbers to subtract: ";
cin >> a >> b;
cout << "Result: " << calc.subtract(a, b) << endl;
break;
case 3:
cout << "Enter two numbers to multiply: ";
cin >> a >> b;
cout << "Result: " << calc.multiply(a, b) << endl;
break;
case 4:
cout << "Enter two numbers to divide: ";
cin >> a >> b;
cout << "Result: " << calc.divide(a, b) << endl;
break;
case 5:
cout << "Enter angle in degrees for sine: ";
cin >> a;
cout << "Result: " << calc.sine(a) << endl;
break;
case 6:
cout << "Enter angle in degrees for cosine: ";
cin >> a;
cout << "Result: " << calc.cosine(a) << endl;
break;
case 7:
cout << "Enter angle in degrees for tangent: ";
cin >> a;
cout << "Result: " << calc.tangent(a) << endl;
break;
case 8:
cout << "Enter base and exponent for power: ";
cin >> a >> b;
cout << "Result: " << calc.power(a, b) << endl;
break;
case 9:
cout << "Enter number to find square root: ";
cin >> a;
cout << "Result: " << calc.squareRoot(a) << endl;
break;
case 10:
cout << "Enter number for natural logarithm: ";
cin >> a;
cout << "Result: " << calc.logarithm(a) << endl;
break;
case 11:
cout << "Enter number and base for logarithm: ";
cin >> a >> b;
cout << "Result: " << calc.logarithm(a, b) << endl;
break;
default:
cout << "Invalid choice!" << endl;
}
return 0;
}