SlideShare ist ein Scribd-Unternehmen logo
1 von 9
/* Write a C program that uses Stack operations to perform the following:

            i) Converting infix expression into postfix expression

            ii) Evaluating the postfix expression */



#include<stdio.h>

#include<conio.h>



int st[100];

int st_top=-1;



int cal(char post[]);

void in_post(char in[]);

void push_item(int it);

int pop_item();

int st_ISP(char t);

int st_ICP(char t);



/*main function*/

void main()

{

    char in[100],post[100];

    clrscr();

    printf("ntEnter the Infix Expression: ");

    gets(in);
in_post(in);

    getch();

}

/*end main*/



void push_item(int it)

{

    if(st_top==99)

    {

        printf("nnt*STACK is Full*");

        getch();

        exit(1);

    }

    st[++st_top]=it;

}



int pop_item()

{

    int it;

    if(st_top==-1)

    {

        getch();

    }

    return(st[st_top--]);
}



/*Function for converting an infix expression to a postfix expression. */

void in_post(char in[])

{

    int x=0,y=0,z,result=0;

    char a,c, post[100];

    char t;

    push_item('0');

    t=in[x];

    while(t!='0')

    {

        if(isalnum(t))

        /*For checking whether the value in t is an alphabet or number. */

        {

            post[y]=t;

            y++;

        }

        else if(t=='(')

        {

            push_item('(');

        }

        else if(t==')')

        {
while(st[st_top]!='(')

        {

             c=pop_item();

             post[y]=c;

             y++;

        }

    c=pop_item();

    }

    else

    {

        while(st_ISP(st[st_top])>=st_ICP(t))

        {

             c=pop_item();

             post[y]=c;

             y++;

        }

        push_item(t);

    }

    x++;

    t=in[x];

}



while(st_top!=-1)

{
c=pop_item();

        post[y]=c;

        y++;

    }

    printf("ntThe Postfix Expression is:");



    for(z=0;z<y;z++)

        printf("%c",post[z]);

    printf("nnDo you want to evaluate the Result of Postfix Expression?(Y/N):");

    scanf("%c",&a);

    if(a=='y' || a=='Y')

    {

        result=cal(post);

        printf("nntResult is: %dn",result);

        getch();

    }

    else if(a=='n' || a=='N')

    {

        exit(0);

    }

}



/*Determining priority of inside elements*/

int st_ISP(char t)
{

    switch(t)

    {

        case '(':return (10);

        case ')':return (9);

        case '+':return (7);

        case '-':return (7);

        case '*':return (8);

        case '/':return (8);

        case '0':return (0);

        default: printf("Expression is invalid.");

        break;

    }

    return 0;

}



/*Determining priority of approaching elements*/

int st_ICP(char t)

{

    switch(t)

    {



        case '(':return (10);

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

        case '-':return (7);

        case '*':return (8);

        case '/':return (8);

        case '0':return (0);

        default: printf("Expression is invalid.");

        break;

    }

    return 0;

}



/*Evaluating the result of postfix expression*/

int cal(char post[])

{

    int m,n,x,y,j=0,len;

    len=strlen(post);

    while(j<len)

    {

        if(isdigit(post[j]))

        {

            x=post[j]-'0';

            push_item(x);

        }

        else
{

        m=pop_item();

        n=pop_item();



        switch(post[j])

        {

             case '+':x=n+m;

             break;

             case '-':x=n-m;

             break;

             case '*':x=n*m;

             break;

             case '/':x=n/m;

             break;

        }

        push_item(x);

    }

    j++;

}

if(st_top>0)

{

    printf("Number of Operands are more than Operators.");

    exit(0);

}
else

    {

        y=pop_item();

        return (y);

    }

    return 0;

}

Weitere ähnliche Inhalte

Was ist angesagt? (20)

Function basics
Function basicsFunction basics
Function basics
 
week-1x
week-1xweek-1x
week-1x
 
week-16x
week-16xweek-16x
week-16x
 
week-6x
week-6xweek-6x
week-6x
 
C programming array & shorting
C  programming array & shortingC  programming array & shorting
C programming array & shorting
 
Circular linked list
Circular linked listCircular linked list
Circular linked list
 
Single linked list
Single linked listSingle linked list
Single linked list
 
week-4x
week-4xweek-4x
week-4x
 
C Programming Language Part 8
C Programming Language Part 8C Programming Language Part 8
C Programming Language Part 8
 
DataStructures notes
DataStructures notesDataStructures notes
DataStructures notes
 
Bcsl 033 data and file structures lab s1-4
Bcsl 033 data and file structures lab s1-4Bcsl 033 data and file structures lab s1-4
Bcsl 033 data and file structures lab s1-4
 
C Programming Language Part 9
C Programming Language Part 9C Programming Language Part 9
C Programming Language Part 9
 
ADA FILE
ADA FILEADA FILE
ADA FILE
 
c-programming-using-pointers
c-programming-using-pointersc-programming-using-pointers
c-programming-using-pointers
 
Circular queue
Circular queueCircular queue
Circular queue
 
Implementing stack
Implementing stackImplementing stack
Implementing stack
 
Intro to c programming
Intro to c programmingIntro to c programming
Intro to c programming
 
Inheritance and polymorphism
Inheritance and polymorphismInheritance and polymorphism
Inheritance and polymorphism
 
Recursion concepts by Divya
Recursion concepts by DivyaRecursion concepts by Divya
Recursion concepts by Divya
 
C Programming Language Part 11
C Programming Language Part 11C Programming Language Part 11
C Programming Language Part 11
 

Andere mochten auch

Andere mochten auch (14)

Deber henrry
Deber henrryDeber henrry
Deber henrry
 
Natalie Barnhart pd. 5
Natalie Barnhart pd. 5Natalie Barnhart pd. 5
Natalie Barnhart pd. 5
 
ePagine boekenbeurs presentatie 02/11/2009
ePagine boekenbeurs presentatie 02/11/2009ePagine boekenbeurs presentatie 02/11/2009
ePagine boekenbeurs presentatie 02/11/2009
 
Facebook
FacebookFacebook
Facebook
 
Eduvision - Proefles Cursus Joomla
Eduvision - Proefles Cursus JoomlaEduvision - Proefles Cursus Joomla
Eduvision - Proefles Cursus Joomla
 
Performing Tolerance Stack Ups Using Automated Excel Calculator By Arash Vakily
Performing Tolerance Stack Ups Using Automated Excel Calculator By Arash VakilyPerforming Tolerance Stack Ups Using Automated Excel Calculator By Arash Vakily
Performing Tolerance Stack Ups Using Automated Excel Calculator By Arash Vakily
 
BrainFingerprintingpresentation
BrainFingerprintingpresentationBrainFingerprintingpresentation
BrainFingerprintingpresentation
 
Compiler design lab programs
Compiler design lab programs Compiler design lab programs
Compiler design lab programs
 
DISTRIBUTED INTERACTIVE VIRTUAL ENVIRONMENT
DISTRIBUTED INTERACTIVE VIRTUAL ENVIRONMENTDISTRIBUTED INTERACTIVE VIRTUAL ENVIRONMENT
DISTRIBUTED INTERACTIVE VIRTUAL ENVIRONMENT
 
ch6
ch6ch6
ch6
 
PPT (2)
PPT (2)PPT (2)
PPT (2)
 
It4466 finale 2_kz2
It4466 finale 2_kz2It4466 finale 2_kz2
It4466 finale 2_kz2
 
An Impromptu Introduction to HTML5 Canvas
An Impromptu Introduction to HTML5 CanvasAn Impromptu Introduction to HTML5 Canvas
An Impromptu Introduction to HTML5 Canvas
 
Seq 3 v3 management motivation equipe deuxieme partie 75 diapos-marcel nizon
Seq 3 v3  management motivation equipe deuxieme partie 75 diapos-marcel nizonSeq 3 v3  management motivation equipe deuxieme partie 75 diapos-marcel nizon
Seq 3 v3 management motivation equipe deuxieme partie 75 diapos-marcel nizon
 

Ähnlich wie week-17x

#includeiostream#includecctypeusing namespace std;cl.docx
#includeiostream#includecctypeusing namespace std;cl.docx#includeiostream#includecctypeusing namespace std;cl.docx
#includeiostream#includecctypeusing namespace std;cl.docxkatherncarlyle
 
Sorting programs
Sorting programsSorting programs
Sorting programsVarun Garg
 
data structure and algorithm.pdf
data structure and algorithm.pdfdata structure and algorithm.pdf
data structure and algorithm.pdfAsrinath1
 
L25-L26-Parameter passing techniques.pptx
L25-L26-Parameter passing techniques.pptxL25-L26-Parameter passing techniques.pptx
L25-L26-Parameter passing techniques.pptxhappycocoman
 
Data Structures Using C Practical File
Data Structures Using C Practical File Data Structures Using C Practical File
Data Structures Using C Practical File Rahul Chugh
 
Paradigmas de Linguagens de Programacao - Aula #4
Paradigmas de Linguagens de Programacao - Aula #4Paradigmas de Linguagens de Programacao - Aula #4
Paradigmas de Linguagens de Programacao - Aula #4Ismar Silveira
 
Implementing Software Machines in Go and C
Implementing Software Machines in Go and CImplementing Software Machines in Go and C
Implementing Software Machines in Go and CEleanor McHugh
 
Daapracticals 111105084852-phpapp02
Daapracticals 111105084852-phpapp02Daapracticals 111105084852-phpapp02
Daapracticals 111105084852-phpapp02Er Ritu Aggarwal
 
Program of sorting using shell sort #include stdio.h #de.pdf
 Program of sorting using shell sort  #include stdio.h #de.pdf Program of sorting using shell sort  #include stdio.h #de.pdf
Program of sorting using shell sort #include stdio.h #de.pdfanujmkt
 
DATA STRUCTURE USING C & C++
DATA STRUCTURE USING C & C++DATA STRUCTURE USING C & C++
DATA STRUCTURE USING C & C++mustkeem khan
 
DSU C&C++ Practical File Diploma
DSU C&C++ Practical File DiplomaDSU C&C++ Practical File Diploma
DSU C&C++ Practical File Diplomamustkeem khan
 
Solutionsfor co2 C Programs for data structures
Solutionsfor co2 C Programs for data structuresSolutionsfor co2 C Programs for data structures
Solutionsfor co2 C Programs for data structuresLakshmi Sarvani Videla
 

Ähnlich wie week-17x (20)

VTU Data Structures Lab Manual
VTU Data Structures Lab ManualVTU Data Structures Lab Manual
VTU Data Structures Lab Manual
 
week-15x
week-15xweek-15x
week-15x
 
#includeiostream#includecctypeusing namespace std;cl.docx
#includeiostream#includecctypeusing namespace std;cl.docx#includeiostream#includecctypeusing namespace std;cl.docx
#includeiostream#includecctypeusing namespace std;cl.docx
 
Sorting programs
Sorting programsSorting programs
Sorting programs
 
data structure and algorithm.pdf
data structure and algorithm.pdfdata structure and algorithm.pdf
data structure and algorithm.pdf
 
Stack prgs
Stack prgsStack prgs
Stack prgs
 
Struct examples
Struct examplesStruct examples
Struct examples
 
Lab Question
Lab QuestionLab Question
Lab Question
 
L25-L26-Parameter passing techniques.pptx
L25-L26-Parameter passing techniques.pptxL25-L26-Parameter passing techniques.pptx
L25-L26-Parameter passing techniques.pptx
 
Cpds lab
Cpds labCpds lab
Cpds lab
 
Data Structures Using C Practical File
Data Structures Using C Practical File Data Structures Using C Practical File
Data Structures Using C Practical File
 
Paradigmas de Linguagens de Programacao - Aula #4
Paradigmas de Linguagens de Programacao - Aula #4Paradigmas de Linguagens de Programacao - Aula #4
Paradigmas de Linguagens de Programacao - Aula #4
 
C programs
C programsC programs
C programs
 
Implementing Software Machines in Go and C
Implementing Software Machines in Go and CImplementing Software Machines in Go and C
Implementing Software Machines in Go and C
 
Daapracticals 111105084852-phpapp02
Daapracticals 111105084852-phpapp02Daapracticals 111105084852-phpapp02
Daapracticals 111105084852-phpapp02
 
Program of sorting using shell sort #include stdio.h #de.pdf
 Program of sorting using shell sort  #include stdio.h #de.pdf Program of sorting using shell sort  #include stdio.h #de.pdf
Program of sorting using shell sort #include stdio.h #de.pdf
 
Blocks+gcd入門
Blocks+gcd入門Blocks+gcd入門
Blocks+gcd入門
 
DATA STRUCTURE USING C & C++
DATA STRUCTURE USING C & C++DATA STRUCTURE USING C & C++
DATA STRUCTURE USING C & C++
 
DSU C&C++ Practical File Diploma
DSU C&C++ Practical File DiplomaDSU C&C++ Practical File Diploma
DSU C&C++ Practical File Diploma
 
Solutionsfor co2 C Programs for data structures
Solutionsfor co2 C Programs for data structuresSolutionsfor co2 C Programs for data structures
Solutionsfor co2 C Programs for data structures
 

Mehr von KITE www.kitecolleges.com (19)

ch14
ch14ch14
ch14
 
ch16
ch16ch16
ch16
 
holographic versatile disc
holographic versatile discholographic versatile disc
holographic versatile disc
 
week-22x
week-22xweek-22x
week-22x
 
week-5x
week-5xweek-5x
week-5x
 
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-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
 
ch3
ch3ch3
ch3
 
Entity Classes and Attributes
Entity Classes and AttributesEntity Classes and Attributes
Entity Classes and Attributes
 

Kürzlich hochgeladen

Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxnegromaestrong
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17Celine George
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibitjbellavia9
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxVishalSingh1417
 
Role Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptxRole Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptxNikitaBankoti2
 
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural ResourcesEnergy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural ResourcesShubhangi Sonawane
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfChris Hunter
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxVishalSingh1417
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin ClassesCeline George
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfAyushMahapatra5
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Shubhangi Sonawane
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDThiyagu K
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701bronxfugly43
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-IIFood Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-IIShubhangi Sonawane
 

Kürzlich hochgeladen (20)

Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Role Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptxRole Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptx
 
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural ResourcesEnergy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-IIFood Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
 

week-17x

  • 1. /* Write a C program that uses Stack operations to perform the following: i) Converting infix expression into postfix expression ii) Evaluating the postfix expression */ #include<stdio.h> #include<conio.h> int st[100]; int st_top=-1; int cal(char post[]); void in_post(char in[]); void push_item(int it); int pop_item(); int st_ISP(char t); int st_ICP(char t); /*main function*/ void main() { char in[100],post[100]; clrscr(); printf("ntEnter the Infix Expression: "); gets(in);
  • 2. in_post(in); getch(); } /*end main*/ void push_item(int it) { if(st_top==99) { printf("nnt*STACK is Full*"); getch(); exit(1); } st[++st_top]=it; } int pop_item() { int it; if(st_top==-1) { getch(); } return(st[st_top--]);
  • 3. } /*Function for converting an infix expression to a postfix expression. */ void in_post(char in[]) { int x=0,y=0,z,result=0; char a,c, post[100]; char t; push_item('0'); t=in[x]; while(t!='0') { if(isalnum(t)) /*For checking whether the value in t is an alphabet or number. */ { post[y]=t; y++; } else if(t=='(') { push_item('('); } else if(t==')') {
  • 4. while(st[st_top]!='(') { c=pop_item(); post[y]=c; y++; } c=pop_item(); } else { while(st_ISP(st[st_top])>=st_ICP(t)) { c=pop_item(); post[y]=c; y++; } push_item(t); } x++; t=in[x]; } while(st_top!=-1) {
  • 5. c=pop_item(); post[y]=c; y++; } printf("ntThe Postfix Expression is:"); for(z=0;z<y;z++) printf("%c",post[z]); printf("nnDo you want to evaluate the Result of Postfix Expression?(Y/N):"); scanf("%c",&a); if(a=='y' || a=='Y') { result=cal(post); printf("nntResult is: %dn",result); getch(); } else if(a=='n' || a=='N') { exit(0); } } /*Determining priority of inside elements*/ int st_ISP(char t)
  • 6. { switch(t) { case '(':return (10); case ')':return (9); case '+':return (7); case '-':return (7); case '*':return (8); case '/':return (8); case '0':return (0); default: printf("Expression is invalid."); break; } return 0; } /*Determining priority of approaching elements*/ int st_ICP(char t) { switch(t) { case '(':return (10); case ')':return (9);
  • 7. case '+':return (7); case '-':return (7); case '*':return (8); case '/':return (8); case '0':return (0); default: printf("Expression is invalid."); break; } return 0; } /*Evaluating the result of postfix expression*/ int cal(char post[]) { int m,n,x,y,j=0,len; len=strlen(post); while(j<len) { if(isdigit(post[j])) { x=post[j]-'0'; push_item(x); } else
  • 8. { m=pop_item(); n=pop_item(); switch(post[j]) { case '+':x=n+m; break; case '-':x=n-m; break; case '*':x=n*m; break; case '/':x=n/m; break; } push_item(x); } j++; } if(st_top>0) { printf("Number of Operands are more than Operators."); exit(0); }
  • 9. else { y=pop_item(); return (y); } return 0; }