SlideShare ist ein Scribd-Unternehmen logo
1 von 21
/* Write C program to implement Trapezoidal method.*/



#include<stdio.h>

#include<conio.h>

#include<math.h>



char postfix[80];

float stack[80];

char stack1[80];

int top=-1,top1=-1;



float eval(char postfix[], float x1);

void infix_postfix(char infix[]);



main()

{

float x0, xn, h, s,e1,e2;

char exp[80], arr[80];

int i,n,l=0;

clrscr();

printf("nEnter an expression: ");

gets(exp);

puts("Enter x0, xn and number of subintervals");

scanf("%f%f%d", &x0, &xn, &n);
h=(xn-x0)/n;

if(exp[0]=='l'&& exp[1]=='o'&& exp[2]=='g')

{

l=strlen(exp);

for(i=0;i<l-3; i++)

    arr[0]=exp[i+3];

arr[i]='0';

infix_postfix(arr);

e1=eval(postfix,x0);

e2=eval(postfix,xn);

s=log(e1)+log(e2);

for (i=1;i<=n-1;i++)

s+=2*log(eval(postfix,x0+i*h));

}

else

{

infix_postfix(exp);

s=eval(postfix,x0)+eval(postfix,xn);

for (i=1;i<=n-1;i++)

s+=2*eval(postfix,x0+i*h);

}

printf("Value of the integral is %6.3fn",(h/2)*s);

return(0);

}
/*Inserting the operands in a stack. */

void push(float item)

{

if(top==99)

{

printf("ntThe stack is full");

getch();

exit(0);

}

else

{

top++;

stack[top]=item;

}

return;

}

/*Removing the operands from a stack. */

float pop()

{

float item;

if(top==-1)

{

    printf("ntThe stack is emptynt");

    getch();
}

item=stack[top];

top--;

return (item);

}

void push1(char item)

{

if(top1==79)

{

    printf("ntThe stack is full");

    getch();

    exit(0);

}

else

{

top1++;

stack1[top1]=item;

}

return;

}

/*Removing the operands from a stack. */

char pop1()

{

char item;
if(top1==-1)

{

printf("ntThe stack1 is emptynt");

getch();

}

item=stack1[top1];

top1--;

return (item);

}



/*Converting an infix expression to a postfix expression. */

void infix_postfix(char infix[])

{

int i=0,j=0,k;

char ch;

char token;

for(i=0;i<79;i++)

postfix[i]=' ';

push1('?');

i=0;

token=infix[i];

while(token!='0')

{

if(isalnum(token))
{

postfix[j]=token;

j++;

}

else if(token=='(')

{

push1('(');

}

else if(token==')')

{

while(stack1[top1]!='(')

{

    ch=pop1();

    postfix[j]=ch;

    j++;

}

ch=pop1();

}

else

{



while(ISPriority(stack1[top1])>=ICP(token))

{

    ch=pop1();
/*Assigning the popped element into the postfix array. */

    postfix[j]=ch;

    j++;

}

push1(token);

}

i++;

token=infix[i];

}

while(top1!=0)

{

ch=pop1();

postfix[j]=ch;

j++;

}

postfix[j]='0';

}



int ISPriority(char token)

{

switch(token)

{

case '(':return (0);

case ')':return (9);
case '+':return (7);

case '-':return (7);

case '*':return (8);

case '/':return (8);

case '?':return (0);

default: printf("Invalid expression");

break;

}

return 0;

}

/*Determining the priority of elements that are approaching towards the stack. */

int ICP(char token)

{

switch(token)

{

    case '(':return (10);

    case ')':return (9);

    case '+':return (7);

    case '-':return (7);

    case '*':return (8);

    case '/':return (8);

    case '0':return (0);

    default: printf("Invalid expression");

    break;
}

return 0;

}

/*Calculating the result of expression, which is converted in postfix notation. */

float eval(char p[], float x1)

{

float t1,t2,k,r;

int i=0,l;

l=strlen(p);

while(i<l)

{

if(p[i]=='x')

push(x1);

else

if(isdigit(p[i]))

{

k=p[i]-'0';

push(k);

}

else

{

t1=pop();

t2=pop();

switch(p[i])
{

case '+':k=t2+t1;

break;

case '-':k=t2-t1;

break;

case '*':k=t2*t1;

break;

case '/':k=t2/t1;

break;

default: printf("ntInvalid expression");

break;

}

push(k);

}

i++;

}

if(top>0)

{

printf("You have entered the operands more than the operators");

exit(0);

}

else

{

r=pop();
return (r);

}

return 0;

}




/* Write C program to implement Simpson method. */



#include<stdio.h>

#include<conio.h>

#include<math.h>



char postfix[80];

float stack[80];

char stack1[80];

int top=-1,top1=-1;

float eval(char postfix[], float x1);

void infix_postfix(char infix[]);



main()
{

float x0, xn, h, s,e1,e2, e3;

char exp[80], arr[80];

int i,n,l=0;

clrscr();

printf("nEnter an expression: ");

gets(exp);

puts("Enter x0, xn and number of sub-intervals: ");

scanf("%f%f%d", &x0, &xn, &n);

h=(xn-x0)/n;

if(exp[0]=='l'&& exp[1]=='o'&& exp[2]=='g')

{

    l=strlen(exp);

    for(i=0;i<l-3; i++)

    arr[0]=exp[i+3];

    arr[i]='0';

    infix_postfix(arr);

    e1=eval(postfix,x0);

    e2=eval(postfix,xn);

    e3=4*eval(postfix, x0+h);

    s=log(e1)+log(e2)+log(e3);

    for (i=3;i<=n-1;i+=2)

      s+=4*eval(postfix,x0+i*h)+2*eval(postfix, x0+(i-1)*h);

}
else

{

    infix_postfix(exp);

    s=eval(postfix,x0)+eval(postfix,xn)+4*eval(postfix, x0+h);

    for (i=3;i<=n-1;i+=2)

    s+=4*eval(postfix,x0+i*h)+2*eval(postfix, x0+(i-1)*h);

}

printf("The value of integral is %6.3fn",(h/3)*s);

return(0);

}

/*Inserting the operands in a stack. */

void push(float item)

{

if(top==99)

{

    printf("ntThe stack is full");

    getch();

    exit(0);

}

else

{

    top++;

    stack[top]=item;

}
return;

}

/*Removing the operands from a stack. */

float pop()

{

    float item;

    if(top==-1)

{

printf("ntThe stack is emptynt");

getch();

}

item=stack[top];

top--;

return (item);

}

void push1(char item)

{

if(top1==79)

{

    printf("ntThe stack is full");

    getch();

    exit(0);

}

else
{

    top1++;

    stack1[top1]=item;

}

return;

}

/*Removing the operands from a stack. */

char pop1()

{

char item;

if(top1==-1)

{

    printf("ntThe stack1 is emptynt");

    getch();

}

item=stack1[top1];

top1--;

return (item);

}

/*Converting an infix expression to a postfix expression. */

void infix_postfix(char infix[])

{

int i=0,j=0,k;

char ch;
char token;

for(i=0;i<79;i++)

postfix[i]=' ';

push1('?');

i=0;

token=infix[i];

while(token!='0')

{

    if(isalnum(token))

    {

        postfix[j]=token;

        j++;

    }

    else if(token=='(')

    {

        push1('(');

    }

    else if(token==')')

    {

    while(stack1[top1]!='(')

    {

        ch=pop1();

        postfix[j]=ch;

        j++;
}

    ch=pop1();

    }

else

{

    while(ISPriority(stack1[top1])>=ICP(token))

    {

    ch=pop1();

    postfix[j]=ch;

    j++;

    }

push1(token);

}

i++;

token=infix[i];

}

while(top1!=0)

{

ch=pop1();

postfix[j]=ch;

j++;

}

postfix[j]='0';

}
/*Determining the priority of elements that are placed inside the stack. */

int ISPriority(char token)

{

switch(token)

{

    case '(':return (0);

    case ')':return (9);

    case '+':return (7);

    case '-':return (7);

    case '*':return (8);

    case '/':return (8);

    case '?':return (0);

    default: printf("Invalid expression");

}

return 0;

}



/*Determining the priority of elements that are approaching towards the stack. */

int ICP(char token)

{

switch(token)

{

    case '(':return (10);
case ')':return (9);

    case '+':return (7);

    case '-':return (7);

    case '*':return (8);

    case '/':return (8);

    case '0':return (0);

    default: printf("Invalid expression");

    }

return 0;

}

/*Calculating the result of expression, which is converted in postfix notation. */

float eval(char p[], float x1)

{

float t1,t2,k,r;

int i=0,l;

l=strlen(p);

while(i<l)

{

    if(p[i]=='x')

    push(x1);

    else

    if(isdigit(p[i]))

        {

        k=p[i]-'0';
push(k);

        }

else

{

    t1=pop();

    t2=pop();

    switch(p[i])

    {

    case '+':k=t2+t1;

    break;

    case '-':k=t2-t1;

    break;

    case '*':k=t2*t1;

    break;

    case '/':k=t2/t1;

    break;

    default: printf("ntInvalid expression");

    }

push(k);

}

i++;

}

if(top>0)

{
printf("You have entered the operands more than the operators");

    exit(0);

}

else

{

    r=pop();

    return (r);

}

return 0;

}

Weitere ähnliche Inhalte

Was ist angesagt?

Javascript i wydajność - czy to się spina?
Javascript i wydajność - czy to się spina?Javascript i wydajność - czy to się spina?
Javascript i wydajność - czy to się spina?Michał Kostrzyński
 
Absolute Loader
Absolute LoaderAbsolute Loader
Absolute Loaderksanthosh
 
(Meta 4) ejemplo calcular la mitad de un numero dev c++
(Meta 4) ejemplo calcular la mitad de un numero dev c++ (Meta 4) ejemplo calcular la mitad de un numero dev c++
(Meta 4) ejemplo calcular la mitad de un numero dev c++ Eli Diaz
 
المحاضره 6 & 7 c#
المحاضره  6   & 7 c#المحاضره  6   & 7 c#
المحاضره 6 & 7 c#nermeenelhamy1
 
Metodos Numericos(Segundo Taller De Aplicadas)
Metodos Numericos(Segundo Taller De Aplicadas)Metodos Numericos(Segundo Taller De Aplicadas)
Metodos Numericos(Segundo Taller De Aplicadas)guest1bb7f49
 
Luis cuñas programacion
Luis cuñas programacionLuis cuñas programacion
Luis cuñas programacionluisitofranklin
 
Program to remove Left factoring
Program to remove Left factoringProgram to remove Left factoring
Program to remove Left factoringShraddha Patel
 
Как помочь и как помешать компилятору. Андрей Олейников ➠ CoreHard Autumn 2019
Как помочь и как помешать компилятору. Андрей Олейников ➠  CoreHard Autumn 2019Как помочь и как помешать компилятору. Андрей Олейников ➠  CoreHard Autumn 2019
Как помочь и как помешать компилятору. Андрей Олейников ➠ CoreHard Autumn 2019corehard_by
 
An introduction to functional programming with Go [redux]
An introduction to functional programming with Go [redux]An introduction to functional programming with Go [redux]
An introduction to functional programming with Go [redux]Eleanor McHugh
 
openFrameworks 動きを生みだす様々なアルゴリズム - 多摩美メディアアートII
openFrameworks 動きを生みだす様々なアルゴリズム - 多摩美メディアアートIIopenFrameworks 動きを生みだす様々なアルゴリズム - 多摩美メディアアートII
openFrameworks 動きを生みだす様々なアルゴリズム - 多摩美メディアアートIIAtsushi Tadokoro
 

Was ist angesagt? (20)

Javascript i wydajność - czy to się spina?
Javascript i wydajność - czy to się spina?Javascript i wydajność - czy to się spina?
Javascript i wydajność - czy to się spina?
 
Rafael vasquez
Rafael vasquezRafael vasquez
Rafael vasquez
 
Absolute Loader
Absolute LoaderAbsolute Loader
Absolute Loader
 
Sbaw090630
Sbaw090630Sbaw090630
Sbaw090630
 
Ngon ngu lap trinh
Ngon ngu lap trinhNgon ngu lap trinh
Ngon ngu lap trinh
 
Taller de string(java)
Taller de string(java)Taller de string(java)
Taller de string(java)
 
(Meta 4) ejemplo calcular la mitad de un numero dev c++
(Meta 4) ejemplo calcular la mitad de un numero dev c++ (Meta 4) ejemplo calcular la mitad de un numero dev c++
(Meta 4) ejemplo calcular la mitad de un numero dev c++
 
All set1
All set1All set1
All set1
 
المحاضره 6 & 7 c#
المحاضره  6   & 7 c#المحاضره  6   & 7 c#
المحاضره 6 & 7 c#
 
Metodos Numericos(Segundo Taller De Aplicadas)
Metodos Numericos(Segundo Taller De Aplicadas)Metodos Numericos(Segundo Taller De Aplicadas)
Metodos Numericos(Segundo Taller De Aplicadas)
 
Bt c cpp_0021
Bt c cpp_0021Bt c cpp_0021
Bt c cpp_0021
 
Programs
ProgramsPrograms
Programs
 
Simulacion - Algoritmo congruencial cuadratico
Simulacion - Algoritmo congruencial cuadraticoSimulacion - Algoritmo congruencial cuadratico
Simulacion - Algoritmo congruencial cuadratico
 
Programming in C
Programming in CProgramming in C
Programming in C
 
Luis cuñas programacion
Luis cuñas programacionLuis cuñas programacion
Luis cuñas programacion
 
Program to remove Left factoring
Program to remove Left factoringProgram to remove Left factoring
Program to remove Left factoring
 
Как помочь и как помешать компилятору. Андрей Олейников ➠ CoreHard Autumn 2019
Как помочь и как помешать компилятору. Андрей Олейников ➠  CoreHard Autumn 2019Как помочь и как помешать компилятору. Андрей Олейников ➠  CoreHard Autumn 2019
Как помочь и как помешать компилятору. Андрей Олейников ➠ CoreHard Autumn 2019
 
An introduction to functional programming with Go [redux]
An introduction to functional programming with Go [redux]An introduction to functional programming with Go [redux]
An introduction to functional programming with Go [redux]
 
Dasar c
Dasar cDasar c
Dasar c
 
openFrameworks 動きを生みだす様々なアルゴリズム - 多摩美メディアアートII
openFrameworks 動きを生みだす様々なアルゴリズム - 多摩美メディアアートIIopenFrameworks 動きを生みだす様々なアルゴリズム - 多摩美メディアアートII
openFrameworks 動きを生みだす様々なアルゴリズム - 多摩美メディアアートII
 

Andere mochten auch (9)

Which is not a step in the problem
Which is not a step in the problemWhich is not a step in the problem
Which is not a step in the problem
 
ch14
ch14ch14
ch14
 
week-10x
week-10xweek-10x
week-10x
 
week-1x
week-1xweek-1x
week-1x
 
week-18x
week-18xweek-18x
week-18x
 
week-11x
week-11xweek-11x
week-11x
 
BrainFingerprintingpresentation
BrainFingerprintingpresentationBrainFingerprintingpresentation
BrainFingerprintingpresentation
 
ch6
ch6ch6
ch6
 
DISTRIBUTED INTERACTIVE VIRTUAL ENVIRONMENT
DISTRIBUTED INTERACTIVE VIRTUAL ENVIRONMENTDISTRIBUTED INTERACTIVE VIRTUAL ENVIRONMENT
DISTRIBUTED INTERACTIVE VIRTUAL ENVIRONMENT
 

Mehr von KITE www.kitecolleges.com (20)

PPT (2)
PPT (2)PPT (2)
PPT (2)
 
ch16
ch16ch16
ch16
 
holographic versatile disc
holographic versatile discholographic versatile disc
holographic versatile disc
 
week-22x
week-22xweek-22x
week-22x
 
week-16x
week-16xweek-16x
week-16x
 
week-5x
week-5xweek-5x
week-5x
 
week-6x
week-6xweek-6x
week-6x
 
week-3x
week-3xweek-3x
week-3x
 
ch8
ch8ch8
ch8
 
Intro Expert Systems test-me.co.uk
Intro Expert Systems test-me.co.ukIntro Expert Systems test-me.co.uk
Intro Expert Systems test-me.co.uk
 
ch17
ch17ch17
ch17
 
ch4
ch4ch4
ch4
 
week-7x
week-7xweek-7x
week-7x
 
week-9x
week-9xweek-9x
week-9x
 
week-4x
week-4xweek-4x
week-4x
 
week-14x
week-14xweek-14x
week-14x
 
AIRBORNE
AIRBORNEAIRBORNE
AIRBORNE
 
ch2
ch2ch2
ch2
 
week-23x
week-23xweek-23x
week-23x
 
week-2x
week-2xweek-2x
week-2x
 

Kürzlich hochgeladen

Bahare Shariat Jild 3 By SadurshSharia Mufti Amjad Ali Azmi
Bahare Shariat Jild 3 By SadurshSharia Mufti Amjad Ali AzmiBahare Shariat Jild 3 By SadurshSharia Mufti Amjad Ali Azmi
Bahare Shariat Jild 3 By SadurshSharia Mufti Amjad Ali Azmibookbahareshariat
 
محاضرات الاحصاء التطبيقي لطلاب علوم الرياضة.pdf
محاضرات الاحصاء التطبيقي لطلاب علوم الرياضة.pdfمحاضرات الاحصاء التطبيقي لطلاب علوم الرياضة.pdf
محاضرات الاحصاء التطبيقي لطلاب علوم الرياضة.pdfKhaled Elbattawy
 
Bahare Shariat Jild 2 By SadurshSharia Mufti Amjad Ali Azmi
Bahare Shariat Jild 2 By SadurshSharia Mufti Amjad Ali AzmiBahare Shariat Jild 2 By SadurshSharia Mufti Amjad Ali Azmi
Bahare Shariat Jild 2 By SadurshSharia Mufti Amjad Ali Azmibookbahareshariat
 
Saunanaine_Helen Moppel_JUHENDATUD SAUNATEENUSE JA LOODUSMATKA SÜNERGIA_strat...
Saunanaine_Helen Moppel_JUHENDATUD SAUNATEENUSE JA LOODUSMATKA SÜNERGIA_strat...Saunanaine_Helen Moppel_JUHENDATUD SAUNATEENUSE JA LOODUSMATKA SÜNERGIA_strat...
Saunanaine_Helen Moppel_JUHENDATUD SAUNATEENUSE JA LOODUSMATKA SÜNERGIA_strat...Eesti Loodusturism
 
Bahare Shariat Jild 4 By SadurshSharia Mufti Amjad Ali Azmi
Bahare Shariat Jild 4 By SadurshSharia Mufti Amjad Ali AzmiBahare Shariat Jild 4 By SadurshSharia Mufti Amjad Ali Azmi
Bahare Shariat Jild 4 By SadurshSharia Mufti Amjad Ali Azmibookbahareshariat
 
Bahare Shariat Jild 1 By SadurshSharia Mufti Amjad Ali Azmi
Bahare Shariat Jild 1 By SadurshSharia Mufti Amjad Ali AzmiBahare Shariat Jild 1 By SadurshSharia Mufti Amjad Ali Azmi
Bahare Shariat Jild 1 By SadurshSharia Mufti Amjad Ali Azmibookbahareshariat
 
FAIL REKOD PENGAJARAN.pptx fail rekod pengajaran
FAIL REKOD PENGAJARAN.pptx fail rekod pengajaranFAIL REKOD PENGAJARAN.pptx fail rekod pengajaran
FAIL REKOD PENGAJARAN.pptx fail rekod pengajaransekolah233
 

Kürzlich hochgeladen (8)

Energy drink .
Energy drink                           .Energy drink                           .
Energy drink .
 
Bahare Shariat Jild 3 By SadurshSharia Mufti Amjad Ali Azmi
Bahare Shariat Jild 3 By SadurshSharia Mufti Amjad Ali AzmiBahare Shariat Jild 3 By SadurshSharia Mufti Amjad Ali Azmi
Bahare Shariat Jild 3 By SadurshSharia Mufti Amjad Ali Azmi
 
محاضرات الاحصاء التطبيقي لطلاب علوم الرياضة.pdf
محاضرات الاحصاء التطبيقي لطلاب علوم الرياضة.pdfمحاضرات الاحصاء التطبيقي لطلاب علوم الرياضة.pdf
محاضرات الاحصاء التطبيقي لطلاب علوم الرياضة.pdf
 
Bahare Shariat Jild 2 By SadurshSharia Mufti Amjad Ali Azmi
Bahare Shariat Jild 2 By SadurshSharia Mufti Amjad Ali AzmiBahare Shariat Jild 2 By SadurshSharia Mufti Amjad Ali Azmi
Bahare Shariat Jild 2 By SadurshSharia Mufti Amjad Ali Azmi
 
Saunanaine_Helen Moppel_JUHENDATUD SAUNATEENUSE JA LOODUSMATKA SÜNERGIA_strat...
Saunanaine_Helen Moppel_JUHENDATUD SAUNATEENUSE JA LOODUSMATKA SÜNERGIA_strat...Saunanaine_Helen Moppel_JUHENDATUD SAUNATEENUSE JA LOODUSMATKA SÜNERGIA_strat...
Saunanaine_Helen Moppel_JUHENDATUD SAUNATEENUSE JA LOODUSMATKA SÜNERGIA_strat...
 
Bahare Shariat Jild 4 By SadurshSharia Mufti Amjad Ali Azmi
Bahare Shariat Jild 4 By SadurshSharia Mufti Amjad Ali AzmiBahare Shariat Jild 4 By SadurshSharia Mufti Amjad Ali Azmi
Bahare Shariat Jild 4 By SadurshSharia Mufti Amjad Ali Azmi
 
Bahare Shariat Jild 1 By SadurshSharia Mufti Amjad Ali Azmi
Bahare Shariat Jild 1 By SadurshSharia Mufti Amjad Ali AzmiBahare Shariat Jild 1 By SadurshSharia Mufti Amjad Ali Azmi
Bahare Shariat Jild 1 By SadurshSharia Mufti Amjad Ali Azmi
 
FAIL REKOD PENGAJARAN.pptx fail rekod pengajaran
FAIL REKOD PENGAJARAN.pptx fail rekod pengajaranFAIL REKOD PENGAJARAN.pptx fail rekod pengajaran
FAIL REKOD PENGAJARAN.pptx fail rekod pengajaran
 

week-24x

  • 1. /* Write C program to implement Trapezoidal method.*/ #include<stdio.h> #include<conio.h> #include<math.h> char postfix[80]; float stack[80]; char stack1[80]; int top=-1,top1=-1; float eval(char postfix[], float x1); void infix_postfix(char infix[]); main() { float x0, xn, h, s,e1,e2; char exp[80], arr[80]; int i,n,l=0; clrscr(); printf("nEnter an expression: "); gets(exp); puts("Enter x0, xn and number of subintervals"); scanf("%f%f%d", &x0, &xn, &n);
  • 2. h=(xn-x0)/n; if(exp[0]=='l'&& exp[1]=='o'&& exp[2]=='g') { l=strlen(exp); for(i=0;i<l-3; i++) arr[0]=exp[i+3]; arr[i]='0'; infix_postfix(arr); e1=eval(postfix,x0); e2=eval(postfix,xn); s=log(e1)+log(e2); for (i=1;i<=n-1;i++) s+=2*log(eval(postfix,x0+i*h)); } else { infix_postfix(exp); s=eval(postfix,x0)+eval(postfix,xn); for (i=1;i<=n-1;i++) s+=2*eval(postfix,x0+i*h); } printf("Value of the integral is %6.3fn",(h/2)*s); return(0); }
  • 3. /*Inserting the operands in a stack. */ void push(float item) { if(top==99) { printf("ntThe stack is full"); getch(); exit(0); } else { top++; stack[top]=item; } return; } /*Removing the operands from a stack. */ float pop() { float item; if(top==-1) { printf("ntThe stack is emptynt"); getch();
  • 4. } item=stack[top]; top--; return (item); } void push1(char item) { if(top1==79) { printf("ntThe stack is full"); getch(); exit(0); } else { top1++; stack1[top1]=item; } return; } /*Removing the operands from a stack. */ char pop1() { char item;
  • 5. if(top1==-1) { printf("ntThe stack1 is emptynt"); getch(); } item=stack1[top1]; top1--; return (item); } /*Converting an infix expression to a postfix expression. */ void infix_postfix(char infix[]) { int i=0,j=0,k; char ch; char token; for(i=0;i<79;i++) postfix[i]=' '; push1('?'); i=0; token=infix[i]; while(token!='0') { if(isalnum(token))
  • 6. { postfix[j]=token; j++; } else if(token=='(') { push1('('); } else if(token==')') { while(stack1[top1]!='(') { ch=pop1(); postfix[j]=ch; j++; } ch=pop1(); } else { while(ISPriority(stack1[top1])>=ICP(token)) { ch=pop1();
  • 7. /*Assigning the popped element into the postfix array. */ postfix[j]=ch; j++; } push1(token); } i++; token=infix[i]; } while(top1!=0) { ch=pop1(); postfix[j]=ch; j++; } postfix[j]='0'; } int ISPriority(char token) { switch(token) { case '(':return (0); case ')':return (9);
  • 8. case '+':return (7); case '-':return (7); case '*':return (8); case '/':return (8); case '?':return (0); default: printf("Invalid expression"); break; } return 0; } /*Determining the priority of elements that are approaching towards the stack. */ int ICP(char token) { switch(token) { case '(':return (10); case ')':return (9); case '+':return (7); case '-':return (7); case '*':return (8); case '/':return (8); case '0':return (0); default: printf("Invalid expression"); break;
  • 9. } return 0; } /*Calculating the result of expression, which is converted in postfix notation. */ float eval(char p[], float x1) { float t1,t2,k,r; int i=0,l; l=strlen(p); while(i<l) { if(p[i]=='x') push(x1); else if(isdigit(p[i])) { k=p[i]-'0'; push(k); } else { t1=pop(); t2=pop(); switch(p[i])
  • 10. { case '+':k=t2+t1; break; case '-':k=t2-t1; break; case '*':k=t2*t1; break; case '/':k=t2/t1; break; default: printf("ntInvalid expression"); break; } push(k); } i++; } if(top>0) { printf("You have entered the operands more than the operators"); exit(0); } else { r=pop();
  • 11. return (r); } return 0; } /* Write C program to implement Simpson method. */ #include<stdio.h> #include<conio.h> #include<math.h> char postfix[80]; float stack[80]; char stack1[80]; int top=-1,top1=-1; float eval(char postfix[], float x1); void infix_postfix(char infix[]); main()
  • 12. { float x0, xn, h, s,e1,e2, e3; char exp[80], arr[80]; int i,n,l=0; clrscr(); printf("nEnter an expression: "); gets(exp); puts("Enter x0, xn and number of sub-intervals: "); scanf("%f%f%d", &x0, &xn, &n); h=(xn-x0)/n; if(exp[0]=='l'&& exp[1]=='o'&& exp[2]=='g') { l=strlen(exp); for(i=0;i<l-3; i++) arr[0]=exp[i+3]; arr[i]='0'; infix_postfix(arr); e1=eval(postfix,x0); e2=eval(postfix,xn); e3=4*eval(postfix, x0+h); s=log(e1)+log(e2)+log(e3); for (i=3;i<=n-1;i+=2) s+=4*eval(postfix,x0+i*h)+2*eval(postfix, x0+(i-1)*h); }
  • 13. else { infix_postfix(exp); s=eval(postfix,x0)+eval(postfix,xn)+4*eval(postfix, x0+h); for (i=3;i<=n-1;i+=2) s+=4*eval(postfix,x0+i*h)+2*eval(postfix, x0+(i-1)*h); } printf("The value of integral is %6.3fn",(h/3)*s); return(0); } /*Inserting the operands in a stack. */ void push(float item) { if(top==99) { printf("ntThe stack is full"); getch(); exit(0); } else { top++; stack[top]=item; }
  • 14. return; } /*Removing the operands from a stack. */ float pop() { float item; if(top==-1) { printf("ntThe stack is emptynt"); getch(); } item=stack[top]; top--; return (item); } void push1(char item) { if(top1==79) { printf("ntThe stack is full"); getch(); exit(0); } else
  • 15. { top1++; stack1[top1]=item; } return; } /*Removing the operands from a stack. */ char pop1() { char item; if(top1==-1) { printf("ntThe stack1 is emptynt"); getch(); } item=stack1[top1]; top1--; return (item); } /*Converting an infix expression to a postfix expression. */ void infix_postfix(char infix[]) { int i=0,j=0,k; char ch;
  • 16. char token; for(i=0;i<79;i++) postfix[i]=' '; push1('?'); i=0; token=infix[i]; while(token!='0') { if(isalnum(token)) { postfix[j]=token; j++; } else if(token=='(') { push1('('); } else if(token==')') { while(stack1[top1]!='(') { ch=pop1(); postfix[j]=ch; j++;
  • 17. } ch=pop1(); } else { while(ISPriority(stack1[top1])>=ICP(token)) { ch=pop1(); postfix[j]=ch; j++; } push1(token); } i++; token=infix[i]; } while(top1!=0) { ch=pop1(); postfix[j]=ch; j++; } postfix[j]='0'; }
  • 18. /*Determining the priority of elements that are placed inside the stack. */ int ISPriority(char token) { switch(token) { case '(':return (0); case ')':return (9); case '+':return (7); case '-':return (7); case '*':return (8); case '/':return (8); case '?':return (0); default: printf("Invalid expression"); } return 0; } /*Determining the priority of elements that are approaching towards the stack. */ int ICP(char token) { switch(token) { case '(':return (10);
  • 19. case ')':return (9); case '+':return (7); case '-':return (7); case '*':return (8); case '/':return (8); case '0':return (0); default: printf("Invalid expression"); } return 0; } /*Calculating the result of expression, which is converted in postfix notation. */ float eval(char p[], float x1) { float t1,t2,k,r; int i=0,l; l=strlen(p); while(i<l) { if(p[i]=='x') push(x1); else if(isdigit(p[i])) { k=p[i]-'0';
  • 20. push(k); } else { t1=pop(); t2=pop(); switch(p[i]) { case '+':k=t2+t1; break; case '-':k=t2-t1; break; case '*':k=t2*t1; break; case '/':k=t2/t1; break; default: printf("ntInvalid expression"); } push(k); } i++; } if(top>0) {
  • 21. printf("You have entered the operands more than the operators"); exit(0); } else { r=pop(); return (r); } return 0; }