Программа для взятия интеграла

code: #c
// Integral.cpp : Defines the entry point for the console application.
//
 
#include "stdafx.h"
  
#define		FUNCTION_SIN	1
#define		FUNCTION_COS	2
#define		FUNCTION_X2		3
#define		FUNCTION_X3		4
#define		FUNCTION_X4		5
 
#define     METHOD_FIRST	1
#define		METHOD_GRAPHIC  2
 
// Globals
 
 int g_nSelectedFunction = 0, g_nSelectedMethod = 0;
char *g_lpszFunctions[] = { "sinX", "cosX", "x^2", "x^3", "x^4" };
char *g_lpszMethods[] = { "function", "graphic" };
const int g_nNumFunctions = 5;
const int g_nNumMethods = 2;
float g_fUp, g_fLow;
float g_fIntegral;
 
 // Functions
void Hello();
void Dialog();
float CalcIntegral();
float FirstMethod( int nFunction );
float GraphicMethod( int nFunction );
void PrintResult( float fResult );
 
int main( int argc, char* argv[] )
{
	Hello();
	Dialog();
	PrintResult( CalcIntegral() );
	return( 0 );
}
 
//-----------------------------------------------
// enter
//-----------------------------------------------
 
void Hello()
{
	printf("calculate integral ver1.0 \n");
}
 
//-----------------------------------------------
// Dialog with user
//-----------------------------------------------
 
void Dialog()
{
	do {
		printf("choose your function \n");
		for( int i = 0; i < g_nNumFunctions; i++ )
			printf("%d. %s\n", i + 1, g_lpszFunctions[ i ] );
		scanf("%d", &g_nSelectedFunction );
	} while( g_nSelectedFunction < 1 || g_nSelectedFunction > 6 );
 
	do {
		printf("choose method \n");
		for( int i = 0; i < g_nNumMethods; i++ )
			printf("%d. %s\n", i + 1, g_lpszMethods[ i ] );
		scanf("%d", &g_nSelectedMethod );
	} while( g_nSelectedMethod < 1 || g_nSelectedMethod > 2 );
 
	printf("enter the low and up limit\n");
	scanf("%f %f", &g_fLow, &g_fUp ); 
 
}
 
//-----------------------------------------------
// Calculating the integral
//-----------------------------------------------
 
float CalcIntegral()
{
	switch( g_nSelectedMethod )
	{
		case METHOD_FIRST:
			return( FirstMethod( g_nSelectedFunction ) );
		case METHOD_GRAPHIC:
			return( GraphicMethod( g_nSelectedFunction ) );
	}
	return( 0 );
}
 
//-----------------------------------------------
 
float FirstMethod( int nFunction )
{
	switch( nFunction )
	{
		case FUNCTION_SIN:
			return( -cosf( g_fUp ) + cosf( g_fLow ) );
		case FUNCTION_COS:
			return( sinf( g_fUp ) - sinf( g_fLow ) );
		case FUNCTION_X2:
			return( ( g_fUp * g_fUp * g_fUp - g_fLow * g_fLow * g_fLow ) / 3.0f );
		case FUNCTION_X3:
			return( ( g_fUp * g_fUp * g_fUp * g_fUp - g_fLow * g_fLow * g_fLow * g_fLow ) / 4.0f );
		case FUNCTION_X4:
			return( ( g_fUp * g_fUp * g_fUp * g_fUp * g_fUp - g_fLow * g_fLow * g_fLow * g_fLow * g_fLow ) / 5.0f );
	}
	return( 0 );
}
 
//-----------------------------------------------
 
float GraphicMethod( int nFunction )
{
	float fDiff = ( g_fUp - g_fLow ) / 100;
	float fDiffHalf = fDiff * 0.5f;
	float fResult = 0;
	float fX = 0;
	for( int i = 0; i < 100; i++ )
	{
		fX = g_fLow + fDiff * i + fDiffHalf;
		switch( nFunction )
		{
			case FUNCTION_SIN:
				fResult += sin( fX ) * fDiff;
				break;
			case FUNCTION_COS:
				fResult += cos( fX ) * fDiff;
				break;
			case FUNCTION_X2:
				fResult += fX * fX * fDiff;
				break;
			case FUNCTION_X3:
				fResult += fX * fX * fX * fDiff;
				break;
			case FUNCTION_X4:
				fResult += fX * fX * fX * fX * fDiff;
				break;
		}
	}
 
	return( fResult );
}
 
//-----------------------------------------------
// Print result and shutdown program
//-----------------------------------------------
 
void PrintResult( float fResult )
{
	printf("your integral is %f \n enter any key to exit the programm\n", fResult );
	getchar();
}
Поделиться:

Похожие статьи: