Advertisement
osipyonok

Ira 4m lab1

Feb 28th, 2017
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.63 KB | None | 0 0
  1. // Ira_4m.cpp: определяет точку входа для консольного приложения.
  2. //
  3.  
  4. #include "stdafx.h" //for visual studio 2010
  5.  
  6. #include <iostream>
  7. #include <cstdio>
  8. #include <vector>
  9. #include "gnuplot_i.hpp"
  10. #include <conio.h>
  11. #include <windows.h>
  12.  
  13. #define cyrillic setlocale(LC_CTYPE, "rus")
  14.  
  15. using namespace std;
  16.  
  17. char* p;
  18.  
  19. double f(double x);
  20. string parse_path(char* path);
  21. void concat(vector<char> & v , string s);
  22.  
  23. double trapezoidal_rule(double a , double b , int n){
  24.     double h = (b - a) / n;
  25.     double sum = 0.0;
  26.     for(int i = 0 ; i < n ; ++i){
  27.         double x = a + h * double(i);
  28.         double xx = a + h * double(i + 1);
  29.         sum += f(x) + f(xx);
  30.     }
  31.     sum *= h;
  32.     sum /= 2.0;
  33.     return sum;
  34. }
  35.  
  36. double simpson(double a , double b , int n){
  37.     double h = (b - a) / n;
  38.     double sum = 0.0;
  39.     for(int i = 1 ; i < n ; ++i){
  40.         double x = a + h * (double)i;
  41.         int c = 2 + 2 * (i % 2);
  42.         sum += (double)c * f(x);
  43.     }
  44.     sum += f(a) + f(b);
  45.     sum *= h;
  46.     sum /= 3.;
  47.     return sum;
  48. }
  49.  
  50. double rectangle_method(double a , double b , int n){
  51.     double h = (b - a) / n;
  52.     double sum = 0.0;
  53.     for(int i = 0 ; i < n ; ++i){
  54.         double x = a + h * (double)i;
  55.         sum += f(0.5 * (x + x + h));
  56.     }
  57.     return h * sum;
  58. }
  59.  
  60. void plot(double a , double b , int n , double (*method)(double a , double b , int n) , string head){
  61.     string parse_p = parse_path(p);
  62.     vector<char> path(parse_p.begin() , parse_p.end());
  63.     path.push_back('\0');
  64.     concat(path , "temp.txt");
  65.  
  66.     double h = (b - a) / n;
  67.  
  68.     vector<pair<double , double>> segments;
  69.     segments.push_back(make_pair(a , 0.));
  70.  
  71.     for(int i = 1 ; i <= n ; ++i){
  72.         double x = a + h * (double)i;
  73.         double xx = a + h * (double)(i - 1);
  74.         double segans = method(xx , x , 100);
  75.         segments.push_back(make_pair(x , segans));
  76.         segments[segments.size() - 1].second += segments[segments.size() - 2].second;      
  77.     }
  78.  
  79.     FILE * file = fopen(&path[0] , "w");
  80.     fprintf(file , "# X   Y\n");
  81.     for(int i = 0 ; i < segments.size() ; ++i){
  82.         fprintf(file , "  %lf   %lf\n" , segments[i].first , segments[i].second);
  83.     }
  84.     fclose(file);
  85.  
  86.     Gnuplot gnu("lines");
  87.     string fi = "set style line 1 lc rgb '#0060ad' lt 2 lw 2 pt 0 ps 1.5";
  88.     string se = &path[0]; se = "plot '" + se; se += "' with linespoints ls 1";
  89.  
  90.     gnu.set_grid();
  91.     gnu.set_title(head);
  92.     gnu.plot_points(se , fi);
  93.  
  94.     FlushConsoleInputBuffer(GetStdHandle(STD_INPUT_HANDLE));
  95.     cout << endl << "Press any key to continue..." << endl;
  96.     _getch();
  97.  
  98.     gnu.remove_tmpfiles();
  99.     remove(&path[0]);
  100. }
  101.  
  102.  
  103. double f(double x){
  104.     return x * x;
  105. }
  106.  
  107.  
  108. int main(int argc , char* argv[]){
  109.     cout.precision(10);
  110.     cout.setf(ios::fixed);
  111.     cyrillic;
  112.  
  113.    
  114.     p = argv[0];
  115.  
  116.     double a , b;
  117.     int n;
  118.     cin >> a >> b >> n;
  119.  
  120.     cout << "Метод трапеций: " << trapezoidal_rule(a , b , n) << endl;
  121.  
  122.     cout << "Формула Симпсона: " << simpson(a , b , n) << endl;
  123.  
  124.     cout << "Метод средних прямоугольников: " << rectangle_method(a , b , n) << endl;
  125.  
  126.     plot(a , b , n , &trapezoidal_rule , "График методом трапеций");
  127.  
  128.     plot(a , b , n , &simpson , "График формулой Симпсона");
  129.  
  130.     plot(a , b , n , &rectangle_method , "График методом средних прямоугольников");
  131.  
  132.     system("pause");
  133.  
  134.     return 0;
  135. }
  136.  
  137. string parse_path(char* path){
  138.     string s = path;
  139.     size_t pos = s.find_last_of('\\');
  140.     if(pos != string::npos){
  141.         return s.substr(0 , pos + 1);
  142.     }
  143.     if(s[s.size() - 1] != '\\')s += '\\';
  144.     return s;
  145. }
  146.  
  147. void concat(vector<char> & v , string s){
  148.     v.pop_back();
  149.     for(int i = 0 ; i < s.size() ; ++i){
  150.         v.push_back(s[i]);
  151.     }
  152.     v.push_back('\0');
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement