klasscho

Untitled

Dec 19th, 2019
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.18 KB | None | 0 0
  1. #include <fstream>
  2. #include <iostream>
  3. #include <string>
  4. using namespace std;
  5.  
  6.  
  7.  
  8. double Epsilon(double LimitEps){
  9. double Eps = 0.0f;
  10. bool isCorrect = false;
  11. do
  12. {
  13. cin >> Eps;
  14. isCorrect = true;
  15. if ((Eps < LimitEps) && (Eps > 1))
  16. {
  17. isCorrect = false;
  18. cout << ("Your number must belong to the range of [ ") << LimitEps << (";") << 1 << (" ] ") << endl;
  19.  
  20. }
  21. } while (!isCorrect);
  22. return Eps;
  23. }
  24.  
  25. double FunctionArgument(double limitX) {
  26. double x = 0.0f;
  27. bool isCorrect = false;
  28. do
  29. {
  30. cin >> x;
  31. isCorrect = true;
  32. if ((x < 0) && (x > limitX))
  33. {
  34. isCorrect = false;
  35. cout << ("Your number must belong to the range of [ ") << 0 << (";") << limitX << (" ] ") << endl;
  36.  
  37. }
  38. } while (!isCorrect);
  39. return x;
  40. }
  41.  
  42. double MaclaurinSeries(double Eps, double x) {
  43. double y = 0.0, value = 0.0;
  44. double factorial = 1.0, power = x, sign = 1.0;
  45. int n = 1;
  46. do {
  47. value = power / factorial;
  48. y += sign * value;
  49. n++;
  50. sign *= -1.0;
  51. power *= x * x;
  52. factorial *= (double)(2 * n - 1) * (2 * n - 2);
  53. } while (value > Eps);
  54. return y;
  55. }
  56.  
  57. void Input(double Eps, double x, double result) {
  58. printf("%.4f ", Eps, x, result);
  59. }
  60.  
  61. void saveResult(string result)
  62. {
  63. bool AttemptOne = true;
  64. bool AttemptTwo = true;
  65. try {
  66. ofstream output;
  67. output.open("max.txt");
  68. if (output.is_open()) {
  69. output << result;
  70. output.close();
  71. }
  72. else {
  73. cout << "Ошибка. Проблема при записи результата!" << endl;
  74. AttemptOne = false;
  75. }
  76. }
  77. catch (ios_base::failure& f1) {
  78. cout << "Ошибка. Проблема при записи результата!" << endl;
  79. AttemptTwo = false;
  80. }
  81. }
  82.  
  83. int main()
  84. {
  85. const double LimitEps = 0.000001;
  86. const double LimitX = 124.0;
  87. double y, Eps, x;
  88. cout << "\nThis program calculates the value of a function y = sin(x) with precision e by expanding the function in a Maclaurin series.\n";
  89. cout << "\nEnter the precision value:\n";
  90. Eps = Epsilon(LimitEps);
  91. cout << "\nEnter the value of the function argument:\n";
  92. x = FunctionArgument(LimitX);
  93. y = MaclaurinSeries(Eps, x);
  94. Input(Eps, x, y);
  95. void saveResult(string result);
  96. return 0;
  97. }
Advertisement
Add Comment
Please, Sign In to add comment