Advertisement
1k6j01

Integr

Dec 12th, 2015
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.48 KB | None | 0 0
  1. // semestrovaya_2.1.cpp : Defines the entry point for the console application.
  2. // Методичка 2: 1.5 (в); (е); (ж);
  3.  
  4. #include "stdafx.h"
  5. #include <iostream>
  6. #include <conio.h>
  7. #include <cmath>
  8. #include <iomanip>
  9. #include <fstream>
  10.  
  11. using namespace std;
  12.  
  13. double fi = 1;
  14. int qwe;
  15.  
  16. void create(ifstream&f, double *x, int n);
  17.  
  18. void show(ofstream&f, double *x, int n);
  19.  
  20. inline double sqr(double x);
  21.  
  22. inline double g(double x);
  23.  
  24. inline double h(double z);
  25.  
  26. double sum_g(double *x, int M);
  27.  
  28. double T(double h);
  29.  
  30. void change_fi();
  31.  
  32. void change_x(double *x, double t, int M);
  33.  
  34. int main()
  35. {
  36.     setlocale(0, "Russian");
  37.     ifstream in("in.txt");
  38.     ofstream out("out.txt");
  39.  
  40.     int m;  //"Введите количество верхних границ интеграла t:\nm = ";
  41.     in >> m;
  42.     int M;  //"Введите количесвто точек x для полученя значения интеграла \nM = ";
  43.     in >> M;
  44.  
  45.     double *t = new double[m];
  46.     create(in, t, m);
  47.     in.close();
  48.  
  49.     double *ans = new double[m]; //массив значений интеграла в точках t[i] : (i=[0;m]);
  50.     double *x = new double [M];
  51.  
  52.     for (int i(0); i < m; i++) //для проверки
  53.     {
  54.         change_x(x, t[i], M);
  55.     }
  56.  
  57.     for (int i(0); i < m; i++)
  58.     {
  59.         change_x(x, t[i], M);
  60.         ans[i] = (t[i] - 1) * (sum_g(x, M) / M);
  61.     }
  62.  
  63.     for (int i(0); i < m; i++)
  64.     {
  65.         cout << "Значение интеграла #" << i + 1 << " = " << ans[i] << endl;
  66.     }
  67.     show(out, ans, m);
  68.  
  69.     system("pause");
  70.     return 0;
  71. }
  72.  
  73. void create(ifstream&f, double *x, int n)
  74. {
  75.     if (!f)
  76.     {
  77.         cout << "Can't open file" << endl;
  78.         exit(0);
  79.     }
  80.     for (int i(0); i < n; i++)
  81.         f >> x[i];
  82. }
  83.  
  84. void show(ofstream&f, double *x, int n)
  85. {
  86.     for (int i(0); i <n; i++)
  87.     {
  88.         f << x[i] << ' ';
  89.     }
  90.     f << endl;
  91. }
  92.  
  93. inline double sqr(double x) //Функция возведения в квадрат;
  94. {
  95.     return x * x;
  96. }
  97.  
  98. inline double g(double x) //данная функция g(x);
  99. {
  100.     return x;
  101.     //return sqr(x) + x + 1;
  102. }
  103.  
  104. inline double h(double z) //Данная функция h(z);
  105. {
  106.     //return sqrt(abs(1 - z));
  107.     return (sin(z) + sqr(sin(z))) / 2;
  108. }
  109.  
  110. double sum_g(double *x, int M) // Сумма значений ф-ии g(x) в произвольных точках х[i];
  111. {
  112.     double s(0);
  113.     for (int i(0); i < M; i++)
  114.     {
  115.         s += g(x[i]);
  116.     }
  117.     return s;
  118. }
  119.  
  120. double T(double h)
  121. {
  122.     int n = 10;
  123.     int U[10], V[10];
  124.     double s = 0;
  125.     float q = 0.5;
  126.  
  127.     for (int i(0); i < n; i++)
  128.     {
  129.         U[i] = (int)(h * 2);
  130.         if (h * 2 >= 1)
  131.             h = h * 2 - 1;
  132.         else h = h * 2;
  133.     }
  134.  
  135.     for (int i(0); i < n - 1; i++)
  136.         if (U[i] == U[i + 1])
  137.             V[i] = 0;
  138.         else
  139.             V[i] = 1;
  140.  
  141.     for (int i(0); i < n - 1; i++)
  142.     {
  143.         s += V[i] * q;
  144.         q *= 0.5;
  145.     }
  146.     return s;
  147. }
  148.  
  149. void change_fi() // число фи, образованный случайно, для определения числа x[i];
  150. {
  151.     fi = T(h(fi));
  152. }
  153.  
  154. void change_x(double *x, double t, int M)
  155. {
  156.     for (int i(0); i < M; i++)
  157.     {
  158.         change_fi();
  159.         x[i] = 1 + (t - 1) * fi;
  160.     }
  161. }
  162.  
  163. /*void perevod(double h)
  164. {
  165. int n = 10;
  166. int U[10], V[10];
  167. double s = 0;
  168. cout << "0.";
  169. for (int i(0); i < n; i++)
  170. {
  171. U[i] = (int)(h * 2);
  172. if (h * 2 >= 1)
  173. h = h * 2 - 1;
  174. else h = h * 2;
  175. cout << U[i];
  176. }
  177. cout << endl << endl;
  178. for (int i(0); i < n - 1; i++)
  179. if (U[i] == U[i + 1])
  180. V[i] = 0;
  181. else
  182. V[i] = 1;
  183. for (int i(0); i < n - 1; i++)
  184. cout << V[i];
  185. float q = 0.5;
  186.  
  187. for (int i(0); i < n - 1; i++)
  188. {
  189. s += V[i] * q;
  190. q *= 0.5;
  191. }
  192. cout << endl << s;
  193. }*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement