-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
387 lines (317 loc) · 8.4 KB
/
main.cpp
File metadata and controls
387 lines (317 loc) · 8.4 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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
#include <iostream>
#include <conio.h>
#include <complex>
#include "Mnogochlen.hpp"
using namespace std;
template <typename T>
void check(T& Value)
{
Value = 0;
while (!(cin >> Value) || (cin.peek() != '\n'))
{
cin.clear();
while (cin.get() != '\n');
cout << "Incorrect value\n";
}
}
template <typename T>
Mnogochlen<T> *MenuInput()
{
cout << "Create poilynominal menu\n\n"
<< endl;
cout << "For complex values input must be like: (real,img) where real and img are float/double values\n\n"
<< endl;
cout << "Order of Mnogochlen: " << endl;
long long OrderOfMnogochlen;
check<long long>(OrderOfMnogochlen);
Mnogochlen<T> *Newbie = NULL;
try
{
Newbie = new Mnogochlen<T>(OrderOfMnogochlen);
}
catch (exception e)
{
clog << "An exception occured" << endl;
}
for (long long i = OrderOfMnogochlen; i >= 0; i--)
{
cout << "Coef by " << i << ":\n";
T Coefficient;
check<T>(Coefficient);
Newbie->Set(i, Coefficient);
cout << endl;
}
cout << "Created Mnogochlen: " << *Newbie << endl;
cout << "Press any key" << endl;
_getch();
return Newbie;
}
template <typename T>
ostream &operator<<(ostream &os, const Mnogochlen<T> &Obj)
{
if (Obj.GetOrderOfMnogochlen() == -1)
{
cout << "Mnogochlen has no coefs" << endl;
}
Node<T> *Pointer = Obj.GetHead();
for (int i = 0; i < Obj.GetOrderOfMnogochlen() + 1 && Pointer; i++)
{
os << Pointer->Value << "*x^" << Pointer->MyOrder;
if (Pointer->Next)
os << " + ";
Pointer = Pointer->Next;
}
return os;
}
template <typename T>
T InputValue()
{
cout << "Give a value: " << endl;
T X;
check<T>(X);
return X;
}
template <typename T>
void GiveAnX(const Mnogochlen<T> *Object)
{
T Y = T(0);
T X = InputValue<T>();
for (long long i = Object->GetOrderOfMnogochlen(); i >= 0; i--)
{
Y += (*Object)[i] * T(pow(X, i));
}
cout << "Value for polynom (x = " << X << "): " << Y << endl;
}
template <typename T>
void Sum(const Mnogochlen<T> *Object)
{
system("cls");
cout << "Firstly input another polynom" << endl;
cout << "Any key to input another polynom" << endl;
_getch();
system("cls");
const Mnogochlen<T> *Another = MenuInput<T>();
system("cls");
cout << "Sum of two Mnogochlens:" << endl;
cout << "\n\nFirst: " << (*Object) << endl;
cout << "\nSecond: " << (*Another) << endl;
cout << "\nTheir sum: " << ((*Object) + (*Another)) << endl;
delete Another;
}
template <typename T>
void Substract(const Mnogochlen<T> *Object)
{
system("cls");
cout << "Substract menu\n"
<< endl;
cout << "Firstly input another polynom" << endl;
cout << "Any key to input another polynom" << endl;
_getch();
system("cls");
const Mnogochlen<T> *Another = MenuInput<T>();
system("cls");
cout << "Substract menu\n"
<< endl;
cout << "Substract of two Mnogochlens:" << endl;
cout << "First: " << (*Object) << endl;
cout << "Second: " << (*Another) << endl;
cout << "Their substruction result: " << ((*Object) - (*Another)) << endl;
delete Another;
}
template <typename T>
void MultiplyByArg(const Mnogochlen<T> *Object)
{
cout << "Multiply by arg menu\n"
<< endl;
T Arg = InputValue<T>();
system("cls");
cout << "Multiply by arg menu\n"
<< endl;
cout << "(" << (*Object) << ")"
<< " * " << Arg << " :" << endl;
cout << (*Object) * Arg << endl;
}
template <typename T>
void ChangeCoefByIndex(Mnogochlen<T> *Object)
{
system("cls");
cout << "Change coefficient by index menu\n"
<< endl;
cout << (*Object) << endl;
cout << "Input index: ";
int Index;
check<int>(Index);
cout << "Input new value: ";
T value;
check<T>(value);
(*Object).Set(Index, value);
cout << "Value: " << (*Object)[Index] << endl;
}
template <typename T>
void CompareTwoPolynoms(const Mnogochlen<T> *Object)
{
system("cls");
cout << "Compare two polynoms menu\n"
<< endl;
cout << "\nGive an epsilon: ";
check<T>(Mnogochlen<T>::Epsilon);
cout << "Firstly input another polynom" << endl;
cout << "Any key to input another polynom" << endl;
_getch();
system("cls");
const Mnogochlen<T> *Another = MenuInput<T>();
system("cls");
cout << "Compare two polynoms menu\n"
<< endl;
cout << "First " << (*Object) << endl;
cout << "Second " << (*Another) << endl;
if ((*Object) == (*Another))
{
cout << "The first is equal to second." << endl;
}
else
{
cout << "The first is NOT equal to second." << endl;
}
cout << "Press any button to continue" << endl;
_getch();
}
template <typename T>
void GetRoots(const Mnogochlen<T> *Object)
{
try {
if (Object->GetOrderOfMnogochlen() != 3)
{
throw RangeError("Incorrect order");
}
}
catch (RangeError ex)
{
cout<<"The polynom's order does not equals 3!!!"<<endl;
return;
}
system("cls");
cout << "Get roots menu\n"
<< endl;
cout << (*Object) << endl;
Object->GetRoots();
}
int MenuChoice()
{
cout << "\n\t[1] - Give an X" << endl;
cout << "\n\t[2] - Sum" << endl;
cout << "\n\t[3] - Substract" << endl;
cout << "\n\t[4] - Multiply by arg" << endl;
cout << "\n\t[5] - Change coefficinent by index" << endl;
cout << "\n\t[6] - Get roots (3rd order)" << endl;
cout << "\n\t[7] - Compare two polynoms" << endl;
cout << "\n\t[BACKSPACE] - Set new polynoms" << endl;
cout << "\n\n\tEsc - Exit" << endl;
while (true)
{
int key = _getch();
if ((key == 49) || (key == 50) || (key == 51) || (key == 52) || (key == 53) || (key == 54) || (key == 55) || (key == 127) || (key == 27))
{
return key;
}
}
}
template <typename T>
int Menu1()
{
while (true)
{
cout << "Hellow, world!" << endl;
system("cls");
Mnogochlen<T> *Object = nullptr;
Object = MenuInput<T>();
while (Object)
{
system("cls");
cout << "Your polymon: " << *Object << endl;
int Choice = MenuChoice();
switch (Choice)
{
case 49: // Give an X
GiveAnX<T>(Object);
break;
case 50: // Sum
Sum<T>(Object);
break;
case 51: // Substract
Substract<T>(Object);
break;
case 52: // Multiply by arg
MultiplyByArg<T>(Object);
break;
case 53: // Get doef by index
ChangeCoefByIndex<T>(Object);
break;
case 54: // Get premitive fucntion
GetRoots<T>(Object);
break;
case 55: // Compare two polynoms
CompareTwoPolynoms<T>(Object);
break;
case 127: // Set new polynoms
delete Object;
Object = nullptr;
break;
case 27: // Exit
system("cls");
cout << "Work is done" << endl;
return EXIT_SUCCESS;
break;
default:
break;
}
cout << "Press any key" << endl;
_getch();
}
}
}
int main()
{
while (true)
{
system("cls");
cout << "Choose type:" << endl;
;
cout << "[1] - int" << endl;
;
cout << "[2] - float" << endl;
;
cout << "[3] - double" << endl;
;
cout << "[4] - complex (float)" << endl;
;
cout << "[5] - complex (double)" << endl;
;
cout << "[ESC] - Exit" << endl;
;
int Choice = _getch();
switch (Choice)
{
case 49:
Menu1<int>();
break;
case 50:
Menu1<float>();
break;
case 51:
Menu1<double>();
break;
case 52:
Menu1<complex<float>>();
break;
case 53:
Menu1<complex<double>>();
break;
case 27:
return EXIT_SUCCESS;
default:
system("clear");
break;
}
}
}