forked from Abhijeet5665/c-programs-tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinfix.c
More file actions
75 lines (75 loc) · 1.08 KB
/
infix.c
File metadata and controls
75 lines (75 loc) · 1.08 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
68
69
70
71
72
73
74
75
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<ctype.h>
#define SIZE 10
void push(char item);
char pop();
int prec(char item);
int top=-1;
char stack[SIZE];
char infix[20];
char postfix[20];
void main(){
int n;
printf("enter the infix expression\n");
scanf("%s",infix);
n=strlen(infix);
int i,k=0;
for(i=0;i!=n;i++)
{
if(isalnum(infix[i]))
postfix[k++]=infix[i];
else if(infix[i]=='(')
push(infix[i]);
else if(stack[top]=='(' || top==-1)
push(infix[i]);
else if(infix[i]==')'){
while(stack[top]!='(')
postfix[k++]=pop();
pop();
}
else{
if(prec(infix[i])>prec(stack[top]))
push(infix[i]);
else{
//postfix[k++]=pop();
while(prec(infix[i])<=prec(stack[top]))
postfix[k++]=pop();
push(infix[i]);
}
}
}
while(top!=-1){
postfix[k++]=pop();
}
printf("%s",postfix);
}
void push(char ch){
if(top==SIZE-1)
printf("stack is full\n");
else{
top++;
stack[top]=ch;}
}
char pop(){
char z;
if(top==-1)
printf("stack is empty\n");
else{
z=stack[top];
top--;
}
return z;
}
int prec(char p)
{
if(p=='^')
return 10;
else if(p=='*' || p=='/')
return 8;
else if(p=='+' || p=='-')
return 6;
else
return 0;
}