repeat_t.cpp

来自「从出版社求得的经典数值算法」· C++ 代码 · 共 51 行

CPP
51
字号
/////////////////////////////////////////////////////////////////
//	§3  数值积分
//
//	程序3.1 — 复化T型公式

#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <graphics.h>
#include <math.h>

#include "expressi.cpp"

float RepeatT(char FxString[],float a,float b,int n)
{
	int i;
	float h,Value;

	if(n<=0) return 0;
	h=(b-a)/n;
	if(CreateFx(FxString)) return 0;
	Value=0;
	for(i=1;i<n;++i)
		Value+=f(a+i*h);
	return((f(a)+f(b)+2*Value)*h/2);
}

void main()
{
	float i1,a,b;
	int n;
	char FxString[200];

	printf("\nInput function,a,b,n: ");
	scanf("%s %f %f %d",FxString,&a,&b,&n);

	i1=RepeatT(FxString,a,b,n);

	printf("\n%f",i1);

	getch();
}

/*
	运行实例:

Input function,a,b,n: 1/(1+x) 0 1 10

0.693771
*/

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?