Skip to content

Instantly share code, notes, and snippets.

@Y4r0z
Last active June 16, 2023 07:15
Show Gist options
  • Select an option

  • Save Y4r0z/d5ef341621b7c417c50875952348de4f to your computer and use it in GitHub Desktop.

Select an option

Save Y4r0z/d5ef341621b7c417c50875952348de4f to your computer and use it in GitHub Desktop.
Мусор
#include <iostream>
#include <limits>
#include <cmath>
#include <fstream>
double calc(double a, double b, int n, double (*func)(double)) {
double
result = 0,
h = (b - a) / n;
for (int i = 0; i < n; ++i) {
double dx = a + h / 2 + i * h;
result += func(dx);
}
result *= h;
return result;
}
int getIntinput(int max) {
int input = 1;
std::cin >> input;
while (std::cin.fail() || input < 1 || input > max)
{
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cout << "\ns\n";
std::cin >> input;
};
return input;
}
double getDoubleInput() {
double input = 0;
std::cin >> input;
while (std::cin.fail())
{
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cin >> input;
}
return input;
}
int main() {
const int count = 6;
const char* strings[6]{ "sin(x)", "cos(x)", "tan(x)", "e^x", "ln(x)", "x^2" };
double (*functions[count])(double) = { sin, cos, tan, exp, log, [](double x)->double {return x * x; } };
std::cout << "Functions:\n 1. sin(x)\n 2. cos(x)\n 3. tg(x)\n 4. e^x\n 5. ln(x)\n 6. x^2\n";
int func = getIntinput(count) - 1;
std::cout << "Enter calculation bounds [a:b].\nEnter a:\n";
double a = getDoubleInput();
std::cout << "Enter b:\n";
double b = getDoubleInput();
double result = calc(a, b, 1000, functions[func]);
std::cout << "\nResult: " << calc(a, b, 1000, functions[func]);
std::ofstream myFile("result.txt");
myFile << "Result: " << result << "\nFunction: " << strings[func] << "\nBounds: [" << a << " : " << b << "]";
myFile.close();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment