-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmathseries.cpp
More file actions
268 lines (218 loc) · 5.92 KB
/
mathseries.cpp
File metadata and controls
268 lines (218 loc) · 5.92 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
#include <string>
#include <iostream>
#include <cmath>
#include <vector>
// Use std for cout,cin,endl
using namespace std;
typedef unsigned long long int ulli;
/// <summary>
/// The Struct that contains application requirements
/// </summary>
struct infoapp
{
// number of elements of iterations
int Num = 0;
// name of that operation
int OperationID = 0;
// sum of operation
long double Sum = 0;
// the input
double Inp = 0;
};
// Our application info container instance
infoapp app;
// Our container for calculations and summations
vector<long double> v(160);
/// <summary>
/// A Factorial function implementation
/// </summary>
/// <param name="i">the input integer to calculate it's factorial</param>
/// <returns>returns the factorral of input</returns>
unsigned long long int fac(int i)
{
// the result of this function
unsigned long long int res = 1;
if (i % 2 == 0)
{
while (i > 0)
{
res *= i * (i - 1);
i -= 2;
}
}
if (i % 2 != 0)
{
while (i > 0)
{
if (i == 1)
break;
res *= i * (i - 1);
i -= 2;
}
}
return res;
}
/// <summary>
/// The mathematical calculation for taylor series expansion
/// </summary>
/// <param name="num">The number of iterations for series</param>
/// <param name="inp">The input x in series</param>
void expocalc(int num, double inp)
{
for (int i = 0; i < num; i++)
{
// Calculate elements and set the first element as in series
v[0] = 1;
v[i] = pow(inp, i) / fac(i);
}
// Add all elements as in series
for (vector<long double>::iterator it = v.begin(); it!=v.end(); it++)
{
app.Sum += *it;
}
cout << "The Result is : " << app.Sum << endl;
app.Sum = 0;
}
/// <summary>
/// The operations for getting inputs from user for taylor series expansion
/// </summary>
void expoOp()
{
system("cls");
cout << "Enter number of iterations you want to calculate for taylor series expansion : ";
cin >> app.Num;
cout << endl;
cout << "Now Enter The input: ";
cin >> app.Inp;
cout << endl;
v.resize(app.Num);
// call expocalc()
expocalc(app.Num, app.Inp);
}
/// <summary>
/// The mathematical calculation for arctang calculation
/// </summary>
/// <param name="num">The number of iterations for series</param>
/// <param name="inp">The input x in series</param>
void arctancalc(int num, double inp)
{
for (int i = 0; i < num; i++)
{
// Calculate elements
if(i % 2 == 1)
{
v[i] = -(pow(inp, 2 * i + 1) / (2 * i + 1));
}
else
{
v[i] = (pow(inp, 2 * i + 1) / (2 * i + 1));
}
}
for (vector<long double>::iterator it = v.begin(); it != v.end(); it++)
{
app.Sum += *it;
}
cout << "The Result is : " << app.Sum << endl;
app.Sum = 0;
}
/// <summary>
/// The operations for getting inputs from user for arctang calculation
/// </summary>
void arctanOp()
{
cout << "Enter number of iterations you want to calculate for arctang operation : ";
cin >> app.Num;
cout << endl;
cout << "Now Enter The input: ";
cin >> app.Inp;
cout << endl;
arctancalc(app.Num, app.Inp);
}
/// <summary>
/// The mathematical calculation for summation series calculation
/// </summary>
/// <param name="num">The number of iterations for series</param>
/// <param name="inp">The input x in series</param>
void summcalc(int num, double inp)
{
for (int i = 0; i < num; i++)
{
// Calculate elements
v[0] = 1;
v[i] = pow(inp, i);
}
for (vector<long double>::iterator it = v.begin(); it != v.end(); it++)
{
app.Sum += *it;
}
cout << "The Result is : " << app.Sum << endl;
app.Sum = 0;
// system("color 0");
}
/// <summary>
/// The operations for getting inputs from user for summation series
/// </summary>
void summationOp()
{
cout << "Enter number of iterations you want to calculate for summation series : ";
cin >> app.Num;
cout << endl;
cout << "Now Enter The input: ";
cin >> app.Inp;
cout << endl;
summcalc(app.Num, app.Inp);
}
int main()
{
// A boolean for controlling the flow of the program
bool quit = false;
while(!quit)
{
cout << "............................................................................." << endl;
cout << endl << "Hello, Welcome ...." << endl;
cout << "Please enter name of your operation: " << endl << endl;
cout << "1.Taylor series expansion - maximum allowable iterations = 20" << endl;
cout << "2.Summation" << endl;
cout << "3.Arctang Calculator" << endl;
cout << "4.Exit Application" << endl;
cout << "5.Clear Console" << endl << endl;
cout << "Your Choice : ";
// Enter Operation ID
cin >> app.OperationID;
cout << endl;
switch (app.OperationID)
{
case 1:
system("color 9");
cout << "Running The Operation (1) ... " << endl;
expoOp();
break;
case 2:
system("color 2");
cout << "Running The Operation (2) ... " << endl;
summationOp();
break;
case 3:
system("color 3");
cout << "Running The Operation (3) ... " << endl;
arctanOp();
break;
case 4:
system("color 4");
// Here the user wants to quit the program
quit = true;
cout << "GoodBye. See you soon" << endl;
break;
case 5:
system("cls");
break;
default:
// Here the user send wrong input
cout << "Wrong Value OR Operation; Please enter one of the items in the menu!" << endl;
cout << endl << endl;
break;
}
system("color 7");
}
return 0;
}