-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscientificCalculator.cpp
208 lines (200 loc) · 7 KB
/
scientificCalculator.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
#include <iostream>
#include <cmath>
#include <vector>
#include <limits>
#include <iomanip>
using namespace std;
void showMenu()
{
cout << "\nScientific Calculator Menu:" << endl;
cout << "1. Addition" << endl;
cout << "2. Subtraction" << endl;
cout << "3. Multiplication" << endl;
cout << "4. Division" << endl;
cout << "5. Square Root" << endl;
cout << "6. Power (Exponentiation)" << endl;
cout << "7. Logarithm (Base 10)" << endl;
cout << "8. Natural Logarithm (Base e)" << endl;
cout << "9. Sine" << endl;
cout << "10. Cosine" << endl;
cout << "11. Tangent" << endl;
cout << "12. Factorial" << endl;
cout << "0. Exit" << endl;
cout << "Enter your choice: ";
}
double factorial(int num) {
if (num < 0) return -1; // Error case
if (num == 0 || num == 1) return 1;
double result = 1;
for (int i = 2; i <= num; ++i) {
result *= i;
}
return result;
}
void performOperation(int choice) {
vector<double> numbers;
double num;
cout << "Enter numbers (separated by space). Type 'x' to stop: ";
// Read multiple numbers from the user
while (cin >> num) {
numbers.push_back(num);
}
cin.clear(); // Clear the error flag
cin.ignore(numeric_limits<streamsize>::max(), '\n'); // Ignore the invalid input
switch (choice) {
case 1: { // Addition
double sum = 0;
for (double n : numbers) {
sum += n;
}
cout << "Result: " << fixed << setprecision(2) << sum << endl;
break;
}
case 2: { // Subtraction
if (numbers.size() < 2) {
cout << "Error: At least two numbers are required for subtraction." << endl;
break;
}
double result = numbers[0];
for (size_t i = 1; i < numbers.size(); ++i) {
result -= numbers[i];
}
cout << "Result: " << fixed << setprecision(2) << result << endl;
break;
}
case 3: { // Multiplication
double product = 1;
for (double n : numbers) {
product *= n;
}
cout << "Result: " << fixed << setprecision(2) << product << endl;
break;
}
case 4: { // Division
if (numbers.size() < 2) {
cout << "Error: At least two numbers are required for division." << endl;
break;
}
double result = numbers[0];
for (size_t i = 1; i < numbers.size(); ++i) {
if (numbers[i] != 0) {
result /= numbers[i];
} else {
cout << "Error: Division by zero is not allowed." << endl;
return;
}
}
cout << "Result: " << fixed << setprecision(2) << result << endl;
break;
}
case 5: { // Square Root
if (numbers.size() != 1) {
cout << "Error: Square root requires exactly one number." << endl;
break;
}
if (numbers[0] >= 0) {
cout << "Result: " << fixed << setprecision(2) << sqrt(numbers[0]) << endl;
} else {
cout << "Error: Cannot compute the square root of a negative number." << endl;
}
break;
}
case 6: { // Power
if (numbers.size() != 2) {
cout << "Error: Power operation requires exactly two numbers (base and exponent)." << endl;
break;
}
cout << "Result: " << fixed << setprecision(2) << pow(numbers[0], numbers[1]) << endl;
break;
}
case 7: { // Logarithm (Base 10)
if (numbers.size() != 1) {
cout << "Error: Logarithm requires exactly one number." << endl;
break;
}
if (numbers[0] > 0) {
cout << "Result: " << fixed << setprecision(2) << log10(numbers[0]) << endl;
} else {
cout << "Error: Logarithm of zero or negative number is undefined." << endl;
}
break;
}
case 8: { // Natural Logarithm (Base e)
if (numbers.size() != 1) {
cout << "Error: Natural logarithm requires exactly one number." << endl;
break;
}
if (numbers[0] > 0) {
cout << "Result: " << fixed << setprecision(2) << log(numbers[0]) << endl;
} else {
cout << "Error: Natural logarithm of zero or negative number is undefined." << endl;
}
break;
}
case 9: { // Sine
if (numbers.size() != 1) {
cout << "Error: Sine function requires exactly one angle in degrees." << endl;
break;
}
double angle = numbers[0] * (M_PI / 180.0); // Convert degrees to radians
cout << "Result: " << fixed << setprecision(2) << sin(angle) << endl;
break;
}
case 10: { // Cosine
if (numbers.size() != 1) {
cout << "Error: Cosine function requires exactly one angle in degrees." << endl;
break;
}
double angle = numbers[0] * (M_PI / 180.0); // Convert degrees to radians
cout << "Result: " << fixed << setprecision(2) << cos(angle) << endl;
break;
}
case 11: { // Tangent
if (numbers.size() != 1) {
cout << "Error: Tangent function requires exactly one angle in degrees." << endl;
break;
}
double angle = numbers[0] * (M_PI / 180.0); // Convert degrees to radians
cout << "Result: " << fixed << setprecision(2) << tan(angle) << endl;
break;
}
case 12: { // Factorial
if (numbers.size() != 1) {
cout << "Error: Factorial function requires exactly one non-negative integer." << endl;
break;
}
int intNum = static_cast<int>(numbers[0]);
double factResult = factorial(intNum);
if (factResult < 0) {
cout << "Error: Factorial is not defined for negative numbers." << endl;
} else {
cout << "Result: " << fixed << setprecision(2) << factResult << endl;
}
break;
}
default:
cout << "Invalid choice. Please enter a number from the menu." << endl;
break;
}
}
int main()
{
int choice;
while(true){
showMenu();
cin >> choice;
if (cin.fail()){
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Invalid input. Please enter a number from the menu." << endl;
continue;
}
if (choice == 0)
{
cout << "Exiting program. Goodbye!" << endl;
return 0;
}
performOperation(choice);
}
return 0;
}