# include <iostream.h>
# include <string.h>
# include <stdlib.h>
# include <conio.h>
# include <ctype.h>
# include <math.h>
# include <dos.h>
constint max_size=30;
constint table_size=12;
int top=-1;
int n=0;
int order=0;
int input_choice=0;
longdouble x0=0;
longdouble difference=0;
longdouble x_values[table_size]={0};
longdouble difference_table[table_size][table_size]={0};
char Non_linear_equation[100]={NULL};
char Stack[max_size][max_size]={NULL};
char Postfix_expression[max_size][max_size]={NULL};
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_mode( );
void get_input_non_linear_equation( );
void get_input_table( );
void construct_difference_table( );
void show_difference_table( );
void compute_and_display_difference_operators( );
int main( )
{
clrscr( );
textmode(C4350);
show_screen( );
get_input_mode( );
construct_difference_table( );
show_difference_table( );
compute_and_display_difference_operators( );
return 0;
}
/*************************************************************************///-------------------------- 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--;
}
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)");
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;
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( ));
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( ));
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;
}
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;
}
/*************************************************************************///-------------------------- show_screen( ) ---------------------------///*************************************************************************/void show_screen( )
{
cprintf("\n********************************************************************************");
cprintf("****************************- -****************************");
cprintf("*---------------------------- ");
textbackground(1);
cprintf(" Finite-Differences ");
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);
}
/*************************************************************************///------------------------- get_input_mode( ) -------------------------///*************************************************************************/void get_input_mode( )
{
gotoxy(4,11);
cout<<"Input Mode :";
gotoxy(4,12);
cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍ";
gotoxy(8,15);
cout<<"a)- Press `1' to enter a function f(x).";
gotoxy(8,18);
cout<<"b)- Press `2' to enter values of the function.";
do
{
gotoxy(8,25);
cout<<"Enter your choice = ";
gotoxy(28,25);
input_choice=(int(getche( ))-48);
if(input_choice!=1 && input_choice!=2)
{
sound(2500);
delay(500);
nosound( );
}
}
while(input_choice!=1 && input_choice!=2);
if(input_choice==1)
get_input_non_linear_equation( );
elseif(input_choice==2)
get_input_table( );
gotoxy(1,2);
}
/*************************************************************************///------------------ get_input_non_linear_equation( ) -----------------///*************************************************************************/void get_input_non_linear_equation( )
{
clear_screen( );
gotoxy(4,11);
cout<<"Non-Linear Function with One Variable:";
gotoxy(4,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(4,15);
cout<<"Enter the Function = f(x) = ";
cin>>Non_linear_equation;
gotoxy(4,20);
cout<<"Enter the initial value of x = x0 = ";
cin>>x0;
gotoxy(4,23);
cout<<"Enter the interval b/w two points = ";
cin>>difference;
gotoxy(4,28);
cout<<" [ Max. n = 12 ] ";
gotoxy(4,26);
cout<<"Enter the total number of values = n = ";
cin>>n;
if(n>12)
n=12;
convert_infix_expression_to_postfix_expression(Non_linear_equation);
longdouble x=x0;
for(int count_1=0;count_1<n;count_1++)
{
difference_table[0][count_1]=evaluate_postfix_expression(x);
x_values[count_1]=x;
x+=difference;
}
gotoxy(1,2);
}
/*************************************************************************///-------------------------- get_input_table( ) -----------------------///*************************************************************************/void get_input_table( )
{
clear_screen( );
gotoxy(4,9);
cout<<"Table of Values for Function :";
gotoxy(4,10);
cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ";
gotoxy(4,14);
cout<<" [ Max. n = 12 ]";
gotoxy(4,12);
cout<<"Enter the total number of values = n = ";
cin>>n;
if(n>12)
n=12;
gotoxy(4,16);
cout<<"Enter the initial value of x = x0 = ";
cin>>x0;
gotoxy(4,18);
cout<<"Enter the interval b/w two points = ";
cin>>difference;
double x=x0;
for(int count_1=0;count_1<n;count_1++)
{
x_values[count_1]=x;
x+=difference;
}
int x_cord=45;
int y_cord=19;
gotoxy(x_cord,(y_cord-4));
cout<<"ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿";
gotoxy(x_cord,(y_cord-3));
cout<<"³ Xi ³ f(Xi) ³";
gotoxy(x_cord,(y_cord-2));
cout<<"ÃÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ´";
gotoxy(x_cord,(y_cord-1));
cout<<"³ ³ ³";
gotoxy(x_cord,y_cord);
cout<<"³ ³ ³";
for(int count_2=0;count_2<n;count_2++)
{
gotoxy(x_cord,(y_cord+1));
cout<<"³ ³ ³";
gotoxy(x_cord,(y_cord+2));
cout<<"³ ³ ³";
y_cord+=2;
}
gotoxy(x_cord,y_cord);
cout<<"ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÁÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ";
y_cord=19;
for(int count_3=0;count_3<n;count_3++)
{
gotoxy((x_cord+2),y_cord);
cout<<x_values[count_3];
gotoxy((x_cord+18),y_cord);
cin>>difference_table[0][count_3];
y_cord+=2;
}
gotoxy(1,2);
}
/*************************************************************************///------------------- construct_difference_table( ) -------------------///*************************************************************************/void construct_difference_table( )
{
int flag=0;
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;
}
flag=1;
for(int count_3=count_1;count_3<n;count_3++)
{
if(difference_table[count_1][count_3]!=0)
{
flag=0;
break;
}
}
if(flag)
{
order=count_1;
break;
}
}
if(count_1==n)
order=(n-1);
}
/*************************************************************************///---------------------- 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<<x_values[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+=20;
int count_2=0;
for(int count_3=1;count_3<=order;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;
}
x_cord+=16;
count_2++;
if((count_2%3)==0 && count_3<order)
{
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=24;
for(int count_6=0;count_6<=(n+2);count_6++)
{
gotoxy(x_cord,y_cord);
cout<<" ";
gotoxy(x_cord,(y_cord+1));
cout<<" ";
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_and_display_difference_operators( ) -------------///*************************************************************************/void compute_and_display_difference_operators( )
{
clear_screen( );
gotoxy(6,10);
cout<<"Difference Operators :";
gotoxy(6,11);
cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ";
gotoxy(10,13);
cout<<"E = Shift Operator";
gotoxy(10,15);
cout<<"D = Forward Difference Operator";
gotoxy(10,17);
cout<<"d = Backward Difference Operator";
gotoxy(6,20);
cout<<"Examples :";
gotoxy(6,21);
cout<<"ÍÍÍÍÍÍÍÍÍÍ";
gotoxy(10,23);
cout<<"Shift Operator : Ef2 , E3f4 , E3f1";
gotoxy(10,25);
cout<<"Forward Difference Operator : Df3 , D2f1 , D3f0";
gotoxy(10,27);
cout<<"Backward Difference Operator : df3 , d2f3 , d4f4";
gotoxy(6,44);
cout<<"Note : Enter `0' to exit, `V' to view Difference Table again.";
gotoxy(6,32);
cout<<"Calculate ?";
gotoxy(6,33);
cout<<"ÍÍÍÍÍÍÍÍÍÍÍ";
char Operator[10]={NULL};
longdoublevalue=0;
do
{
strset(Operator,NULL);
value=0;
gotoxy(10,35);
cout<<" ";
gotoxy(10,37);
cout<<" ";
gotoxy(10,40);
cout<<" ";
gotoxy(10,35);
cout<<"Operator = ";
cin>>Operator;
if(strcmp(Operator,"v")==0 || strcmp(Operator,"V")==0)
{
show_difference_table( );
compute_and_display_difference_operators( );
}
elseif(strcmp(Operator,"0")!=0)
{
if(Operator[0]=='E' || Operator[0]=='D' ||
Operator[0]=='d')
{
int flag=0;
if(strchr(Operator,'f')==NULL)
flag=1;
if(Operator[0]=='E' && !flag)
{
int fn=1;
char Temp[10]={NULL};
if(isdigit(Operator[1]))
{
strcpy(Temp,strtok(Operator,"E"));
strcpy(Temp,strtok(Temp,"f"));
}
fn=atoi(Temp);
if(strcmp(Temp,NULL)==0)
fn=1;
strset(Temp,NULL);
strtok(Operator,"f");
strcpy(Temp,strtok(NULL,NULL));
fn+=atoi(Temp);
if(fn>(n-1) || fn<0)
flag=1;
value=difference_table[0][fn];
}
elseif(Operator[0]=='D' && !flag)
{
int D=1;
int f=0;
char Temp[10]={NULL};
if(isdigit(Operator[1]))
{
strcpy(Temp,strtok(Operator,"D"));
strcpy(Temp,strtok(Temp,"f"));
}
D=atoi(Temp);
if(strcmp(Temp,NULL)==0)
D=1;
strset(Temp,NULL);
strtok(Operator,"f");
strcpy(Temp,strtok(NULL,NULL));
f=atoi(Temp);
if(D>(n-1) || D<=0 || (D+f)>(n-1) || D<0)
flag=1;
value=difference_table[D][(D+f)];
}
elseif(Operator[0]=='d' && !flag)
{
int d=1;
int f=0;
char Temp[10]={NULL};
if(isdigit(Operator[1]))
{
strcpy(Temp,strtok(Operator,"d"));
strcpy(Temp,strtok(Temp,"f"));
}
d=atoi(Temp);
if(strcmp(Temp,NULL)==0)
d=1;
strset(Temp,NULL);
strtok(Operator,"f");
strcpy(Temp,strtok(NULL,NULL));
f=atoi(Temp);
if(d>(n-1) || d<=0 || f<d || f>(n-1))
flag=1;
value=difference_table[d][f];
}
if(!flag)
{
gotoxy(10,37);
cout<<"Value = "<<value;
gotoxy(25,40);
cout<<"Press any key to continue...";
}
else
{
gotoxy(25,40);
cout<<"Wrong Values. Try again...";
}
}
else
{
gotoxy(25,40);
cout<<"Wrong Operator. Try again...";
}
getch( );
}
elseif(strcmp(Operator,"0")==0)
{
gotoxy(1,2);
delay(500);
exit(0);
}
}
while(1);
}