Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- double power(double base, int n) {
- double result = 1.0;
- if (n == 0) {
- return 1;
- }
- for (int i = 1; i <= n; i++) {
- result = result * base;
- }
- return result;
- }
- double fact(int n) {
- if (n == 0) {
- return 1.0;
- } else {
- return (double)n * fact(n - 1);
- }
- }
- double exponent(double epsilon, double x) {
- int n = 1;
- double current = 1.0;
- double result = current;
- while (abs(current) >= epsilon) {
- current = power(x, n) / fact(n);
- result = result + current;
- n++;
- }
- return result;
- }
- double merkator(double epsilon, double x) {
- int n = 2;
- double current = x;
- double result = current;
- while (abs(current) >= epsilon) {
- current = (power(-1, n - 1) * power(x, n)) / n;
- result = result + current;
- n++;
- }
- return result;
- }
- double cosine(double epsilon, double x) {
- int n = 1;
- double current = 1.0;
- double result = current;
- while (abs(current) >= epsilon) {
- current = power(-1, n) * (power(x, 2 * n) / fact(2*n));
- result = result + current;
- n++;
- }
- return result;
- }
- double arctg(double epsilon, double x) {
- int n = 2;
- double current = x;
- double result = current;
- while (abs(current) >= epsilon) {
- current = (power(-1, n - 1) / (2 * n - 1)) * power(x, 2 * n - 1);
- result = result + current;
- n++;
- }
- return result;
- }
- int main()
- {
- setlocale(LC_ALL, "Russian");
- int n;
- double x;
- double accuracy;
- cout << "1 - e^x" << endl << "2 - ln(1+x)" << endl << "3 - cos(x)" << endl << "4 - arctg(x)" << endl;
- cout << "Введите номер функции > "; cin >> n; cout << endl;
- cout << "Введите точность > "; cin >> accuracy; cout << endl;
- double result;
- switch (n)
- {
- case 1:
- cout << "Введите x > "; cin >> x; cout << endl;
- result = exponent(accuracy, x);
- break;
- case 2:
- while (true) {
- cout << "Введите x > "; cin >> x; cout << endl;
- if (x > -1 && x <= 1) {
- break;
- }
- else {
- cout << "Ошибка: х должен быть в пределах (-1; 1]" << endl;
- }
- }
- result = merkator(accuracy, x);
- break;
- case 3:
- cout << "Введите x > "; cin >> x; cout << endl;
- result = cosine(accuracy, x);
- break;
- case 4:
- while (true) {
- cout << "Введите x > "; cin >> x; cout << endl;
- if (x >= -1 && x <= 1) {
- break;
- }
- else {
- cout << "Ошибка: х должен быть в пределах [-1; 1]" << endl;
- }
- }
- result = arctg(accuracy, x);
- break;
- }
- cout << "Результат: " << result << endl;
- system("pause");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement