-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlb5s.cpp
More file actions
134 lines (119 loc) · 3.15 KB
/
lb5s.cpp
File metadata and controls
134 lines (119 loc) · 3.15 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
#include <iostream>
#include <fstream>
#include <Windows.h>
using namespace std;
void checkValidInput() {
if (cin.fail()) {
SetConsoleCP(1251);
SetConsoleOutputCP(1251);
throw "Ïîìèëêà ââîäó";
}
}
void checkParamN(double n) {
if (n <= 0) {
SetConsoleCP(1251);
SetConsoleOutputCP(1251);
throw "Ïàðàìåòð n ìຠáóòè á³ëüøèé çà 0";
}
}
void checkParamsMinMax(double a, double b) {
if (a >= b) {
SetConsoleCP(1251);
SetConsoleOutputCP(1251);
throw "Ïàðàìåòð a ïîâèíåí áóòè ìåíøèì çà ïàðàìåòð b";
}
}
void checkParamH(double h) {
if (h <= 0) {
SetConsoleCP(1251);
SetConsoleOutputCP(1251);
throw "Ïàðàìåòð h ìຠáóòè á³ëüøèé çà 0";
}
}
void readInput(double& a, double& b, double& h, double& n) {
SetConsoleCP(1251);
SetConsoleOutputCP(1251);
cout << "Ââåä³òü çíà÷åííÿ a: ";
cin >> a;
checkValidInput();
cout << "Ââåä³òü çíà÷åííÿ b: ";
cin >> b;
checkValidInput();
checkParamsMinMax(a, b);
cout << "Ââåä³òü çíà÷åííÿ h: ";
cin >> h;
checkValidInput();
checkParamH(h);
cout << "Ââåä³òü çíà÷åííÿ n: ";
cin >> n;
checkValidInput();
checkParamN(n);
}
double func_y(double n, double x) {
double y = 0;
if (x < 0) {
y = 1;
for (int i = 0; i < (n - 1); i++) {
y *= (i * i * i) + x;
}
}
else {
double sum = 0;
for (int i = 1; i <= n - 1; i++) {
sum = 0;
for (int j = 1; j <= n; j++) {
sum += x / (i + j);
}
y += sum;
}
}
return y;
}
int main() {
try {
SetConsoleCP(1251);
SetConsoleOutputCP(1251);
double a, b, h, n;
readInput(a, b, h, n);
int choice;
cout << "Çáåðåãòè ðåçóëüòàò ó ôàéë³ ÷è ó êîíñîë³? (Ââåä³òü 1 ÷è 2): ";
cin >> choice;
checkValidInput();
if (choice == 1) { //÷àñòèíà êîäó ÿêà â³äïîâ³äຠçà çàïèñ ó ôàéë
string path = "results.txt";
ofstream fout(path);
if (!fout.is_open()) {
cout << "Ôàéë íå â³äêðèâàºòüñÿ" << endl;
}
else {
for (double x = a; x <= b; x += h) {
double y = func_y(n, x);
fout << "x = " << x << "\t y = " << y << endl;
}
fout.close();
}
}
else if (choice == 2) {
for (double x = a; x <= b; x += h) {
double y = func_y(n, x);
cout << "x = " << x << "\t y = " << y << endl;
}
}
else {
cout << "Ââåä³òü 1 àáî 2" << endl;
return 1;
}
}
catch (const char* ex) {
SetConsoleCP(1251);
SetConsoleOutputCP(1251);
cout << "\n" << ex << endl;
cerr << "Ïîìèëêà." << endl;
return 1;
}
catch (...) {
cout << "\n" << "Ïîìèëêà." << endl;
return 1;
}
return 0;
}