-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
88 lines (86 loc) · 1.77 KB
/
main.c
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//A-(B/C+(D%E*F))/G*H
char A[155],*B,C[155];
int top=-1,i=0,k=0;
int isempty()
{
if(top==-1)
return 1;
else
return 0;
}
void pop()
{ if(isempty())
{printf("Your stack is empty\n");
}
else
{ if(*(B+top)!='(')
{C[k]=*(B+top);
k++;
}
printf("%c has been poped out\n",*(B+top));
top--;
}
}
void push(char b)
{ top++;
*(B+top)=b;
printf("%c has been pushed in\n",b);
}
void prefer(char a)
{
switch(a)
{ case '-':if(!isempty()&&*(B+top)!='(')
pop();
push(a);
break;
case '+': if(*(B+top)!='-'||*(B+top)!='(')
pop();
push(a);
break;
case '%': if(*(B+top)!=='+'||*(B+top)!=='-'||*(B+top)!='(')
pop();
push(a);
break;
case '/': if(*(B+top)=='*'||*(B+top)=='^')
pop();
push(a);
break;
case '*': if(*(B+top)=='^')
pop();
push(a);
break;
case '^': push(a);
break;
case '(': push(a);
break;
case ')': while(*(B+top)!='(')
{ pop();
}
pop();
break;
}
}
int main()
{ int j;
printf("Enter the infix\n");
gets(A);
i=strlen(A);
printf("%d\n",i);
B=(char*)malloc(i*sizeof(char));
for(j=0;j<i;j++)
{ if((int)A[j]>64&&(int)A[j]<92)
{ C[k]=A[j];
k++;
}
else
{
prefer(A[j]);
}
}
printf("Your Postfix is\n");
puts(C);
return 0;
}