Sc3ric

lab4-veronika

Dec 13th, 2022 (edited)
869
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.15 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <cmath>
  4. #include <iomanip>
  5. #include <limits>
  6.  
  7. double fact(double);
  8. double E(double, double);
  9. double sec(double, double);
  10. void printStr(double, double);
  11.  
  12. std::string modifyString(std::string);
  13.  
  14. int main() {
  15.     setlocale(LC_CTYPE, "Russian");
  16.  
  17.     std::string tempString;
  18.  
  19.     double absError = 0;
  20.     double xMin = 1.7;
  21.     double xMax = 1.7;
  22.     double step = 0;
  23.     double xIdeal = 1.7;
  24.  
  25.  
  26.     do {
  27.         std::cout << "Введите точность E от 10^(-7) до 10^(-1): ";
  28.         std::cin >> tempString;
  29.  
  30.         if (modifyString(tempString).size() != 0) {
  31.             absError = std::stod(modifyString(tempString));
  32.         }
  33.         else {
  34.             std::cout << "Введите корректное значение!\n";
  35.  
  36.             continue;
  37.         }
  38.  
  39.         if (absError < pow(10, -7) || absError > pow(10, -1)) {
  40.             std::cout << "Введите корректное значение!\n";
  41.         }
  42.  
  43.     } while (absError < pow(10, -7) || absError > pow(10, -1));
  44.  
  45.     do {
  46.         std::cout << "Введите x_start: ";
  47.         std::cin >> tempString;
  48.  
  49.         if (modifyString(tempString).size() != 0) {
  50.             xMin = std::stod(modifyString(tempString));
  51.         }
  52.         else {
  53.             std::cout << "Введите корректное значение!\n";
  54.  
  55.             continue;
  56.         }
  57.  
  58.         if (xMin < -1.57 || xMin > 1.57) {
  59.             std::cout << "Введите корректное значение!\n";
  60.         }
  61.  
  62.     } while (xMin < -1.57 || xMin > 1.57);
  63.  
  64.     do {
  65.         std::cout << "Введите xEnd: ";
  66.         std::cin >> tempString;
  67.  
  68.         if (modifyString(tempString).size() != 0) {
  69.             xMax = std::stod(modifyString(tempString));
  70.         }
  71.         else {
  72.             std::cout << "Введите корректное значение!\n";
  73.  
  74.             continue;
  75.         }
  76.  
  77.         if (xMax < -1.57 || xMax > 1.57) {
  78.             std::cout << "Введите корректное значение!\n";
  79.         }
  80.  
  81.     } while (xMax < -1.57 || xMax > 1.57);
  82.  
  83.     bool flag = false;
  84.  
  85.     do {
  86.         std::cout << "Введите шаг: ";
  87.         std::cin >> tempString;
  88.  
  89.  
  90.         if (modifyString(tempString).size() != 0) {
  91.             step = std::stod(modifyString(tempString));
  92.             flag = false;
  93.         }
  94.         else {
  95.             std::cout << "Введите корректное значение!\n";
  96.             flag = true;
  97.             continue;
  98.         }
  99.  
  100.  
  101.         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)) {
  102.             std::cout << "Введите корректное значение!\n";
  103.         }
  104.  
  105.     } 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));
  106.  
  107.  
  108.     printf("_________________________________________________________________________________________________\n");
  109.     printf("|           x           |         f(x)          |        F(x)           |          del          |\n");
  110.     printf("|_______________________|_______________________|_______________________|_______________________|\n");
  111.  
  112.     bool pFlag = false;
  113.  
  114.     if (xMax > xMin) {
  115.         for (double x = xMin; x <= xMax; x = round((x + step) * 100000000) / 100000000) {
  116.  
  117.             if (std::fabs(x - xMax) < pow(10, -7)) {
  118.                 pFlag = true;
  119.                 break;
  120.             }
  121.  
  122.             printStr(x, absError);
  123.         }
  124.  
  125.         if (pFlag) {
  126.             printStr(xMax, absError);
  127.         }
  128.     }
  129.     else {
  130.         for (double x = xMin; x >= xMax; x = round((x + step) * 100000000) / 100000000) {
  131.             if (std::fabs(x - xMax) < pow(10, -7)) {
  132.                 pFlag = true;
  133.                 break;
  134.             }
  135.             std::cout << "oao" << std::endl;
  136.  
  137.             printStr(x, absError);
  138.         }
  139.         if (pFlag) {
  140.             printStr(xMax, absError);
  141.         }
  142.     }
  143.  
  144.  
  145.     std::cout << "\n\n";
  146.  
  147.     do {
  148.         std::cout << "Введите Xideal: ";
  149.         std::cin >> tempString;
  150.  
  151.         if (modifyString(tempString).size() != 0) {
  152.             xIdeal = std::stod(modifyString(tempString));
  153.         }
  154.         else {
  155.             std::cout << "Введите корректное значение!";
  156.  
  157.             continue;
  158.         }
  159.  
  160.     } while (xIdeal < -1.6 || xIdeal >= 1.6);
  161.  
  162.     double systemAnswer = 1 / cos(xIdeal);
  163.  
  164.     printf("_________________________________________________________________________________________________\n");
  165.     printf("|           e           |         f(x)          |        F(x)           |          del          |\n");
  166.     printf("|_______________________|_______________________|_______________________|_______________________|\n");
  167.     for (double i = 0.1; i > 0.0000001; i /= 10)
  168.     {
  169.         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)));
  170.         printf("|_______________________|_______________________|_______________________|_______________________|\n");
  171.     }
  172. }
  173.  
  174. double fact(double N)
  175. {
  176.     if (N < 0)
  177.         return 0;
  178.     if (N == 0)
  179.         return 1;
  180.     else
  181.         return N * fact(N - 1);
  182. }
  183.  
  184. double E(double n, double absError) {
  185.     double summ = std::pow(2, 2 * n + 2) * fact(2 * n) / std::pow(atan(1) * 4, 2 * n + 1);
  186.  
  187.     int mult = (int)(n) % 2 == 0 ? 1 : -1;
  188.     summ = round(summ);
  189.  
  190.     if (isnan(summ) || !isfinite(summ)) return 1;
  191.  
  192.     return summ * mult;
  193. }
  194.  
  195. double sec(double x, double absError) {
  196.     double pi = atan(1) * 4;
  197.     double summ = E(0, absError);
  198.     double last_a = 0, a = E(0, absError);
  199.     double n = 1;
  200.  
  201.     do {
  202.         last_a = a;
  203.         a *= fabs(E(n, absError) / E(n - 1, absError)) * x * x /
  204.             static_cast<double>(2 * n) / static_cast<double>(2 * n - 1);
  205.         summ += a;
  206.         ++n;
  207.     } while (absError <= fabs(fabs(a) - fabs(last_a)));
  208.  
  209.     return summ;
  210. }
  211.  
  212. std::string modifyString(const std::string st) {
  213.     std::string newStr = "";
  214.  
  215.     for (size_t i = 0; i < st.size(); i++) {
  216.         if ((st[i] >= '0' && st[i] <= '9') || st[i] == '.' || st[i] == '-') {
  217.             newStr += st[i];
  218.         }
  219.         else {
  220.             return "";
  221.         }
  222.     }
  223.  
  224.     if (newStr == "." || newStr == "-") {
  225.         return "";
  226.     }
  227.  
  228.     return newStr;
  229. }
  230.  
  231. void printStr(double x, double absError) {
  232.     double systemAnswer = 1 / cos(x);
  233.  
  234.     printf("| %-15f       | %-15f       | %-15f       | %-15f       |\n", x, sec(x, absError), systemAnswer, sqrt(fabs(sec(x, absError) * sec(x, absError) - systemAnswer * systemAnswer)));
  235.     printf("|_______________________|_______________________|_______________________|_______________________|\n");
  236. }
  237.  
Advertisement
Add Comment
Please, Sign In to add comment