SHOW:
|
|
- or go back to the newest paste.
1 | #include<stdio.h> | |
2 | #include<string.h> | |
3 | #include<ctype.h> | |
4 | #define MAX 100 | |
5 | ||
6 | typedef struct | |
7 | { | |
8 | char s[MAX]; // Operator Stack | |
9 | char a[MAX]; //Infix Expression | |
10 | char b[MAX]; //Postfix Expression | |
11 | int tos; //Top of stack | |
12 | int postindex; //Index of Postfix. Don't ask why defined here. Just feeling like it :P | |
13 | }infix; | |
14 | ||
15 | void transfer(infix *t,char k) | |
16 | { | |
17 | t->b[t->postindex]=k; | |
18 | t->postindex++; | |
19 | } | |
20 | ||
21 | int ipr(char a) | |
22 | { | |
23 | if (a=='(') | |
24 | return 3; | |
25 | else if (a=='+'||a=='-') | |
26 | return 1; | |
27 | else if (a=='%'||a=='/'||a=='*') | |
28 | return 2; | |
29 | } | |
30 | ||
31 | int rpr(char a) | |
32 | { | |
33 | if (a=='(') | |
34 | return 0; | |
35 | else if (a=='+'||a=='-') | |
36 | return 1; | |
37 | else if (a=='%'||a=='/'||a=='*') | |
38 | return 2; | |
39 | } | |
40 | ||
41 | int isempty(infix *t) | |
42 | { | |
43 | if (t->tos==-1) | |
44 | return 1; | |
45 | else | |
46 | return 0; | |
47 | } | |
48 | ||
49 | void display(infix *t) | |
50 | { | |
51 | int i; | |
52 | int n=(int)strlen(t->b); | |
53 | for(i=0;i<n;i++) | |
54 | { | |
55 | - | printf("%d",t->b[i]); |
55 | + | printf("%s",t->b[i]); |
56 | } | |
57 | } | |
58 | ||
59 | ||
60 | void push(infix *t,char a) | |
61 | { | |
62 | ||
63 | t->tos++; | |
64 | t->s[t->tos]=a; | |
65 | } | |
66 | ||
67 | void pop(infix *t,char k) | |
68 | { | |
69 | if(k=='(') | |
70 | { | |
71 | t->tos--; | |
72 | } | |
73 | else | |
74 | { | |
75 | transfer(t,k); | |
76 | t->tos--; | |
77 | } | |
78 | } | |
79 | ||
80 | int isoperand(char a) | |
81 | { | |
82 | if(a=='+'||a=='-'||a=='%'||a=='/'||a=='*'||a=='(') | |
83 | return 1; | |
84 | else | |
85 | return 0; | |
86 | } | |
87 | ||
88 | ||
89 | ||
90 | void convert(infix *t) | |
91 | { | |
92 | int i; | |
93 | int n=(int)strlen(t->a); | |
94 | for(i=0;i<n;i++) | |
95 | { | |
96 | if(isoperand(t->a[i])) | |
97 | { | |
98 | if (isempty(t)) | |
99 | { | |
100 | push(t,t->a[i]); | |
101 | } | |
102 | else if(ipr(t->a[i])>rpr(t->s[t->tos])) | |
103 | { | |
104 | push(t,t->a[i]); | |
105 | } | |
106 | else if(ipr(t->a[i])<rpr(t->s[t->tos])) | |
107 | { | |
108 | while(isempty(t)!=1||ipr(t->a[i])>rpr(t->s[t->tos])) | |
109 | { | |
110 | pop(t,t->a[i]); | |
111 | } | |
112 | push(t,t->a[i]); | |
113 | } | |
114 | else if(t->a[i]==')') | |
115 | { | |
116 | while(t->s[t->tos]!='(') | |
117 | { | |
118 | pop(t,t->a[i]); | |
119 | } | |
120 | ||
121 | } | |
122 | else | |
123 | { | |
124 | transfer(t,t->a[i]); | |
125 | } | |
126 | ||
127 | } | |
128 | } | |
129 | } | |
130 | ||
131 | void main() | |
132 | { | |
133 | ||
134 | infix s; | |
135 | s.postindex=0; | |
136 | s.tos=-1; | |
137 | printf("Enter the infix expression\n"); | |
138 | gets(s.a); | |
139 | convert(&s); | |
140 | printf("\nThe Postfix expression is \n"); | |
141 | display(&s); | |
142 | getchar(); | |
143 | } |