Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <fstream>
- #include <iostream>
- #include <string>
- using namespace std;
- double Epsilon(double LimitEps){
- double Eps = 0.0f;
- bool isCorrect = false;
- do
- {
- cin >> Eps;
- isCorrect = true;
- if ((Eps < LimitEps) && (Eps > 1))
- {
- isCorrect = false;
- cout << ("Your number must belong to the range of [ ") << LimitEps << (";") << 1 << (" ] ") << endl;
- }
- } while (!isCorrect);
- return Eps;
- }
- double FunctionArgument(double limitX) {
- double x = 0.0f;
- bool isCorrect = false;
- do
- {
- cin >> x;
- isCorrect = true;
- if ((x < 0) && (x > limitX))
- {
- isCorrect = false;
- cout << ("Your number must belong to the range of [ ") << 0 << (";") << limitX << (" ] ") << endl;
- }
- } while (!isCorrect);
- return x;
- }
- double MaclaurinSeries(double Eps, double x) {
- double y = 0.0, value = 0.0;
- double factorial = 1.0, power = x, sign = 1.0;
- int n = 1;
- do {
- value = power / factorial;
- y += sign * value;
- n++;
- sign *= -1.0;
- power *= x * x;
- factorial *= (double)(2 * n - 1) * (2 * n - 2);
- } while (value > Eps);
- return y;
- }
- void Input(double Eps, double x, double result) {
- printf("%.4f ", Eps, x, result);
- }
- void saveResult(string result)
- {
- bool AttemptOne = true;
- bool AttemptTwo = true;
- try {
- ofstream output;
- output.open("max.txt");
- if (output.is_open()) {
- output << result;
- output.close();
- }
- else {
- cout << "Ошибка. Проблема при записи результата!" << endl;
- AttemptOne = false;
- }
- }
- catch (ios_base::failure& f1) {
- cout << "Ошибка. Проблема при записи результата!" << endl;
- AttemptTwo = false;
- }
- }
- int main()
- {
- const double LimitEps = 0.000001;
- const double LimitX = 124.0;
- double y, Eps, x;
- cout << "\nThis program calculates the value of a function y = sin(x) with precision e by expanding the function in a Maclaurin series.\n";
- cout << "\nEnter the precision value:\n";
- Eps = Epsilon(LimitEps);
- cout << "\nEnter the value of the function argument:\n";
- x = FunctionArgument(LimitX);
- y = MaclaurinSeries(Eps, x);
- Input(Eps, x, y);
- void saveResult(string result);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment