Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <cmath>
- #include <iomanip>
- #include <limits>
- int checkForBadFormat(std::string);
- double fact(double);
- double E(double, double);
- double sec(double, double);
- void printStr(double, double);
- std::string modifyString(std::string);
- int main() {
- setlocale(LC_CTYPE, "Russian");
- std::string tempString;
- double absError = 0;
- double xMin = 1.7;
- double xMax = 1.7;
- double step = 0;
- double xIdeal = 1.7;
- do {
- std::cout << "Введите точность E от 10^(-7) до 10^(-1): ";
- std::cin >> tempString;
- if (modifyString(tempString).size() != 0) {
- absError = std::stod(modifyString(tempString));
- }
- else {
- std::cout << "Введите корректное значение!\n";
- continue;
- }
- if (absError < pow(10, -7) || absError > pow(10, -1) || checkForBadFormat(tempString)) {
- std::cout << "Введите корректное значение!\n";
- }
- } while (absError < pow(10, -7) || absError > pow(10, -1) || checkForBadFormat(tempString));
- do {
- std::cout << "Введите x_start: ";
- std::cin >> tempString;
- if (modifyString(tempString).size() != 0) {
- xMin = std::stod(modifyString(tempString));
- }
- else {
- std::cout << "Введите корректное значение!\n";
- continue;
- }
- if (xMin < -1.57 || xMin > 1.57 || checkForBadFormat(tempString)) {
- std::cout << "Введите корректное значение!\n";
- }
- } while (xMin < -1.57 || xMin > 1.57 || checkForBadFormat(tempString));
- do {
- std::cout << "Введите xEnd: ";
- std::cin >> tempString;
- if (modifyString(tempString).size() != 0) {
- xMax = std::stod(modifyString(tempString));
- }
- else {
- std::cout << "Введите корректное значение!\n";
- continue;
- }
- if (xMax < -1.57 || xMax > 1.57 || checkForBadFormat(tempString)) {
- std::cout << "Введите корректное значение!\n";
- }
- } while (xMax < -1.57 || xMax > 1.57 || checkForBadFormat(tempString));
- bool flag = false;
- do {
- std::cout << "Введите шаг: ";
- std::cin >> tempString;
- if (modifyString(tempString).size() != 0) {
- step = std::stod(modifyString(tempString));
- flag = false;
- }
- else {
- std::cout << "Введите корректное значение!\n";
- flag = true;
- continue;
- }
- if (flag || (std::fabs(xMax - xMin) >= std::numeric_limits<double>::epsilon() && std::fabs(step) < std::numeric_limits<double>::epsilon()) || (step < 0 && xMin < xMax) || (step > 0 && xMin > xMax) || checkForBadFormat(tempString)) {
- std::cout << "Введите корректное значение!\n";
- }
- } while (flag || (std::fabs(xMax - xMin) >= std::numeric_limits<double>::epsilon() && std::fabs(step) < std::numeric_limits<double>::epsilon()) || (step < 0 && xMin < xMax) || (step > 0 && xMin > xMax) || checkForBadFormat(tempString));
- printf("_________________________________________________________________________________________________\n");
- printf("| x | f(x) | F(x) | del |\n");
- printf("|_______________________|_______________________|_______________________|_______________________|\n");
- bool pFlag = false;
- if (xMax > xMin) {
- for (double x = xMin; x <= xMax; x = round((x + step) * 100000000) / 100000000) {
- if (std::fabs(x - xMax) < pow(10, -7)) {
- pFlag = true;
- break;
- }
- printStr(x, absError);
- }
- if (pFlag) {
- printStr(xMax, absError);
- }
- }
- else {
- for (double x = xMin; x >= xMax; x = round((x + step) * 100000000) / 100000000) {
- if (std::fabs(x - xMax) < pow(10, -7)) {
- pFlag = true;
- break;
- }
- std::cout << "oao" << std::endl;
- printStr(x, absError);
- }
- if (pFlag) {
- printStr(xMax, absError);
- }
- }
- std::cout << "\n\n";
- do {
- std::cout << "Введите Xideal: ";
- std::cin >> tempString;
- if (modifyString(tempString).size() != 0 && !checkForBadFormat(tempString)) {
- xIdeal = std::stod(modifyString(tempString));
- }
- else {
- std::cout << "Введите корректное значение!\n";
- continue;
- }
- } while (xIdeal < -1.6 || xIdeal >= 1.6 || checkForBadFormat(tempString));
- double systemAnswer = 1 / cos(xIdeal);
- printf("_________________________________________________________________________________________________\n");
- printf("| e | f(x) | F(x) | del |\n");
- printf("|_______________________|_______________________|_______________________|_______________________|\n");
- for (double i = 0.1; i > 0.0000001; i /= 10)
- {
- printf("| %-15.7f | %-15.6f | %-15.6f | %-15.6f |\n", i, sec(xIdeal, i), systemAnswer, sqrt(fabs(sec(xIdeal, i) * sec(xIdeal, i) - systemAnswer * systemAnswer)));
- printf("|_______________________|_______________________|_______________________|_______________________|\n");
- }
- }
- int checkForBadFormat(std::string a) {
- return a[0] == '.' || (a[0] == '-' && a[1] == '.');
- }
- double fact(double N)
- {
- if (N < 0)
- return 0;
- if (N == 0)
- return 1;
- else
- return N * fact(N - 1);
- }
- double E(double n, double absError) {
- double res = std::pow(2, 2 * n + 2) * fact(2 * n) / std::pow(atan(1) * 4, 2 * n + 1);
- double summ = 0;
- double la = 0, a = 0;
- int k = 0;
- while (true) {
- la = a;
- a = pow(-1, k % 2) / pow(2 * k + 1, 2 * n + 1);
- if (isnan(a + summ) || !isfinite(a + summ) || abs(abs(a) - abs(la)) < absError) break;
- summ += a;
- k++;
- }
- if (isnan(summ * res) || !isfinite(summ * res)) return 1;
- return summ * res;
- }
- double sec(double x, double absError) {
- double pi = atan(1) * 4;
- double summ = E(0, absError);
- double last_a = 0, a = E(0, absError);
- double n = 1;
- do {
- last_a = a;
- a *= fabs(E(n, absError) / E(n - 1, absError)) * x * x /
- static_cast<double>(2 * n) / static_cast<double>(2 * n - 1);
- summ += a;
- ++n;
- } while (absError <= fabs(fabs(a) - fabs(last_a)));
- return summ;
- }
- std::string modifyString(const std::string st) {
- std::string newStr = "";
- for (size_t i = 0; i < st.size(); i++) {
- if ((st[i] >= '0' && st[i] <= '9') || st[i] == '.' || st[i] == '-') {
- newStr += st[i];
- }
- else {
- return "";
- }
- }
- if (newStr == "." || newStr == "-") {
- return "";
- }
- return newStr;
- }
- void printStr(double x, double absError) {
- double systemAnswer = 1 / cos(x);
- printf("| %-15f | %-15f | %-15f | %-15f |\n", x, sec(x, absError), systemAnswer, sqrt(fabs(sec(x, absError) * sec(x, absError) - systemAnswer * systemAnswer)));
- printf("|_______________________|_______________________|_______________________|_______________________|\n");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement