-
Notifications
You must be signed in to change notification settings - Fork 0
/
sample.cpp
108 lines (107 loc) · 1.5 KB
/
sample.cpp
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
104
105
106
107
108
#include<iostream>
#include<cstring>
using namespace std;
struct node
{
char data ;
node *next ;
};
class stack
{
private :
node *head;
public :
stack()
{
head = NULL;
}
void push(char a)
{
node * temp = new node();
temp ->data = a ;
temp -> next = head ;
head = temp ;
}
char pop()
{
node *temp = head ;
head = temp->next ;
char a = temp->data ;
delete temp;
return a;
}
char see_top()
{
if(is_empty())
return '0' ;
node * temp = head ;
return (temp->data);
}
int is_empty()
{
if(head == NULL)
return 1;
else
return 0 ;
}
};
int is_allnum(char a)
{
if(isalnum(a))
return 1;
else
return 0;
}
int is_operand(char a)
{
switch(a)
{
case '+' :
case '-' :
case '*' :
case '/' : return 1;
default : return 0;
}
}
void reverse(char a[] , int l)
{
int i=0;
stack b ;
for(int j=0;j<l;j++)
{
b.push(a[j]);
}
while(!b.is_empty())
a[i++]=b.pop();
return ;
}
int main()
{
int i , l , j=0;
char exp[100] , infix[100];
stack s ;
cout << "Enter a postfix expression : ";
cin.getline(exp,100);
l=strlen(exp);
reverse(exp,l);
for(i=0;i<l;i++)
{
if(is_allnum(exp[i]))
{
infix[j++] = exp[i];
if(i==l-1)
break ;
infix[j++] = s.pop() ;
}
else
s.push(exp[i]);
}
while(!s.is_empty())
infix[j++] =s.pop();
reverse(infix,l);
cout << "\nInfix is : " ;
for(i=0;i<l;i++)
cout << infix[i] ;
cout << endl;
return 0;
}