Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <conio.h>
- #include <windows.h>
- #include <iomanip>
- #include <cmath>
- using namespace std;
- double Count_Y(double, int);
- double Count_S(double, int);
- double Count_Ost(double, int);
- typedef double(*CAF) (double, int);
- void Print_Rez(CAF, double, double, double, int);
- void Print_Rez_All(CAF, CAF, CAF, double, double, double, int);
- int main()
- {
- system("color 08");
- setlocale(0, "rus");
- HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
- double a = 0, b = 0, h = 0;
- int n = 0;
- cout << "\nFor each {x}, varying from {a} to {b} in step {h}: " << endl
- << "1.Find function values Y(x);" << endl
- << "2.Amounts S(x);" << endl
- << "3.|Y(x)–S(x)|;" << endl
- << "The values {a, b, h, n} are entered from the keyboard." << endl << endl;
- do {
- cout << "Enter the lower limit {a}... ";
- cin >> a;
- cout << endl;
- cout << "Enter the upper limit {b}... ";
- cin >> b;
- cout << endl;
- if (a == b) {
- SetConsoleTextAttribute(console, FOREGROUND_RED);
- cout << "\t!ERROR You have entered equal limits, the program cannot be executed. Try again... " << endl << endl;
- SetConsoleTextAttribute(console, FOREGROUND_INTENSITY);
- continue;
- }
- cout << "Enter step {h} to change variable {x} from limit {a} to {b}...";
- cin >> h;
- cout << endl;
- cout << "Enter the upper limit of the number {n}, and {n} is an integer...";
- cin >> n;
- cout << endl;
- if (n == 0) {
- SetConsoleTextAttribute(console, FOREGROUND_RED);
- cout << "\n\t!ERROR {n} can't be equal to 0. Try again... " << endl << endl;
- SetConsoleTextAttribute(console, FOREGROUND_INTENSITY);
- continue;
- }
- cout << endl;
- break;
- } while (true);
- cout << endl << endl;
- cout << "To display different expressions on the screen, type from the keyboard: " << endl << endl
- << "1.To derive a number Y(x) " << endl
- << "2.To derive a number S(x) " << endl
- << "3.To derive a number |Y(x) - S(x)| " << endl
- << "4.To get all the expressions out " << endl << endl;
- int choice = 0;
- cout << "Your choice: ";
- cin >> choice;
- cout << endl;
- switch (choice) {
- case 1:
- cout << setw(21) << " Y(x) " << endl;
- Print_Rez(Count_Y, a, b, h, n);
- break;
- case 2:
- cout << setw(21) << " S(x) " << endl;
- Print_Rez(Count_S, a, b, h, n);
- break;
- case 3:
- cout << setw(26) << " |Y(x) - S(x)| " << endl;
- Print_Rez(Count_Ost, a, b, h, n);
- break;
- case 4:
- cout << setw(22) <<"Y(x)" << setw(20) << "S(x)" << setw(27) << "|Y(x) - S(x)|" << endl;
- Print_Rez_All(Count_Y, Count_S, Count_Ost, a, b, h, n);
- break;
- }
- cout << endl << endl;
- return 0;
- }
- double Count_Y(double x, int n) {
- double Y = 0;
- Y = (1 + x * x) / 2.0 * atan(x) - (x / 2.0);
- return Y;
- }
- double Count_S(double x, int n) {
- double E = 1, p = 0, S = 1;
- S = x * x * x / 3.0;
- for (int k = 2; k <= n; k++) {
- E = -E * (x * x);
- p = E * (x * x * x) / ((2.0 * k - 1) * (2.0 * k + 1));;
- S += p;
- }
- return S;
- }
- double Count_Ost(double x, int n) {
- double ost = Count_Y(x, n) - Count_S(x, n);
- return fabs(ost);
- }
- void Print_Rez(CAF aF, double a, double b, double h, int n) {
- for (double x = a; x <= b; x += h) {
- cout << setw(22) << setprecision(5) << fixed << aF(x, n) << endl;
- }
- }
- void Print_Rez_All(CAF aF_Y, CAF aF_S, CAF aF_YS, double a, double b, double h, int n) {
- for (double x = a; x <= b; x += h) {
- cout << setw(22) << setprecision(5) << fixed << Count_Y(x, n)
- << setw(20) << setprecision(5) << Count_S(x, n)
- << setw(23) << setprecision(5) << fixed << Count_Ost(x, n) << endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement