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 Swap(double*, double*);
- void Select_Menu();
- double Defend();
- int Defend_Switch();
- int Defend_Int();
- 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}... ";
- a = Defend();
- cout << endl;
- cout << "\tEnter the upper limit {b}... ";
- b = Defend();
- cout << endl;
- if (a > b) {
- SetConsoleTextAttribute(console, FOREGROUND_RED);
- cout << "\tERROR! You've entered a lowerlimit larger than the upper one, cause of that they'll swap places: ";
- SetConsoleTextAttribute(console, FOREGROUND_INTENSITY);
- Swap(&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}...";
- h = fabs(Defend());
- cout << endl;
- cout << "\tEnter the upper limit of the number {n}, and {n} is an integer...";
- n = fabs(Defend_Int());
- 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:";
- choice = Defend_Switch();
- 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 Swap(double* a, double* b) {
- double temp = 0;
- temp = *a;
- *a = *b;
- *b = temp;
- }
- double Defend() {
- int c = 0;
- int arr[20];
- int size = 0;
- double sumFirst = 0;
- double sumSecond = 0;
- double sum = 0;
- int fullStop = -1;
- bool forMinus = false;
- while (c != 13 || size == 0) { // enter
- c = _getch();
- if ((c >= 48 && c <= 57) || (c == 46 && size != 0) || (c == 45)) {
- if (c == 46) { // .
- if (fullStop != -1) {
- continue;
- }
- fullStop = size;
- arr[size] = c - 48;
- size++;
- cout << '.';
- }
- else if (c == 45) { // -
- if (forMinus == true || size != 0) {
- continue;
- }
- forMinus = true;
- cout << '-';
- }
- else {
- cout << c - 48;
- arr[size] = c - 48;
- size++;
- }
- }
- }
- for (int i = 0; i < size; i++)
- {
- if (i < fullStop)
- sumFirst += arr[i] * pow(10, fullStop - i - 1);
- else if (i == fullStop)
- continue;
- else if (i > fullStop)
- sumSecond += arr[i] * pow(10, size - i - 1);
- }
- for (int i = 0; i < size; i++) {
- if (fullStop != -1)
- sum = sumFirst + sumSecond / (pow(10, size - fullStop - 1));
- else //(fullStop = -1)
- sum = sumSecond;
- }
- if (forMinus == true)
- sum = -sum;
- cout << endl;
- return sum;
- }
- int Defend_Switch() {
- int x;
- while (true) {
- x = _getch();
- if (x >= '1' && x <= '4') {
- cout << x - 48 << endl;
- break;
- }
- }
- cout << endl;
- return x - 48;
- }
- int Defend_Int() {
- int c = 0;
- int arr[20];
- int size = 0;
- int sum = 0;
- bool forMinus = false;
- while (c != 13 || size == 0) { //enter
- c = _getch();
- if ((c >= 48 && c <= 57) || (c == 45)) {
- if (c == 45) { //'-'
- if (forMinus == true || size != 0) {
- continue;
- }
- forMinus = true;
- cout << '-';
- }
- else {
- cout << c - 48;
- arr[size] = c - 48;
- size++;
- }
- }
- }
- for (int i = 0; i < size; i++)
- {
- sum += arr[i] * pow(10, size - i - 1);
- }
- if (forMinus == true)
- sum = -sum;
- cout << " ";
- return sum;
- }
- 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