Sc3ric

Untitled

Dec 10th, 2022
1,052
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.45 KB | None | 0 0
  1. #include<iostream>
  2. #include<math.h>
  3.  
  4. using namespace std;
  5.  
  6. const int LE = 20;
  7. const int L = 5 + LE*4;
  8.  
  9. char* replaceComma(char* a, int length) {
  10.     for (int i = 0; i < length; i++) {
  11.         if (a[i] == ',') a[i] = '.';
  12.     }
  13.     return a;
  14. }
  15.  
  16. int findStringLength(char* a, int limit) {
  17.     int i = 0;
  18.     while (a[i] != '\0' && ++i != limit);
  19.     return i;
  20. }
  21.  
  22. char* doubleToString(double a) {
  23.     char* sa = new char[20];
  24.     snprintf(sa, 20, "%.12lf", a);
  25.     return sa;
  26. }
  27.  
  28. char* moveToRight(char* a, int length) {
  29.     int i = length + 1;
  30.  
  31.     while (i-- != 1) a[i] = a[i - 1];
  32.     a[0] = ' ';
  33.  
  34.     return a;
  35. }
  36.  
  37. char* formatString(const char* a, int desiredLength) {
  38.     char* sa = new char[desiredLength+1];
  39.     sa[desiredLength] = '\0';
  40.     snprintf(sa, desiredLength, "%s", a);
  41.     int numLength = findStringLength(sa, desiredLength);
  42.     int d = desiredLength - numLength;
  43.     for (int i = 0; i < d; i++) {
  44.         sa[numLength + i] = ' ';
  45.     }
  46.  
  47.     int offset = d / 2;
  48.     for (int i = 0; i < offset; i++) sa = moveToRight(sa, numLength + i);
  49.     return sa;
  50. }
  51.  
  52. char* formatDouble(double a, int desiredLength = LE) {
  53.     return formatString(doubleToString(a), desiredLength);
  54. }
  55.  
  56. void printS() {
  57.     for (int i = 0; i < L; i++) printf("-");
  58.     printf("\n");
  59. }
  60.  
  61. void printHeader(bool s = true) {
  62.     printS();
  63.  
  64.     printf("|%s|%s|%s|%s|\n",
  65.         formatString(s ? "x" : "e", LE),
  66.         formatString("f(x)", LE),
  67.         formatString("F(x)", LE),
  68.         formatString("delta", LE)
  69.     );
  70.  
  71.     printS();
  72. }
  73.  
  74. double getValue(const char* message, bool lock = true) {
  75.     double r;
  76.     char* a = new char[100];
  77.     char* eptr;
  78.     do {
  79.         a = new char[100];
  80.         cout << message << endl;
  81.         cin >> a;
  82.         a = replaceComma(a, 100);
  83.        
  84.         r = strtod(a, &eptr);
  85.     } while (*eptr != '\0' || (lock && r*r >= 1 || r == 0));
  86.     return r;
  87. }
  88.  
  89.  
  90.  
  91. double fff(double x, double e) {
  92.     double l = -1, c = x;
  93.     double s = c;
  94.     int k = 0, n = 0;
  95.     do {
  96.         n = 2 * k + 1;
  97.         l = c;
  98.         c *= x * x * n / (n+2);
  99.         s += c;
  100.         k++;
  101.     } while (abs(c - l) >= e);
  102.  
  103.     return 2 * s;
  104. }
  105.  
  106. double ff(double x, double e) {
  107.     return fff(x / (2 - x), e);
  108. }
  109.  
  110. double f(double x, double e) {
  111.     double l = -1, c = x / 2;
  112.     double s = c;
  113.     int k = 1;
  114.     do {
  115.         l = c;
  116.         c *= x * k / (k + 2);
  117.         s += c;
  118.         k++;
  119.     } while (abs(c - l) >= e);
  120.  
  121.     return 1 - s;
  122. }
  123.  
  124. double F(double x) {
  125.     return ((1 - x) / x) * log(1 / (1 - x));
  126. }
  127.  
  128. double delta(double f, double F) {
  129.     return abs(f*f - F*F);
  130. }
  131.  
  132. void printRow(double x, double e) {
  133.     double mf = f(x, e);
  134.     double lf = F(x);
  135.     double d = delta(mf, lf);
  136.     printf("|%s|%s|%s|%s|\n",
  137.         formatDouble(x),
  138.         formatDouble(mf),
  139.         formatDouble(lf),
  140.         formatDouble(d));
  141. }
  142.  
  143. int main() {
  144.     double xs = 0, xe = 0.9, dx = -0.1, xi = 3, e = -0.00001;
  145.     cout << "((1 - x) / x) * log(1 / (1 - x))" << endl;
  146.     cout << "0 < |x| < 1" << endl;
  147.     xs = getValue("Enter x start");
  148.     xe = getValue("Enter x end");
  149.     dx = getValue("Enter delta x");
  150.     while (e <= 0 || e < 1e-7) {
  151.         e = getValue("Enter e", false);
  152.     }
  153.    
  154.     xi = getValue("Enter x ideal");
  155.     double x = xs;
  156.  
  157.     printHeader();
  158.     do {
  159.         printRow(x, e);
  160.         x += dx;
  161.     } while ((xe > xs && dx > 0 && xe > x) || (xe < xs && dx < 0 && x > xe));
  162.     if (fabs(xe - x + dx) > 1e-6) printRow(xe, e);
  163.  
  164.     printS();
  165.     cout << endl;
  166.  
  167.     printHeader(false);
  168.     e = 0.1;
  169.     while (e > 1e-7) {
  170.         double mf = f(xi, e);
  171.         double lf = F(xi);
  172.         double d = delta(mf, lf);
  173.         printf("|%s|%s|%s|%s|\n",
  174.             formatDouble(e),
  175.             formatDouble(mf),
  176.             formatDouble(lf),
  177.             formatDouble(d));
  178.         e *= 0.1;
  179.     }
  180.  
  181.     printS();
  182.     return 0;
  183. }
  184.  
Advertisement
Add Comment
Please, Sign In to add comment