 Welcome to this session in the course data structures and algorithms. Last time we looked at the algorithm for converting an infix expression into a post fix expression. Today we shall see a C plus plus program implementing that logic. Here is the algorithm that we shall use. Given an infix expression as a string input, we need to create an output string which is the post fix expression. So, the process is very simple which was explained last time. Validate the infix expression first. If the fix expression itself is not valid, then I will display an appropriate error message otherwise I proceed with the conversion. The conversion process itself again is very simple. Firstly, I traverse the infix expression character by character. So, I will have an outer iteration which will look at every incoming character of the infix expression. Now, if the character is operand, then append it to the string post fix. If the character is not operand, it will be an operator. If it is an operator, we will do the following. Firstly, if the operator is opening parenthesis, push it to the operator stack and continue with the point traversing further. If the stack is empty and operator is not closing parenthesis, then I push the operator on the stack. If the stack is not empty, then I will compare the current operator with the one which is already on the top of the stack. Remember, last time we discussed this that we need to worry about the precedence of the two consecutive operators that might come. If the operator that we now encounter as a higher precedence, push the operator on the stack. If it is a lower precedence, then we have to append the top element of the stack to the post fix ring, pop the top operator from the stack. Now, we will continue doing these operations one and two till a opening parenthesis is encountered or the stack becomes empty. Finally, we will push the current operator, but not the closing parenthesis on the stack. At the end, I will append the remaining operators from the stack to the string post fix by using top followed by pop till stack becomes empty. Simple procedure, let us look at its implementation using a C plus plus program. First, we identify the member functions which we need to write in order to achieve this conversion. Check parenthesis. This is a function which we will write. The parameter is an infix expression which is a character array. It will be called from the validate function and it will return true or false. Purpose is to check if the open and close parenthesis match properly. Is valid is another function. The parameter is the current character. Now, the current character is valid only if it is either an operator or an operand. Note that for this example, we have presumed that all our operands are single later operands A, B, C up to Z or capital A, B, C up to Z. This is of course, a simplification, but for the current purposes, this is sufficient to check whether the character is valid or not using this. Validate is another function which validates the entire infix expression which is a character array. It will return true or false to the main function and the purpose is to check whether the entire expression is valid or not. Of course, while executing the logic of this validate function, it will invoke other functions like is valid. Is operand is again look at the current character and it will check whether the current character is an operand that is small a to z or capital A to z. Priority number is actually an artificial number which we are creating to be allocated to every operator that we encounter. So, the parameter to this particular function is a character which is an operator and it will determine the priority of the operator. The artificial numbers that we have decided are 0 for the highest priority operator namely, raise to followed by 1 which will be used for multiplication or division followed by 2 which will be used for addition or subtraction and so on. Check priority, this will compare the current operator and the top operator on the stack and it will return true or false depending upon which has the higher priority. Infix to post fix, this is a function which takes in fix expression and post fix expression as strings which are character arrays and it will return the post fix string to the main function. Input of course, will be the infix expression. So, this is the main activity converting a valid infix expression into a post fix expression. Let us look at the program itself. What we have done is looking at the program, we will also look simultaneously at the logic which the program is trying to implement. First traversing the entire infix expression. So, for counting the number of opening parenthesis and closing parenthesis and finding out whether they are matching or not. So, this is the check parenthesis thing. It declare some temporary variables and while there is a valid character in the input string namely infix ith element is not backslash 0 which is the end of the string. I will examine if this is equal to opening parenthesis. If it is I add 1 to the count of open, if it is closing parenthesis I add 1 to the closing parenthesis count. If close is greater than open then I will have to say this is not valid. I will return false otherwise I will increment I by 1 and go back continuing with this situation. If open is exactly equal to close which is what is required for a valid expression this function will return true otherwise it will return false. Simple program, simple function this is the one which will check initially whether the parenthesis match in numbers. Next is is valid function. The purpose of this function is to check whether the character that comes in the infix expression is either of a to z or small a to z which corresponds to our operand or opening parenthesis closing parenthesis star plus minus slash or raise to which are the valid operator. In short is valid will check every character in the expression and will confirm that it is either a valid operator or it is a valid operand. Once again please note that we are presuming that our operands are variables with single character name a to z a to z. We continue looking at the next function this is the validate function it is input is entire infix string. We need to traverse the string and if the character is not an operand or operator we should return false else point to the next character. So, while this string is not ended we will call the is valid character for that particular ith element of the infix string which means the ish character of the string. If it is not valid as shown here it will return false else it will go to the next character. This way it will check everything it will finally check parenthesis given the infix string if that is false then again it will return false. If the parenthesis match and if all other characters are valid this function will return true. Again a very simple exercise a simple function which will validate our expression. Next let us look at assignment of priority numbers these are numbers which I mentioned to be indicating the priority of various operators. This will return value 0 1 2 or 3 depending upon the operator. So, see the simple implementation we look at every care which is an operator which is the character in the infix string which is identified as operator. If operator is this it will return 0 if operator is star or operator is slash it will return 1 if it is plus or minus it will return 2 and if the operator is a opening parenthesis then it will return 3 this is the end of the function. We also need to check the priority when we are actually implementing the infix to post infix conversion. This will require looking at the current operator and looking at the operator which is already on top of the stack. So, it has two parameters all that it does is calculate the priority number for the current operator calculate the priority number for the top operator. If the current operator has a lower priority value then top operator which means it is of a higher precedence then it will return true otherwise it is in false. So, please remember check priority will return true if the priority of the current operator is higher than the priority of the operator on the stack. One more function that we need is to check whether a particular character is an operand the current character is given. We already know that we have assumed that the operands will be variables single character variable names capital A to Z or small A to Z all that this small function does is checks whether the characters are within this range if it is then they are truly these characters otherwise not. So, notice a simple written statement is used and if these things are true the function will return true if any other character comes here this will return false a simple implementation. Now we come to the that part of the program function which actually does in fix to post fix. This has two parameters in fix as the input array and the post fix array which is actually assembled in an array called answer in this function. So, I declare these current care i j etcetera. I declare a care post fix internal array which I initialize to blank. I have a stack care which is the operator stack and now I start my procedure which is I look at the in fix string character by character and as long as the character is not backslash 0 that means as long as there is a character in the in fix string. I will examine that character and act accordingly. So, this is what we mean by traversing the in fix expression. We put the ith character into the current care and this current care will have now have to be handled. There are two or three segments which will handle this current care. We have identified them arbitrarily as 2.1, 2.2 the two segments the code which will follow one after another. After this i is incremented and next character is examined. When the entire traversal is complete there is another segment which will possibly help us assemble the final output. Finally, since we have assembled the final output in post fix string we will copy that string to the answer which is the parameter to be written that is the end of the function. Let us look at what is the code for 2.1, 2.2 and the code for 3 in the subsequent slides. These incidentally are point numbers which are given in our algorithm. So, you can easily identify what is the logic that we need to implement here, what is the logic that we need to implement here and what is the logic that we need to implement here. So, the code for 2.1 that is character is an operand we need to append it to the string post fix very simple is operand then post fix j is equal to current character j plus plus. Notice that post fix string will get assembled one by one as and when something is to be appended and that is why the variable j will increment independent of other variables only when something is appended to the post fix string. Let us look at 2.2 if that is not so then we have to do the following namely that it is an operator. If it is an operator then if the operator is opening parenthesis push it to the operator stack and continue at point 2. So, the code is else it is an operator that is it. Now, if it is an operator we have to do variety of things this 2.2.1 is if the current care is opening parenthesis then operator stack dot push current care means it will push it on to the stack and increment i continue means go to the next iteration corresponding to i. So, this is again a simple functionality let us look at what happens if stack is empty and operator is not closing parenthesis this is the point 2.2.2 in our algorithm in such a case we need to push the operator on the stack. So, else it is an operator and we have done this now we are coming to code for 2.2.2 which is very simple if operator stack empty and current care is not equal to closing parenthesis operator stack push current care simple implementation. Let us look at 2.2.3 next for your benefit we have repeated the logic of 2.2.3 inverse which was already included in the earlier explanation. Let us very quickly go through it if stack is not empty then I have to compare the current operator with the one which is present on top of the stack. If it has a higher precedence push the operator on the stack if it has less precedence then append the top element of the stack to the post fixed string pop the top operator out continue these till you encounter opening parenthesis and finally push the current operator, but not the closing parenthesis on the stack. So, this is the logic implementation this is the code for 2.2.3 is slightly larger code, but you will be able to easily understand it. First I check priority if when I check priority I will do operator stack dot push. If the priority is not appropriate then I will have to look at removing the top item from the stack and pushing it on to the output string provided there is a top up. So, this is the entire logic of 2.2.3 b which says while the operator stack is not empty if the operator stack top is opening parenthesis then I pop it off and break. If what now I have to do is I have the post fixed string I will have to append to that post fixed string the top of the operator stack. So, I take the operator stack dot top and assign it to the jth element of post fixed string and increment j plus plus. So, this is the equivalent of appending finally, I pop off the top of the stack which has already been accounted for by including it in the post fixed string. This is the end of while at the end of this while if the current character is not equal to closing parenthesis then I have to push the current character which is an operator on to the stack. This is the end of this l string. It may appear slightly complex, but when you read the entire program in one go which is available at the end of the slides here you will be able to understand it very clearly. Next let us look at appending the remaining operators from the stack on the string which is very simple while operator stack is not empty all that I do is I look at the top of the operator stack and that element I assign to the jth element of post fixed which is the next in the post fixed string and pop that off of course, I increment j plus plus. I do it for all the remaining operators on the stack this completes my program and now I can write my main program which is rather simple. I have an in fixed string a post fixed string I collect from the user an in fixed string. I validate the in fixed string if the validate shun returns false then I will give a message sorry boss you have not given me a correct expression. So, I cannot do anything and return minus one otherwise I will output the in fixed expression indicating there is a correct expression simply invoke in fixed to post fixed which is the main function that I have written supplying it as a parameter in fixed and collecting back the parameter of post fixed and I simply print the post fixed string. Notice once again that the program is actually easy primarily because although we have to write lot of functions primarily the STL has provided all the operations related to stack which we did not have to rewrite. Once again you are reminded to look at these references which were indicated even in the last time. To conclude we have considered the in fixed to post fixed conversion process in a previous lecture and in this session we looked at a C plus plus program which implements a conversion. In the subsequent sessions we will look at the evaluation of a post fixed expression. Thank you.