#include <stdio.h>
class jifen
{
protected:
double a;
double b;
double n;
public:
double high1(double a ,double b ,double n)
{double i; i=(b-a)/n; return i;}
double high2(double a ,double b ,double n)
{double i; i=(b-a)/(2*n);return i;}
};
class jifen_T: public jifen
{
public:
double F1(double x,double y,int z); // 函数原型说明
double F2(double x,double y,int z);
double f1(double u);
double f2(double v);
double sum1(double c,double d,int e);
double sum2(double p,double q,int r);
void integralt (double x,double y,int z,double(*f)(double,double,int));
};
class jifen_X: public jifen
{
public:
double F3(double x,double y,int z); //函数说明
double F4(double x,double y,int z);
double f3(double u);
double f4(double v);
double sum5(double p,double q,int r);
double sum6(double p,double q,int r);
double sum3(double c,double d,int e);
double sum4(double c,double d,int e);
void integralt(double x,double y,int z,double (*f)(double,double ,int));
};
void jifen_T::integralt(double x,double y,int z,double (*f)(double,double,int)) // 利用梯形法球定积分
{
double ff;
ff=(*f)(x,y,z); //用函数指针调用F1函数
printf("%f",ff);
}
double jifen_T::F1(double x,double y,int z)
{
double h,Y1; //定义局部变量
h=high1(x,y,z); //调用宏定义
Y1=h/2*(f1(x)+f1(y)+2*sum1(x,y,z)); //梯形法公式
return Y1; //返回梯形法得到的1+x*x的定积分的值
}
double jifen_T::F2(double x,double y,int z)
{
double h,Y2; //定义局部变量
h=high1(x,y,z); //调用宏定义
Y2=h/2*(f2(x)+f2(y)+2*sum2(x,y,z)); //梯形法公式,调用f2,sum2函数
return Y2; //返回梯形法得到的1+x+x*x+x*x*x 的定积分的值
}
double jifen_T::f1(double u)
{
double y1; //定义变量
y1=u*u+1;
return y1; //返回值
}
double jifen_T::f2(double v)
{
double y2; //定义变量
y2=1+v+v*v+v*v*v;
return y2; //返回值
}
double jifen_T::sum1(double c,double d,int e)
{
double h=high1(c,d,e); //调用宏定义
double s1=0; //定义变量
for(int i=1;i<=e-1;i++) //for循环
{
double g=c+i*h;
s1+=f1(g);
}
return s1; // 返回值
}
double jifen_T::sum2(double p,double q,int r)
{
double h=high1(p,q,r); //引用宏定义
double s2=0; //定义变量
for(int i=1;i<=r-1;i++) //for循环
{
double g=p+i*h;
s2+=f2(g);
}
return s2; //返回值
}
void jifen_X::integralt (double x,double y,int z,double (*f)(double,double,int)) //辛普生法
{
double ff;
ff=(*f)(x,y,z); //函数指针
printf("%f",ff);
}
double jifen_X::F3(double x,double y,int z)
{
double h,Y1; //定义变量
h=high2(x,y,z);
Y1=h/3*(f3(x)+f3(y)+4*sum5(x,y,z)+2*sum6(x,y,z)); //调用f3,sum5,sum6函数
return Y1; //返回值
}
double jifen_X::F4(double x,double y,int z)
{
double h,Y2;
h=high2(x,y,z);
Y2=h/3*(f4(x)+f4(y)+4*sum3(x,y,z)+2*sum4(x,y,z));
return Y2;
}
double jifen_X::f3(double u)
{
double y1=1+u*u;
return y1;
}
double jifen_X::f4(double v)
{
double y2=1+v+v*v+v*v*v;
return y2;
}
double jifen_X::sum5(double p,double q,int r)
{
double h=high2(p,q,r);
double s=0;
for(int i=1;i<=2*r-1;i+=2) //for循环
{
double g=p+i*h;
s+=f3(g);
}
return s;
}
double jifen_X::sum6(double p,double q,int r)
{
double h=high2(p,q,r);
double s=0;
for(int i=2;i<=2*r-2;i+=2) //for循环
{
double g=p+i*h;
s+=f3(g);
}
return s;
}
double jifen_X::sum3(double c,double d,int e)
{
double h=high2(c,d,e);
double s=0;
for(int i=1;i<=2*e-1;i+=2)
{
double g=c+i*h;
s+=f4(g);
}
return s;
}
double jifen_X::sum4(double c,double d,int e)
{
double h=high2(c,d,e);
double s=0;
for(int i=2;i<=2*e-2;i+=2)
{
double g=c+i*h;
s+=f4(g);
}
return s;
}
#include <stdio.h>
#include <conio.h>
#include "dingjifen.h"
int main() //主函数
{
jifen a,b,n;
jifen_T* p; //定义变量
jifen_X* q;
printf("please input a,b,n= \n"); //输出提示
scanf("%f%f%f",&a,&b,&n);
printf("a=%d\n b=%d\n n=%d\n",a,b,n);
printf("梯形法:");
printf("intesum1=");
p->integralt(a,b,n,F1); //应用函数指针调用函数
printf("intesum2=");
p->integralt(a,b,n,F2); //应用函数指针调用函数
printf("辛普生法:");
printf("intesum1=");
q->integralt(a,b,n,F3); //应用函数指针调用函数
printf("intesum2=");
q->integralt(a,b,n,F4); //应用函数指针调用函数
getch();
return 0;
}
错误是:
.\jifen.cpp(16) : error C2065: 'F1' : undeclared identifier
.\jifen.cpp(19) : error C2065: 'F2' : undeclared identifier
.\jifen.cpp(23) : error C2065: 'F3' : undeclared identifier
.\jifen.cpp(26) : error C2065: 'F4' : undeclared identifier