0% found this document useful (0 votes)
28 views2 pages

Infix To Post Fix in C

This document defines functions to convert an infix notation mathematical expression to postfix notation. It includes functions to initialize an empty stack, check if a stack is empty, push operands onto the stack, and pop operands off the stack. The infix_to_postfix function uses these stack functions to process an infix string character by character, pushing operators onto the stack and appending operands to the output postfix string based on operator precedence.

Uploaded by

BABAR SOHAIL
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views2 pages

Infix To Post Fix in C

This document defines functions to convert an infix notation mathematical expression to postfix notation. It includes functions to initialize an empty stack, check if a stack is empty, push operands onto the stack, and pop operands off the stack. The infix_to_postfix function uses these stack functions to process an infix string character by character, pushing operators onto the stack and appending operands to the output postfix string based on operator precedence.

Uploaded by

BABAR SOHAIL
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

1 // Operator supported: +,-,*,/,%,^,(,)

2 // Operands supported: all single character operands


3
4 #include<stdio.h>
5 #include<conio.h>
6 #include<ctype.h>
7 #define MAX 100
8 #include"infix2post.h"
9
10
11
12
13 void infix_to_postfix(char infix[],char postfix[])
14 {
15 stack s ;
16 char x,token;
17 int i,j; //i-index of infix,j-index of postfix
18 init (&s );
19 j =0;
20
21 for( i=0; infix[ i]!='\0';i ++)
22 {
23 token=infix[i];
24 if(isalnum(token))
25 postfix[ j++]=token;
26 else
27 if(token=='(')
28 push(&s, '(');
29 else
30 if(token==')')
31 while((x=pop(&s))!= '(')
32 postfix [j++]=x ;
33 else
34 {
35 while(precedence(token)<=precedence( top(&s ))&&!empty(&s))
36 {
37 x= pop(&s );
38 postfix[ j++]=x;
39 }
40 push(&s,token);
41 }
42 }
43
44 while(!empty(&s ))
45 {
46 x=pop(&s);
47 postfix[j ++]=x;
48 }
49
50 postfix [j]='\0';
51 }
52
53 int precedence (char x )
54 {
55 if(x =='(')
56 return(0);
57 if(x =='+'||x=='-')
58 return (1);
59 if(x =='*'||x=='/'||x=='%')
60 return(2);
61
62 return ( 3);
63 }
64
65 void init(stack *s )
66 {
67 s ->top=-1;
68 }
69
70 int empty(stack * s)
71 {
72 if(s ->top==-1 )
73 return (1);
74
75 return( 0);
76 }
77
78 int full (stack *s)
79 {
80 if(s ->top==MAX-1 )
81 return (1);
82
83 return( 0);
84 }
85
86 void push(stack *s ,int x)
87 {
88 s ->top= s->top+1;
89 s ->data [s->top]=x;
90 }
91
92 int pop( stack *s)
93 {
94 int x;
95 x =s->data[s ->top];
96 s ->top= s->top-1;
97 return ( x);
98 }
99
100 int top( stack *p)
101 {
102 return (p->data[p ->top]);
103 }
104

You might also like