All pastes #973928 Raw Edit

Something

public text v1 · immutable
·published 2008-04-06 12:29 UTC
rendered paste body
using namespace std;
#include <iostream>
#include <math.h>

long double IntervalHigh = 0,
            IntervalLow = 0,
            Sensitivity = 0.000001;

int PositionInEquation = 0;

char Equation[ 129 ],
     Element[ 129 ];

void PrintHeader();
void GetInterval();
void GetEquation();
void GetSensitivity();
void GetRoot();
char GetElement();
long double F( long double x);
long double CalculateElement( long double x );


void main()
{
    PrintHeader();
    GetEquation();
    GetInterval();
    GetSensitivity();
    system("pause");
    GetRoot();
    system("pause");
}

void PrintHeader()
{
    printf( "\n\nTazi programa namira koren na f(x)=0 po metoda na razpolovqvaneto\n"
            "Avtor: Ime Prezime Famiq\n\n" );
}

void GetEquation()
{
    Equation[ 0 ] = 0;
    while( strlen( Equation ) < 2 )
    {
        printf( "\nVyvedete funkciqta f(x)=" );
        gets( Equation );
        if( strlen( Equation ) < 2 )
            printf( "\n\7Uravnenieto trqbva da byde pone 2 simvola!\n\n" );
    }
}

void GetInterval()
{
    char TempString[ 129 ],
         Done = 0;
    while( !Done )
    {
        Done = 1;
        TempString[ 0 ] = 0;
        while( !strlen( TempString ) )
        {
            printf( "\nVyvedete dolnata granica na intervala za tyrsene: " );
            gets( TempString );
        }
        IntervalLow = atof( TempString );
        TempString[ 0 ] = 0;
        while( !strlen( TempString ) )
        {
            printf( "Vyvedete gornata granica na intervala za tyrsene: " );
            gets( TempString );
        }
        IntervalHigh = atof( TempString );
        if( IntervalLow == IntervalHigh )
        {
            Done = 0;
            printf( "\n\7Gornata i dolnata granica na intervala syvpadat. Molq, vyvedete novi granici.\n" );
        }
        if( IntervalLow > IntervalHigh )
        {
            Done = 0;
            printf( "\n\7Dolnata granica na intervala e po-golqma ot gornata. Molq, vyvedete novi granici.\n" );
        }
    }

}    

void GetSensitivity()
{
    char TempString[ 129 ];
    printf( "\nShte byde izpolzvana standartna tochnost %1.16f\n"
            "Vyvedete tochnost, koqto iskate da se izpolva\n"
            "ili natisnete ENTER za zapazvane na standartnata: ", Sensitivity );
    gets( TempString );
    if( ( atof( TempString ) ) &&
        ( atof( TempString ) > 0 ) )
        Sensitivity = atof( TempString );
    printf( "\nIzpolzvana tochnost: %1.16f\n\n", Sensitivity );
}

void GetRoot()
{      
    long double low = IntervalLow,
                 high = IntervalHigh,
                 middle, value;
      
    while( 1 )
    {
        middle = low + ( high - low ) / 2;
        value = F(low)*F(high);
        printf( "Interval: %2.16f : %2.16f\n", low, high );

        if( fabs( value ) < Sensitivity )
        {
            printf( "\nKorenyt e: %2.16f\n", middle );
            break;;
        }

        if( high == low )
        {
            printf( "\nNqma koren v posocheniq interval!\7\n" );
            break;
        }

        if( F(low)*F(middle) < 0 )
            high = middle;
        else
            low = middle;
    }
}

long double F(long double x)
{        
    long double ReturnValue = 0;
    PositionInEquation = 0;
    while( GetElement() )
        ReturnValue += CalculateElement( x );
    return ReturnValue;
}

long double CalculateElement( long double x )
{
    long double Temp = 0,
                ReturnValue = 0;
    int Loop = 1, Exponent = 0;
    if( ( Element[ 1 ] >= '0' ) && ( Element[ 1 ] <= '9' ) )
    {
        for(; Loop < strlen( Element ); Loop++ )
        {
            if( ( Element[ Loop ] < '0' ) || ( Element[ Loop ] > '9' ) )
                break;
            if( !Temp )
                Temp = Element[ Loop ] - 48;
            else
                Temp = Temp * 10 + Element[ Loop ] - 48;
        }
    }

    if( ( Element[ Loop ] == 'x' ) || ( Element[ Loop ] == 'X' ) )
    {
        if ( Element[ Loop + 1 ] == '^' )
        {
            for( Loop += 2; Loop < strlen( Element ); Loop++ )
            {
                if( ( Element[ Loop ] < '0' ) || ( Element[ Loop ] > '9' ) )
                    break;
                if( !Exponent )
                    Exponent = Element[ Loop ] - 48;
                else
                    Exponent = Exponent * 10 + Element[ Loop ] - 48;
            }
        }
        else
            Exponent = 1;
    }

    if( Temp )
        ReturnValue = Temp;
    else
        ReturnValue = 1;

    if( Exponent )
        ReturnValue *= pow( x, Exponent );

    if( Element[ 0 ] == '-' )
        ReturnValue = -ReturnValue;

    return ReturnValue;
}

char GetElement()
{
    char ReturnValue = 0,
         TempString[ 2 ] = { 0, 0 };
    Element[ 0 ] = 0;

    if( !PositionInEquation )
    {
        if( Equation[ 0 ] == '-' )
        {
            strcat( Element, "-" );
            PositionInEquation++;
        }
        else
        {
            strcat( Element, "+" );
            if( Equation[ 0 ] == '+' )
                PositionInEquation++;
        }
    }
    else
    {
        if( ( Equation[ PositionInEquation ] == '+' ) ||
            ( Equation[ PositionInEquation ] == '-' ) )
        {
            TempString[ 0 ] = Equation[ PositionInEquation ];
            strcat( Element, TempString );
            PositionInEquation++;
        }
    }
    for(; PositionInEquation < strlen( Equation ); PositionInEquation++ )
    {
        if( ( Equation[ PositionInEquation ] == '+' ) || ( Equation[ PositionInEquation ] == '-' ) )
            break;
        TempString[ 0 ] = Equation[ PositionInEquation ];
        strcat( Element, TempString );
        ReturnValue = 1;
    }
    return ReturnValue;
}