Advertisement
Sc3ric

lab4-veronika-true

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