zoukankan      html  css  js  c++  java
  • 定积分解法

    定义了一个基类的指针,通过基类操纵派生类,初始化派生类对象。。粤粤大神教我的。。首先须要定义一个抽象函数类,然后派生出须要求得函数。。然后定义一个抽象的求积分的类,然后派生出不同的求函数的类。。当中增加函数作为私有成员。。。。注意()的重载。。方便求函数的值。。。

    代码例如以下:

    #include<iostream>
    #include<cstdlib>
    #include<cmath>
    using namespace std;
    
    void menu1()   //  选择积分函数功能菜单
    {
    	    cout<<"            请  选  择 被 积 函 数"<<endl;
    		cout<<"
               1.  求 y=log(1.0+x)/(1.0+x*x)的 积 分
    ";
            cout<<"
               2.  求 y=x*x 的 积 分   
    ";
    		cout<<"
               3.  求 y=2*x 的 积 分
    ";
    		cout<<"
               0.  退 出
    ";
    }
    
    void menu2()   //  选择积分方法功能菜单
    {
            cout<<"
                   请  选 择 积 分 方 法"<<endl;
    		cout<<"
               1.  用 梯 形 法 求 积 分
    ";
            cout<<"
               2.  用 矩 形 法 求 积 分
    ";
    		cout<<"
               3.  用 辛 普 生 法 求 积 分
    ";
    		cout<<"
               4.  用 变 步 长 梯 形 法 求 积 分
    ";
    		cout<<"
               0.  返 回 上 级 菜 单
    ";
    }
    
    class Function
    {
    public:
        virtual double operator()(double x)=0;
        virtual ~Function(){}
    };
    
    class  Myfunction1:public Function
    {
    public:
        virtual double operator()(double x)
        {
            return log(1.0+x)/(1.0+x*x);
        }
    };
    
    class  Myfunction2:public Function
    {
    public:
        virtual double operator()(double x)
        {
            return x*x;
        }
    };
    
    class  Myfunction3:public Function
    {
    public:
        virtual double operator()(double x)
        {
            return 2*x;
        }
    };
    
    
    class Integration
    {
    public:
        virtual double operator()(double a,double b,double eps)=0;
        virtual  ~Integration(){}
    };
    
    class Trapz1:public Integration
    {
    private:
        Function &f;
    public:
        Trapz1(Function &fun):f(fun){}
        virtual double operator()(double a,double b,double eps)
        {
            int n=200;
            double h=(b-a)/n;
            double sum=0,tmp;
            for(int i=1;i<n;i++)
            {
                tmp=2*f(a+i*h);
                sum+=tmp;
            }
            sum=(sum+f(a)+f(b))*h/2;
            return sum;
        }
    
    };//梯形法求积分
    
    class Trapz2:public Integration
    {
    private:
        Function &f;
    public:
        Trapz2(Function &fun):f(fun){}
        virtual double operator()(double a,double b,double eps)
        {
            bool done=false;
            int n=1;
            double h=(b-a);
            double tn=h*(f(a)+f(b))/2;
            double t2n;
            do
            {
                double sum=0;
                for(int k=0;k<n;k++)
                {
                    double  x=a+(k+0.5)*h;
                    sum+=f(x);
                }
                t2n=(tn+h*sum)/2.0;
                if(fabs(tn-t2n)<eps)
                    done=true;
                else
                {
                    n=n*2;
                    h=h/2;
                    tn=t2n;
                }
            }while(!done);
        return t2n;
        }
    };//变步长球积分
    
    class Simpson:public Integration
    {
    private:
        Function &f;
    public:
        Simpson(Function &fun):f(fun){}
        virtual double operator()(double a,double b,double eps)
        {
            int n=400;
            double h=(b-a)/(2*n);
            double sum=0;
            for(int i=1;i<=(2*n-1);i=i+2)
            {
                double tmp=4*f(a+i*h);
                sum=sum+tmp;
            }
            for(int i=2;i<=(2*n-2);i=i+2)
            {
                double tmp=2*f(a+i*h);
                sum=sum+tmp;
            }
            sum=(sum+f(a)+f(b))*h/3;
            return sum;
        }
    };//辛普生法求积分
    
    class Rectangle:public Integration
    {
    private:
        Function &f;
    public:
        Rectangle(Function &fun):f(fun){}
        virtual double operator()(double a,double b,double eps)
        {
            double sum=0;
            int n=400;
            double h=(b-a)/n;
            for(int i=1;i<n;i++)
            {
                double tmp=h*f(a+i*h+h/2);
                sum=sum+tmp;
            }
            return sum;
        }
    };//矩形法求积分
    Function *f;
    int main()
    {
        int choose1,choose2;
    	int  flag=1;
        while (flag)
    	{
    		system("cls");
        	menu1();
            cin>>choose1;
    		system("cls");
    		int flag1=1;
    		switch(choose1)
    		{
    			case 1:
    			    f=new Myfunction1;
    			     while(flag1)
    				{
    					menu2();
    					cin>>choose2;
    					switch(choose2)
    					{
    				    case 1:
                           {
                            Trapz1 trapz1(*f);
                            cout<<trapz1(0,2,1e-7);
                           }
    						break;
    					case 2:
                            {
                            Rectangle rectangle(*f);
                            cout<<rectangle(0,2,1e-7);
                            }
    						break;
    					case 3:
                           {
                            Simpson  simpson(*f);
                            cout<<simpson(0,2,1e-7);
                           }
    						break;
    					case 4:
                           {
                            Trapz2 trapz2(*f);
                            cout<<trapz2(0,2,1e-7);
                           }
    						break;
    					case 0:
    						flag1=0;break;
    					default: cout<<"
        Wrong Selection !(选择错误,重选)
    ";
    
    					}
    				}//end of while(flag1)
    			    break;
    			case 2:
    			      f=new Myfunction2;
    			      while(flag1)
    				{
    					menu2();
    					cin>>choose2;
    					switch(choose2)
    					{
    				    case 1:
                           {
                            Trapz1 trapz1(*f);
                            cout<<trapz1(0,2,1e-7);
                           }
    						break;
    					case 2:
                            {
                            Rectangle rectangle(*f);
                            cout<<rectangle(0,2,1e-7);
                            }
    						break;
    					case 3:
                           {
                            Simpson  simpson(*f);
                            cout<<simpson(0,2,1e-7);
                           }
    						break;
    					case 4:
                           {
                            Trapz2 trapz2(*f);
                            cout<<trapz2(0,2,1e-7);
                           }
    						break;
    					case 0:
    						flag1=0;break;
    					default: cout<<"
        Wrong Selection !(选择错误,重选)
    ";
    
    					}
    				}//end of while(flag1)
    			    break;
    			case 3:
    			      f=new Myfunction3;
    			      while(flag1)
    				{
    					menu2();
    					cin>>choose2;
    					switch(choose2)
    					{
    				    case 1:
                           {
                            Trapz1 trapz1(*f);
                            cout<<trapz1(0,2,1e-7);
                           }
    						break;
    					case 2:
                            {
                            Rectangle rectangle(*f);
                            cout<<rectangle(0,2,1e-7);
                            }
    						break;
    					case 3:
                           {
                            Simpson  simpson(*f);
                            cout<<simpson(0,2,1e-7);
                           }
    						break;
    					case 4:
                           {
                            Trapz2 trapz2(*f);
                            cout<<trapz2(0,2,1e-7);
                           }
    						break;
    					case 0:
    						flag1=0;break;
    					default: cout<<"
        Wrong Selection !(选择错误,重选)
    ";
    
    					}
    				}//end of while(flag1)
    			    break;
                case 4:
    			       flag=0;
    				   cout<<"
         *** The End! ***
    ";
    				   break;
    		    default: cout<<"
        Wrong Selection !(选择错误,重选)
    ";system("pause");
    		}
    	 }
    	 return 0;
    }
    


  • 相关阅读:
    setTimeOut与循环闭包问题
    ES6----class用法
    JS------对象的继承方式
    JavaScript对象 -构建
    nodejs异步---Async
    mongdb位置索引
    mongodb 索引3
    mongod 索引2
    mongodb 索引1
    3 C++数据类型
  • 原文地址:https://www.cnblogs.com/mengfanrong/p/4272328.html
Copyright © 2011-2022 走看看