-
Notifications
You must be signed in to change notification settings - Fork 3
/
InfixtoPrefix.c
68 lines (61 loc) · 1.11 KB
/
InfixtoPrefix.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
#include<stdio.h>
#include<conio.h>
#include<string.h>
void PushPrefix(char x);
void PushStacks(char x,int rank);
void OutputProcess();
void Display();
//Global Variables
char infix[50];
char stacks[10];
char prefix[50];
int topStacks=-1;
int topPrefix=-1;
int temp=0;
//Main Function
int main(){
printf("\nInfix: ");
gets(infix);
int length=strlen(infix)-1;
for (int x=length;x>=0;x--){
switch (infix[x]){
case '*' :
PushStacks(infix[x],4); break;
case '/' :
PushStacks(infix[x],3); break;
case '+' :
PushStacks(infix[x],2); break;
case '-' :
PushStacks(infix[x],1); break;
default :
PushPrefix(infix[x]);
break;
}
}
OutputProcess();
Display();
}//end of main
void PushPrefix(char x){
prefix[++topPrefix]=x;
}//end of PushPrefix
void PushStacks(char x,int rank){
if (rank>=temp){
stacks[++topStacks]=x;
} else {
PushPrefix(stacks[topStacks]);
topStacks--;
stacks[++topStacks]=x;
}
temp=rank;
}//end of PushStacks
void OutputProcess(){
for (int x=topStacks;x>=0;x--){
PushPrefix(stacks[x]);
}
}//end of Output Process
void Display(){
printf("\nPrefix: ");
for (int x=topPrefix;x>=0;x--){
printf("%c",prefix[x]);
}
}//end of display