1+ #include < iostream>
2+ #include < string>
3+
4+ using namespace std ;
5+
6+ const int MAX_SIZE = 100 ;
7+
8+ struct Stack {
9+ int top; // index of top of stack
10+ string elements[MAX_SIZE];
11+ };
12+
13+ bool isEmpty (Stack& stack) {
14+ if (stack.top == -1 ) {
15+ return true ;
16+ }
17+ return false ;
18+ }
19+
20+ bool isFull (Stack& stack) {
21+ if (stack.top == MAX_SIZE - 1 ) {
22+ return true ;
23+ }
24+ return false ;
25+ }
26+
27+ void push (Stack& stack, string value) {
28+ if (isFull (stack)) {
29+ cout << " Stack overflow!" << endl;
30+ return ;
31+ }
32+ stack.elements [++stack.top ] = value;
33+ }
34+
35+ void pop (Stack& stack) {
36+ if (isEmpty (stack)) {
37+ cout << " Stack underflow!" << endl;
38+ return ;
39+ }
40+ stack.elements [stack.top --] = " " ;
41+ }
42+
43+ string top (Stack& stack) {
44+ if (isEmpty (stack)) {
45+ // cout << "Stack is empty!" << endl;
46+ return " " ;
47+ }
48+ return stack.elements [stack.top ];
49+ }
50+
51+ bool isOperator (char c) {
52+ return (c == ' +' || c == ' -' || c == ' *' || c == ' /' );
53+ }
54+
55+ int getPrecedence (char op) {
56+ if (op == ' +' || op == ' -' )
57+ return 1 ;
58+ else if (op == ' *' || op == ' /' )
59+ return 2 ;
60+ return 0 ;
61+ }
62+
63+ string removeSpaces (string str) {
64+ string result;
65+ for (int i = 0 ; i < str.size () ; i++) {
66+ if (str[i] != ' ' ) {
67+ result += str[i];
68+ }
69+ }
70+ return result;
71+ }
72+
73+ string prefix (Stack& stack ,string infixExpression) {
74+ string prefixExpression;
75+ for (int i = infixExpression.size () - 1 ; i >= 0 ; i--) {
76+ char currentChar = infixExpression[i];
77+ if (currentChar == ' )' || currentChar == ' }' || currentChar == ' ]' ) {
78+ string str (1 , currentChar);
79+ push (stack, str);
80+ } else if (isOperator (currentChar)) {
81+ while (!isEmpty (stack) && isOperator (top (stack)[0 ]) && getPrecedence (currentChar) <= getPrecedence (top (stack)[0 ])) {
82+ prefixExpression += top (stack) + " " ;
83+ pop (stack);
84+ }
85+ string str (1 , currentChar);
86+ push (stack, str);
87+ } else if (currentChar == ' (' || currentChar == ' {' || currentChar == ' [' ) {
88+ while (!isEmpty (stack) && isOperator (top (stack)[0 ])) {
89+ prefixExpression += top (stack) + " " ;
90+ pop (stack);
91+ }
92+ pop (stack); // remove corresponding closing parenthesis
93+ } else {
94+ string operand;
95+ while ( (i >= 0 && isdigit (infixExpression[i])) || (i >= 0 && isalpha (infixExpression[i]) )) {
96+ operand = infixExpression[i] + operand;
97+ i--;
98+ }
99+ // reverse
100+ string tempOp ;
101+ for (int i = operand.size () -1 ; i >= 0 ; i--){
102+ tempOp += operand[i] ;
103+ }
104+
105+ i++; // adjust the index after the loop
106+ prefixExpression += tempOp + " " ;
107+ }
108+ }
109+ while (!isEmpty (stack)) {
110+ prefixExpression += top (stack) + " " ;
111+ pop (stack);
112+ }
113+ // Reverse the prefix expression
114+ string reversedPrefixExpression;
115+ for (int i = prefixExpression.size () - 1 ; i >= 0 ; i--) {
116+ reversedPrefixExpression += prefixExpression[i];
117+ }
118+ return reversedPrefixExpression;
119+ }
120+
121+ int main () {
122+ Stack stack;
123+ stack.top = -1 ;
124+ string infixExpression;
125+ cout << " Enter the infix phrase: " ;
126+ getline (cin, infixExpression);
127+
128+ cout << prefix (stack , removeSpaces (infixExpression)) ;
129+
130+ return 0 ;
131+ }
132+
133+ /*
134+ Reza Asadi (Github : RezaGooner)
135+ 1402/08/24 ~ 2023/11/15
136+ */
0 commit comments