 Welcome to this session in the course on data structures and algorithms. We saw last time the logic of evaluating a post fix expression using a stack. In this session, we will look at the actual computer program which implements that logic. Let us very briefly recapitulate the algorithm that we had developed last time. In a post fix expression as an input string, we want to output the numerical value of the evaluated expression. So, the steps are very simple really. First is get values of all operands present in the post fix expression. Once we get that, we traverse the post fix expression character by character. We examine each character. If the character is an operand, we push the value of that operand on the stack. If the character is an operator, we need to evaluate that operator by doing the following. Assign the value from the top of the stack to a variable operand 2, pop the element from the stack. Assign the value from the top of the stack to a variable operand 1, pop the element from the stack. So, we got two top operands which are stored on the stack into operand 1 and operand 2. Evaluate the result of operand 1 and operand 2 using the current operator character that we have encountered and push the result back on the stack. Finally, when this entire post fix expression string is traversed, at the end we shall have the final result on the stack which will return the top value of the stack as our answer. This simple algorithm, let us quickly see the C plus plus program which implements this algorithm. In our program, we will write several functions. The first function is get values. Now, all that it does is, it is to store the values of the operands in the array, the values array will contain individual values in different elements. The parameter to this function is post fix expression and the array is operator is another function which just looks at the current character and it checks whether the character is an operator or not. Calculate values is actually the function which will take operand 1 and operand 2 and the operator will evaluate the operand 1 and operand 2 with the operator that has been given and it will calculate the resultant value. Evaluate post fix is the entire post fix operation and this is the one which is called from the main program will do all the calculations for the entire expression and return the final value. So, it is a simple set of functions that we will see. We will let us look at now individual functions and the implementation of the entire logic. Let us look at the is operator function. What is the objective? Examine the current character. If it is an operator return true, s return false. So, very simple our operators for the current example is plus, minus, star, slash or raise to. If the current character is any one of these it will return true, s return false. Simple logic. Now, let us look at the character being an operand. What are we supposed to do if the character is an operand? We have to accept the value of the operand from the user and store it in an array value. Here we do an interesting thing. We use associative arrays for storing values. Those of you who have done CS101x course from IIT Bombay would recall that in one of the examples on digital image processing for calculating histograms, we had used an associative array. Now, let me explain the purpose of using an associative array here. Ordinarily for few operands, we need only a small array to store the values of these operands. Now, in our examples we are using operands represented by a single character small a to z or capital A to z. Note that their ASCII course are contained within the 0 to 255 which is the total span of all ASCII course. Now imagine that if we use a value array which is of size 256. Now, this is a large array but suppose we said that the value of an operand A will be stored in that element which corresponds to the ASCII code of A. Value of an operand capital P will be stored in that element of this array which corresponds to the ASCII code of operand character P. This way while many of the locations in the array will remain unutilized but we will be able to use the ASCII code of the operand directly as an index of this array. So, for example, if we have a postfix expression AC plus here A and C are operands assume that the user has entered number 5 for value of operand A and number 7 for operand C. What we will do is we store the value 5 in the 97th element of the value array. Why because 97 is the ASCII code of A. So, you see we do not require to search for the actual operand course and then find out in which location the value will be stored etcetera. We directly look at the operand directly find out its ASCII value and directly use it as an index. Similarly, if somebody inputs a value 7 for operand C we know the operand C we directly use the ASCII code of the operand C which is 99 and push the value 7 in 99th element of the array. This makes our program work far more efficiently. Let us look at the implementation of this in the get values function. The get values functions if you recall if the character is an operand we are supposed to accept the value of the operand from the user and store it in the array value. This is the same explanation that we have seen in the last slide, but this is the implementation of our logic. We define a care as current care and examine the postfix string. If the current care is an operator we are not supposed to do anything here, but if the current care is not an operator we will come out here. This is an operand now we need to collect the value of the operand. We ask the user to enter the value of the current care operator and whatever value the user enters is directly inserted into the current careth element of the value which corresponds to the ASCII code of the care. So, see how easily every value is captured and directly inserted at its logical place in the large array value. When we have completed all of this we would have collected all the values. Note that the postfix expression string itself will contain operands as well as operators. We are ignoring all operators we are only looking at operands and collecting values of those operands. Next we look at actually calculating the value of the operation. So, we want to perform the operation on operand 1 and operand 2 and the result is to be returned to the function evaluate postfix. This is the function calculate value. It has two operands and the operator as the parameters is extremely simple. If operator is plus or minus or star or slash or raise to we perform the necessary operation and this is the end of the function. The result is returned simply like this here. Next we look at the evaluate postfix function. This will get the postfix expression array itself as a parameter. What it is doing? We first declare the stack a float stack because that is what will contain all the values. There is a current care here. We start examining the postfix array starting with i equal to 0. We of course will require result operand 1, operand 2 and remember the array of values 256 size is the array but it will contain some elements with appropriate values corresponding to the operands that we have encountered in the postfix array. Please remember before coming here we would have already collected the values in this array. By get values giving the postfix string and the value array this will get all the values from the user. Now we will examine each and every character of the postfix string. Assign the postfix ith character to current care and based on the current care we have to evaluate. Now there is a code for evaluation which will have to be written here. Once that code is executed I will go to the next character. So every character in the postfix expression I will handle we will see the code for evaluation in a moment on the next line. When this entire by loop ends we would have completed all our evaluation and we know that the final result will be the top of the stack. We return the stack dot top which is the final answer is the end of the function. Let us look at the code for evaluation. In this program the code for evaluation is expanded here. There are two possibilities. Remember we are looking at the current character. The current character is either an operand or an operator. If the current character is an operator then we have to evaluate that operation. If not it is an operand we simply push the value on to the stack. What is the value? Value current care. Remember current care is an operand its ASCII code itself serves as an index to the array value which will get pushed on the stack. Let us look at how the actual operation is evaluated if the character is an operator. What we are supposed to do? You recall from that logic that we have examined earlier. We first take the top of the stack and assign it to operand 2. Then we pop that element out. Then we take the top of the stack and assign it to operand 1. We pop that element out. The result is simply calculated by using the calculate values function which we had written earlier which takes operand 1, operand 2 and the current care which is the operation. Finally it will result into the calculated value and that needs to be pushed on to top of the stack. This completes the entire operation associated with one operator. So, anytime we come across an operator this is what will be executed. Anytime we come across an operand this is what will be executed. You will notice that at the end I would have completed the entire evaluation when this while loop ends and that is the end of the function. We have in this session looked at a C++ program which implements evaluation of a post fix expression using a stack. This concludes our examples of use of stack in various applications. Thank you.