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;
- void ChangeTheValue(double&, double&);
- void Select_Menu();
- 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 << "\n\t---------------------------------------------------------------------------------------------" << endl << endl
- << "\tFor each {x}, varying from {a} to {b} in step {h}: " << endl
- << "\t1.Find function values Y(x);" << endl
- << "\t2.Amounts S(x);" << endl
- << "\t3.|Y(x)–S(x)|;" << endl
- << "\tThe values {a, b, h, n} are entered from the keyboard." << endl << endl
- << "\t----------------------------------------------------------------------------------------------" << endl << endl;;
- do {
- cout << "\tEnter the lower limit {a}... ";
- cin >> a;
- cout << "\tEnter the upper limit {b}... ";
- cin >> b;
- if (a > b) {
- SetConsoleTextAttribute(console, FOREGROUND_RED);
- cout << "\tERROR! You've entered a lowerlimit larger than the upper one" << endl;
- cout << "\tcause of that they'll swap places: ";
- SetConsoleTextAttribute(console, FOREGROUND_INTENSITY);
- ChangeTheValue(a, b);
- cout << "a = " << setprecision(10) << a << ", " << "b = " << setprecision(10) << b << ". " << endl << endl;
- }
- else 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 << "\tEnter step {h} to change variable {x} from limit {a} to {b}...";
- cin >> h;
- cout << endl;
- cout << "\tEnter the upper limit of the number {n}, and {n} is an integer...";
- cin >> n;
- 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 << "\n\t-----------------------------------------------------------------------------------------------" << endl << endl;
- Select_Menu();
- int choice = 0;
- cout << "\tYour choice:";
- cin >> choice;
- 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(24) << " |Y(x) - S(x)| " << endl;
- Print_Rez(Count_Ost, a, b, h, n);
- break;
- case 4:
- cout << "\t_____________________________________________________________________" << endl
- << setw(10) << "|"
- << setw(10) << "Y(x)"
- << setw(10) << "|"
- << setw(10) << "S(x)"
- << setw(10) << "|"
- << setw(16) << "|Y(x) - S(x)|" << setw(10) << "|" << endl;
- Print_Rez_All(Count_Y, Count_S, Count_Ost, a, b, h, n);
- break;
- }
- cout << "\n\n\t ----------------------------------------------------------------------------------------------" << endl << endl;
- return 0;
- }
- void ChangeTheValue(double& a, double& b) {
- double temp = 0;
- temp = a;
- a = b;
- b = temp;
- }
- void Select_Menu() {
- cout << "\n\tTo display different expressions on the screen, type from the keyboard: " << endl << endl
- << "\t\t1.To derive a number Y(x) " << endl
- << "\t\t2.To derive a number S(x) " << endl
- << "\t\t3.To derive a number |Y(x) - S(x)| " << endl
- << "\t\t4.To get all the expressions out " << endl
- << endl;
- }
- 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;
- for (int k = 1; k <= n; k++) {
- E = -E * (x * x) / (2.0 * k * (2.0 * k - 1));
- p = (2.0 * k * k + 1) * E;
- 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