-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInfixtoPostfix.c
More file actions
67 lines (67 loc) · 1.32 KB
/
InfixtoPostfix.c
File metadata and controls
67 lines (67 loc) · 1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<stdbool.h>
#define SIZE 10
char s[SIZE];
int top=-1;
bool isempty(){
if(top == -1){
return true;
}
return false;
}
void push(char ch){
s[++top]=ch;
}
char pop(){
if(isempty()){
printf("Stack is empty");
}
return s[top--];
}
int prior(char op){
if(op == '^'){
return 3;
}
else if(op=='/' || op=='*'){
return 2;
}
else if(op=='+'||op=='-'){
return 1;
}
}
void infixtopostfix(char exp1[], char postfix[]){
int k=0;
while((ch=infix[i++])!='\0'){
if(exp1[i]=='('){
push(exp1[i]);
}
else if(isalnum(exp1[i])){
postfix[k++]=exp1[i];
}
else if(exp1[i]==')'){
while(exp1[top]!='('){
postfix[k++]=pop();
}
char elem=pop();
}
else{
while(prior(exp1[top])>=prior(exp1[i])){
postfix[k++]=pop();
}
push(exp1[i]);
}
while(!isempty()){
postfix[k++]=pop();
}
}
}
int main(){
char infix[10],postfix[10];
printf("Please enter the infix expression\n");
scanf("%s",infix);
infixtopostfix(infix,postfix);
printf("Postfix Expression is %s",postfix);
return 0;
}