Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<math.h>
- using namespace std;
- const int LE = 20;
- const int L = 5 + LE*4;
- char* replaceComma(char* a, int length) {
- for (int i = 0; i < length; i++) {
- if (a[i] == ',') a[i] = '.';
- }
- return a;
- }
- int findStringLength(char* a, int limit) {
- int i = 0;
- while (a[i] != '\0' && ++i != limit);
- return i;
- }
- char* doubleToString(double a) {
- char* sa = new char[20];
- snprintf(sa, 20, "%.12lf", a);
- return sa;
- }
- char* moveToRight(char* a, int length) {
- int i = length + 1;
- while (i-- != 1) a[i] = a[i - 1];
- a[0] = ' ';
- return a;
- }
- char* formatString(const char* a, int desiredLength) {
- char* sa = new char[desiredLength+1];
- sa[desiredLength] = '\0';
- snprintf(sa, desiredLength, "%s", a);
- int numLength = findStringLength(sa, desiredLength);
- int d = desiredLength - numLength;
- for (int i = 0; i < d; i++) {
- sa[numLength + i] = ' ';
- }
- int offset = d / 2;
- for (int i = 0; i < offset; i++) sa = moveToRight(sa, numLength + i);
- return sa;
- }
- char* formatDouble(double a, int desiredLength = LE) {
- return formatString(doubleToString(a), desiredLength);
- }
- void printS() {
- for (int i = 0; i < L; i++) printf("-");
- printf("\n");
- }
- void printHeader(bool s = true) {
- printS();
- printf("|%s|%s|%s|%s|\n",
- formatString(s ? "x" : "e", LE),
- formatString("f(x)", LE),
- formatString("F(x)", LE),
- formatString("delta", LE)
- );
- printS();
- }
- double getValue(const char* message, bool lock = true) {
- double r;
- char* a = new char[100];
- char* eptr;
- do {
- a = new char[100];
- cout << message << endl;
- cin >> a;
- a = replaceComma(a, 100);
- r = strtod(a, &eptr);
- } while (*eptr != '\0' || (lock && r*r >= 1 || r == 0));
- return r;
- }
- double fff(double x, double e) {
- double l = -1, c = x;
- double s = c;
- int k = 0, n = 0;
- do {
- n = 2 * k + 1;
- l = c;
- c *= x * x * n / (n+2);
- s += c;
- k++;
- } while (abs(c - l) >= e);
- return 2 * s;
- }
- double ff(double x, double e) {
- return fff(x / (2 - x), e);
- }
- double f(double x, double e) {
- double l = -1, c = x / 2;
- double s = c;
- int k = 1;
- do {
- l = c;
- c *= x * k / (k + 2);
- s += c;
- k++;
- } while (abs(c - l) >= e);
- return 1 - s;
- }
- double F(double x) {
- return ((1 - x) / x) * log(1 / (1 - x));
- }
- double delta(double f, double F) {
- return abs(f*f - F*F);
- }
- void printRow(double x, double e) {
- double mf = f(x, e);
- double lf = F(x);
- double d = delta(mf, lf);
- printf("|%s|%s|%s|%s|\n",
- formatDouble(x),
- formatDouble(mf),
- formatDouble(lf),
- formatDouble(d));
- }
- int main() {
- double xs = 0, xe = 0.9, dx = -0.1, xi = 3, e = -0.00001;
- cout << "((1 - x) / x) * log(1 / (1 - x))" << endl;
- cout << "0 < |x| < 1" << endl;
- xs = getValue("Enter x start");
- xe = getValue("Enter x end");
- dx = getValue("Enter delta x");
- while (e <= 0 || e < 1e-7) {
- e = getValue("Enter e", false);
- }
- xi = getValue("Enter x ideal");
- double x = xs;
- printHeader();
- do {
- printRow(x, e);
- x += dx;
- } while ((xe > xs && dx > 0 && xe > x) || (xe < xs && dx < 0 && x > xe));
- if (fabs(xe - x + dx) > 1e-6) printRow(xe, e);
- printS();
- cout << endl;
- printHeader(false);
- e = 0.1;
- while (e > 1e-7) {
- double mf = f(xi, e);
- double lf = F(xi);
- double d = delta(mf, lf);
- printf("|%s|%s|%s|%s|\n",
- formatDouble(e),
- formatDouble(mf),
- formatDouble(lf),
- formatDouble(d));
- e *= 0.1;
- }
- printS();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment