SlideShare uma empresa Scribd logo
1 de 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;

}

Mais conteúdo relacionado

Mais procurados

Mais procurados (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
 

Destaque

Natalie Barnhart pd. 5
Natalie Barnhart pd. 5Natalie Barnhart pd. 5
Natalie Barnhart pd. 5
LigScience2
 
It4466 finale 2_kz2
It4466 finale 2_kz2It4466 finale 2_kz2
It4466 finale 2_kz2
lukatak
 
An Impromptu Introduction to HTML5 Canvas
An Impromptu Introduction to HTML5 CanvasAn Impromptu Introduction to HTML5 Canvas
An Impromptu Introduction to HTML5 Canvas
Ben Hodgson
 

Destaque (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
 

Semelhante a week-17x

Sorting programs
Sorting programsSorting programs
Sorting programs
Varun Garg
 
Daapracticals 111105084852-phpapp02
Daapracticals 111105084852-phpapp02Daapracticals 111105084852-phpapp02
Daapracticals 111105084852-phpapp02
Er 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.pdf
anujmkt
 
DATA STRUCTURE USING C & C++
DATA STRUCTURE USING C & C++DATA STRUCTURE USING C & C++
DATA STRUCTURE USING C & C++
mustkeem khan
 

Semelhante a 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
 

Mais de 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
 

Último

Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.
MateoGardella
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
Chris Hunter
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
QucHHunhnh
 
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
QucHHunhnh
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
kauryashika82
 
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
negromaestrong
 

Último (20)

This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
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
 
Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.
 
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
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
 
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
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
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
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
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
 

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; }