-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathINPOST
77 lines (74 loc) · 1.24 KB
/
INPOST
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
76
77
#include<stdlib.h>
#include<stdio.h>
#define MAX 100
int isOperator(char);
int Prior(char);
void push(char);
int pop();
int i,j,top=-1;
char infix[MAX],postfix[MAX],stack[MAX],temp;
int main(){
printf("\nEnter an expression...\n");scanf("%s",infix);
for(i=0,j=0 ;infix[i]!='\0'; i++){
if(!isOperator(infix[i]))
{
postfix[j++]=infix[i];
}
else if(infix[i]=='(')
{
push(infix[i]);
}
else if(infix[i]==')')
{
while(stack[top]!='('){
postfix[j++]=pop();
}
pop();
}
else{
if(stack[top]=='(' || top==-1)
{
push(infix[i]);
}
else{
if(infix[i]=='^' && stack[top]=='^')
push(infix[i]);
else if(Prior(infix[i])<=Prior(stack[top])){
postfix[j++]=pop();
push(infix[i]);
}
else push(infix[i]);
}
}
}
while(top!=-1){
postfix[j++]=pop();
}
printf("\nPOSTFIX EXPRESSION : %s",postfix);
return(0);
}
int isOperator(char a){
switch(a){
case '+':
case '-':
case '*':
case '/':
case '^':
case '(':
case ')':
return(1);
default :return(0);
}
}
int Prior(char x){
if(x=='^')return(3);
if(x=='*'||x=='/')return(2);
if(x=='+'||x=='-')return(1);
return(0);
}
void push(char item){
stack[++top]=item;
}
int pop(){
return(stack[top--]);
}