infix to postfix conversion

It does not show any output.

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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
 #include <iostream>
#include <conio.h>
#include <string.h>
#include <sstream>
									
					/*C++ code to convert infix expression to postfix exprssion .*/
						
using namespace std;

//node class
class node{
    
    private:
    	char object;
		node *nextNode;
	
	public:
	
		char get() {
			
			return object;
		}
	
		void set(char object) {
			
			this->object = object;
		}	
	
		node *getNext() {
			
			return nextNode;
		}
	
		void setNext(node *nextNode) {
			
			this->nextNode = nextNode;			
		}
};node *headNode = NULL, *currentNode = NULL, *lastcurrentNode = NULL, *pointer; //objects of node class
//node class end

class stack{//stack class start
	
	private:
		int size;
		
	public:
		
		stack(){
			size=0;
		}
		
		void push(char x){ //push function of Stack //which pushes the values 
	    	
			headNode = new node;
	    	headNode-> set(x);
	    	headNode->setNext(NULL);
	    	
			if ( lastcurrentNode == NULL ) {	//this conditions checks if single node is created
	        	lastcurrentNode = headNode;
	    }
			
			else { //this conditions checks if more than one node is created
	     
		    	currentNode = lastcurrentNode; 
	        	lastcurrentNode = headNode;
	        	headNode->setNext(currentNode);
	    }
	    size++;
	}
	
		char pop() { //pop function of stack class //which pop up the values
	    	
			if (lastcurrentNode == NULL) {
	        	cout<<"Stack is Empty";
	    }
			else {
	        	pointer = lastcurrentNode;
	        	lastcurrentNode = lastcurrentNode->getNext();
	        	return(pointer->get());
	        	delete pointer;
	    }
	    size--;
	}
	
		char top(){
		
			return headNode->get();
		
		}
		
		char empty(){
			
			return (headNode==NULL);
		}
};
//stack class ends

int priority(char x){
	
	int temp;
	if(x == '*' || x == '/'){
		temp = 1;
	}
	if(x == '+' || x == '-'){
		temp = 2;
	}
	return temp;
}

/*void converter(char infix[], char postfix[], int size){
	
	stack obj;
	int prio;
	int i=0;
	int k=0;
	char itrever;
	
	//itrever the infix expression
	while(i<size){
		itrever = infix[i];
		if(itrever == '('){
			obj.push(itrever);
			i++;
			continue;
		}
		if(itrever == ')'){
			while(!obj.empty() && obj.top() != '('){
				postfix[k++]=obj.top();
				obj.pop();
			}
			if(!obj.empty()){
				obj.pop();
			}
			i++;
			continue;
		}
		prio = priority(itrever);
			if(prio == 0){
				postfix[k++] = itrever;
			}
			else{
				if(obj.empty()){
					obj.push(itrever);
				}
				else{
					while(!obj.empty() && obj.top() != '(' && prio<=priority(obj.top())){
						postfix[k++] = obj.top();
						obj.pop();
					}
					obj.push(itrever);
				}
			}
			i++;
		}
		
	
	while(!obj.empty()){
		postfix[k++] = obj.top();
		obj.pop();
	}
	postfix[k] = 0;
} */

	
//main
int main() {
    
	char infix[] = "A*(B+C)";
	/*int size = strlen(infix);
	char postfix[size];
	converter(infix,postfix,size);
	cout<<"\nInfix Expression: "<<infix;
	cout<<"\nPostfix Expression: "<<postfix;*/
	
	stack obj;
	int size = strlen(infix);
	stringstream output;
	for(int i=0; i<size; i++){
		if(infix[i] == '+' || infix[i] == '-' || infix[i] == '*' || infix[i] == '/'){
			while(!obj.empty() && priority(obj.top()) <= priority(infix[i])){
				output<<obj.top();
			}
			obj.push(infix[i]);
		} 
		else if(infix[i] == '('){
			obj.push(infix[i]);
		}
		else if(infix[i] == ')'){
			while(obj.top() != '('){
				output<<obj.top();
				obj.pop();
			}
			obj.pop();
		} 
		else{
			output<<infix[i];
		}	
	}
	while(!obj.empty()){
		output<<obj.top();
		obj.pop();
	}
	cout<<output.str();
    getch();
}
	
													/*    Program End     */
Topic archived. No new replies allowed.