aplikasi menghitung matematika dengan c++

128
1. Program untuk memperkirakan nilai Integral dari fungsi yang diberikan menggunakan aturan kuadrat Gaussian di C ++ Programming # include <iostream.h> # include <stdlib.h> # include <string.h> # include <stdio.h> # include <conio.h> # include <math.h> constint max_size=13; int n=0; int top=-1; longdouble a=-1; longdouble b=1; longdouble two_point_result=0; longdouble three_point_result=0; char Fx[100]={NULL}; char Stack[30][30]={NULL}; char Postfix_expression[30][30]={NULL}; /*************************************************************************// *************************************************************************/// ------------------------ Funcion Prototypes ------------------------- ///************************************************************************* //*************************************************************************/ void push(constchar *); void convert_ie_to_pe(constchar *); constchar* pop( ); constlongdouble evaluate_postfix_expression(constlongdouble); void show_screen( ); void clear_screen( ); void get_input( ); void change_limits( ); void apply_gaussian_quadrature_rule( ); void show_result( ); /*************************************************************************// *************************************************************************/// ------------------------------ main( ) ------------------------------ ///************************************************************************* //*************************************************************************/ int main( ) {

Upload: firmanwahyudi-anagti

Post on 24-Jan-2017

128 views

Category:

Education


0 download

TRANSCRIPT

Page 1: Aplikasi menghitung matematika dengan c++

1. Program untuk memperkirakan nilai Integral dari fungsi yang diberikan

menggunakan aturan kuadrat Gaussian di C ++ Programming

# include <iostream.h>

# include <stdlib.h>

# include <string.h>

# include <stdio.h>

# include <conio.h>

# include <math.h>

constint max_size=13;

int n=0;

int top=-1;

longdouble a=-1;

longdouble b=1;

longdouble two_point_result=0;

longdouble three_point_result=0;

char Fx[100]={NULL};

char Stack[30][30]={NULL};

char Postfix_expression[30][30]={NULL};

/*************************************************************************//

*************************************************************************///

------------------------ Funcion Prototypes -------------------------

///*************************************************************************

//*************************************************************************/

void push(constchar *);

void convert_ie_to_pe(constchar *);

constchar* pop( );

constlongdouble evaluate_postfix_expression(constlongdouble);

void show_screen( );

void clear_screen( );

void get_input( );

void change_limits( );

void apply_gaussian_quadrature_rule( );

void show_result( );

/*************************************************************************//

*************************************************************************///

------------------------------ main( ) ------------------------------

///*************************************************************************

//*************************************************************************/

int main( )

{

Page 2: Aplikasi menghitung matematika dengan c++

clrscr( );

textmode(C4350);

show_screen( );

get_input( );

show_result( );

getch( );

return 0;

}

/*************************************************************************//

*************************************************************************///

------------------------ Funcion Definitions ------------------------

///*************************************************************************

//*************************************************************************/

/*************************************************************************//

/-------------------------- show_screen( ) ---------------------------

///*************************************************************************

/void show_screen( )

