-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpractical2-2.c
78 lines (69 loc) · 1.18 KB
/
practical2-2.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
// Name- Karan Parmar Roll No- B_306
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define SIZE 100
typedef struct Stack{
int top;
char a[SIZE];
}Stack;
Stack s;
int isEmpty(){
return s.top == -1;
}
int isFull(){
return s.top== SIZE-1;
}
void push(char x){
if(isFull()){
return;
}
s.top++;
s.a[s.top] = x;
}
char StackTop(){
if(isEmpty()){
return 0;
}
return s.a[s.top];
}
char pop(){
if(isEmpty()){
return 0;
}
char x ;
x = s.a[s.top];
return x;
}
int isOperator(char x){
if(x == '+' || x == '+' || x == '-' || x == '*' || x == '/' ||x == '$' || x == '(' || x == ')')
return 1;
return 0;
}
char * prefix_infix(char *str){
int n,i,j=0;
char a,b,op;
char *infix= (char*)malloc(strlen(str)*sizeof(char));
for(i=0;i<strlen(str);i++){
if(isOperator(str[i]))
push(str[i]);
else{
op = pop();
a = str[i];
infix[j++] = a;
infix[j++] = op;
}
}
return infix;
}
int main()
{
char *infix = malloc(sizeof(char) * 25);
printf("Enter the expression");
gets(infix);
push(' ');
char * str = prefix_infix(infix);
printf("Infix Expression");
puts(str);
return 0;
}