-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path31InfixToPostfix.c
More file actions
103 lines (103 loc) · 2.25 KB
/
31InfixToPostfix.c
File metadata and controls
103 lines (103 loc) · 2.25 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include<stdio.h>
#include<string.h>
#include<ctype.h>
typedef struct Stack{
int a;
char item[30];
}stack;
stack convertion(char eqn[30]);
void compare(stack *operator,char c,stack* pfexp);
char pop(stack *Operand);
void push(stack *Operand,char b);
int com(char key);
int main(){
char eqn[30];
stack pfexp;
printf("Enter the expression:\n");
scanf("%s",&eqn);
pfexp=convertion(eqn);
for(int i=0;i<=pfexp.a;i++){
printf("%c",pfexp.item[i]);
}
return 0;
}
stack convertion(char eqn[30]){
stack pfexp;
stack operator;
operator.a=pfexp.a=-1;
char op,c;
int k=0;
for(int i=0;(c=eqn[i])!='\0';i++){
if(isdigit(c)){
push(&pfexp,c);
}
else if(c=='('){
push(&operator,c);
}
else if(c==')'){
while(com(c)!=com(operator.item[operator.a]) && operator.a>=0){
op=pop(&operator);
push(&pfexp,op);
}
if(com(c)==com(operator.item[operator.a])){
pop(&operator);
}
}
else{
compare(&operator,c,&pfexp);
}
}
k=operator.a;
for(int i=0;i<=k;i++){
op=pop(&operator);
push(&pfexp,op);
}
return pfexp;
}
void compare(stack *operator,char c,stack* pfexp){
char op1='+';
if(operator->a>=0 ){
if(com(c)<=com(operator->item[operator->a])){
if(operator->item[operator->a]!='('){
op1=pop(operator);
push(pfexp,op1);
compare(operator,c,pfexp);
}
else{
push(operator,c);
}
}
else{
push(operator,c);
}
}
else{
push(operator,c);
}
}
int com(char key){
switch (key)
{
case '(':
case ')':
return 2;
break;
case '+':
case '-':
return 0;
break;
case '*':
case '/':
return 1;
break;
default:
return 3;
break;
}
}
char pop(stack *Operand){
return Operand->item[(Operand->a)--];
}
void push(stack *Operand,char b){
Operand->item[++Operand->a]=b;
}