-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalculator.cpp
More file actions
246 lines (205 loc) · 5.41 KB
/
calculator.cpp
File metadata and controls
246 lines (205 loc) · 5.41 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
// Calculator programming with C++ on MS Visual Studio 2019
// calculator.cpp
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cmath>
using namespace std;
void showMenu();
void runMenu();
void redoMenu();
void ArithmeticNumbers();
void FactorialNumbers();
void ExponentialNumbers();
void PrimeNumbers();
void TheBackgammonDice();
int main() // Works with nested global void functions.
{
runMenu();
return 0;
}
void showMenu()
{
cout << "\n-->Hello, World!" << endl << endl;
cout << "-->Press \"1\" to calculate arithmetic numbers!" << endl;
cout << "-->Press \"2\" to calculate factorial numbers!" << endl;
cout << "-->Press \"3\" to calculate exponential numbers!" << endl;
cout << "-->Press \"4\" to find prime numbers!" << endl;
cout << "-->Press \"5\" to roll random backgammon dice!" << endl;
}
void runMenu()
{
showMenu();
int choice;
cout << "\n-->Enter your choice: ";
cin >> choice;
cout << "\n" << endl;
if (choice < 1 || choice > 5)
{
cout << "---An invalid choice! Choose again!" << endl << endl; runMenu();
}
if (choice == 1)
{
ArithmeticNumbers();
}
else if (choice == 2)
{
FactorialNumbers();
}
else if (choice == 3)
{
ExponentialNumbers();
}
else if (choice == 4)
{
PrimeNumbers();
}
else if (choice == 5)
{
TheBackgammonDice();
}
}
void redoMenu()
{
cout << "\n-->Enter the 'M' key to return to the menu or enter the 'E' key to exit: " << flush;
char redo;
cin >> redo;
switch(redo)
{
case 'M': case 'm':
runMenu();
break;
case 'E': case 'e':
cout << "\nThis program was developed by headdawnLight" << endl;
break;
}
}
void ArithmeticNumbers()
{
cout << "\n-->DEFINITION AND EXAMPLE" << endl << endl;
cout << "---Arithmetic is a branch of mathematics that consists of" << endl;
cout << "---the study of numbers. The basic arithmetic operations" << endl;
cout << "---are addition(+), subtraction(-), multiplication(*) and division(/)." << endl;
char op;
double num1, num2;
cout << "\n-->Enter the first number: ";
cin >> num1;
cout << "-->Enter the operator (+) (-) (*) (/) : ";
cin >> op;
cout << "-->Enter the second number: ";
cin >> num2;
switch (op) { // Addition, subtraction, multiplication and division operations
case '+':
cout << "\n---The result of addition: "
<< num1 << '+' << num2 << '=' << (num1 + num2) << endl << endl << endl;
break;
case '-':
cout << "\n---The result of subtraction: "
<< num1 << '-' << num2 << '=' << (num1 - num2) << endl << endl << endl;
break;
case '*':
cout << "\n---The result of multiplication: "
<< num1 << '*' << num2 << '=' << (num1 * num2) << endl << endl << endl;
break;
case '/':
if (num2 != 0.0)
cout << "\n---The result of division: "
<< num1 << '/' << num2 << '=' << (num1 / num2) << endl << endl << endl;
else
cout << "\n---An invalid operation" << endl;
break;
default:
cout << "\n---" << op << " is an invalid operator" << endl;
}
redoMenu();
}
int CalculateFactors(int factor)
{
int sum = 1;
for (int i = 1; i <= factor; i++)
sum = sum * i;
return sum;
}
void FactorialNumbers()
{
cout << "\n-->DEFINITION AND EXAMPLE" << endl << endl;
cout << "---In mathematics, the factorial of a non-negative integer n," << endl;
cout << "---denoted by n!, is the product of all positive integers less than or" << endl;
cout << "---equal to n: n! = n*(n-1)*(n-2)*(n-3)*...*3*2*1" << endl;
cout << "---for example, 5! = 5*4*3*2*1" << endl;
int facto;
cout << "\n-->Enter the factorial number: ";
cin >> facto;
cout << "\n---The result of the calculation: "
<< CalculateFactors(facto) << endl << endl << endl;
redoMenu();
}
int CalculateExponents(int base, int expo)
{
int sum = 1;
for (int i = 1; i <= expo; i++)
sum = sum * base;
return sum;
}
void ExponentialNumbers()
{
cout << "\n-->DEFINITION AND EXAMPLE" << endl << endl;
cout << "---Exponentiation is a mathematical operation," << endl;
cout << "---written as 2^3(equals 2*2*2=8), involving two numbers," << endl;
cout << "---the base '2' and the exponent or power '3'," << endl;
cout << "---and pronounced as '2 raised to the power of 3'." << endl;
int base, expo;
cout << "\n-->Enter the base of the number: ";
cin >> base;
cout << "\n-->Enter the exponent of the number: ";
cin >> expo;
cout << "\n---The result of the calculation: ";
cout << CalculateExponents(base, expo) << endl << endl << endl;
redoMenu();
}
bool PrimeNumber(int control)
{
int i;
for (i = 2; i < control; i++)
{
if (control % i == 0)
return false;
}
return true;
}
int ListOfPrimeNumbers(int lastValue)
{
int j;
for (j = lastValue; j > 2; j--)
{
if (PrimeNumber(j) == true)
{
cout << j << ", ";
}
}
return 2;
}
void PrimeNumbers()
{
cout << "\n-->DEFINITION AND EXAMPLE" << endl << endl;
cout << "---Prime numbers are integers greater than 1 that are" << endl;
cout << "---only divisible by itself and 1 without a remainder." << endl;
int primo;
cout << "\n-->Enter a integer value for prime number list: ";
cin >> primo;
cout << "\n---List of prime numbers from 1 to " << primo << ": ";
cout << ListOfPrimeNumbers(primo) << endl << endl << endl;
redoMenu();
}
void TheBackgammonDice()
{
srand(time(NULL));
int TheBackgammonDice1, TheBackgammonDice2;
{
TheBackgammonDice1 = 1 + rand() % 6;
TheBackgammonDice2 = 1 + rand() % 6;
cout << "---First dice: " << TheBackgammonDice1 << endl << endl;
cout << "---Second dice: " << TheBackgammonDice2 << endl << endl;
}
redoMenu();
}