Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // 4m_lab1_s2.cpp: определяет точку входа для консольного приложения.
- //
- #include "stdafx.h"
- #include<bits/stdc++.h>
- #include"gnuplot_i.hpp"
- #include <conio.h>
- #include <windows.h>
- #define INF 1000010000
- #define nl '\n'
- #define pb push_back
- #define ppb pop_back
- #define mp make_pair
- #define fi first
- #define se second
- #define pii pair<int,int>
- #define pdd pair<double,double>
- #define all(c) (c).begin(), (c).end()
- #define SORT(c) sort(all(c))
- #define sz(c) (c).size()
- #define rep(i,n) for( int i = 0; i < n; ++i )
- #define repi(i,n) for( int i = 1 ; i <= n; ++i )
- #define repn(i,n) for( int i = n - 1 ; i >= 0 ; --i )
- #define repf(j,i,n) for( int j = i ; j < n ; ++j )
- #define die(s) {std::cout << s << nl;}
- #define dier(s) {std::cout << s; return 0;}
- #define vi vector<int>
- typedef long long ll;
- #define M_PI 3.14159265358979323846
- #define M_E 2.71828182845904523536
- using namespace std;
- string parse_path(char* path);
- void concat(vector<char> & v , string s);
- class Solver{
- protected:
- double (*f)(double x);
- double a , b , h;
- int n;
- public:
- Solver(double _a , double _b , int _n , double (*_func)(double x)){
- a = _a;
- b = _b;
- n = _n;
- f = _func;
- h = (b - a) / n;
- }
- virtual double Solve() = 0;
- };
- class Trapezoidal_rule : public Solver{
- public:
- Trapezoidal_rule(double _a , double _b , int _n , double (*_func)(double x))
- : Solver(_a , _b , _n , _func){}
- double Solve(){
- double sum = 0.;
- for(int i = 0 ; i < n ; ++i){
- double x = a + h * (double)i;
- double xx = a + h * (double)(i + 1);
- sum += f(x) + f(xx);
- }
- sum *= h;
- sum /= 2.;
- return sum;
- }
- };
- class Simpson : public Solver{
- public:
- Simpson(double _a , double _b , int _n , double (*_func)(double x))
- : Solver(_a , _b , _n , _func){}
- double Solve(){
- if(a == b)return 0;
- double sum = 0.;
- int k = 0;
- for(int i = 1 ; i < n ; ++i){
- double x = a + h * (double)i;
- int c = 2 + 2 * (i & 1);
- sum += (double)c * f(x);
- }
- sum += f(a) + f(b);
- sum *= h;
- sum /= 3.;
- return sum;
- }
- };
- class Rectangle_method : public Solver{
- public:
- Rectangle_method(double _a , double _b , int _n , double (*_func)(double x))
- : Solver(_a , _b , _n , _func){}
- double Solve(){
- double sum = 0.;
- for(int i = 0 ; i < n ; ++i){
- double x = a + h * (double)i;
- sum += f(0.5 * (x + x + h));
- }
- return h * sum;
- }
- };
- template <typename D , typename B>
- class IsDeriviedFrom{
- private:
- class No {};
- class Yes { No no[2]; };
- static No Test(...);
- static Yes Test(B*);
- public:
- enum{
- Is = (sizeof(Test(static_cast<D*>(0))) == sizeof(Yes))
- };
- };
- template<class T , int = IsDeriviedFrom<T , Solver>::Is>
- class IFUL{ //integral as a function of the upper limit
- private:
- double a , b;
- int n;
- double (*f)(double x);
- vector<char> path;
- public:
- IFUL(double _a , double _b , int _n , double (*_func)(double x) , char* _path){
- a = _a;
- b = _b;
- n = _n;
- f = _func;
- string parse_p = parse_path(_path);
- path = vector<char>(parse_p.begin() , parse_p.end());
- path.push_back('\0');
- concat(path , "temp.txt");
- }
- void Plot(){
- double h = (b - a) / (double)n;
- vector<pair<double , double>> segments;
- segments.push_back(make_pair(a , 0.));
- for(int i = 1 ; i <= n ; ++i){
- double x = a + h * (double)i;
- double xx = a + h * (double)(i - 1);
- T solver = T(xx , x , 100 , f);
- segments.push_back(make_pair(x , solver.Solve()));
- segments[segments.size() - 1].second += segments[segments.size() - 2].second;
- }
- GnuPlot(segments);
- }
- private:
- void GnuPlot(vector<pair<double , double>> & segments){
- FILE * file = fopen(&path[0] , "w");
- fprintf(file , "# X Y\n");
- for(int i = 0 ; i < segments.size() ; ++i){
- fprintf(file , " %lf %lf\n" , segments[i].first , segments[i].second);
- }
- fclose(file);
- Gnuplot gnu("lines");
- string a = "set style line 1 lc rgb '#0060ad' lt 2 lw 2 pt 0 ps 1.5";
- string b = &path[0]; b = "plot '" + b; b += "' with linespoints ls 1";
- gnu.set_title("IFUL");
- gnu.plot_points(b , a);
- wait();
- gnu.remove_tmpfiles();
- remove(&path[0]);
- }
- void wait(){
- FlushConsoleInputBuffer(GetStdHandle(STD_INPUT_HANDLE));
- #ifndef FOUT
- cout << endl << "Press any key to continue..." << endl;
- #endif
- _getch();
- }
- };
- template <class T>
- class IFUL<T , 0>{
- IFUL(double _a , double _b , int _n , double (*_func)(double x) , char* _path){}
- };
- double f(double x){
- return sin(x * x);
- }
- int main(int argc , char* argv[]) {
- ios_base::sync_with_stdio(false);
- cin.tie(NULL);
- cout.precision(10);
- cout.setf(ios::fixed);
- double a , b;
- int n;
- cin >> a >> b >> n;
- Trapezoidal_rule tr(a , b , n , &f);
- cout << "Trapezoidal_rule " << tr.Solve() << nl;
- Simpson si(a , b , n , &f);
- cout << "Simpson " << si.Solve() << nl;
- Rectangle_method re(a , b , n , &f);
- cout << "Rectangle_method " << re.Solve() << nl;
- system("pause");
- IFUL<Trapezoidal_rule> iful_tr(a , b , n , &f , argv[0]);
- iful_tr.Plot();
- // IFUL<Simpson> iful_si(a , b , n , &f , argv[0]);
- // iful_si.Plot();
- // IFUL<Rectangle_method> iful_re(a , b , n , &f , argv[0]);
- // iful_re.Plot();
- system("pause");
- return 0;
- }
- string parse_path(char* path){
- string s = path;
- size_t pos = s.find_last_of('\\');
- if(pos != string::npos){
- return s.substr(0 , pos + 1);
- }
- if(s[s.size() - 1] != '\\')s += '\\';
- return s;
- }
- void concat(vector<char> & v , string s){
- v.pop_back();
- for(int i = 0 ; i < s.size() ; ++i){
- v.push_back(s[i]);
- }
- v.push_back('\0');
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement