-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCalculator.cpp
More file actions
255 lines (217 loc) · 5.77 KB
/
Copy pathCalculator.cpp
File metadata and controls
255 lines (217 loc) · 5.77 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
//Description: A basic calculator for +, -, *, /, mod, square and squareroot
#include <iostream>
#include <cmath>
#include <cstdlib> // For holding output
#include <windows.h>
using namespace std;
int options;
float val1, val2;
// Function for invalid input like abc
float getNumber()
{
float num;
while (true)
{
cin >> num;
if (!cin.fail())
{
return num;
}
cout << "Invalid number.\nPlease enter digits(e.g. 123): ";
cin.clear();
cin.ignore(1000, '\n');
}
}
// Function for factorial
long long factorial(int n)
{
if (n <= 1)
{
return 1;
}
return n * factorial(n - 1);
}
void opt()
{
cout << "Choose your operation:\n1. Addition\n2. Subtracion\n3. Multiplication\n4. Division\n5. Remainder\n6. Square root\n7. Square\n8. Factorial\n0. Exit" << endl;
cin >> options;
}
void value1()
{
cout << "Enter your first value:" << endl;
val1 = getNumber();
cout << "Enter your second value:" << endl;
val2 = getNumber();
}
void value2()
{
cout << "Enter your value:" << endl;
val1 = getNumber();
}
void sum()
{
cout << val1 << " + " << val2 << " = " << val1 + val2 << endl;
}
void sub()
{
cout << val1 << " - " << val2 << " = " << val1 - val2 << endl;
}
void mul()
{
cout << val1 << " X " << val2 << " = " << val1 * val2 << endl;
}
void div()
{
cout << val1 << " ÷ " << val2 << " = " << val1 / val2 << endl;
}
void mod()
{
cout << val1 << " mod " << val2 << " = " << int(val1) % int(val2) << endl;
}
void sroot()
{
cout << "√" << val1 << " = " << sqrt(val1) << endl;
}
void squ()
{
cout << val1 << "² " << " = " << val1 * val1 << endl;
}
void fac()
{
cout << val1 << "! " << " = " << factorial(val1) << endl;
}
int exit()
{
cout << "Thank you for using the calculator!" << endl;
system("pause");
}
int main()
{
SetConsoleOutputCP(CP_UTF8);
while (true) // Return to the "Choose your operation" after done with any operation
{
system("cls"); // To clear screen after every operation
cout << "Name: Calculator" << endl;
cout << "Version: 1.6" << endl;
cout << "Author: Mr.AJ" << endl;
cout << "Date: 06/03/2026" << endl;
cout<<"\n\n";
opt();
cout<<"\n\n";
if (cin.fail())
{
cout << "Please choose the number from 0 to 8" << endl;
cin.clear();
cin.ignore(1000, '\n');
system("pause");
continue;
}
// For closing calculator
if (options == 0)
{
exit();
return 0;
}
// For operation that requires two values(e.g. 5 + +)
if (options >= 1 && options <= 5)
{
value1();
// For Sum
if (options == 1)
{
cout << "\n";
sum();
}
// For subtraction
else if (options == 2)
{
cout << "\n";
sub();
}
// For multiplication
else if (options == 3)
{
cout << "\n";
mul();
}
// For division
else if (options == 4)
{
// if user enters 0 in denominator
if (val2 == 0.0f)
{
cout<<"Error: Division by 0 is not allowed"<<endl;
}
else
{
cout << "\n";
div();
}
}
// For Remainder/mod
else if (options == 5)
{
if (val2 == 0.0f) // if user enters 0 in denominator
{
cout<<"Error: Division by 0 is not allowed"<<endl;
}
else
{
cout << "\n";
mod();
}
}
}
// For operation that requires one value
else if (options >= 6 && options <= 8)
{
value2();
// For Square root
if (options == 6)
{
// if user enter negative number
if (val1 < 0)
{
cout<<"Please choose positive numbers"<<endl;
}
else
{
cout << "\n";
sroot();
}
}
// For square
else if (options == 7)
{
cout << "\n";
squ();
}
// For Factorial
else if (options == 8)
{
if (val1 < 0) // If user enter negative value
{
cout<<"Please choose positive numbers"<<endl;
}
else if (val1 > 20) // For factorial, the value greater than 20 can create an error so we create a limit
{
cout << "Maximum possible value is 20" << endl;
}
else if (val1 != int(val1)) // If user enter float value
{
cout<<"Factorial works only with whole numbers"<<endl;
}
else
{
cout << "\n";
fac();
}
}
}
else
{
cout<<"Error: Please choose the numbers from 0 to 8"<<endl;
}
system("pause");
}
}