{

cprintf("\n******************************************************************

**************");

cprintf("***************************- -

**************************");

cprintf("*--------------------------- ");

textbackground(1);

cprintf("Integrasi numerik ");

textbackground(8);

cprintf(" --------------------------*");

cprintf("*-*************************- -

************************-*");

cprintf("*-

***************************************************************************

*-*");

for(int count=0;count<42;count++)

cprintf("*-* *-*");

gotoxy(1,46);

cprintf("*-

***************************************************************************

*-*");

cprintf("*------------------------------------------------------------------------------*");

Page 3: Aplikasi menghitung matematika dengan c++

cprintf("********************************************************************

************");

gotoxy(1,2);

}

/*************************************************************************//

/------------------------- clear_screen( ) ---------------------------

///*************************************************************************

/void clear_screen( )

{

for(int count=0;count<37;count++)

{

gotoxy(5,8+count);

cout<<" ";

}

gotoxy(1,2);

}

/*************************************************************************//

/-------------------------- push(const char*) ------------------------

///*************************************************************************

/void push(constchar* Operand)

{

if(top==(max_size-1))

{

cout<<"Error : Stack is full."<<endl;

cout<<"\n Press any key to exit.";

getch( );

exit(0);

}

else

{

top++;

strcpy(Stack[top],Operand);

}

}

/*************************************************************************//

/------------------------------ pop( ) -------------------------------

///*************************************************************************

/constchar* pop( )

{

Page 4: Aplikasi menghitung matematika dengan c++

char Operand[40]={NULL};

if(top==-1)

{

cout<<"Error : Stack is empty."<<endl;

cout<<"\n Press any key to exit.";

getch( );

exit(0);

}

else

{

strcpy(Operand,Stack[top]);

strset(Stack[top],NULL);

top--;

}

return Operand;

}

/*************************************************************************//

/-------------------- convert_ie_to_pe(const char*) ------------------

///*************************************************************************

/void convert_ie_to_pe(constchar* Expression)

{

char Infix_expression[100]={NULL};

char Symbol_scanned[30]={NULL};

push("(");

strcpy(Infix_expression,Expression);

strcat(Infix_expression,"+0)");

int flag=0;

int count_1=0;

int count_2=0;

int equation_length=strlen(Infix_expression);

if(Infix_expression[0]=='(')

flag=1;

do

{

strset(Symbol_scanned,NULL);

if(flag==0)

{

int count_3=0;

Page 5: Aplikasi menghitung matematika dengan c++

do

{

Symbol_scanned[count_3]=Infix_expression[count_1];

count_1++;

count_3++;

}

while(count_1<=equation_length &&

Infix_expression[count_1]!='(' &&

Infix_expression[count_1]!='+' &&

Infix_expression[count_1]!='-' &&

Infix_expression[count_1]!='*' &&

Infix_expression[count_1]!='/' &&

Infix_expression[count_1]!='^' &&

Infix_expression[count_1]!=')');

flag=1;

}

elseif(flag==1)

{

Symbol_scanned[0]=Infix_expression[count_1];

count_1++;

if(Infix_expression[count_1]!='(' &&

Infix_expression[count_1]!='^' &&

Infix_expression[count_1]!='*' &&

Infix_expression[count_1]!='/' &&

Infix_expression[count_1]!='+' &&

Infix_expression[count_1]!='-' &&

Infix_expression[count_1]!=')')

flag=0;

if(Infix_expression[count_1-1]=='(' &&

(Infix_expression[count_1]=='-' ||

Infix_expression[count_1]=='+'))

flag=0;

}

if(strcmp(Symbol_scanned,"(")==0)

push("(");

elseif(strcmp(Symbol_scanned,")")==0)

{

while(strcmp(Stack[top],"(")!=0)

{

strcpy(Postfix_expression[count_2],pop( ));

Page 6: Aplikasi menghitung matematika dengan c++

count_2++;

}

pop( );

}

elseif(strcmp(Symbol_scanned,"^")==0 ||

strcmp(Symbol_scanned,"+")==0 ||

strcmp(Symbol_scanned,"-")==0 ||

strcmp(Symbol_scanned,"*")==0 ||

strcmp(Symbol_scanned,"/")==0)

{

if(strcmp(Symbol_scanned,"^")==0)

{ }

elseif(strcmp(Symbol_scanned,"*")==0 ||

strcmp(Symbol_scanned,"/")==0)

{

while(strcmp(Stack[top],"^")==0 ||

strcmp(Stack[top],"*")==0 ||

strcmp(Stack[top],"/")==0)

{

strcpy(Postfix_expression[count_2],pop( ));

count_2++;

}

}

elseif(strcmp(Symbol_scanned,"+")==0 ||

strcmp(Symbol_scanned,"-")==0)

{

while(strcmp(Stack[top],"(")!=0)

{

strcpy(Postfix_expression[count_2],pop( ));

count_2++;

}

}

push(Symbol_scanned);

}

else

{

strcat(Postfix_expression[count_2],Symbol_scanned);

count_2++;

}

}

while(strcmp(Stack[top],NULL)!=0);

Page 7: Aplikasi menghitung matematika dengan c++

strcat(Postfix_expression[count_2],"=");

count_2++;

}

/*************************************************************************//

/---------- evaluate_postfix_expression(const long double) -----------

///*************************************************************************

/constlongdouble evaluate_postfix_expression(constlongdouble x)

{

longdouble function_value=0;

int count_1=-1;

char Symbol_scanned[30]={NULL};

do

{

count_1++;

strcpy(Symbol_scanned,Postfix_expression[count_1]);

if(strcmp(Symbol_scanned,"^")==0 ||

strcmp(Symbol_scanned,"*")==0 ||

strcmp(Symbol_scanned,"/")==0 ||

strcmp(Symbol_scanned,"+")==0 ||

strcmp(Symbol_scanned,"-")==0)

{

char Result[30]={NULL};

char Operand[2][30]={NULL};

strcpy(Operand[0],pop( ));

strcpy(Operand[1],pop( ));

longdouble operand[2]={0};

longdouble result=0;

char *endptr;

for(int count_2=0;count_2<2;count_2++)

{

int flag=0;

if(Operand[count_2][0]=='-')

{

int length=strlen(Operand[count_2]);

for(int count_3=0;count_3<(length-1);count_3++)

Page 8: Aplikasi menghitung matematika dengan c++

Operand[count_2][count_3]=Operand[count_2][(count_3+1)];

Operand[count_2][count_3]=NULL;

flag=1;

}

if(strcmp(Operand[count_2],"x")==0)

operand[count_2]=x;

elseif(strcmp(Operand[count_2],"e")==0)

operand[count_2]=2.718282;

elseif(strcmp(Operand[count_2],"sinx")==0)

operand[count_2]=sinl(x);

elseif(strcmp(Operand[count_2],"cosx")==0)

operand[count_2]=cosl(x);

elseif(strcmp(Operand[count_2],"tanx")==0)

operand[count_2]=tanl(x);

elseif(strcmp(Operand[count_2],"lnx")==0)

operand[count_2]=logl(x);

elseif(strcmp(Operand[count_2],"logx")==0)

operand[count_2]=log10l(x);

else

operand[count_2]=strtod(Operand[count_2],&endptr);

if(flag)

operand[count_2]*=-1;

}

switch(Symbol_scanned[0])

{

case'^' : result=powl(operand[1],operand[0]);

break;

case'*' : result=operand[1]*operand[0];

break;

case'/' : result=operand[1]/operand[0];

break;

case'+' : result=operand[1]+operand[0];

break;

case'-' : result=operand[1]-operand[0];

Page 9: Aplikasi menghitung matematika dengan c++

break;

}

gcvt(result,25,Result);

push(Result);

}

elseif(strcmp(Symbol_scanned,"=")!=0)

push(Symbol_scanned);

}

while(strcmp(Symbol_scanned,"=")!=0);

char Function_value[30]={NULL};

char *endptr;

strcpy(Function_value,pop( ));

function_value=strtod(Function_value,&endptr);

return function_value;

}

/*************************************************************************//

/----------------------------- get_input( ) --------------------------

///*************************************************************************

/void get_input( )

{

clear_screen( );

gotoxy(6,10);

cout<<"Non-Linear Function :";

gotoxy(6,11);

cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ";

gotoxy(6,37);

cout<<"Note : Write the function with proper Braces ( ) e.g; 2x+3 as (2*x)+3";

gotoxy(6,40);

cout<<"Available Operators : ^ (raised to power) , * , / , + , -";

gotoxy(6,42);

cout<<"Available Operands : x , e , sinx , cosx , tanx , lnx , logx ,";

gotoxy(6,44);

cout<<" n = any number";

gotoxy(6,14);

cout<<"Enter the Function : f(x) = ";

Page 10: Aplikasi menghitung matematika dengan c++

cin>>Fx;

convert_ie_to_pe(Fx);

}

/*************************************************************************//

/----------------- apply_gaussian_quadrature_rule( ) -----------------

///*************************************************************************

/void apply_gaussian_quadrature_rule( )

{

longdouble temp_1=0;

longdouble temp_2=0;

longdouble temp_3=0;

temp_1=evaluate_postfix_expression((-sqrtl(0.6)));

temp_1*=(0.555555556);

temp_2=evaluate_postfix_expression(0);

temp_2*=(0.888888889);

temp_3=evaluate_postfix_expression(sqrtl(0.6));

temp_3*=(0.555555556);

three_point_result=(temp_1+temp_2+temp_3);

temp_1=0;

temp_2=0;

temp_1=evaluate_postfix_expression((-1/sqrtl(3)));

temp_2=evaluate_postfix_expression((1/sqrtl(3)));

two_point_result=(temp_1+temp_2);

}

/*************************************************************************//

/---------------------------- show_result( ) -------------------------

///*************************************************************************

/void show_result( )

{

clear_screen( );

gotoxy(6,9);

cout<<"Gaussian Quadrature Rule:";

gotoxy(6,10);

cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ";

Page 11: Aplikasi menghitung matematika dengan c++

gotoxy(24,12);

cout<<" 1ô";

gotoxy(6,13);

cout<<"Two Point Rule: ³f(x)dx ÷ f(-1/û3)+f(1/û3)";

gotoxy(24,14);

cout<<"-1õ";

gotoxy(24,16);

cout<<" 1ô";

gotoxy(6,17);

cout<<"Three Point Rule: ³f(x)dx ÷ (5/9)f(-û(3/5))+(8/9)f(0)+(5/9)f(û(3/5))";

gotoxy(24,18);

cout<<"-1õ";

apply_gaussian_quadrature_rule( );

gotoxy(6,22);

cout<<" 1ô";

gotoxy(6,24);

cout<<" -1õ";

gotoxy(6,23);

cout<<"Estimation of ³f(x)dx :";

gotoxy(6,25);

cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ";

gotoxy(8,27);

cout<<"Estimated Integral Value (using 2-Point Rule) = "<<two_point_result;

gotoxy(8,29);

cout<<"Estimated Integral Value (using 3-Point Rule) = "<<three_point_result;

gotoxy(1,2);

}

2. Program untuk memperkirakan nilai Integral dari fungsi di poin yang diberikan dari

data yang diberikan dengan Metode Romberg di C ++ Programming

# include <iostream.h>

# include <stdlib.h>

# include <string.h>

# include <stdio.h>

# include <conio.h>

# include <math.h>

Page 12: Aplikasi menghitung matematika dengan c++

constint max_size=8;

int n=0;

int top=-1;

longdouble h=0;

longdouble a=0;

longdouble b=0;

longdouble ri[max_size][max_size]={0};

longdouble xn[max_size]={0};

longdouble fx[max_size]={0};

char Fx[100]={NULL};

char Stack[30][30]={NULL};

char Postfix_expression[30][30]={NULL};

/*************************************************************************//

*************************************************************************///

------------------------ Funcion Prototypes -------------------------

///*************************************************************************

//*************************************************************************/

void push(constchar *);

void convert_ie_to_pe(constchar *);

constchar* pop( );

constlongdouble evaluate_postfix_expression(constlongdouble);

void show_screen( );

void clear_screen( );

void get_input( );

void apply_trapezoidal_rule( );

void apply_romberg_method( );

void show_result( );

/*************************************************************************//

*************************************************************************///

------------------------------ main( ) ------------------------------

///*************************************************************************

//*************************************************************************/

int main( )

{

clrscr( );

textmode(C4350);

show_screen( );

get_input( );

Page 13: Aplikasi menghitung matematika dengan c++

apply_trapezoidal_rule( );

apply_romberg_method( );

show_result( );

getch( );

return 0;

}

/*************************************************************************//

*************************************************************************///

------------------------ Funcion Definitions ------------------------

///*************************************************************************

//*************************************************************************/

/*************************************************************************//

/-------------------------- show_screen( ) ---------------------------

///*************************************************************************

/void show_screen( )

{

cprintf("\n******************************************************************

**************");

cprintf("****************************- -

***************************");

cprintf("*---------------------------- ");

textbackground(1);

cprintf(" Romberg Integration ");

textbackground(8);

cprintf(" ---------------------------*");

cprintf("*-**************************- -

*************************-*");

cprintf("*-

***************************************************************************

*-*");

for(int count=0;count<42;count++)

cprintf("*-* *-*");

gotoxy(1,46);

cprintf("*-

***************************************************************************

*-*");

cprintf("*------------------------------------------------------------------------------*");

cprintf("********************************************************************

************");

gotoxy(1,2);

Page 14: Aplikasi menghitung matematika dengan c++

}

/*************************************************************************//

/------------------------- clear_screen( ) ---------------------------

///*************************************************************************

/void clear_screen( )

{

for(int count=0;count<37;count++)

{

gotoxy(5,8+count);

cout<<" ";

}

gotoxy(1,2);

}

/*************************************************************************//

/-------------------------- push(const char*) ------------------------

///*************************************************************************

/void push(constchar* Operand)

{

if(top==(max_size-1))

{

cout<<"Error : Stack is full."<<endl;

cout<<"\n Press any key to exit.";

getch( );

exit(0);

}

else

{

top++;

strcpy(Stack[top],Operand);

}

}

/*************************************************************************//

/------------------------------ pop( ) -------------------------------

///*************************************************************************

/constchar* pop( )

{

char Operand[40]={NULL};

if(top==-1)

{

cout<<"Error : Stack is empty."<<endl;

Page 15: Aplikasi menghitung matematika dengan c++

cout<<"\n Press any key to exit.";

getch( );

exit(0);

}

else

{

strcpy(Operand,Stack[top]);

strset(Stack[top],NULL);

top--;

}

return Operand;

}

/*************************************************************************//

/-------------------- convert_ie_to_pe(const char*) ------------------

///*************************************************************************

/void convert_ie_to_pe(constchar* Expression)

{

char Infix_expression[100]={NULL};

char Symbol_scanned[30]={NULL};

push("(");

strcpy(Infix_expression,Expression);

strcat(Infix_expression,"+0)");

int flag=0;

int count_1=0;

int count_2=0;

int equation_length=strlen(Infix_expression);

if(Infix_expression[0]=='(')

flag=1;

do

{

strset(Symbol_scanned,NULL);

if(flag==0)

{

int count_3=0;

do

{

Symbol_scanned[count_3]=Infix_expression[count_1];

count_1++;

Page 16: Aplikasi menghitung matematika dengan c++

count_3++;

}

while(count_1<=equation_length &&

Infix_expression[count_1]!='(' &&

Infix_expression[count_1]!='+' &&

Infix_expression[count_1]!='-' &&

Infix_expression[count_1]!='*' &&

Infix_expression[count_1]!='/' &&

Infix_expression[count_1]!='^' &&

Infix_expression[count_1]!=')');

flag=1;

}

elseif(flag==1)

{

Symbol_scanned[0]=Infix_expression[count_1];

count_1++;

if(Infix_expression[count_1]!='(' &&

Infix_expression[count_1]!='^' &&

Infix_expression[count_1]!='*' &&

Infix_expression[count_1]!='/' &&

Infix_expression[count_1]!='+' &&

Infix_expression[count_1]!='-' &&

Infix_expression[count_1]!=')')

flag=0;

if(Infix_expression[count_1-1]=='(' &&

(Infix_expression[count_1]=='-' ||

Infix_expression[count_1]=='+'))

flag=0;

}

if(strcmp(Symbol_scanned,"(")==0)

push("(");

elseif(strcmp(Symbol_scanned,")")==0)

{

while(strcmp(Stack[top],"(")!=0)

{

strcpy(Postfix_expression[count_2],pop( ));

count_2++;

}

pop( );

}

Page 17: Aplikasi menghitung matematika dengan c++

elseif(strcmp(Symbol_scanned,"^")==0 ||

strcmp(Symbol_scanned,"+")==0 ||

strcmp(Symbol_scanned,"-")==0 ||

strcmp(Symbol_scanned,"*")==0 ||

strcmp(Symbol_scanned,"/")==0)

{

if(strcmp(Symbol_scanned,"^")==0)

{ }

elseif(strcmp(Symbol_scanned,"*")==0 ||

strcmp(Symbol_scanned,"/")==0)

{

while(strcmp(Stack[top],"^")==0 ||

strcmp(Stack[top],"*")==0 ||

strcmp(Stack[top],"/")==0)

{

strcpy(Postfix_expression[count_2],pop( ));

count_2++;

}

}

elseif(strcmp(Symbol_scanned,"+")==0 ||

strcmp(Symbol_scanned,"-")==0)

{

while(strcmp(Stack[top],"(")!=0)

{

strcpy(Postfix_expression[count_2],pop( ));

count_2++;

}

}

push(Symbol_scanned);

}

else

{

strcat(Postfix_expression[count_2],Symbol_scanned);

count_2++;

}

}

while(strcmp(Stack[top],NULL)!=0);

strcat(Postfix_expression[count_2],"=");

count_2++;

}

Page 18: Aplikasi menghitung matematika dengan c++

/*************************************************************************//

/---------- evaluate_postfix_expression(const long double) -----------

///*************************************************************************

/constlongdouble evaluate_postfix_expression(constlongdouble x)

{

longdouble function_value=0;

int count_1=-1;

char Symbol_scanned[30]={NULL};

do

{

count_1++;

strcpy(Symbol_scanned,Postfix_expression[count_1]);

if(strcmp(Symbol_scanned,"^")==0 ||

strcmp(Symbol_scanned,"*")==0 ||

strcmp(Symbol_scanned,"/")==0 ||

strcmp(Symbol_scanned,"+")==0 ||

strcmp(Symbol_scanned,"-")==0)

{

char Result[30]={NULL};

char Operand[2][30]={NULL};

strcpy(Operand[0],pop( ));

strcpy(Operand[1],pop( ));

longdouble operand[2]={0};

longdouble result=0;

char *endptr;

for(int count_2=0;count_2<2;count_2++)

{

int flag=0;

if(Operand[count_2][0]=='-')

{

int length=strlen(Operand[count_2]);

for(int count_3=0;count_3<(length-1);count_3++)

Operand[count_2][count_3]=Operand[count_2][(count_3+1)];

Operand[count_2][count_3]=NULL;

flag=1;

Page 19: Aplikasi menghitung matematika dengan c++

}

if(strcmp(Operand[count_2],"x")==0)

operand[count_2]=x;

elseif(strcmp(Operand[count_2],"e")==0)

operand[count_2]=2.718282;

elseif(strcmp(Operand[count_2],"sinx")==0)

operand[count_2]=sinl(x);

elseif(strcmp(Operand[count_2],"cosx")==0)

operand[count_2]=cosl(x);

elseif(strcmp(Operand[count_2],"tanx")==0)

operand[count_2]=tanl(x);

elseif(strcmp(Operand[count_2],"lnx")==0)

operand[count_2]=logl(x);

elseif(strcmp(Operand[count_2],"logx")==0)

operand[count_2]=log10l(x);

else

operand[count_2]=strtod(Operand[count_2],&endptr);

if(flag)

operand[count_2]*=-1;

}

switch(Symbol_scanned[0])

{

case'^' : result=powl(operand[1],operand[0]);

break;

case'*' : result=operand[1]*operand[0];

break;

case'/' : result=operand[1]/operand[0];

break;

case'+' : result=operand[1]+operand[0];

break;

case'-' : result=operand[1]-operand[0];

break;

}

gcvt(result,25,Result);

Page 20: Aplikasi menghitung matematika dengan c++

push(Result);

}

elseif(strcmp(Symbol_scanned,"=")!=0)

push(Symbol_scanned);

}

while(strcmp(Symbol_scanned,"=")!=0);

char Function_value[30]={NULL};

char *endptr;

strcpy(Function_value,pop( ));

function_value=strtod(Function_value,&endptr);

return function_value;

}

/*************************************************************************//

/----------------------------- get_input( ) --------------------------

///*************************************************************************

/void get_input( )

{

do

{

clear_screen( );

gotoxy(6,9);

cout<<"Input :";

gotoxy(6,10);

cout<<"ÍÍÍÍÍÍÍ";

gotoxy(37,13);

cout<<"[ n = 2,4,8 ]";

gotoxy(6,12);

cout<<"Enter the max. number of sub-intervals = n = ";

cin>>n;

if(n!=2 && n!=4 && n!=8)

{

gotoxy(12,25);

cout<<"Error : Wrong Input. Press <Esc> to exit or any other key";

gotoxy(12,26);

cout<<" to try again.";

n=int(getche( ));

Page 21: Aplikasi menghitung matematika dengan c++

if(n==27)

exit(0);

}

}

while(n!=2 && n!=4 && n!=8);

gotoxy(6,16);

cout<<"Enter the value of Lower limit = a = ";

cin>>a;

gotoxy(6,18);

cout<<"Enter the value of Upper Limit = b = ";

cin>>b;

gotoxy(25,43);

cout<<"Press any key to continue...";

getch( );

clear_screen( );

gotoxy(6,10);

cout<<"Non-Linear Function :";

gotoxy(6,11);

cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ";

gotoxy(6,37);

cout<<"Note : Write the function with proper Braces ( ) e.g; 2x+3 as (2*x)+3";

gotoxy(6,40);

cout<<"Available Operators : ^ (raised to power) , * , / , + , -";

gotoxy(6,42);

cout<<"Available Operands : x , e , sinx , cosx , tanx , lnx , logx ,";

gotoxy(6,44);

cout<<" n = any number";

gotoxy(6,14);

cout<<"Enter the Function : f(x) = ";

cin>>Fx;

convert_ie_to_pe(Fx);

}

Page 22: Aplikasi menghitung matematika dengan c++

/*************************************************************************//

/---------------------- apply_trapezoidal_rule( ) --------------------

///*************************************************************************

/void apply_trapezoidal_rule( )

{

int count_1=0;

for(int count_2=1;count_2<=n;count_2+=count_2)

{

for(int count_3=0;count_3<max_size;count_3++)

{

xn[count_3]=0;

fx[count_3]=0;

}

h=((b-a)/count_2);

xn[0]=a;

for(int count_4=0;count_4<count_2;count_4++)

xn[(count_4+1)]=(xn[count_4]+h);

for(int count_5=0;count_5<=count_2;count_5++)

fx[count_5]=evaluate_postfix_expression(xn[count_5]);

for(int count_6=1;count_6<count_2;count_6++)

ri[count_1][0]+=fx[count_6];

ri[count_1][0]*=2;

ri[count_1][0]+=fx[0];

ri[count_1][0]+=fx[count_2];

ri[count_1][0]*=(h/2);

count_1++;

}

}

/*************************************************************************//

/---------------------- apply_romberg_method( ) ----------------------

///*************************************************************************

/void apply_romberg_method( )

{

int counter=0;

switch(n)

{

case 2 : counter=1;

break;

Page 23: Aplikasi menghitung matematika dengan c++

case 4 : counter=2;

break;

case 8 : counter=3;

break;

}

for(int count_1=1;count_1<=counter;count_1++)

{

for(int count_2=count_1;count_2<=counter;count_2++)

{

longdouble rjk_1=0;

longdouble rj_1k_1=0;

rjk_1=ri[count_2][(count_1-1)];

rj_1k_1=ri[(count_2-1)][(count_1-1)];

ri[count_2][count_1]=(((powl(4,count_1)*rjk_1)-rj_1k_1)/(powl(4,count_1)-1));

}

}

}

/*************************************************************************//

/---------------------------- show_result( ) -------------------------

///*************************************************************************

/void show_result( )

{

clear_screen( );

int counter=0;

switch(n)

{

case 2 : counter=1;

break;

case 4 : counter=2;

break;

case 8 : counter=3;

break;

}

gotoxy(6,10);

cout<<"Solution :";

gotoxy(6,11);

cout<<"ÍÍÍÍÍÍÍÍÍÍ";

Page 24: Aplikasi menghitung matematika dengan c++

if(n==1)

{

gotoxy(6,13);

cout<<"ÚÄÄÄÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄ¿";

}

elseif(n==2)

{

gotoxy(6,13);

cout<<"ÚÄÄÄÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿";

}

elseif(n==4)

{

gotoxy(6,13);

cout<<"ÚÄÄÄÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ

ÄÄÄÄÄÄÄ¿";

}

elseif(n==8)

{

gotoxy(6,13);

cout<<"ÚÄÄÄÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ

ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿";

}

if(n==1)

{

gotoxy(6,14);

cout<<"³ Number of ³ Trapezoidal ³";

}

elseif(n==2)

{

gotoxy(6,14);

cout<<"³ Number of ³ Trapezoidal ³ Romberg Values ³";

}

elseif(n==4)

{

gotoxy(6,14);

cout<<"³ Number of ³ Trapezoidal ³ Romberg Values ³";

}

elseif(n==8)

{

gotoxy(6,14);

Page 25: Aplikasi menghitung matematika dengan c++

cout<<"³ Number of ³ Trapezoidal ³ Romberg Values ³";

}

if(n==1)

{

gotoxy(6,15);

cout<<"³ ³ ³";

}

elseif(n==2)

{

gotoxy(6,15);

cout<<"³ ³ ÃÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ´";

}

elseif(n==4)

{

gotoxy(6,15);

cout<<"³ ³ ÃÄÄÄÄÄÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄ´";

}

elseif(n==8)

{

gotoxy(6,15);

cout<<"³ ³

ÃÄÄÄÄÄÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄ´";

}

if(n==1)

{

gotoxy(6,16);

cout<<"³ Intervals ³ Sums ³";

}

elseif(n==2)

{

gotoxy(6,16);

cout<<"³ Intervals ³ Sums ³ 1ts Order ³";

}

elseif(n==4)

{

gotoxy(6,16);

cout<<"³ Intervals ³ Sums ³ 1ts Order ³ 2nd Order ³";

}

elseif(n==8)

{

gotoxy(6,16);

cout<<"³ Intervals ³ Sums ³ 1ts Order ³ 2nd Order ³ 3rd Order ³";

Page 26: Aplikasi menghitung matematika dengan c++

}

if(n==1)

{

gotoxy(6,17);

cout<<"ÃÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄ´";

}

elseif(n==2)

{

gotoxy(6,17);

cout<<"ÃÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ´";

}

elseif(n==4)

{

gotoxy(6,17);

cout<<"ÃÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄ

ÄÄÄÄÄÄÄ´";

}

elseif(n==8)

{

gotoxy(6,17);

cout<<"ÃÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄ

ÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄ´";

}

if(n==1)

{

gotoxy(6,18);

cout<<"³ ³ ³";

}

elseif(n==2)

{

gotoxy(6,18);

cout<<"³ ³ ³ ³";

}

elseif(n==4)

{

gotoxy(6,18);

cout<<"³ ³ ³ ³ ³";

}

elseif(n==8)

{

Page 27: Aplikasi menghitung matematika dengan c++

gotoxy(6,18);

cout<<"³ ³ ³ ³ ³ ³";

}

int y_cord=19;

int intervals=1;

for(int count_1=0;count_1<=counter;count_1++)

{

if(n==1)

{

gotoxy(6,y_cord);

cout<<"³ ³ ³";

gotoxy(6,(y_cord+1));

cout<<"³ ³ ³";

}

elseif(n==2)

{

gotoxy(6,y_cord);

cout<<"³ ³ ³ ³";

gotoxy(6,(y_cord+1));

cout<<"³ ³ ³ ³";

}

elseif(n==4)

{

gotoxy(6,y_cord);

cout<<"³ ³ ³ ³ ³";

gotoxy(6,(y_cord+1));

cout<<"³ ³ ³ ³ ³";

}

elseif(n==8)

{

gotoxy(6,y_cord);

cout<<"³ ³ ³ ³ ³ ³";

gotoxy(6,(y_cord+1));

cout<<"³ ³ ³ ³ ³ ³";

}

gotoxy(11,(19+(count_1*2)));

cout<<intervals;

intervals+=intervals;

y_cord+=2;

Page 28: Aplikasi menghitung matematika dengan c++

}

for(int count_2=0;count_2<=counter;count_2++)

{

for(int count_3=0;count_3<=counter;count_3++)

{

if(count_2==0)

{

gotoxy(20,(19+(count_3*2)));

cout<<ri[count_3][0];

}

elseif(count_2==1 && count_3>=count_2)

{

gotoxy(34,(19+(count_3*2)));

cout<<ri[count_3][1];

}

elseif(count_2==2 && count_3>=count_2)

{

gotoxy(48,(19+(count_3*2)));

cout<<ri[count_3][2];

}

elseif(count_2==3 && count_3>=count_2)

{

gotoxy(62,(19+(count_3*2)));

cout<<ri[count_3][3];

}

}

}

if(n==1)

{

gotoxy(6,y_cord);

cout<<"ÀÄÄÄÄÄÄÄÄÄÄÄÁÄÄÄÄÄÄÄÄÄÄÄÄÄÙ";

}

elseif(n==2)

{

gotoxy(6,y_cord);

cout<<"ÀÄÄÄÄÄÄÄÄÄÄÄÁÄÄÄÄÄÄÄÄÄÄÄÄÄÁÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ";

}

elseif(n==4)

{

gotoxy(6,y_cord);

Page 29: Aplikasi menghitung matematika dengan c++

cout<<"ÀÄÄÄÄÄÄÄÄÄÄÄÁÄÄÄÄÄÄÄÄÄÄÄÄÄÁÄÄÄÄÄÄÄÄÄÄÄÄÄÁÄÄÄÄÄÄ

ÄÄÄÄÄÄÄÙ";

}

elseif(n==8)

{

gotoxy(6,y_cord);

cout<<"ÀÄÄÄÄÄÄÄÄÄÄÄÁÄÄÄÄÄÄÄÄÄÄÄÄÄÁÄÄÄÄÄÄÄÄÄÄÄÄÄÁÄÄÄÄÄÄ

ÄÄÄÄÄÄÄÁÄÄÄÄÄÄÄÄÄÄÄÄÄÙ";

}

gotoxy(6,32);

cout<<" bô";

gotoxy(6,34);

cout<<" aõ";

gotoxy(6,33);

cout<<"Estimation of ³f(x)dx :";

gotoxy(6,35);

cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ";

gotoxy(8,37);

cout<<"Estimated Integral Value = ";

cout<<ri[counter][counter];

gotoxy(1,2);

}

3. Program untuk memperkirakan nilai Integral dari fungsi di poin yang diberikan dari

data yang diberikan menggunakan 1/3 Aturan Simpson di C ++ Programming

# include <iostream.h>

# include <stdlib.h>

# include <string.h>

# include <stdio.h>

# include <conio.h>

# include <math.h>

constint max_size=13;

int n=0;

int top=-1;

int choice=0;

longdouble h=0;

longdouble a=0;

Page 30: Aplikasi menghitung matematika dengan c++

longdouble b=0;

longdouble estimated_value=0;

longdouble xn[max_size]={0};

longdouble fx[max_size]={0};

char Fx[100]={NULL};

char Stack[30][30]={NULL};

char Postfix_expression[30][30]={NULL};

/*************************************************************************//

*************************************************************************///

------------------------ Funcion Prototypes -------------------------

///*************************************************************************

//*************************************************************************/

void push(constchar *);

void convert_ie_to_pe(constchar *);

constchar* pop( );

constlongdouble evaluate_postfix_expression(constlongdouble);

void show_screen( );

void clear_screen( );

void get_input( );

void apply_simpsons_rule( );

void show_result( );

/*************************************************************************//

*************************************************************************///

------------------------------ main( ) ------------------------------

///*************************************************************************

//*************************************************************************/

int main( )

{

clrscr( );

textmode(C4350);

show_screen( );

get_input( );

apply_simpsons_rule( );

show_result( );

getch( );

return 0;

}

/*************************************************************************//

Page 31: Aplikasi menghitung matematika dengan c++

*************************************************************************///

------------------------ Funcion Definitions ------------------------

///*************************************************************************

//*************************************************************************/

/*************************************************************************//

/-------------------------- show_screen( ) ---------------------------

///*************************************************************************

/void show_screen( )

{

cprintf("\n******************************************************************

**************");

cprintf("***************************- -

**************************");

cprintf("*--------------------------- ");

textbackground(1);

cprintf(" Numerical Integration ");

textbackground(8);

cprintf(" --------------------------*");

cprintf("*-*************************- -

************************-*");

cprintf("*-

***************************************************************************

*-*");

for(int count=0;count<42;count++)

cprintf("*-* *-*");

gotoxy(1,46);

cprintf("*-

***************************************************************************

*-*");

cprintf("*------------------------------------------------------------------------------*");

cprintf("********************************************************************

************");

gotoxy(1,2);

}

/*************************************************************************//

/------------------------- clear_screen( ) ---------------------------

///*************************************************************************

/void clear_screen( )

{

for(int count=0;count<37;count++)

{

Page 32: Aplikasi menghitung matematika dengan c++

gotoxy(5,8+count);

cout<<" ";

}

gotoxy(1,2);

}

/*************************************************************************//

/-------------------------- push(const char*) ------------------------

///*************************************************************************

/void push(constchar* Operand)

{

if(top==(max_size-1))

{

cout<<"Error : Stack is full."<<endl;

cout<<"\n Press any key to exit.";

getch( );

exit(0);

}

else

{

top++;

strcpy(Stack[top],Operand);

}

}

/*************************************************************************//

/------------------------------ pop( ) -------------------------------

///*************************************************************************

/constchar* pop( )

{

char Operand[40]={NULL};

if(top==-1)

{

cout<<"Error : Stack is empty."<<endl;

cout<<"\n Press any key to exit.";

getch( );

exit(0);

}

else

{

strcpy(Operand,Stack[top]);

strset(Stack[top],NULL);

Page 33: Aplikasi menghitung matematika dengan c++

top--;

}

return Operand;

}

/*************************************************************************//

/-------------------- convert_ie_to_pe(const char*) ------------------

///*************************************************************************

/void convert_ie_to_pe(constchar* Expression)

{

char Infix_expression[100]={NULL};

char Symbol_scanned[30]={NULL};

push("(");

strcpy(Infix_expression,Expression);

strcat(Infix_expression,"+0)");

int flag=0;

int count_1=0;

int count_2=0;

int equation_length=strlen(Infix_expression);

if(Infix_expression[0]=='(')

flag=1;

do

{

strset(Symbol_scanned,NULL);

if(flag==0)

{

int count_3=0;

do

{

Symbol_scanned[count_3]=Infix_expression[count_1];

count_1++;

count_3++;

}

while(count_1<=equation_length &&

Infix_expression[count_1]!='(' &&

Infix_expression[count_1]!='+' &&

Infix_expression[count_1]!='-' &&

Infix_expression[count_1]!='*' &&

Infix_expression[count_1]!='/' &&

Infix_expression[count_1]!='^' &&

Infix_expression[count_1]!=')');

Page 34: Aplikasi menghitung matematika dengan c++

flag=1;

}

elseif(flag==1)

{

Symbol_scanned[0]=Infix_expression[count_1];

count_1++;

if(Infix_expression[count_1]!='(' &&

Infix_expression[count_1]!='^' &&

Infix_expression[count_1]!='*' &&

Infix_expression[count_1]!='/' &&

Infix_expression[count_1]!='+' &&

Infix_expression[count_1]!='-' &&

Infix_expression[count_1]!=')')

flag=0;

if(Infix_expression[count_1-1]=='(' &&

(Infix_expression[count_1]=='-' ||

Infix_expression[count_1]=='+'))

flag=0;

}

if(strcmp(Symbol_scanned,"(")==0)

push("(");

elseif(strcmp(Symbol_scanned,")")==0)

{

while(strcmp(Stack[top],"(")!=0)

{

strcpy(Postfix_expression[count_2],pop( ));

count_2++;

}

pop( );

}

elseif(strcmp(Symbol_scanned,"^")==0 ||

strcmp(Symbol_scanned,"+")==0 ||

strcmp(Symbol_scanned,"-")==0 ||

strcmp(Symbol_scanned,"*")==0 ||

strcmp(Symbol_scanned,"/")==0)

{

if(strcmp(Symbol_scanned,"^")==0)

{ }

Page 35: Aplikasi menghitung matematika dengan c++

elseif(strcmp(Symbol_scanned,"*")==0 ||

strcmp(Symbol_scanned,"/")==0)

{

while(strcmp(Stack[top],"^")==0 ||

strcmp(Stack[top],"*")==0 ||

strcmp(Stack[top],"/")==0)

{

strcpy(Postfix_expression[count_2],pop( ));

count_2++;

}

}

elseif(strcmp(Symbol_scanned,"+")==0 ||

strcmp(Symbol_scanned,"-")==0)

{

while(strcmp(Stack[top],"(")!=0)

{

strcpy(Postfix_expression[count_2],pop( ));

count_2++;

}

}

push(Symbol_scanned);

}

else

{

strcat(Postfix_expression[count_2],Symbol_scanned);

count_2++;

}

}

while(strcmp(Stack[top],NULL)!=0);

strcat(Postfix_expression[count_2],"=");

count_2++;

}

/*************************************************************************//

/---------- evaluate_postfix_expression(const long double) -----------

///*************************************************************************

/constlongdouble evaluate_postfix_expression(constlongdouble x)

{

longdouble function_value=0;

int count_1=-1;

Page 36: Aplikasi menghitung matematika dengan c++

char Symbol_scanned[30]={NULL};

do

{

count_1++;

strcpy(Symbol_scanned,Postfix_expression[count_1]);

if(strcmp(Symbol_scanned,"^")==0 ||

strcmp(Symbol_scanned,"*")==0 ||

strcmp(Symbol_scanned,"/")==0 ||

strcmp(Symbol_scanned,"+")==0 ||

strcmp(Symbol_scanned,"-")==0)

{

char Result[30]={NULL};

char Operand[2][30]={NULL};

strcpy(Operand[0],pop( ));

strcpy(Operand[1],pop( ));

longdouble operand[2]={0};

longdouble result=0;

char *endptr;

for(int count_2=0;count_2<2;count_2++)

{

int flag=0;

if(Operand[count_2][0]=='-')

{

int length=strlen(Operand[count_2]);

for(int count_3=0;count_3<(length-1);count_3++)

Operand[count_2][count_3]=Operand[count_2][(count_3+1)];

Operand[count_2][count_3]=NULL;

flag=1;

}

if(strcmp(Operand[count_2],"x")==0)

operand[count_2]=x;

elseif(strcmp(Operand[count_2],"e")==0)

operand[count_2]=2.718282;

elseif(strcmp(Operand[count_2],"sinx")==0)

operand[count_2]=sinl(x);

Page 37: Aplikasi menghitung matematika dengan c++

elseif(strcmp(Operand[count_2],"cosx")==0)

operand[count_2]=cosl(x);

elseif(strcmp(Operand[count_2],"tanx")==0)

operand[count_2]=tanl(x);

elseif(strcmp(Operand[count_2],"lnx")==0)

operand[count_2]=logl(x);

elseif(strcmp(Operand[count_2],"logx")==0)

operand[count_2]=log10l(x);

else

operand[count_2]=strtod(Operand[count_2],&endptr);

if(flag)

operand[count_2]*=-1;

}

switch(Symbol_scanned[0])

{

case'^' : result=powl(operand[1],operand[0]);

break;

case'*' : result=operand[1]*operand[0];

break;

case'/' : result=operand[1]/operand[0];

break;

case'+' : result=operand[1]+operand[0];

break;

case'-' : result=operand[1]-operand[0];

break;

}

gcvt(result,25,Result);

push(Result);

}

elseif(strcmp(Symbol_scanned,"=")!=0)

push(Symbol_scanned);

}

while(strcmp(Symbol_scanned,"=")!=0);

char Function_value[30]={NULL};

char *endptr;

Page 38: Aplikasi menghitung matematika dengan c++

strcpy(Function_value,pop( ));

function_value=strtod(Function_value,&endptr);

return function_value;

}

/*************************************************************************//

/----------------------------- get_input( ) --------------------------

///*************************************************************************

/void get_input( )

{

do

{

clear_screen( );

gotoxy(6,9);

cout<<"Number of Sub-Intervals :";

gotoxy(6,10);

cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ";

gotoxy(20,13);

cout<<"[ min. n = 2 | max. n = 12 ]";

gotoxy(6,12);

cout<<"Enter the max. number of sub-intervals = n = ";

cin>>n;

if(n<2 || n>12)

{

gotoxy(12,25);

cout<<"Error : Wrong Input. Press <Esc> to exit or any other key";

gotoxy(12,26);

cout<<" to try again.";

n=int(getche( ));

if(n==27)

exit(0);

}

}

while(n<2 || n>12);

gotoxy(6,16);

cout<<"Enter the value of Lower limit = a = ";

Page 39: Aplikasi menghitung matematika dengan c++

cin>>a;

gotoxy(6,18);

cout<<"Enter the value of Upper Limit = b = ";

cin>>b;

h=((b-a)/n);

gotoxy(6,24);

cout<<"Input Mode :";

gotoxy(6,25);

cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍ";

gotoxy(8,28);

cout<<"Press : ";

gotoxy(10,30);

cout<<"- 'Y' or <Enter> to enter function";

gotoxy(10,32);

cout<<"- 'N' or <Any other key> to enter values of the function";

gotoxy(8,35);

cout<<"Enter your choice : ";

char Choice=NULL;

Choice=getch( );

if(Choice=='y' || Choice=='Y' || int(Choice)==13)

{

choice=1;

gotoxy(28,35);

cout<<"Y";

}

else

{

gotoxy(28,35);

cout<<"N";

}

gotoxy(25,43);

cout<<"Press any key to continue...";

getch( );

Page 40: Aplikasi menghitung matematika dengan c++

if(choice)

{

clear_screen( );

gotoxy(6,10);

cout<<"Non-Linear Function :";

gotoxy(6,11);

cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ";

gotoxy(6,37);

cout<<"Note : Write the function with proper Braces ( ) e.g; 2x+3 as (2*x)+3";

gotoxy(6,40);

cout<<"Available Operators : ^ (raised to power) , * , / , + , -";

gotoxy(6,42);

cout<<"Available Operands : x , e , sinx , cosx , tanx , lnx , logx ,";

gotoxy(6,44);

cout<<" n = any number";

gotoxy(6,14);

cout<<"Enter the Function : f(x) = ";

cin>>Fx;

convert_ie_to_pe(Fx);

}

clear_screen( );

gotoxy(6,9);

cout<<"Data Points & Values of Function :";

gotoxy(6,10);

cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ";

gotoxy(25,12);

cout<<"ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿";

gotoxy(25,13);

cout<<"³ x ³ f(x) ³";

gotoxy(25,14);

cout<<"ÃÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ´";

gotoxy(25,15);

cout<<"³ ³ ³";

Page 41: Aplikasi menghitung matematika dengan c++

for(int count_1=0;count_1<=n;count_1++)

{

gotoxy(25,(wherey( )+1));

cout<<"³ ³ ³";

gotoxy(25,(wherey( )+1));

cout<<"³ ³ ³";

}

gotoxy(25,(wherey( )+1));

cout<<"ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÁÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ";

xn[0]=a;

for(int count_2=0;count_2<n;count_2++)

xn[(count_2+1)]=(xn[count_2]+h);

gotoxy(25,16);

for(int count_3=0;count_3<=n;count_3++)

{

gotoxy(27,wherey( ));

cout<<xn[count_3];

if(choice)

{

fx[count_3]=evaluate_postfix_expression(xn[count_3]);

gotoxy(43,wherey( ));

cout<<fx[count_3];

}

else

{

gotoxy(43,wherey( ));

cin>>fx[count_3];

}

if(choice)

gotoxy(25,(wherey( )+2));

else

gotoxy(25,(wherey( )+1));

}

gotoxy(25,43);

cout<<"Press any key to continue...";

getch( );

}

Page 42: Aplikasi menghitung matematika dengan c++

/*************************************************************************//

/---------------------- apply_simpsons_rule( ) -----------------------

///*************************************************************************

/void apply_simpsons_rule( )

{

longdouble temp=0;

estimated_value=(fx[0]+fx[n]);

estimated_value*=h;

estimated_value/=3;

temp=0;

for(int count_1=2;count_1<=(n-2);count_1+=2)

temp+=fx[count_1];

temp*=(2*h);

temp/=3;

estimated_value+=temp;

temp=0;

for(int count_2=1;count_2<=(n-1);count_2+=2)

temp+=fx[count_2];

temp*=(4*h);

temp/=3;

estimated_value+=temp;

}

/*************************************************************************//

/----------------------------- show_result( ) ------------------------

///*************************************************************************

/void show_result( )

{

clear_screen( );

gotoxy(6,9);

cout<<"Simpson's 1/3 Rule :";

gotoxy(6,10);

cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ";

gotoxy(8,12);

cout<<" bô";

Page 43: Aplikasi menghitung matematika dengan c++

gotoxy(10,13);

cout<<"³f(x)dx ÷ (h/3)(f0+fn) + (2h/3)[f2+f4+...+f(n-2)] +";

gotoxy(50,15);

cout<<"(4h/3)[f1+f3+...+f(n-1)]";

gotoxy(8,14);

cout<<" aõ";

gotoxy(6,17);

cout<<" bô";

gotoxy(6,19);

cout<<" aõ";

gotoxy(6,18);

cout<<"Estimation of ³f(x)dx :";

gotoxy(6,20);

cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ";

gotoxy(8,23);

cout<<"Estimated Integral Value = ";

cout<<estimated_value;

gotoxy(1,2);

}

4. Program untuk memperkirakan nilai Integral dari fungsi di poin yang diberikan dari

data yang diberikan menggunakan aturan trapesium di C ++ Programming

# include <iostream.h>

# include <stdlib.h>

# include <string.h>

# include <stdio.h>

# include <conio.h>

# include <math.h>

constint max_size=13;

int n=0;

int top=-1;

int choice=0;

longdouble h=0;

longdouble a=0;

longdouble b=0;

longdouble estimated_value=0;

Page 44: Aplikasi menghitung matematika dengan c++

longdouble xn[max_size]={0};

longdouble fx[max_size]={0};

char Fx[100]={NULL};

char Stack[30][30]={NULL};

char Postfix_expression[30][30]={NULL};

/*************************************************************************//

*************************************************************************///

------------------------ Funcion Prototypes -------------------------

///*************************************************************************

//*************************************************************************/

void push(constchar *);

void convert_ie_to_pe(constchar *);

constchar* pop( );

constlongdouble evaluate_postfix_expression(constlongdouble);

void show_screen( );

void clear_screen( );

void get_input( );

void apply_trapezoidal_rule( );

void show_result( );

/*************************************************************************//

*************************************************************************///

------------------------------ main( ) ------------------------------

///*************************************************************************

//*************************************************************************/

int main( )

{

clrscr( );

textmode(C4350);

show_screen( );

get_input( );

apply_trapezoidal_rule( );

show_result( );

getch( );

return 0;

}

/*************************************************************************//

*************************************************************************///

------------------------ Funcion Definitions ------------------------

Page 45: Aplikasi menghitung matematika dengan c++

///*************************************************************************

//*************************************************************************/

/*************************************************************************//

/-------------------------- show_screen( ) ---------------------------

///*************************************************************************

/void show_screen( )

{

cprintf("\n******************************************************************

**************");

cprintf("***************************- -

**************************");

cprintf("*--------------------------- ");

textbackground(1);

cprintf(" Numerical Integration ");

textbackground(8);

cprintf(" --------------------------*");

cprintf("*-*************************- -

************************-*");

cprintf("*-

***************************************************************************

*-*");

for(int count=0;count<42;count++)

cprintf("*-* *-*");

gotoxy(1,46);

cprintf("*-

***************************************************************************

*-*");

cprintf("*------------------------------------------------------------------------------*");

cprintf("********************************************************************

************");

gotoxy(1,2);

}

/*************************************************************************//

/------------------------- clear_screen( ) ---------------------------

///*************************************************************************

/void clear_screen( )

{

for(int count=0;count<37;count++)

{

gotoxy(5,8+count);

cout<<" ";

Page 46: Aplikasi menghitung matematika dengan c++

}

gotoxy(1,2);

}

/*************************************************************************//

/-------------------------- push(const char*) ------------------------

///*************************************************************************

/void push(constchar* Operand)

{

if(top==(max_size-1))

{

cout<<"Error : Stack is full."<<endl;

cout<<"\n Press any key to exit.";

getch( );

exit(0);

}

else

{

top++;

strcpy(Stack[top],Operand);

}

}

/*************************************************************************//

/------------------------------ pop( ) -------------------------------

///*************************************************************************

/constchar* pop( )

{

char Operand[40]={NULL};

if(top==-1)

{

cout<<"Error : Stack is empty."<<endl;

cout<<"\n Press any key to exit.";

getch( );

exit(0);

}

else

{

strcpy(Operand,Stack[top]);

strset(Stack[top],NULL);

top--;

}

Page 47: Aplikasi menghitung matematika dengan c++

return Operand;

}

/*************************************************************************//

/-------------------- convert_ie_to_pe(const char*) ------------------

///*************************************************************************

/void convert_ie_to_pe(constchar* Expression)

{

char Infix_expression[100]={NULL};

char Symbol_scanned[30]={NULL};

push("(");

strcpy(Infix_expression,Expression);

strcat(Infix_expression,"+0)");

int flag=0;

int count_1=0;

int count_2=0;

int equation_length=strlen(Infix_expression);

if(Infix_expression[0]=='(')

flag=1;

do

{

strset(Symbol_scanned,NULL);

if(flag==0)

{

int count_3=0;

do

{

Symbol_scanned[count_3]=Infix_expression[count_1];

count_1++;

count_3++;

}

while(count_1<=equation_length &&

Infix_expression[count_1]!='(' &&

Infix_expression[count_1]!='+' &&

Infix_expression[count_1]!='-' &&

Infix_expression[count_1]!='*' &&

Infix_expression[count_1]!='/' &&

Infix_expression[count_1]!='^' &&

Infix_expression[count_1]!=')');

Page 48: Aplikasi menghitung matematika dengan c++

flag=1;

}

elseif(flag==1)

{

Symbol_scanned[0]=Infix_expression[count_1];

count_1++;

if(Infix_expression[count_1]!='(' &&

Infix_expression[count_1]!='^' &&

Infix_expression[count_1]!='*' &&

Infix_expression[count_1]!='/' &&

Infix_expression[count_1]!='+' &&

Infix_expression[count_1]!='-' &&

Infix_expression[count_1]!=')')

flag=0;

if(Infix_expression[count_1-1]=='(' &&

(Infix_expression[count_1]=='-' ||

Infix_expression[count_1]=='+'))

flag=0;

}

if(strcmp(Symbol_scanned,"(")==0)

push("(");

elseif(strcmp(Symbol_scanned,")")==0)

{

while(strcmp(Stack[top],"(")!=0)

{

strcpy(Postfix_expression[count_2],pop( ));

count_2++;

}

pop( );

}

elseif(strcmp(Symbol_scanned,"^")==0 ||

strcmp(Symbol_scanned,"+")==0 ||

strcmp(Symbol_scanned,"-")==0 ||

strcmp(Symbol_scanned,"*")==0 ||

strcmp(Symbol_scanned,"/")==0)

{

if(strcmp(Symbol_scanned,"^")==0)

{ }

elseif(strcmp(Symbol_scanned,"*")==0 ||

strcmp(Symbol_scanned,"/")==0)

Page 49: Aplikasi menghitung matematika dengan c++

{

while(strcmp(Stack[top],"^")==0 ||

strcmp(Stack[top],"*")==0 ||

strcmp(Stack[top],"/")==0)

{

strcpy(Postfix_expression[count_2],pop( ));

count_2++;

}

}

elseif(strcmp(Symbol_scanned,"+")==0 ||

strcmp(Symbol_scanned,"-")==0)

{

while(strcmp(Stack[top],"(")!=0)

{

strcpy(Postfix_expression[count_2],pop( ));

count_2++;

}

}

push(Symbol_scanned);

}

else

{

strcat(Postfix_expression[count_2],Symbol_scanned);

count_2++;

}

}

while(strcmp(Stack[top],NULL)!=0);

strcat(Postfix_expression[count_2],"=");

count_2++;

}

/*************************************************************************//

/---------- evaluate_postfix_expression(const long double) -----------

///*************************************************************************

/constlongdouble evaluate_postfix_expression(constlongdouble x)

{

longdouble function_value=0;

int count_1=-1;

char Symbol_scanned[30]={NULL};

Page 50: Aplikasi menghitung matematika dengan c++

do

{

count_1++;

strcpy(Symbol_scanned,Postfix_expression[count_1]);

if(strcmp(Symbol_scanned,"^")==0 ||

strcmp(Symbol_scanned,"*")==0 ||

strcmp(Symbol_scanned,"/")==0 ||

strcmp(Symbol_scanned,"+")==0 ||

strcmp(Symbol_scanned,"-")==0)

{

char Result[30]={NULL};

char Operand[2][30]={NULL};

strcpy(Operand[0],pop( ));

strcpy(Operand[1],pop( ));

longdouble operand[2]={0};

longdouble result=0;

char *endptr;

for(int count_2=0;count_2<2;count_2++)

{

int flag=0;

if(Operand[count_2][0]=='-')

{

int length=strlen(Operand[count_2]);

for(int count_3=0;count_3<(length-1);count_3++)

Operand[count_2][count_3]=Operand[count_2][(count_3+1)];

Operand[count_2][count_3]=NULL;

flag=1;

}

if(strcmp(Operand[count_2],"x")==0)

operand[count_2]=x;

elseif(strcmp(Operand[count_2],"e")==0)

operand[count_2]=2.718282;

elseif(strcmp(Operand[count_2],"sinx")==0)

operand[count_2]=sinl(x);

elseif(strcmp(Operand[count_2],"cosx")==0)

Page 51: Aplikasi menghitung matematika dengan c++

operand[count_2]=cosl(x);

elseif(strcmp(Operand[count_2],"tanx")==0)

operand[count_2]=tanl(x);

elseif(strcmp(Operand[count_2],"lnx")==0)

operand[count_2]=logl(x);

elseif(strcmp(Operand[count_2],"logx")==0)

operand[count_2]=log10l(x);

else

operand[count_2]=strtod(Operand[count_2],&endptr);

if(flag)

operand[count_2]*=-1;

}

switch(Symbol_scanned[0])

{

case'^' : result=powl(operand[1],operand[0]);

break;

case'*' : result=operand[1]*operand[0];

break;

case'/' : result=operand[1]/operand[0];

break;

case'+' : result=operand[1]+operand[0];

break;

case'-' : result=operand[1]-operand[0];

break;

}

gcvt(result,25,Result);

push(Result);

}

elseif(strcmp(Symbol_scanned,"=")!=0)

push(Symbol_scanned);

}

while(strcmp(Symbol_scanned,"=")!=0);

char Function_value[30]={NULL};

char *endptr;

strcpy(Function_value,pop( ));

Page 52: Aplikasi menghitung matematika dengan c++

function_value=strtod(Function_value,&endptr);

return function_value;

}

/*************************************************************************//

/----------------------------- get_input( ) --------------------------

///*************************************************************************

/void get_input( )

{

do

{

clear_screen( );

gotoxy(6,9);

cout<<"Number of Sub-Intervals :";

gotoxy(6,10);

cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ";

gotoxy(20,13);

cout<<"[ min. n = 3 | max. n = 12 ]";

gotoxy(6,12);

cout<<"Enter the max. number of sub-intervals = n = ";

cin>>n;

if(n<3 || n>12)

{

gotoxy(12,25);

cout<<"Error : Wrong Input. Press <Esc> to exit or any other key";

gotoxy(12,26);

cout<<" to try again.";

n=int(getche( ));

if(n==27)

exit(0);

}

}

while(n<3 || n>12);

gotoxy(6,16);

cout<<"Enter the value of Lower limit = a = ";

cin>>a;

Page 53: Aplikasi menghitung matematika dengan c++

gotoxy(6,18);

cout<<"Enter the value of Upper Limit = b = ";

cin>>b;

h=((b-a)/n);

gotoxy(6,24);

cout<<"Input Mode :";

gotoxy(6,25);

cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍ";

gotoxy(8,28);

cout<<"Press : ";

gotoxy(10,30);

cout<<"- 'Y' or <Enter> to enter function";

gotoxy(10,32);

cout<<"- 'N' or <Any other key> to enter values of the function";

gotoxy(8,35);

cout<<"Enter your choice : ";

char Choice=NULL;

Choice=getch( );

if(Choice=='y' || Choice=='Y' || int(Choice)==13)

{

choice=1;

gotoxy(28,35);

cout<<"Y";

}

else

{

gotoxy(28,35);

cout<<"N";

}

gotoxy(25,43);

cout<<"Press any key to continue...";

getch( );

if(choice)

{

Page 54: Aplikasi menghitung matematika dengan c++

clear_screen( );

gotoxy(6,10);

cout<<"Non-Linear Function :";

gotoxy(6,11);

cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ";

gotoxy(6,37);

cout<<"Note : Write the function with proper Braces ( ) e.g; 2x+3 as (2*x)+3";

gotoxy(6,40);

cout<<"Available Operators : ^ (raised to power) , * , / , + , -";

gotoxy(6,42);

cout<<"Available Operands : x , e , sinx , cosx , tanx , lnx , logx ,";

gotoxy(6,44);

cout<<" n = any number";

gotoxy(6,14);

cout<<"Enter the Function : f(x) = ";

cin>>Fx;

convert_ie_to_pe(Fx);

}

clear_screen( );

gotoxy(6,9);

cout<<"Data Points & Values of Function :";

gotoxy(6,10);

cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ";

gotoxy(25,12);

cout<<"ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿";

gotoxy(25,13);

cout<<"³ x ³ f(x) ³";

gotoxy(25,14);

cout<<"ÃÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ´";

gotoxy(25,15);

cout<<"³ ³ ³";

for(int count_1=0;count_1<=n;count_1++)

{

Page 55: Aplikasi menghitung matematika dengan c++

gotoxy(25,(wherey( )+1));

cout<<"³ ³ ³";

gotoxy(25,(wherey( )+1));

cout<<"³ ³ ³";

}

gotoxy(25,(wherey( )+1));

cout<<"ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÁÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ";

xn[0]=a;

for(int count_2=0;count_2<n;count_2++)

xn[(count_2+1)]=(xn[count_2]+h);

gotoxy(25,16);

for(int count_3=0;count_3<=n;count_3++)

{

gotoxy(27,wherey( ));

cout<<xn[count_3];

if(choice)

{

fx[count_3]=evaluate_postfix_expression(xn[count_3]);

gotoxy(43,wherey( ));

cout<<fx[count_3];

}

else

{

gotoxy(43,wherey( ));

cin>>fx[count_3];

}

if(choice)

gotoxy(25,(wherey( )+2));

else

gotoxy(25,(wherey( )+1));

}

gotoxy(25,43);

cout<<"Press any key to continue...";

getch( );

}

Page 56: Aplikasi menghitung matematika dengan c++

/*************************************************************************//

/---------------------- apply_trapezoidal_rule( ) --------------------

///*************************************************************************

/void apply_trapezoidal_rule( )

{

for(int count=1;count<n;count++)

estimated_value+=fx[count];

estimated_value*=2;

estimated_value+=fx[0];

estimated_value+=fx[n];

estimated_value*=(h/2);

}

/*************************************************************************//

/--------------------------- show_result( ) --------------------------

///*************************************************************************

/void show_result( )

{

clear_screen( );

gotoxy(6,9);

cout<<"Trapeziodal Rule :";

gotoxy(6,10);

cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ";

gotoxy(8,12);

cout<<" bô";

gotoxy(10,13);

cout<<"³f(x)dx ÷ (h/2)[f0+{2*(f1+f2+...+fn-1)}+fn]";

gotoxy(8,14);

cout<<" aõ";

gotoxy(6,17);

cout<<" bô";

gotoxy(6,19);

cout<<" aõ";

gotoxy(6,18);

cout<<"Estimation of ³f(x)dx :";

gotoxy(6,20);

cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ";

Page 57: Aplikasi menghitung matematika dengan c++

gotoxy(8,23);

cout<<"Estimated Integral Value = ";

cout<<estimated_value;

gotoxy(1,2);

}

5. Program untuk memperkirakan nilai derivatif pertama dari fungsi pada titik-titik

tertentu dari data yang diberikan menggunakan Tengah Perbedaan Formula di C ++

Programming

# include <iostream.h>

# include <stdlib.h>

# include <string.h>

# include <stdio.h>

# include <conio.h>

# include <math.h>

constint max_size=13;

int n=0;

int top=-1;

int choice=0;

longdouble h=0;

longdouble x0=0;

longdouble xn[max_size]={0};

longdouble fx[max_size]={0};

char Fx[100]={NULL};

char Dfx[100]={NULL};

char Stack[30][30]={NULL};

char Postfix_expression[2][30][30]={NULL};

void push(constchar *);

void convert_ie_to_pe(constchar *,constint);

constchar* pop( );

constlongdouble evaluate_postfix_expression(constlongdouble,constint);

void show_screen( );

void clear_screen( );

void get_input( );

void estimate_dfx( );

constint get_index(constlongdouble);

int main( )

{

clrscr( );

Page 58: Aplikasi menghitung matematika dengan c++

textmode(C4350);

show_screen( );

get_input( );

estimate_dfx( );

return 0;

}

/*************************************************************************//

/-------------------------- show_screen( ) ---------------------------

///*************************************************************************

/void show_screen( )

{

cprintf("\n******************************************************************

**************");

cprintf("*************************- -

************************");

cprintf("*------------------------- ");

textbackground(1);

cprintf(" Numerical Differentiation ");

textbackground(8);

cprintf(" ------------------------*");

cprintf("*-***********************- -**********************-

*");

cprintf("*-

***************************************************************************

*-*");

for(int count=0;count<42;count++)

cprintf("*-* *-*");

gotoxy(1,46);

cprintf("*-

***************************************************************************

*-*");

cprintf("*------------------------------------------------------------------------------*");

cprintf("********************************************************************

************");

gotoxy(1,2);

}

Page 59: Aplikasi menghitung matematika dengan c++

/*************************************************************************//

/------------------------- clear_screen( ) ---------------------------

///*************************************************************************

/void clear_screen( )

{

for(int count=0;count<37;count++)

{

gotoxy(5,8+count);

cout<<" ";

}

gotoxy(1,2);

}

/*************************************************************************//

/-------------------------- push(const char*) ------------------------

///*************************************************************************

/void push(constchar* Operand)

{

if(top==(max_size-1))

{

cout<<"Error : Stack is full."<<endl;

cout<<"\n Press any key to exit.";

getch( );

exit(0);

}

else

{

top++;

strcpy(Stack[top],Operand);

}

}

/*************************************************************************//

/------------------------------ pop( ) -------------------------------

///*************************************************************************

/constchar* pop( )

{

char Operand[40]={NULL};

if(top==-1)

{

cout<<"Error : Stack is empty."<<endl;

cout<<"\n Press any key to exit.";

Page 60: Aplikasi menghitung matematika dengan c++

getch( );

exit(0);

}

else

{

strcpy(Operand,Stack[top]);

strset(Stack[top],NULL);

top--;

}

return Operand;

}

/*************************************************************************//

/---------------- convert_ie_to_pe(const char*,const int) ------------

///*************************************************************************

/void convert_ie_to_pe(constchar* Expression,constint index)

{

char Infix_expression[100]={NULL};

char Symbol_scanned[30]={NULL};

push("(");

strcpy(Infix_expression,Expression);

strcat(Infix_expression,"+0)");

int flag=0;

int count_1=0;

int count_2=0;

int equation_length=strlen(Infix_expression);

if(Infix_expression[0]=='(')

flag=1;

do

{

strset(Symbol_scanned,NULL);

if(flag==0)

{

int count_3=0;

do

{

Symbol_scanned[count_3]=Infix_expression[count_1];

count_1++;

count_3++;

}

Page 61: Aplikasi menghitung matematika dengan c++

while(count_1<=equation_length &&

Infix_expression[count_1]!='(' &&

Infix_expression[count_1]!='+' &&

Infix_expression[count_1]!='-' &&

Infix_expression[count_1]!='*' &&

Infix_expression[count_1]!='/' &&

Infix_expression[count_1]!='^' &&

Infix_expression[count_1]!=')');

flag=1;

}

elseif(flag==1)

{

Symbol_scanned[0]=Infix_expression[count_1];

count_1++;

if(Infix_expression[count_1]!='(' &&

Infix_expression[count_1]!='^' &&

Infix_expression[count_1]!='*' &&

Infix_expression[count_1]!='/' &&

Infix_expression[count_1]!='+' &&

Infix_expression[count_1]!='-' &&

Infix_expression[count_1]!=')')

flag=0;

if(Infix_expression[count_1-1]=='(' &&

(Infix_expression[count_1]=='-' ||

Infix_expression[count_1]=='+'))

flag=0;

}

if(strcmp(Symbol_scanned,"(")==0)

push("(");

elseif(strcmp(Symbol_scanned,")")==0)

{

while(strcmp(Stack[top],"(")!=0)

{

strcpy(Postfix_expression[index][count_2],pop( ));

count_2++;

}

pop( );

}

elseif(strcmp(Symbol_scanned,"^")==0 ||

Page 62: Aplikasi menghitung matematika dengan c++

strcmp(Symbol_scanned,"+")==0 ||

strcmp(Symbol_scanned,"-")==0 ||

strcmp(Symbol_scanned,"*")==0 ||

strcmp(Symbol_scanned,"/")==0)

{

if(strcmp(Symbol_scanned,"^")==0)

{ }

elseif(strcmp(Symbol_scanned,"*")==0 ||

strcmp(Symbol_scanned,"/")==0)

{

while(strcmp(Stack[top],"^")==0 ||

strcmp(Stack[top],"*")==0 ||

strcmp(Stack[top],"/")==0)

{

strcpy(Postfix_expression[index][count_2],pop( ));

count_2++;

}

}

elseif(strcmp(Symbol_scanned,"+")==0 ||

strcmp(Symbol_scanned,"-")==0)

{

while(strcmp(Stack[top],"(")!=0)

{

strcpy(Postfix_expression[index][count_2],pop( ));

count_2++;

}

}

push(Symbol_scanned);

}

else

{

strcat(Postfix_expression[index][count_2],Symbol_scanned);

count_2++;

}

}

while(strcmp(Stack[top],NULL)!=0);

strcat(Postfix_expression[index][count_2],"=");

count_2++;

}

/*************************************************************************//

Page 63: Aplikasi menghitung matematika dengan c++

/----- evaluate_postfix_expression(const long double,const int) ------

///*************************************************************************

/constlongdouble evaluate_postfix_expression(

constlongdouble x,constint index)

{

longdouble function_value=0;

int count_1=-1;

char Symbol_scanned[30]={NULL};

do

{

count_1++;

strcpy(Symbol_scanned,Postfix_expression[index][count_1]);

if(strcmp(Symbol_scanned,"^")==0 ||

strcmp(Symbol_scanned,"*")==0 ||

strcmp(Symbol_scanned,"/")==0 ||

strcmp(Symbol_scanned,"+")==0 ||

strcmp(Symbol_scanned,"-")==0)

{

char Result[30]={NULL};

char Operand[2][30]={NULL};

strcpy(Operand[0],pop( ));

strcpy(Operand[1],pop( ));

longdouble operand[2]={0};

longdouble result=0;

char *endptr;

for(int count_2=0;count_2<2;count_2++)

{

int flag=0;

if(Operand[count_2][0]=='-')

{

int length=strlen(Operand[count_2]);

for(int count_3=0;count_3<(length-1);count_3++)

Operand[count_2][count_3]=Operand[count_2][(count_3+1)];

Operand[count_2][count_3]=NULL;

flag=1;

}

Page 64: Aplikasi menghitung matematika dengan c++

if(strcmp(Operand[count_2],"x")==0)

operand[count_2]=x;

elseif(strcmp(Operand[count_2],"e")==0)

operand[count_2]=2.718282;

elseif(strcmp(Operand[count_2],"sinx")==0)

operand[count_2]=sinl(x);

elseif(strcmp(Operand[count_2],"cosx")==0)

operand[count_2]=cosl(x);

elseif(strcmp(Operand[count_2],"tanx")==0)

operand[count_2]=tanl(x);

elseif(strcmp(Operand[count_2],"lnx")==0)

operand[count_2]=logl(x);

elseif(strcmp(Operand[count_2],"logx")==0)

operand[count_2]=log10l(x);

else

operand[count_2]=strtod(Operand[count_2],&endptr);

if(flag)

operand[count_2]*=-1;

}

switch(Symbol_scanned[0])

{

case'^' : result=powl(operand[1],operand[0]);

break;

case'*' : result=operand[1]*operand[0];

break;

case'/' : result=operand[1]/operand[0];

break;

case'+' : result=operand[1]+operand[0];

break;

case'-' : result=operand[1]-operand[0];

break;

}

gcvt(result,25,Result);

push(Result);

Page 65: Aplikasi menghitung matematika dengan c++

}

elseif(strcmp(Symbol_scanned,"=")!=0)

push(Symbol_scanned);

}

while(strcmp(Symbol_scanned,"=")!=0);

char Function_value[30]={NULL};

char *endptr;

strcpy(Function_value,pop( ));

function_value=strtod(Function_value,&endptr);

return function_value;

}

/*************************************************************************//

/----------------------------- get_input( ) --------------------------

///*************************************************************************

/void get_input( )

{

do

{

clear_screen( );

gotoxy(6,9);

cout<<"Number of Distinct Data Points :";

gotoxy(6,10);

cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ";

gotoxy(27,13);

cout<<"[ min. n = 3 | max. n = 12 ]";

gotoxy(6,12);

cout<<"Enter the max. number of distinct data points = n = ";

cin>>n;

if(n<3 || n>12)

{

gotoxy(12,25);

cout<<"Error : Wrong Input. Press <Esc> to exit or any other key";

gotoxy(12,26);

cout<<" to try again.";

n=int(getche( ));

Page 66: Aplikasi menghitung matematika dengan c++

if(n==27)

exit(0);

}

}

while(n<3 || n>12);

gotoxy(6,16);

cout<<"Enter the value of x0 = ";

cin>>x0;

gotoxy(6,18);

cout<<"Enter the value of h = ";

cin>>h;

gotoxy(6,24);

cout<<"Input Mode :";

gotoxy(6,25);

cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍ";

gotoxy(8,28);

cout<<"Press : ";

gotoxy(10,30);

cout<<"- 'Y' or <Enter> to enter function";

gotoxy(10,32);

cout<<"- 'N' or <Any other key> to enter values of the function";

gotoxy(8,35);

cout<<"Enter your choice : ";

char Choice=NULL;

Choice=getch( );

if(Choice=='y' || Choice=='Y' || int(Choice)==13)

{

choice=1;

gotoxy(28,35);

cout<<"Y";

}

else

{

gotoxy(28,35);

cout<<"N";

Page 67: Aplikasi menghitung matematika dengan c++

}

gotoxy(25,43);

cout<<"Press any key to continue...";

getch( );

if(choice)

{

clear_screen( );

gotoxy(6,11);

cout<<"Non-Linear Function :";

gotoxy(6,12);

cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ";

gotoxy(6,37);

cout<<"Note : Write the function with proper Braces ( ) e.g; 2x+3 as (2*x)+3";

gotoxy(6,40);

cout<<"Available Operators : ^ (raised to power) , * , / , + , -";

gotoxy(6,42);

cout<<"Available Operands : x , e , sinx , cosx , tanx , lnx , logx ,";

gotoxy(6,44);

cout<<" n = any number";

gotoxy(6,14);

cout<<"Enter the Function : f(x) = ";

cin>>Fx;

gotoxy(6,17);

cout<<"Enter the Differential Function : f'(x) = ";

cin>>Dfx;

convert_ie_to_pe(Fx,0);

convert_ie_to_pe(Dfx,1);

}

clear_screen( );

gotoxy(6,9);

cout<<"Data Points & Values of Function :";

gotoxy(6,10);

cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ";

Page 68: Aplikasi menghitung matematika dengan c++

gotoxy(25,12);

cout<<"ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿";

gotoxy(25,13);

cout<<"³ x ³ f(x) ³";

gotoxy(25,14);

cout<<"ÃÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ´";

gotoxy(25,15);

cout<<"³ ³ ³";

for(int count_1=0;count_1<n;count_1++)

{

gotoxy(25,(wherey( )+1));

cout<<"³ ³ ³";

gotoxy(25,(wherey( )+1));

cout<<"³ ³ ³";

}

gotoxy(25,(wherey( )+1));

cout<<"ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÁÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ";

xn[0]=x0;

for(int count_2=0;count_2<(n-1);count_2++)

xn[(count_2+1)]=(xn[count_2]+h);

gotoxy(25,16);

for(int count_3=0;count_3<n;count_3++)

{

gotoxy(27,wherey( ));

cout<<xn[count_3];

if(choice)

{

fx[count_3]=evaluate_postfix_expression(xn[count_3],0);

gotoxy(43,wherey( ));

cout<<fx[count_3];

}

else

{

gotoxy(43,wherey( ));

cin>>fx[count_3];

}

Page 69: Aplikasi menghitung matematika dengan c++

if(choice)

gotoxy(25,(wherey( )+2));

else

gotoxy(25,(wherey( )+1));

}

gotoxy(25,43);

cout<<"Press any key to continue...";

getch( );

}

/*************************************************************************//

/------------------- get_index(const long double) --------------------

///*************************************************************************

/constint get_index(constlongdouble x)

{

for(int count=0;count<n;count++)

{

if(xn[count]==x)

break;

}

return count;

}

/*************************************************************************//

/---------------------------- estimate_dfx( ) ------------------------

///*************************************************************************

/void estimate_dfx( )

{

clear_screen( );

gotoxy(6,9);

cout<<"Centeral Difference Formula of Order 2 :";

gotoxy(6,10);

cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ";

gotoxy(8,13);

cout<<"f'(x) ÷ [f(x+h)-f(x-h)]/2h";

gotoxy(6,17);

cout<<"Estimation of f'(x) :";

gotoxy(6,18);

Page 70: Aplikasi menghitung matematika dengan c++

cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ";

char Choice=NULL;

longdouble x=0;

longdouble dfx=0;

longdouble actual_dfx=0;

int error_flag=0;

do

{

Choice=NULL;

x=0;

dfx=0;

actual_dfx=0;

error_flag=0;

gotoxy(10,20);

cout<<"Enter the value of x = ";

cin>>x;

if(x<=xn[0] || x>=xn[(n-1)])

{

error_flag=1;

gotoxy(10,23);

cout<<"Error: Please enter x greater than x(0) and less than x(n).";

}

else

{

int index=0;

index=get_index(x);

longdouble fxph=fx[(index+1)];

longdouble fxmh=fx[(index-1)];

dfx=((fxph-fxmh)/(2*h));

gotoxy(10,23);

cout<<"The estimated value of f'("<<x<<") ÷ "<<dfx;

}

if(choice && !error_flag)

{

actual_dfx=evaluate_postfix_expression(x,1);

Page 71: Aplikasi menghitung matematika dengan c++

gotoxy(10,25);

cout<<"The Actual value of f'("<<x<<") = "<<actual_dfx;

gotoxy(10,28);

cout<<"Absolute Error = E(abs) = "<<fabs((actual_dfx-dfx));

}

gotoxy(15,42);

cout<<"Press <Esc> to exit or any other key to continue...";

Choice=getch( );

if(int(Choice)!=27)

{

gotoxy(10,20);

cout<<" ";

gotoxy(10,23);

cout<<" ";

gotoxy(10,25);

cout<<" ";

gotoxy(10,28);

cout<<" ";

gotoxy(15,42);

cout<<" ";

}

elseif(int(Choice)==27)

exit(0);

}

while(1);

}

6. Program untuk membaca Sistem Linear dari Persamaan, kemudian mengevaluasi

dengan menggunakan Gauss-Seidel Metode Iteratif dan menampilkan hasil di C ++

Programming

# include <iostream.h>

# include <stdlib.h>

# include <stdio.h>

# include <conio.h>

# include <math.h>

constint max_size=5;

int n=0;

int iterations=0;

Page 72: Aplikasi menghitung matematika dengan c++

char Diagonally_dominant=NULL;

longdouble input[max_size][max_size]={0};

longdouble previous_output[max_size]={0};

longdouble output[max_size]={0};

void show_screen( );

void clear_screen( );

void get_size_of_linear_equations( );

void show_input(constint,constint);

void get_input_linear_equation( );

void sort_system_of_linear_equations( );

void apply_guass_seidel_iterative_method( );

void show_result( );

int main( )

{

clrscr( );

textmode(C4350);

show_screen( );

get_size_of_linear_equations( );

get_input_linear_equation( );

if(Diagonally_dominant=='Y' || Diagonally_dominant=='y')

sort_system_of_linear_equations( );

apply_guass_seidel_iterative_method( );

show_result( );

getch( );

return 0;

}

/*************************************************************************//

/-------------------------- show_screen( ) ---------------------------

///*************************************************************************

/void show_screen( )

{

cprintf("\n******************************************************************

**************");

cprintf("**********************- -**********************");

cprintf("*---------------------- ");

textbackground(1);

cprintf(" Guass-Seidel's Itrative Method ");

textbackground(8);

Page 73: Aplikasi menghitung matematika dengan c++

cprintf(" ----------------------*");

cprintf("**********************- -**********************");

cprintf("********************************************************************

************");

for(int count=0;count<42;count++)

cprintf("* *");

gotoxy(1,46);

cprintf("********************************************************************

************");

cprintf("*------------------------------------------------------------------------------*");

cprintf("********************************************************************

************");

gotoxy(1,2);

}

/*************************************************************************//

/------------------------- clear_screen( ) ---------------------------

///*************************************************************************

/void clear_screen( )

{

textbackground(8);

for(int count=0;count<37;count++)

{

gotoxy(3,8+count);

cout<<" ";

}

gotoxy(1,2);

}

/*************************************************************************//

/------------------ get_size_of_linear_equations( ) ------------------

///*************************************************************************

/void get_size_of_linear_equations( )

{

do

{

clear_screen( );

gotoxy(38,11);

Page 74: Aplikasi menghitung matematika dengan c++

cout<<"[ Maximum size = 4 ]";

gotoxy(4,9);

cout<<"Enter the size of the System of Linear Equations = n = ";

cin>>n;

if(n<=0 || n>max_size)

{

gotoxy(12,25);

cout<<"Error : Wrong Input. Press <Esc> to exit or any other key";

gotoxy(12,26);

cout<<" to try again.";

n=int(getche( ));

if(n==27)

exit(0);

}

}

while(n<=0 || n>max_size);

gotoxy(4,15);

cout<<"Enter the number of iterations that you want to perform = ";

cin>>iterations;

gotoxy(4,18);

cout<<"Do you want to make the System Diagonally Dominant : (Y/N) ? : ";

cin>>Diagonally_dominant;

gotoxy(1,2);

}

/*************************************************************************//

/------------------ show_input(const int,const int) ------------------

///*************************************************************************

/void show_input(constint x,constint y)

{

int counter=0;

int number_of_inputs=((x*n)+y+x);

for(int count_1=0;count_1<n;count_1++)

{

gotoxy(4,(17+(count_1*2)));

cout<<" ";

Page 75: Aplikasi menghitung matematika dengan c++

gotoxy(4,(17+(count_1*2)));

for(int count_2=0;count_2<=n;count_2++)

{

if(counter==(number_of_inputs+1))

textbackground(12);

else

textbackground(8);

if(count_2==n)

gotoxy((wherex( )+5),(17+(count_1*2)));

gotoxy(wherex( ),(17+(count_1*2)));

cprintf(" ");

if(count_2==n)

gotoxy((wherex( )-5),(17+(count_1*2)));

gotoxy((wherex( )-6),(17+(count_1*2)));

if(counter<=number_of_inputs && counter!=-1)

{

if(count_2==n)

{

cout<<" = ";

cout<<input[count_1][count_2];

}

elseif(count_2==0)

cout<<input[count_1][count_2]<<" x"<<(count_2+1);

else

cout<<fabs(input[count_1][count_2])<<" x"<<(count_2+1);

}

elseif(count_2==n)

cout<<" = b"<<(count_1+1);

else

cout<<"a"<<(count_1+1)<<(count_2+1)<<" x"<<(count_2+1);

if(count_2<(n-1) && input[count_1][(count_2+1)]>=0)

cout<<" + ";

elseif(count_2<(n-1))

cout<<" - ";

counter++;

}

Page 76: Aplikasi menghitung matematika dengan c++

}

}

/*************************************************************************//

/-------------------- get_input_linear_equation( ) -------------------

///*************************************************************************

/void get_input_linear_equation( )

{

clear_screen( );

gotoxy(4,9);

cout<<"Size of the System of Linear Equations = n = "<<n;

gotoxy(4,13);

cout<<"System of Linear Equations :";

gotoxy(4,14);

cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ";

show_input(0,-1);

for(int count_1=0;count_1<n;count_1++)

{

for(int count_2=0;count_2<=n;count_2++)

{

gotoxy(4,35);

cout<<" ";

gotoxy(4,35);

if(count_2<n)

cout<<"Enter the value of a"<<(count_1+1)<<(count_2+1)<<" = ";

else

cout<<"Enter the value of b"<<(count_1+1)<<" = ";

cin>>input[count_1][count_2];

show_input(count_1,count_2);

}

}

}

/*************************************************************************//

/----------------- sort_system_of_linear_equations( ) ----------------

///*************************************************************************

/void sort_system_of_linear_equations( )

{

Page 77: Aplikasi menghitung matematika dengan c++

for(int count_1=0;count_1<(n-1);count_1++)

{

for(int count_2=count_1;count_2<(n-1);count_2++)

{

for(int count_3=count_1;count_3<(n-1);count_3++)

{

longdouble temp[max_size]={0};

if(fabs(input[count_3][count_1])<

fabs(input[(count_3+1)][count_1]))

{

for(int count_4=0;count_4<=n;count_4++)

temp[count_4]=input[count_3][count_4];

for(int count_5=0;count_5<=n;count_5++)

input[count_3][count_5]=

input[(count_3+1)][count_5];

for(int count_6=0;count_6<=n;count_6++)

input[(count_3+1)][count_6]=temp[count_6];

}

}

}

}

}

/*************************************************************************//

/--------------- apply_guass_seidel_iterative_method( ) --------------

///*************************************************************************

/void apply_guass_seidel_iterative_method( )

{

clear_screen( );

gotoxy(4,10);

cout<<"Solution :";

gotoxy(4,11);

cout<<"ÍÍÍÍÍÍÍÍÍÍ";

if(n==1)

{

gotoxy(4,13);

cout<<"ÚÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿";

gotoxy(4,14);

cout<<"³ n ³ X1 ³";

gotoxy(4,15);

cout<<"ÃÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ´";

Page 78: Aplikasi menghitung matematika dengan c++

gotoxy(4,16);

cout<<"³ ³ ³";

}

elseif(n==2)

{

gotoxy(4,13);

cout<<"ÚÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿";

gotoxy(4,14);

cout<<"³ n ³ X1 ³ X2 ³";

gotoxy(4,15);

cout<<"ÃÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ´";

gotoxy(4,16);

cout<<"³ ³ ³ ³";

}

elseif(n==3)

{

gotoxy(4,13);

cout<<"ÚÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄ

ÄÄÄÄÄÄÄÄÄ¿";

gotoxy(4,14);

cout<<"³ n ³ X1 ³ X2 ³ X3 ³";

gotoxy(4,15);

cout<<"ÃÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄ

ÄÄÄÄÄÄÄÄÄ´";

gotoxy(4,16);

cout<<"³ ³ ³ ³ ³";

}

elseif(n==4)

{

gotoxy(4,13);

cout<<"ÚÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄ

ÄÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿";

gotoxy(4,14);

cout<<"³ n ³ X1 ³ X2 ³ X3 ³ X4 ³";

gotoxy(4,15);

Page 79: Aplikasi menghitung matematika dengan c++

cout<<"ÃÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄ

ÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ´";

gotoxy(4,16);

cout<<"³ ³ ³ ³ ³ ³";

}

for(int count_1=0;count_1<n;count_1++)

{

longdouble temp[max_size]={0};

for(int count_2=0;count_2<=n;count_2++)

{

if(count_2!=count_1)

temp[count_2]=(input[count_1][count_2]/input[count_1][count_1]);

}

for(int count_3=0;count_3<=n;count_3++)

input[count_1][count_3]=temp[count_3];

}

for(int count_4=0;count_4<n;count_4++)

{

for(int count_5=0;count_5<n;count_5++)

input[count_4][count_5]*=-1;

}

int x_cord=4;

int y_cord=17;

for(int count_6=0;count_6<=iterations;count_6++)

{

if(n==1)

{

gotoxy(x_cord,y_cord);

cout<<"³ ³ ³";

gotoxy(x_cord,(y_cord+1));

cout<<"³ ³ ³";

}

elseif(n==2)

{

gotoxy(x_cord,y_cord);

cout<<"³ ³ ³ ³";

gotoxy(x_cord,(y_cord+1));

cout<<"³ ³ ³ ³";

}

Page 80: Aplikasi menghitung matematika dengan c++

elseif(n==3)

{

gotoxy(x_cord,y_cord);

cout<<"³ ³ ³ ³ ³";

gotoxy(x_cord,(y_cord+1));

cout<<"³ ³ ³ ³ ³";

}

elseif(n==4)

{

gotoxy(x_cord,y_cord);

cout<<"³ ³ ³ ³ ³ ³";

gotoxy(x_cord,(y_cord+1));

cout<<"³ ³ ³ ³ ³ ³";

}

gotoxy((x_cord+3),y_cord);

cout<<count_6;

gotoxy((x_cord+10),y_cord);

cout<<output[0];

if(n>=2)

{

gotoxy((x_cord+26),y_cord);

cout<<output[1];

}

if(n>=3)

{

gotoxy((x_cord+42),y_cord);

cout<<output[2];

}

if(n>=4)

{

gotoxy((x_cord+58),y_cord);

cout<<output[3];

}

for(int count_7=0;count_7<n;count_7++)

{

output[count_7]=0;

for(int count_8=0;count_8<n;count_8++)

{

if(count_8!=count_7)

Page 81: Aplikasi menghitung matematika dengan c++

output[count_7]+=(input[count_7][count_8]*previous_output[count_8]);

}

output[count_7]+=input[count_7][count_8];

previous_output[count_7]=output[count_7];

}

y_cord+=2;

if((count_6%12)==0 && count_6<iterations && count_6>0)

{

y_cord=17;

gotoxy(30,44);

cout<<"Press any key to continue...";

getch( );

for(int count_9=1;count_9<25;count_9++)

{

gotoxy(3,(16+count_9));

cout<<" ";

}

}

}

if(n==1)

{

gotoxy(x_cord,y_cord);

cout<<"ÀÄÄÄÄÄÄÄÁÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ";

}

elseif(n==2)

{

gotoxy(x_cord,y_cord);

cout<<"ÀÄÄÄÄÄÄÄÁÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÁÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ";

}

elseif(n==3)

{

gotoxy(x_cord,y_cord);

cout<<"ÀÄÄÄÄÄÄÄÁÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÁÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÁÄÄÄÄÄÄ

ÄÄÄÄÄÄÄÄÄÙ";

}

elseif(n==4)

{

gotoxy(x_cord,y_cord);

Page 82: Aplikasi menghitung matematika dengan c++

cout<<"ÀÄÄÄÄÄÄÄÁÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÁÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÁÄÄÄÄÄÄ

ÄÄÄÄÄÄÄÄÄÁÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ";

}

gotoxy(30,44);

cout<<"Press any key to continue...";

getch( );

}

/*************************************************************************//

/----------------------------- show_result( ) ------------------------

///*************************************************************************

/void show_result( )

{

clear_screen( );

gotoxy(4,9);

cout<<"Gauss-Seidel System of Linear Equations :";

gotoxy(4,10);

cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ";

for(int count_1=0;count_1<n;count_1++)

{

gotoxy(4,(13+(count_1*3)));

cout<<"x"<<(count_1+1);

gotoxy((wherex( )-1),(wherey( )-1));

cout<<"(k+1)";

gotoxy((wherex( )-3),(wherey( )+1));

cout<<" = ";

for(int count_2=0;count_2<=n;count_2++)

{

gotoxy(wherex( ),(13+(count_1*3)));

if(count_2!=count_1 && count_2<n)

{

if(count_2!=0)

cout<<fabs(input[count_1][count_2])<<" x"<<(count_2+1);

else

cout<<input[count_1][count_2]<<" x"<<(count_2+1);

gotoxy((wherex( )-1),(wherey( )-1));

if(count_2<count_1)

Page 83: Aplikasi menghitung matematika dengan c++

cout<<"(k+1)";

else

cout<<"(k)";

gotoxy((wherex( )-1),(wherey( )+1));

if(input[count_1][(count_2+1)]>=0)

cout<<" + ";

else

cout<<" - ";

}

elseif(count_2!=count_1)

cout<<fabs(input[count_1][count_2]);

}

}

if(Diagonally_dominant=='Y' || Diagonally_dominant=='y')

{

gotoxy(4,26);

cout<<"Note: The given System of Linear Equations is solved by making it";

gotoxy(10,28);

cout<<"Diagonally Dominant.";

}

gotoxy(4,31);

cout<<"Result of Given System of Linear Equations :";

gotoxy(4,32);

cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ";

for(int count_3=0;count_3<n;count_3++)

{

gotoxy(8,(34+count_3+count_3));

cout<<"x"<<(count_3+1)<<" = "<<output[count_3];

}

gotoxy(4,(36+count_3+count_3));

cout<<"* Number of Iterations for the above result = "<<iterations<<".";

gotoxy(1,2);

}

7. Program untuk membaca Sistem Linear dari Persamaan, kemudian mengevaluasi

dengan menggunakan Metode Itrative Jacobi dan menampilkan hasil di C ++

Programming

# include <iostream.h>

Page 84: Aplikasi menghitung matematika dengan c++

# include <stdlib.h>

# include <stdio.h>

# include <conio.h>

# include <math.h>

constint max_size=5;

int n=0;

int iterations=0;

char Diagonally_dominant=NULL;

longdouble input[max_size][max_size]={0};

longdouble previous_output[max_size]={0};

longdouble output[max_size]={0};

void show_screen( );

void clear_screen( );

void get_size_of_linear_equations( );

void show_input(constint,constint);

void get_input_linear_equation( );

void sort_system_of_linear_equations( );

void apply_jacobi_iterative_method( );

void show_result( );

int main( )

{

clrscr( );

textmode(C4350);

show_screen( );

get_size_of_linear_equations( );

get_input_linear_equation( );

if(Diagonally_dominant=='Y' || Diagonally_dominant=='y')

sort_system_of_linear_equations( );

apply_jacobi_iterative_method( );

show_result( );

getch( );

return 0;

}

/*************************************************************************//

/-------------------------- show_screen( ) ---------------------------

///*************************************************************************

/void show_screen( )

{

Page 85: Aplikasi menghitung matematika dengan c++

cprintf("\n******************************************************************

**************");

cprintf("*************************- -

*************************");

cprintf("*------------------------- ");

textbackground(1);

cprintf(" Jacobi's Itrative Method ");

textbackground(8);

cprintf(" -------------------------*");

cprintf("************************- -

*************************");

cprintf("********************************************************************

************");

for(int count=0;count<42;count++)

cprintf("* *");

gotoxy(1,46);

cprintf("********************************************************************

************");

cprintf("*------------------------------------------------------------------------------*");

cprintf("********************************************************************

************");

gotoxy(1,2);

}

/*************************************************************************//

/------------------------- clear_screen( ) ---------------------------

///*************************************************************************

/void clear_screen( )

{

textbackground(8);

for(int count=0;count<37;count++)

{

gotoxy(3,8+count);

cout<<" ";

}

gotoxy(1,2);

}

Page 86: Aplikasi menghitung matematika dengan c++

/*************************************************************************//

/------------------ get_size_of_linear_equations( ) ------------------

///*************************************************************************

/void get_size_of_linear_equations( )

{

do

{

clear_screen( );

gotoxy(38,11);

cout<<"[ Maximum size = 4 ]";

gotoxy(4,9);

cout<<"Enter the size of the System of Linear Equations = n = ";

cin>>n;

if(n<=0 || n>max_size)

{

gotoxy(12,25);

cout<<"Error : Wrong Input. Press <Esc> to exit or any other key";

gotoxy(12,26);

cout<<" to try again.";

n=int(getche( ));

if(n==27)

exit(0);

}

}

while(n<=0 || n>max_size);

gotoxy(4,15);

cout<<"Enter the number of iterations that you want to perform = ";

cin>>iterations;

gotoxy(4,18);

cout<<"Do you want to make the System Diagonally Dominant : (Y/N) ? : ";

cin>>Diagonally_dominant;

gotoxy(1,2);

}

/*************************************************************************//

/----------------- show_input(const int,const int) -------------------

Page 87: Aplikasi menghitung matematika dengan c++

///*************************************************************************

/void show_input(constint x,constint y)

{

int counter=0;

int number_of_inputs=((x*n)+y+x);

for(int count_1=0;count_1<n;count_1++)

{

gotoxy(4,(17+(count_1*2)));

cout<<" ";

gotoxy(4,(17+(count_1*2)));

for(int count_2=0;count_2<=n;count_2++)

{

if(counter==(number_of_inputs+1))

textbackground(12);

else

textbackground(8);

if(count_2==n)

gotoxy((wherex( )+5),(17+(count_1*2)));

gotoxy(wherex( ),(17+(count_1*2)));

cprintf(" ");

if(count_2==n)

gotoxy((wherex( )-5),(17+(count_1*2)));

gotoxy((wherex( )-6),(17+(count_1*2)));

if(counter<=number_of_inputs && counter!=-1)

{

if(count_2==n)

{

cout<<" = ";

cout<<input[count_1][count_2];

}

elseif(count_2==0)

cout<<input[count_1][count_2]<<" x"<<(count_2+1);

else

cout<<fabs(input[count_1][count_2])<<" x"<<(count_2+1);

}

elseif(count_2==n)

cout<<" = b"<<(count_1+1);

Page 88: Aplikasi menghitung matematika dengan c++

else

cout<<"a"<<(count_1+1)<<(count_2+1)<<" x"<<(count_2+1);

if(count_2<(n-1) && input[count_1][(count_2+1)]>=0)

cout<<" + ";

elseif(count_2<(n-1))

cout<<" - ";

counter++;

}

}

}

/*************************************************************************//

/-------------------- get_input_linear_equation( ) -------------------

///*************************************************************************

/void get_input_linear_equation( )

{

clear_screen( );

gotoxy(4,9);

cout<<"Size of the System of Linear Equations = n = "<<n;

gotoxy(4,13);

cout<<"System of Linear Equations :";

gotoxy(4,14);

cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ";

show_input(0,-1);

for(int count_1=0;count_1<n;count_1++)

{

for(int count_2=0;count_2<=n;count_2++)

{

gotoxy(4,35);

cout<<" ";

gotoxy(4,35);

if(count_2<n)

cout<<"Enter the value of a"<<(count_1+1)<<(count_2+1)<<" = ";

else

cout<<"Enter the value of b"<<(count_1+1)<<" = ";

cin>>input[count_1][count_2];

Page 89: Aplikasi menghitung matematika dengan c++

show_input(count_1,count_2);

}

}

}

/*************************************************************************//

/----------------- sort_system_of_linear_equations( ) ----------------

///*************************************************************************

/void sort_system_of_linear_equations( )

{

for(int count_1=0;count_1<(n-1);count_1++)

{

for(int count_2=count_1;count_2<(n-1);count_2++)

{

for(int count_3=count_1;count_3<(n-1);count_3++)

{

longdouble temp[max_size]={0};

if(fabs(input[count_3][count_1])<

fabs(input[(count_3+1)][count_1]))

{

for(int count_4=0;count_4<=n;count_4++)

temp[count_4]=input[count_3][count_4];

for(int count_5=0;count_5<=n;count_5++)

input[count_3][count_5]=

input[(count_3+1)][count_5];

for(int count_6=0;count_6<=n;count_6++)

input[(count_3+1)][count_6]=temp[count_6];

}

}

}

}

}

/*************************************************************************//

/------------------ apply_jacobi_iterative_method( ) -----------------

///*************************************************************************

/void apply_jacobi_iterative_method( )

{

clear_screen( );

gotoxy(4,10);

cout<<"Solution :";

gotoxy(4,11);

cout<<"ÍÍÍÍÍÍÍÍÍÍ";

Page 90: Aplikasi menghitung matematika dengan c++

if(n==1)

{

gotoxy(4,13);

cout<<"ÚÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿";

gotoxy(4,14);

cout<<"³ n ³ X1 ³";

gotoxy(4,15);

cout<<"ÃÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ´";

gotoxy(4,16);

cout<<"³ ³ ³";

}

elseif(n==2)

{

gotoxy(4,13);

cout<<"ÚÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿";

gotoxy(4,14);

cout<<"³ n ³ X1 ³ X2 ³";

gotoxy(4,15);

cout<<"ÃÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ´";

gotoxy(4,16);

cout<<"³ ³ ³ ³";

}

elseif(n==3)

{

gotoxy(4,13);

cout<<"ÚÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄ

ÄÄÄÄÄÄÄÄÄ¿";

gotoxy(4,14);

cout<<"³ n ³ X1 ³ X2 ³ X3 ³";

gotoxy(4,15);

cout<<"ÃÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄ

ÄÄÄÄÄÄÄÄÄ´";

gotoxy(4,16);

cout<<"³ ³ ³ ³ ³";

}

Page 91: Aplikasi menghitung matematika dengan c++

elseif(n==4)

{

gotoxy(4,13);

cout<<"ÚÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄ

ÄÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿";

gotoxy(4,14);

cout<<"³ n ³ X1 ³ X2 ³ X3 ³ X4 ³";

gotoxy(4,15);

cout<<"ÃÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄ

ÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ´";

gotoxy(4,16);

cout<<"³ ³ ³ ³ ³ ³";

}

for(int count_1=0;count_1<n;count_1++)

{

longdouble temp[max_size]={0};

for(int count_2=0;count_2<=n;count_2++)

{

if(count_2!=count_1)

temp[count_2]=(input[count_1][count_2]/input[count_1][count_1]);

}

for(int count_3=0;count_3<=n;count_3++)

input[count_1][count_3]=temp[count_3];

}

for(int count_4=0;count_4<n;count_4++)

{

for(int count_5=0;count_5<n;count_5++)

input[count_4][count_5]*=-1;

}

int x_cord=4;

int y_cord=17;

for(int count_6=0;count_6<=iterations;count_6++)

{

if(n==1)

{

gotoxy(x_cord,y_cord);

cout<<"³ ³ ³";

gotoxy(x_cord,(y_cord+1));

Page 92: Aplikasi menghitung matematika dengan c++

cout<<"³ ³ ³";

}

elseif(n==2)

{

gotoxy(x_cord,y_cord);

cout<<"³ ³ ³ ³";

gotoxy(x_cord,(y_cord+1));

cout<<"³ ³ ³ ³";

}

elseif(n==3)

{

gotoxy(x_cord,y_cord);

cout<<"³ ³ ³ ³ ³";

gotoxy(x_cord,(y_cord+1));

cout<<"³ ³ ³ ³ ³";

}

elseif(n==4)

{

gotoxy(x_cord,y_cord);

cout<<"³ ³ ³ ³ ³ ³";

gotoxy(x_cord,(y_cord+1));

cout<<"³ ³ ³ ³ ³ ³";

}

gotoxy((x_cord+3),y_cord);

cout<<count_6;

gotoxy((x_cord+10),y_cord);

cout<<output[0];

if(n>=2)

{

gotoxy((x_cord+26),y_cord);

cout<<output[1];

}

if(n>=3)

{

gotoxy((x_cord+42),y_cord);

cout<<output[2];

}

if(n>=4)

{

Page 93: Aplikasi menghitung matematika dengan c++

gotoxy((x_cord+58),y_cord);

cout<<output[3];

}

for(int count_7=0;count_7<n;count_7++)

{

output[count_7]=0;

for(int count_8=0;count_8<n;count_8++)

{

if(count_8!=count_7)

output[count_7]+=(input[count_7][count_8]*previous_output[count_8]);

}

output[count_7]+=input[count_7][count_8];

}

for(int count_9=0;count_9<n;count_9++)

previous_output[count_9]=output[count_9];

y_cord+=2;

if((count_6%12)==0 && count_6<iterations && count_6>0)

{

y_cord=17;

gotoxy(30,44);

cout<<"Press any key to continue...";

getch( );

for(int count_10=1;count_10<25;count_10++)

{

gotoxy(3,(16+count_10));

cout<<" ";

}

}

}

if(n==1)

{

gotoxy(x_cord,y_cord);

cout<<"ÀÄÄÄÄÄÄÄÁÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ";

}

elseif(n==2)

{

gotoxy(x_cord,y_cord);

cout<<"ÀÄÄÄÄÄÄÄÁÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÁÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ";

}

Page 94: Aplikasi menghitung matematika dengan c++

elseif(n==3)

{

gotoxy(x_cord,y_cord);

cout<<"ÀÄÄÄÄÄÄÄÁÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÁÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÁÄÄÄÄÄÄ

ÄÄÄÄÄÄÄÄÄÙ";

}

elseif(n==4)

{

gotoxy(x_cord,y_cord);

cout<<"ÀÄÄÄÄÄÄÄÁÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÁÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÁÄÄÄÄÄÄ

ÄÄÄÄÄÄÄÄÄÁÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ";

}

gotoxy(30,44);

cout<<"Press any key to continue...";

getch( );

}

/*************************************************************************//

/----------------------------- show_result( ) ------------------------

///*************************************************************************

/void show_result( )

{

clear_screen( );

gotoxy(4,9);

cout<<"Jacobi's System of Linear Equations :";

gotoxy(4,10);

cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ";

for(int count_1=0;count_1<n;count_1++)

{

gotoxy(4,(13+(count_1*3)));

cout<<"x"<<(count_1+1);

gotoxy((wherex( )-1),(wherey( )-1));

cout<<"(k+1)";

gotoxy((wherex( )-3),(wherey( )+1));

cout<<" = ";

for(int count_2=0;count_2<=n;count_2++)

{

gotoxy(wherex( ),(13+(count_1*3)));

Page 95: Aplikasi menghitung matematika dengan c++

if(count_2!=count_1 && count_2<n)

{

if(count_2!=0)

cout<<fabs(input[count_1][count_2])<<" x"<<(count_2+1);

else

cout<<input[count_1][count_2]<<" x"<<(count_2+1);

gotoxy((wherex( )-1),(wherey( )-1));

cout<<"(k)";

gotoxy((wherex( )-1),(wherey( )+1));

if(input[count_1][(count_2+1)]>=0)

cout<<" + ";

else

cout<<" - ";

}

elseif(count_2!=count_1)

cout<<fabs(input[count_1][count_2]);

}

}

if(Diagonally_dominant=='Y' || Diagonally_dominant=='y')

{

gotoxy(4,26);

cout<<"Note: The given System of Linear Equations is solved by making it";

gotoxy(10,28);

cout<<"Diagonally Dominant.";

}

gotoxy(4,31);

cout<<"Result of Given System of Linear Equations :";

gotoxy(4,32);

cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ";

for(int count_3=0;count_3<n;count_3++)

{

gotoxy(8,(34+count_3+count_3));

cout<<"x"<<(count_3+1)<<" = "<<output[count_3];

}

gotoxy(4,(36+count_3+count_3));

cout<<"* Number of Iterations for the above result = "<<iterations<<".";

gotoxy(1,2);

Page 96: Aplikasi menghitung matematika dengan c++

}

8. Program untuk membaca Sistem Linear dari Persamaan, kemudian mengevaluasi

dengan menggunakan metode eliminasi Gauss dan menampilkan hasil di C++

Programming

# include <iostream.h>

# include <stdlib.h>

# include <stdio.h>

# include <conio.h>

# include <math.h>

constint max_size=5;

int n=0;

char Row_interchanging=NULL;

longdouble A[max_size][max_size]={0};

longdouble L[max_size][max_size]={0};

longdouble U[max_size][max_size]={0};

longdouble X[max_size]={0};

void show_screen( );

void clear_screen( );

void get_size_of_linear_equations( );

void show_input(constint,constint,constint=0);

void get_input_linear_equation( );

void show_AB_matrices( );

void make_first_row_largest( );

void initialize_LU_matrices( );

void apply_LU_decomposition_method( );

void interchange_rows(constint,constint);

void show_LU_matrices( );

void generate_result( );

void show_result( );

int main( )

{

clrscr( );

textmode(C4350);

show_screen( );

get_size_of_linear_equations( );

get_input_linear_equation( );

show_AB_matrices( );

apply_LU_decomposition_method( );

show_LU_matrices( );

generate_result( );

show_result( );

Page 97: Aplikasi menghitung matematika dengan c++

getch( );

return 0;

}

/*************************************************************************//

/-------------------------- show_screen( ) ---------------------------

///*************************************************************************

/void show_screen( )

{

cprintf("\n******************************************************************

**************");

cprintf("*************************- -

**************************");

cprintf("*------------------------- ");

textbackground(1);

cprintf(" LU-Decomposition Method ");

textbackground(8);

cprintf(" --------------------------*");

cprintf("*-***********************- -************************-

*");

cprintf("*-

***************************************************************************

*-*");

for(int count=0;count<42;count++)

cprintf("*-* *-*");

gotoxy(1,46);

cprintf("*-

***************************************************************************

*-*");

cprintf("*------------------------------------------------------------------------------*");

cprintf("********************************************************************

************");

gotoxy(1,2);

}

/*************************************************************************//

/------------------------- clear_screen( ) ---------------------------

///*************************************************************************

/void clear_screen( )

{

for(int count=0;count<37;count++)

Page 98: Aplikasi menghitung matematika dengan c++

{

gotoxy(5,8+count);

cout<<" ";

}

gotoxy(1,2);

}

/*************************************************************************//

/------------------ get_size_of_linear_equations( ) ------------------

///*************************************************************************

/void get_size_of_linear_equations( )

{

do

{

clear_screen( );

gotoxy(44,11);

cout<<"[ Maximum size = 4 ]";

gotoxy(6,9);

cout<<"Enter the size of the System of Linear Equations = n = ";

cin>>n;

if(n<=0 || n>max_size)

{

gotoxy(12,25);

cout<<"Error : Wrong Input. Press <Esc> to exit or any other key";

gotoxy(12,26);

cout<<" to try again.";

n=int(getche( ));

if(n==27)

exit(0);

}

}

while(n<=0 || n>max_size);

gotoxy(6,15);

cout<<"Do you want to use Row Interchanging Technique : (Y/N) ? : ";

cin>>Row_interchanging;

gotoxy(1,2);

}

Page 99: Aplikasi menghitung matematika dengan c++

/*************************************************************************//

/------------- show_input(const int,const int,const int) -------------

///*************************************************************************

/void show_input(constint x,constint y,constint y_cord)

{

int counter=0;

int number_of_inputs=((x*n)+y+x);

for(int count_1=0;count_1<n;count_1++)

{

gotoxy(6,(17+(count_1*2)+y_cord));

cout<<" ";

gotoxy(6,(17+(count_1*2)+y_cord));

for(int count_2=0;count_2<=n;count_2++)

{

if(counter==(number_of_inputs+1))

textbackground(12);

else

textbackground(8);

if(count_2==n)

gotoxy((wherex( )+5),(17+(count_1*2)+y_cord));

gotoxy(wherex( ),(17+(count_1*2)+y_cord));

cprintf(" ");

if(count_2==n)

gotoxy((wherex( )-5),(17+(count_1*2)+y_cord));

gotoxy((wherex( )-6),(17+(count_1*2)+y_cord));

if(counter<=number_of_inputs && counter!=-1)

{

if(count_2==n)

{

cout<<" = ";

cout<<A[count_1][count_2];

}

elseif(count_2==0)

cout<<A[count_1][count_2]<<" x"<<(count_2+1);

else

cout<<fabs(A[count_1][count_2])<<" x"<<(count_2+1);

}

Page 100: Aplikasi menghitung matematika dengan c++

elseif(count_2==n)

cout<<" = b"<<(count_1+1);

else

cout<<"a"<<(count_1+1)<<(count_2+1)<<" x"<<(count_2+1);

if(count_2<(n-1) && A[count_1][(count_2+1)]>=0)

cout<<" + ";

elseif(count_2<(n-1))

cout<<" - ";

counter++;

}

}

}

/*************************************************************************//

/-------------------- get_input_linear_equation( ) -------------------

///*************************************************************************

/void get_input_linear_equation( )

{

clear_screen( );

gotoxy(5,9);

cout<<"Size of the System of Linear Equations = n = "<<n;

gotoxy(5,13);

cout<<"System of Linear Equations :";

gotoxy(5,14);

cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ";

show_input(0,-1);

for(int count_1=0;count_1<n;count_1++)

{

for(int count_2=0;count_2<=n;count_2++)

{

gotoxy(5,35);

cout<<" ";

gotoxy(5,35);

if(count_2<n)

cout<<"Enter the value of a"<<(count_1+1)<<(count_2+1)<<" = ";

else

Page 101: Aplikasi menghitung matematika dengan c++

cout<<"Enter the value of b"<<(count_1+1)<<" = ";

cin>>A[count_1][count_2];

show_input(count_1,count_2);

}

}

}

/*************************************************************************//

/------------------------ show_AB_matrices( ) ------------------------

///*************************************************************************

/void show_AB_matrices( )

{

clear_screen( );

gotoxy(5,9);

cout<<"Convertion of given System of Linear Equations into Matrix-A & Matrix-B";

gotoxy(5,10);

cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ";

gotoxy(12,13);

cout<<"ÚÄ";

gotoxy((12+(n*14)),13);

cout<<"Ä¿";

gotoxy(12,14);

cout<<"³";

gotoxy((13+(n*14)),14);

cout<<"³";

if((n%2)==0)

gotoxy(6,(14+((n/2)*2)));

else

gotoxy(6,(15+((n/2)*2)));

cout<<"A =";

for(int count_1=0;count_1<n;count_1++)

{

gotoxy(12,(15+(count_1*2)));

cout<<"³";

gotoxy(12,(16+(count_1*2)));

cout<<"³";

Page 102: Aplikasi menghitung matematika dengan c++

for(int count_2=0;count_2<n;count_2++)

{

gotoxy(15+(count_2*14),(15+(count_1*2)));

cout<<A[count_1][count_2];

}

gotoxy((13+(n*14)),(15+(count_1*2)));

cout<<"³";

gotoxy((13+(n*14)),((count_1*2)+16));

cout<<"³";

}

gotoxy(12,(15+(count_1*2)));

cout<<"ÀÄ";

gotoxy((12+(n*14)),(15+(count_1*2)));

cout<<"ÄÙ";

gotoxy(12,(20+(count_1*2)));

cout<<"ÚÄ";

gotoxy(26,(20+(count_1*2)));

cout<<"Ä¿";

gotoxy(12,(21+(count_1*2)));

cout<<"³";

gotoxy(27,(21+(count_1*2)));

cout<<"³";

gotoxy(6,(18+(count_1*2)+(n*2)));

if((n%2)==0)

gotoxy(6,(21+(count_1*2)+((n/2)*2)));

else

gotoxy(6,(22+(count_1*2)+((n/2)*2)));

cout<<"B =";

for(int count_3=0;count_3<n;count_3++)

{

gotoxy(12,((22+(count_1*2))+(count_3*2)));

cout<<"³";

gotoxy(12,((23+(count_1*2))+(count_3*2)));

cout<<"³";

Page 103: Aplikasi menghitung matematika dengan c++

gotoxy(15,((22+(count_1*2))+(count_3*2)));

cout<<A[count_3][n];

gotoxy(27,((22+(count_1*2))+(count_3*2)));

cout<<"³";

gotoxy(27,((23+(count_1*2))+(count_3*2)));

cout<<"³";

}

gotoxy(12,((22+(count_1*2))+(count_3*2)));

cout<<"ÀÄ";

gotoxy(26,((22+(count_1*2))+(count_3*2)));

cout<<"ÄÙ";

gotoxy(25,43);

cout<<"Press any key to continue...";

getch( );

}

/*************************************************************************//

/---------------------- make_first_row_largest( ) --------------------

///*************************************************************************

/void make_first_row_largest( )

{

int max_row_number=0;

for(int count_1=0;count_1<n;count_1++)

{

if(fabs(A[max_row_number][0])<fabs(A[count_1][0]))

max_row_number=count_1;

}

if(max_row_number>0)

{

longdouble temp[(max_size+1)]={0};

for(int count_2=0;count_2<=n;count_2++)

temp[count_2]=A[0][count_2];

for(int count_3=0;count_3<=n;count_3++)

A[0][count_3]=A[max_row_number][count_3];

for(int count_4=0;count_4<=n;count_4++)

A[max_row_number][count_4]=temp[count_4];

}

}

Page 104: Aplikasi menghitung matematika dengan c++

/*************************************************************************//

/---------------------- initialize_LU_matrices( ) --------------------

///*************************************************************************

/void initialize_LU_matrices( )

{

for(int count_1=0;count_1<n;count_1++)

{

L[count_1][count_1]=1;

L[count_1][n]=A[count_1][n];

}

for(int count_2=1;count_2<n;count_2++)

L[count_2][0]=(A[count_2][0]/A[0][0]);

for(int count_3=0;count_3<n;count_3++)

U[0][count_3]=A[0][count_3];

}

/*************************************************************************//

/------------------ apply_LU_decomposition_method( ) -----------------

///*************************************************************************

/void apply_LU_decomposition_method( )

{

if(Row_interchanging=='Y' || Row_interchanging=='y')

make_first_row_largest( );

initialize_LU_matrices( );

for(int count_1=0;count_1<(n-1);count_1++)

{

longdouble options[max_size]={0};

int option_number=0;

for(int count_2=(count_1+1);count_2<n;count_2++)

{

for(int count_3=0;count_3<n;count_3++)

options[option_number]+=(L[count_2][count_3]*U[count_3][(count_1+1)]);

options[option_number]=(A[count_2][(count_1+1)]-options[option_number]);

option_number++;

}

int max_row_number=0;

for(int count_4=0;count_4<option_number;count_4++)

{

Page 105: Aplikasi menghitung matematika dengan c++

if(fabs(options[max_row_number])<=fabs(options[count_4]))

{

if(fabs(options[max_row_number])==fabs(options[count_4]) &&

options[max_row_number]!=options[count_4])

{

if(options[max_row_number]<options[count_4])

max_row_number=count_4;

}

else

max_row_number=count_4;

}

}

if(max_row_number>0 && (Row_interchanging=='Y' || Row_interchanging=='y'))

interchange_rows((count_1+1),(count_1+1+max_row_number));

U[(count_1+1)][(count_1+1)]=options[max_row_number];

longdouble temp=0;

for(int count_5=(count_1+2);count_5<n;count_5++)

{

for(int count_6=0;count_6<n;count_6++)

temp+=(L[count_5][count_6]*U[count_6][(count_1+1)]);

temp=A[count_5][(count_1+1)]-temp;

temp/=U[(count_1+1)][(count_1+1)];

L[count_5][(count_1+1)]=temp;

temp=0;

for(int count_7=0;count_7<n;count_7++)

temp+=(L[(count_1+1)][count_7]*U[count_7][count_5]);

temp=A[(count_1+1)][count_5]-temp;

U[(count_1+1)][count_5]=temp;

temp=0;

}

}

}

/*************************************************************************//

/--------------- interchange_rows(const int,const int) ---------------

///*************************************************************************

/void interchange_rows(constint row_1,constint row_2)

{

longdouble temp_A[max_size]={0};

Page 106: Aplikasi menghitung matematika dengan c++

for(int count_1=0;count_1<=n;count_1++)

temp_A[count_1]=A[row_1][count_1];

for(int count_2=0;count_2<=n;count_2++)

A[row_1][count_2]=A[row_2][count_2];

for(int count_3=0;count_3<=n;count_3++)

A[row_2][count_3]=temp_A[count_3];

longdouble temp_L0=L[row_1][0];

longdouble temp_Ln=L[row_1][n];

L[row_1][0]=L[row_2][0];

L[row_2][0]=temp_L0;

L[row_1][n]=L[row_2][n];

L[row_2][n]=temp_Ln;

}

/*************************************************************************//

/------------------------ generate_result( ) -------------------------

///*************************************************************************

/void generate_result( )

{

for(int count_1=0;count_1<n;count_1++)

{

U[count_1][n]=L[count_1][n];

for(int count_2=0;count_2<count_1;count_2++)

L[count_1][count_2]=(L[count_1][count_2]*U[count_2][n]);

for(int count_3=0;count_3<count_1;count_3++)

U[count_1][n]-=L[count_1][count_3];

U[count_1][n]/=L[count_1][count_1];

}

for(int count_4=(n-1);count_4>=0;count_4--)

{

X[count_4]=U[count_4][n];

for(int count_5=(n-1);count_5>count_4;count_5--)

U[count_4][count_5]=(U[count_4][count_5]*X[count_5]);

for(int count_6=(n-1);count_6>count_4;count_6--)

X[count_4]-=U[count_4][count_6];

X[count_4]/=U[count_4][count_4];

}

Page 107: Aplikasi menghitung matematika dengan c++

}

/*************************************************************************//

/------------------------ show_LU_matrices( ) ------------------------

///*************************************************************************

/void show_LU_matrices( )

{

clear_screen( );

gotoxy(6,9);

cout<<"Decomposition of Matrix-A into Matrix-L and Matrix-U :";

gotoxy(6,10);

cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ";

for(int count_1=1;count_1<=2;count_1++)

{

gotoxy(12,((count_1*15)-2));

cout<<"ÚÄ";

gotoxy((12+(n*14)),((count_1*15)-2));

cout<<"Ä¿";

gotoxy(12,((count_1*15)-1));

cout<<"³";

gotoxy((13+(n*14)),((count_1*15)-1));

cout<<"³";

if((n%2)==0)

gotoxy(6,((count_1*15)+((n/2)*2)-1));

else

gotoxy(6,((count_1*15)+((n/2)*2)));

if(count_1==1)

cout<<"L =";

elseif(count_1==2)

cout<<"U =";

for(int count_2=0;count_2<n;count_2++)

{

gotoxy(12,((count_1*15)+(count_2*2)));

cout<<"³";

gotoxy(12,((count_1*15)+(count_2*2)+1));

cout<<"³";

Page 108: Aplikasi menghitung matematika dengan c++

for(int count_3=0;count_3<n;count_3++)

{

gotoxy(15+(count_3*14),((count_1*15)+(count_2*2)));

if(count_1==1)

cout<<L[count_2][count_3];

elseif(count_1==2)

cout<<U[count_2][count_3];

}

gotoxy((13+(n*14)),((count_1*15)+(count_2*2)));

cout<<"³";

gotoxy((13+(n*14)),((count_1*15)+(count_2*2)+1));

cout<<"³";

}

gotoxy(12,((count_1*15)+(count_2*2)));

cout<<"ÀÄ";

gotoxy((12+(n*14)),((count_1*15)+(count_2*2)));

cout<<"ÄÙ";

}

gotoxy(25,43);

cout<<"Press any key to continue...";

getch( );

}

/*************************************************************************//

/----------------------------- show_result( ) ------------------------

///*************************************************************************

/void show_result( )

{

clear_screen( );

gotoxy(6,10);

cout<<"Given System of Linear Equations :";

gotoxy(6,11);

cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ";

show_input((n-1),n,-4);

gotoxy(6,26);

cout<<"Result of given System of Linear Equations :";

Page 109: Aplikasi menghitung matematika dengan c++

gotoxy(6,27);

cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ";

for(int count=0;count<n;count++)

{

gotoxy(10,(29+count+count));

cout<<"x"<<(count+1)<<" = "<<X[count];

}

gotoxy(6,41);

cout<<"Note : The given system of Linear Equations is solved by using";

gotoxy(6,43);

cout<<" Do-Little Method";

gotoxy(30,43);

if(Row_interchanging=='Y' || Row_interchanging=='y')

cout<<"and Row-Interchanging Method.";

else

cout<<".";

gotoxy(1,2);

}

9. Program untuk membangun Newton Mundur Perbedaan interpolasi Formula dari yang

berbeda titik data yang diberikan sama spasi di C ++ Programming

# include <iostream.h>

# include <stdlib.h>

# include <string.h>

# include <stdio.h>

# include <conio.h>

# include <math.h>

constint max_size=13;

int n=0;

int top=-1;

int choice=0;

longdouble h=0;

longdouble x0=0;

longdouble xn[max_size]={0};

longdouble difference_table[max_size][max_size]={0};

char Non_linear_equation[100]={NULL};

char Stack[30][30]={NULL};

char Postfix_expression[30][30]={NULL};

Page 110: Aplikasi menghitung matematika dengan c++

void push(constchar *);

void convert_infix_expression_to_postfix_expression(constchar *);

constchar* pop( );

constlongdouble evaluate_postfix_expression(constlongdouble);

void show_screen( );

void clear_screen( );

void get_input( );

void construct_difference_table( );

void show_difference_table( );

void estimate_pnx( );

constlongdouble compute_newtons_bdif(constlongdouble,constint);

int main( )

{

clrscr( );

textmode(C4350);

show_screen( );

get_input( );

construct_difference_table( );

show_difference_table( );

estimate_pnx( );

return 0;

}

/*************************************************************************//

*************************************************************************///

------------------------ Funcion Definitions ------------------------

///*************************************************************************

//*************************************************************************/

/*************************************************************************//

/-------------------------- show_screen( ) ---------------------------

///*************************************************************************

/void show_screen( )

{

cprintf("\n******************************************************************

**************");

cprintf("************- -************");

cprintf("*------------ ");

textbackground(1);

cprintf(" Newton's Backward Difference Interpolation Formula ");

Page 111: Aplikasi menghitung matematika dengan c++

textbackground(8);

cprintf(" ------------*");

cprintf("************- -************");

cprintf("********************************************************************

************");

for(int count=0;count<42;count++)

cprintf("* *");

gotoxy(1,46);

cprintf("********************************************************************

************");

cprintf("*------------------------------------------------------------------------------*");

cprintf("********************************************************************

************");

gotoxy(1,2);

}

/*************************************************************************//

/------------------------- clear_screen( ) ---------------------------

///*************************************************************************

/void clear_screen( )

{

for(int count=0;count<37;count++)

{

gotoxy(3,8+count);

cout<<" ";

}

gotoxy(1,2);

}

/*************************************************************************//

/-------------------------- push(const char*) ------------------------

///*************************************************************************

/void push(constchar* Operand)

{

if(top==(max_size-1))

{

cout<<"Error : Stack is full."<<endl;

cout<<"\n Press any key to exit.";

getch( );

Page 112: Aplikasi menghitung matematika dengan c++

exit(0);

}

else

{

top++;

strcpy(Stack[top],Operand);

}

}

/*************************************************************************//

/------------------------------ pop( ) -------------------------------

///*************************************************************************

/constchar* pop( )

{

char Operand[40]={NULL};

if(top==-1)

{

cout<<"Error : Stack is empty."<<endl;

cout<<"\n Press any key to exit.";

getch( );

exit(0);

}

else

{

strcpy(Operand,Stack[top]);

strset(Stack[top],NULL);

top--;

}

return Operand;

}

/*************************************************************************//

/---- convert_infix_expression_to_postfix_expression(const char*) ----

///*************************************************************************

/void convert_infix_expression_to_postfix_expression(constchar* Expression)

{

char Infix_expression[100]={NULL};

char Symbol_scanned[30]={NULL};

push("(");

strcpy(Infix_expression,Expression);

strcat(Infix_expression,"+0)");

Page 113: Aplikasi menghitung matematika dengan c++

int flag=0;

int count_1=0;

int count_2=0;

int equation_length=strlen(Infix_expression);

if(Infix_expression[0]=='(')

flag=1;

do

{

strset(Symbol_scanned,NULL);

if(flag==0)

{

int count_3=0;

do

{

Symbol_scanned[count_3]=Infix_expression[count_1];

count_1++;

count_3++;

}

while(count_1<=equation_length &&

Infix_expression[count_1]!='(' &&

Infix_expression[count_1]!='+' &&

Infix_expression[count_1]!='-' &&

Infix_expression[count_1]!='*' &&

Infix_expression[count_1]!='/' &&

Infix_expression[count_1]!='^' &&

Infix_expression[count_1]!=')');

flag=1;

}

elseif(flag==1)

{

Symbol_scanned[0]=Infix_expression[count_1];

count_1++;

if(Infix_expression[count_1]!='(' &&

Infix_expression[count_1]!='^' &&

Infix_expression[count_1]!='*' &&

Infix_expression[count_1]!='/' &&

Infix_expression[count_1]!='+' &&

Infix_expression[count_1]!='-' &&

Infix_expression[count_1]!=')')

flag=0;

Page 114: Aplikasi menghitung matematika dengan c++

if(Infix_expression[count_1-1]=='(' &&

(Infix_expression[count_1]=='-' ||

Infix_expression[count_1]=='+'))

flag=0;

}

if(strcmp(Symbol_scanned,"(")==0)

push("(");

elseif(strcmp(Symbol_scanned,")")==0)

{

while(strcmp(Stack[top],"(")!=0)

{

strcpy(Postfix_expression[count_2],pop( ));

count_2++;

}

pop( );

}

elseif(strcmp(Symbol_scanned,"^")==0 ||

strcmp(Symbol_scanned,"+")==0 ||

strcmp(Symbol_scanned,"-")==0 ||

strcmp(Symbol_scanned,"*")==0 ||

strcmp(Symbol_scanned,"/")==0)

{

if(strcmp(Symbol_scanned,"^")==0)

{ }

elseif(strcmp(Symbol_scanned,"*")==0 ||

strcmp(Symbol_scanned,"/")==0)

{

while(strcmp(Stack[top],"^")==0 ||

strcmp(Stack[top],"*")==0 ||

strcmp(Stack[top],"/")==0)

{

strcpy(Postfix_expression[count_2],pop( ));

count_2++;

}

}

elseif(strcmp(Symbol_scanned,"+")==0 ||

strcmp(Symbol_scanned,"-")==0)

{

while(strcmp(Stack[top],"(")!=0)

{

strcpy(Postfix_expression[count_2],pop( ));

Page 115: Aplikasi menghitung matematika dengan c++

count_2++;

}

}

push(Symbol_scanned);

}

else

{

strcat(Postfix_expression[count_2],Symbol_scanned);

count_2++;

}

}

while(strcmp(Stack[top],NULL)!=0);

strcat(Postfix_expression[count_2],"=");

count_2++;

}

/*************************************************************************//

/---------- evaluate_postfix_expression(const long double) -----------

///*************************************************************************

/constlongdouble evaluate_postfix_expression(constlongdouble x)

{

longdouble function_value=0;

int count_1=-1;

char Symbol_scanned[30]={NULL};

do

{

count_1++;

strcpy(Symbol_scanned,Postfix_expression[count_1]);

if(strcmp(Symbol_scanned,"^")==0 ||

strcmp(Symbol_scanned,"*")==0 ||

strcmp(Symbol_scanned,"/")==0 ||

strcmp(Symbol_scanned,"+")==0 ||

strcmp(Symbol_scanned,"-")==0)

{

char Result[30]={NULL};

char Operand[2][30]={NULL};

strcpy(Operand[0],pop( ));

Page 116: Aplikasi menghitung matematika dengan c++

strcpy(Operand[1],pop( ));

longdouble operand[2]={0};

longdouble result=0;

char *endptr;

for(int count_2=0;count_2<2;count_2++)

{

int flag=0;

if(Operand[count_2][0]=='-')

{

int length=strlen(Operand[count_2]);

for(int count_3=0;count_3<(length-1);count_3++)

Operand[count_2][count_3]=Operand[count_2][(count_3+1)];

Operand[count_2][count_3]=NULL;

flag=1;

}

if(strcmp(Operand[count_2],"x")==0)

operand[count_2]=x;

elseif(strcmp(Operand[count_2],"e")==0)

operand[count_2]=2.718282;

elseif(strcmp(Operand[count_2],"sinx")==0)

operand[count_2]=sinl(x);

elseif(strcmp(Operand[count_2],"cosx")==0)

operand[count_2]=cosl(x);

elseif(strcmp(Operand[count_2],"tanx")==0)

operand[count_2]=tanl(x);

elseif(strcmp(Operand[count_2],"lnx")==0)

operand[count_2]=logl(x);

elseif(strcmp(Operand[count_2],"logx")==0)

operand[count_2]=log10l(x);

else

operand[count_2]=strtod(Operand[count_2],&endptr);

if(flag)

operand[count_2]*=-1;

}

Page 117: Aplikasi menghitung matematika dengan c++

switch(Symbol_scanned[0])

{

case'^' : result=powl(operand[1],operand[0]);

break;

case'*' : result=operand[1]*operand[0];

break;

case'/' : result=operand[1]/operand[0];

break;

case'+' : result=operand[1]+operand[0];

break;

case'-' : result=operand[1]-operand[0];

break;

}

gcvt(result,25,Result);

push(Result);

}

elseif(strcmp(Symbol_scanned,"=")!=0)

push(Symbol_scanned);

}

while(strcmp(Symbol_scanned,"=")!=0);

char Function_value[30]={NULL};

char *endptr;

strcpy(Function_value,pop( ));

function_value=strtod(Function_value,&endptr);

return function_value;

}

/*************************************************************************//

/----------------------------- get_input( ) --------------------------

///*************************************************************************

/void get_input( )

{

do

{

clear_screen( );

gotoxy(6,9);

cout<<"Number of Distinct Data Points :";

Page 118: Aplikasi menghitung matematika dengan c++

gotoxy(6,10);

cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ";

gotoxy(27,13);

cout<<"[ min. n = 2 | max. n = 12 ]";

gotoxy(6,12);

cout<<"Enter the max. number of distinct data points = n = ";

cin>>n;

if(n<2 || n>12)

{

gotoxy(12,25);

cout<<"Error : Wrong Input. Press <Esc> to exit or any other key";

gotoxy(12,26);

cout<<" to try again.";

n=int(getche( ));

if(n==27)

exit(0);

}

}

while(n<2 || n>12);

gotoxy(6,16);

cout<<"Enter the value of x0 = ";

cin>>x0;

gotoxy(6,18);

cout<<"Enter the value of h = ";

cin>>h;

gotoxy(6,24);

cout<<"Input Mode :";

gotoxy(6,25);

cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍ";

gotoxy(8,28);

cout<<"Press : ";

gotoxy(10,30);

cout<<"- 'Y' or <Enter> to enter function";

Page 119: Aplikasi menghitung matematika dengan c++

gotoxy(10,32);

cout<<"- 'N' or <Any other key> to enter values of the function";

gotoxy(8,35);

cout<<"Enter your choice : ";

char Choice=NULL;

Choice=getch( );

if(Choice=='y' || Choice=='Y' || int(Choice)==13)

{

choice=1;

gotoxy(28,35);

cout<<"Y";

}

else

{

gotoxy(28,35);

cout<<"N";

}

gotoxy(25,43);

cout<<"Press any key to continue...";

getch( );

if(choice)

{

clear_screen( );

gotoxy(6,11);

cout<<"Non-Linear Function :";

gotoxy(6,12);

cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ";

gotoxy(6,37);

cout<<"Note : Write the function with proper Braces ( ) e.g; 2x+3 as (2*x)+3";

gotoxy(6,40);

cout<<"Available Operators : ^ (raised to power) , * , / , + , -";

gotoxy(6,42);

cout<<"Available Operands : x , e , sinx , cosx , tanx , lnx , logx ,";

gotoxy(6,44);

cout<<" n = any number";

Page 120: Aplikasi menghitung matematika dengan c++

gotoxy(6,14);

cout<<"Enter the Function : f(x) = ";

cin>>Non_linear_equation;

convert_infix_expression_to_postfix_expression(Non_linear_equation);

}

clear_screen( );

gotoxy(6,9);

cout<<"Data Points & Values of Function :";

gotoxy(6,10);

cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ";

gotoxy(25,12);

cout<<"ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿";

gotoxy(25,13);

cout<<"³ x ³ f(x) ³";

gotoxy(25,14);

cout<<"ÃÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ´";

gotoxy(25,15);

cout<<"³ ³ ³";

for(int count_1=0;count_1<n;count_1++)

{

gotoxy(25,(wherey( )+1));

cout<<"³ ³ ³";

gotoxy(25,(wherey( )+1));

cout<<"³ ³ ³";

}

gotoxy(25,(wherey( )+1));

cout<<"ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÁÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ";

xn[0]=x0;

for(int count_2=0;count_2<(n-1);count_2++)

xn[(count_2+1)]=(xn[count_2]+h);

gotoxy(25,16);

for(int count_3=0;count_3<n;count_3++)

{

Page 121: Aplikasi menghitung matematika dengan c++

gotoxy(27,wherey( ));

cout<<xn[count_3];

if(choice)

{

difference_table[0][count_3]=evaluate_postfix_expression(xn[count_3]);

gotoxy(43,wherey( ));

cout<<difference_table[0][count_3];

}

else

{

gotoxy(43,wherey( ));

cin>>difference_table[0][count_3];

}

if(choice)

gotoxy(25,(wherey( )+2));

else

gotoxy(25,(wherey( )+1));

}

gotoxy(25,43);

cout<<"Press any key to continue...";

getch( );

}

/*************************************************************************//

/----------------------- construct_difference_table( ) -----------------------

///*************************************************************************

/void construct_difference_table( )

{

for(int count_1=1;count_1<n;count_1++)

{

for(int count_2=count_1;count_2<n;count_2++)

{

longdouble fx1=difference_table[(count_1-1)][(count_2-1)];

longdouble fx2=difference_table[(count_1-1)][count_2];

difference_table[count_1][count_2]=fx2-fx1;

}

}

}

/*************************************************************************//

Page 122: Aplikasi menghitung matematika dengan c++

/-------------------------- show_difference_table( ) -------------------------

///*************************************************************************

/void show_difference_table( )

{

clear_screen( );

gotoxy(4,10);

cout<<"Difference Table :";

gotoxy(4,11);

cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ";

gotoxy(4,13);

cout<<"ÚÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ";

gotoxy(4,14);

cout<<"³ x ³ f(x) ";

gotoxy(4,15);

cout<<"ÃÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄÄ";

gotoxy(4,16);

cout<<"³ ³ ";

int x_cord=4;

int y_cord=17;

for(int count_1=0;count_1<n;count_1++)

{

gotoxy(x_cord,y_cord);

cout<<"³ ³ ";

gotoxy(x_cord,(y_cord+1));

cout<<"³ ³ ";

gotoxy((x_cord+2),y_cord);

cout<<xn[count_1];

gotoxy((x_cord+11),y_cord);

cout<<difference_table[0][count_1];

y_cord+=2;

}

gotoxy(x_cord,y_cord);

cout<<"ÀÄÄÄÄÄÄÄÄÁÄÄÄÄÄÄÄÄÄÄÄÄÄÄ";

x_cord=28;

int count_2=0;

Page 123: Aplikasi menghitung matematika dengan c++

for(int count_3=1;count_3<n;count_3++)

{

gotoxy(x_cord,13);

cout<<"ÂÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ";

gotoxy(x_cord,14);

cout<<"³";

gotoxy(x_cord,15);

cout<<"ÅÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ";

gotoxy(x_cord,16);

cout<<"³";

gotoxy(x_cord,y_cord);

cout<<"ÁÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ";

gotoxy((x_cord+6),14);

cout<<count_3;

if(count_3==1)

cout<<"st";

elseif(count_3==2)

cout<<"nd";

elseif(count_3==3)

cout<<"rd";

else

cout<<"th";

y_cord=17;

for(int count_4=0;count_4<n;count_4++)

{

gotoxy(x_cord,y_cord);

cout<<"³";

gotoxy(x_cord,(y_cord+1));

cout<<"³";

if(count_4>=count_3)

{

gotoxy((x_cord+2),(y_cord-count_3));

cout<<difference_table[count_3][count_4];

}

y_cord+=2;

Page 124: Aplikasi menghitung matematika dengan c++

}

x_cord+=16;

count_2++;

if((count_2%3)==0 && count_3<(n-1))

{

gotoxy(x_cord,13);

cout<<"ÂÄÄ";

gotoxy(x_cord,14);

cout<<"³";

gotoxy(x_cord,15);

cout<<"ÅÄÄ";

gotoxy(x_cord,16);

cout<<"³";

gotoxy(x_cord,y_cord);

cout<<"ÁÄÄ";

y_cord=17;

for(int count_5=0;count_5<n;count_5++)

{

gotoxy(x_cord,y_cord);

cout<<"³";

gotoxy(x_cord,(y_cord+1));

cout<<"³";

y_cord+=2;

}

gotoxy(30,44);

cout<<"Press any key to continue...";

getch( );

y_cord=13;

x_cord=28;

for(int count_6=0;count_6<=(n+2);count_6++)

{

gotoxy(x_cord,y_cord);

cout<<" ";

gotoxy(x_cord,(y_cord+1));

cout<<" ";

Page 125: Aplikasi menghitung matematika dengan c++

y_cord+=2;

}

y_cord-=2;

count_2=0;

count_3--;

}

}

gotoxy(x_cord,13);

cout<<"¿";

gotoxy(x_cord,14);

cout<<"³";

gotoxy(x_cord,16);

cout<<"³";

y_cord=17;

for(int count_6=0;count_6<n;count_6++)

{

gotoxy(x_cord,y_cord);

cout<<"³";

gotoxy(x_cord,(y_cord+1));

cout<<"³";

y_cord+=2;

}

gotoxy(x_cord,15);

cout<<"´";

gotoxy(x_cord,y_cord);

cout<<"Ù";

gotoxy(30,44);

cout<<"Press any key to continue...";

getch( );

}

/*************************************************************************//

/---------- compute_newtons_bdif(const long double,const int) --------

///*************************************************************************

/constlongdouble compute_newtons_bdif(constlongdouble x,constint n)

{

Page 126: Aplikasi menghitung matematika dengan c++

longdouble s=0;

longdouble pnx=0;

longdouble temp=0;

longdouble n_factorial=1;

int temp_n=::n;

s=((x-xn[(temp_n-1)])/h);

pnx=difference_table[0][(temp_n-1)];

for(int count_1=0;count_1<n;count_1++)

{

temp=s;

temp*=difference_table[(count_1+1)][(temp_n-1)];

temp/=n_factorial;

for(int count_2=0;count_2<count_1;count_2++)

temp*=(s+(count_2+1));

pnx+=temp;

n_factorial*=(count_1+2);

}

return pnx;

}

/*************************************************************************//

/---------------------------- estimate_pnx( ) ------------------------

///*************************************************************************

/void estimate_pnx( )

{

clear_screen( );

gotoxy(6,10);

cout<<"Estimation of Pn(x) :";

gotoxy(6,11);

cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ";

char Choice=NULL;

int n=0;

longdouble x=0;

longdouble fx=0;

longdouble pnx=0;

do

Page 127: Aplikasi menghitung matematika dengan c++

{

Choice=NULL;

n=0;

x=0;

fx=0;

pnx=0;

gotoxy(10,13);

cout<<"Enter the value of n = ";

cin>>n;

gotoxy(10,15);

cout<<"Enter the value of x = ";

cin>>x;

pnx=compute_newtons_bdif(x,n);

gotoxy(10,20);

cout<<"The estimated value of P"<<n<<"("<<x<<") = "<<pnx;

if(choice)

{

fx=evaluate_postfix_expression(x);

gotoxy(10,23);

cout<<"The Actual value of f"<<"("<<x<<") = "<<fx;

gotoxy(10,25);

cout<<"Absolute Error = E(abs) = "<<fabs((fx-pnx));

}

gotoxy(12,40);

cout<<"Press <Esc> to exit, 'V' to view Difference Table again";

gotoxy(25,42);

cout<<"or any other key to continue...";

Choice=getch( );

if(int(Choice)!=27 && Choice!='v' && Choice!='V')

{

gotoxy(10,13);

cout<<" ";

gotoxy(10,15);

cout<<" ";

Page 128: Aplikasi menghitung matematika dengan c++

gotoxy(10,20);

cout<<" ";

gotoxy(10,23);

cout<<" ";

gotoxy(10,25);

cout<<" ";

gotoxy(12,40);

cout<<" ";

gotoxy(25,42);

cout<<" ";

}

elseif(Choice=='v' || Choice=='V')

{

show_difference_table( );

estimate_pnx( );

}

elseif(int(Choice)==27)

exit(0);

}

while(1);

}