1 00:00:00,000 --> 00:00:08,266 [No Audio] 2 00:00:08,267 --> 00:00:10,333 In this tutorial, we will be implementing the 3 00:00:10,334 --> 00:00:12,933 final part of Expression Evaluation, and that 4 00:00:12,934 --> 00:00:14,800 is to compute the final result from the 5 00:00:14,801 --> 00:00:17,500 postfix expression. This tutorial will be 6 00:00:17,501 --> 00:00:19,833 relatively short and easy, because we have 7 00:00:19,834 --> 00:00:22,300 already done the hard part of implementation. 8 00:00:22,866 --> 00:00:24,733 This is the same script that we used in the 9 00:00:24,734 --> 00:00:27,000 last tutorial, I will add the rules for 10 00:00:27,001 --> 00:00:29,700 evaluating postfix. Let us quickly revise 11 00:00:29,701 --> 00:00:32,066 these rules. The first rule is that, if we 12 00:00:32,067 --> 00:00:34,300 have an operand then we will be pushing it 13 00:00:34,301 --> 00:00:36,566 onto the stack. And the second rule is that 14 00:00:36,567 --> 00:00:38,800 if we have an operation, then we will pop two 15 00:00:38,801 --> 00:00:41,033 elements, perform operations and then push it 16 00:00:41,034 --> 00:00:43,933 back onto the stack. Let's define a function 17 00:00:43,934 --> 00:00:46,000 for implementing the evaluation of expression 18 00:00:46,001 --> 00:00:49,100 form postfix form, I will name this function 19 00:00:49,101 --> 00:00:50,933 is postfix_evaluation. 20 00:00:50,934 --> 00:00:53,566 [No Audio] 21 00:00:53,567 --> 00:00:56,166 The input to the function will be the vector of strings 22 00:00:56,167 --> 00:00:58,000 containing the postfix expression. 23 00:00:58,001 --> 00:01:01,766 [No Audio] 24 00:01:01,767 --> 00:01:03,566 The output will be a floating number 25 00:01:03,567 --> 00:01:05,433 representing the evaluation result. 26 00:01:05,434 --> 00:01:10,833 [No Audio] 27 00:01:10,834 --> 00:01:12,000 I will write some dummy 28 00:01:12,001 --> 00:01:14,966 f32 value for now as a return 29 00:01:14,967 --> 00:01:17,733 value, so that I do not see the red warning messages. 30 00:01:18,500 --> 00:01:20,733 Once I am done with the function, I will remove it. 31 00:01:22,166 --> 00:01:24,133 Let us first declare some variables. 32 00:01:24,433 --> 00:01:25,933 I will need to store the size of 33 00:01:25,934 --> 00:01:28,366 the postfix expression, I will define a 34 00:01:28,367 --> 00:01:30,533 variable size_expr, and we'll 35 00:01:30,566 --> 00:01:33,066 assign to it the length of the postfix expression. 36 00:01:33,067 --> 00:01:37,833 [No Audio] 37 00:01:37,834 --> 00:01:39,766 Next, I will declare a stack which 38 00:01:39,767 --> 00:01:42,266 will be used during the computation. I will 39 00:01:42,267 --> 00:01:44,466 name it result_stack, and we'll 40 00:01:44,467 --> 00:01:47,200 initialize it using the new_stack function. 41 00:01:47,201 --> 00:01:51,733 [No Audio] 42 00:01:51,734 --> 00:01:53,300 I am next going to iterate through 43 00:01:53,301 --> 00:01:55,433 all the elements of the postfix expression, 44 00:01:55,434 --> 00:01:57,066 for which I will write a for loop. 45 00:01:57,067 --> 00:02:01,833 [No Audio] 46 00:02:01,834 --> 00:02:04,300 Inside the loop, I am going to use a match statement to 47 00:02:04,301 --> 00:02:06,833 implement the two rules. The match will be on 48 00:02:06,834 --> 00:02:10,199 the looping variable i 49 00:02:10,200 --> 00:02:11,666 where the variable line represents the 50 00:02:11,667 --> 00:02:14,666 tokens or symbols in the postfix expression. 51 00:02:15,100 --> 00:02:17,133 To determine which rule to execute, we need 52 00:02:17,134 --> 00:02:19,000 to identify if the current symbol is an 53 00:02:19,001 --> 00:02:22,166 operation or symbol. First, I will check if 54 00:02:22,167 --> 00:02:24,733 the symbol is an operation or not. So I will 55 00:02:24,734 --> 00:02:26,966 include its relevant arm definition. 56 00:02:26,967 --> 00:02:36,300 [No Audio] 57 00:02:36,301 --> 00:02:38,100 Please note that, I did not mention the 58 00:02:38,101 --> 00:02:40,333 opening and closing parenthesis, because they 59 00:02:40,334 --> 00:02:42,400 are not part of the postfix expression, 60 00:02:43,366 --> 00:02:46,500 I will add the code later on to this arm. If we 61 00:02:46,501 --> 00:02:48,500 do not have an operation, then it means that 62 00:02:48,501 --> 00:02:50,633 it will be an operand. So I will write a 63 00:02:50,634 --> 00:02:53,233 default arm which will corresponds to an operand. 64 00:02:53,234 --> 00:02:59,100 [No Audio] 65 00:02:59,101 --> 00:03:01,666 Now, let us add code to the first arm, this arm 66 00:03:01,667 --> 00:03:03,900 corresponds to the second rule, so let us 67 00:03:03,901 --> 00:03:06,200 revise the rule. The second rule is that, if 68 00:03:06,201 --> 00:03:08,366 the current symbol is an operation, then we 69 00:03:08,367 --> 00:03:10,633 will pop two elements, perform the operation, 70 00:03:10,634 --> 00:03:12,900 and then push the result back onto the stack. 71 00:03:13,733 --> 00:03:15,733 This means that the current symbol is an 72 00:03:15,734 --> 00:03:19,566 operation, so let me store it in a variable called oper. 73 00:03:19,567 --> 00:03:21,633 [No Audio] 74 00:03:21,634 --> 00:03:23,600 Next I will pop up the top most 75 00:03:23,900 --> 00:03:25,933 elements and they will be our operands. 76 00:03:25,934 --> 00:03:30,066 [No Audio] 77 00:03:30,067 --> 00:03:32,366 The element which is being popped first, will be 78 00:03:32,367 --> 00:03:34,400 the second operand for the operation and the 79 00:03:34,401 --> 00:03:36,633 element which is being popped second, will be 80 00:03:36,634 --> 00:03:38,800 the first operand for the operation. 81 00:03:38,801 --> 00:03:40,266 So let me add the code. 82 00:03:40,267 --> 00:03:47,666 [No Audio] 83 00:03:47,667 --> 00:03:49,966 Next to perform the operation, I will define a 84 00:03:49,967 --> 00:03:51,100 function called Operation. 85 00:03:51,101 --> 00:03:53,400 [No Audio] 86 00:03:53,401 --> 00:03:54,266 There will be three 87 00:03:54,267 --> 00:03:56,400 inputs to this function, consisting of two 88 00:03:56,401 --> 00:03:58,466 operands and one operation are of type 89 00:03:58,467 --> 00:04:00,600 string. Let me add the inputs. 90 00:04:00,601 --> 00:04:07,433 [No Audio] 91 00:04:07,434 --> 00:04:09,300 The output will be a floating value. 92 00:04:09,301 --> 00:04:13,333 [No Audio] 93 00:04:13,334 --> 00:04:15,333 Inside the function, I will change the type of the 94 00:04:15,334 --> 00:04:18,166 two operands from string to date of f32. 95 00:04:18,399 --> 00:04:20,733 This can be done, with the help of the parse function. 96 00:04:20,734 --> 00:04:27,000 [No Audio] 97 00:04:27,001 --> 00:04:29,100 We need to unwrap the result of the parse, 98 00:04:29,101 --> 00:04:32,400 because it returns an option. Next, I will 99 00:04:32,401 --> 00:04:34,533 perform the operation to check the type of 100 00:04:34,534 --> 00:04:36,700 the operation I will use a match statement on 101 00:04:36,701 --> 00:04:38,800 the operation variable, and I will use it as a 102 00:04:38,801 --> 00:04:41,666 string reference. First, I will add the arm 103 00:04:41,667 --> 00:04:42,533 for the addition. 104 00:04:42,534 --> 00:04:46,333 [No Audio] 105 00:04:46,334 --> 00:04:48,600 Next, we will have an arm for the subtraction. 106 00:04:48,601 --> 00:04:50,800 [No Audio] 107 00:04:50,801 --> 00:04:53,300 Next, for the multiplication, division, and exponent. 108 00:04:53,301 --> 00:04:57,366 [No Audio] 109 00:04:57,367 --> 00:04:59,600 You may note that, the compiler is not happy and this 110 00:04:59,601 --> 00:05:01,000 is, because the match needs to be 111 00:05:01,001 --> 00:05:03,300 exhaustive, which means it needs to cover all 112 00:05:03,301 --> 00:05:05,733 possible types of strings. To overcome the 113 00:05:05,734 --> 00:05:08,366 error, we will add a default case also. 114 00:05:08,367 --> 00:05:14,166 [No Audio] 115 00:05:14,167 --> 00:05:15,933 Of course, the default case is not very 116 00:05:15,934 --> 00:05:18,166 meaningful, and what you can do is that, you 117 00:05:18,167 --> 00:05:20,500 can mention the last term as a default case. 118 00:05:20,501 --> 00:05:22,566 But in that case, the code may be a bit 119 00:05:22,567 --> 00:05:25,433 confusing, so let us leave it as it is for now. 120 00:05:25,600 --> 00:05:27,400 Since we know that the default case will 121 00:05:27,401 --> 00:05:31,133 never be true based on our inputs. Finally, I 122 00:05:31,134 --> 00:05:33,066 will assign result from the match to a 123 00:05:33,067 --> 00:05:35,268 variable of, let's say result. 124 00:05:35,269 --> 00:05:39,366 [No Audio] 125 00:05:39,367 --> 00:05:42,266 At the end of the match, I will also write a semicolon, 126 00:05:42,267 --> 00:05:44,766 because the whole match is now an assignment statement. 127 00:05:45,100 --> 00:05:46,366 Finally, I will return the 128 00:05:46,367 --> 00:05:49,158 variable result by writing result without a semicolon. 129 00:05:49,159 --> 00:05:52,000 [No Audio] 130 00:05:52,001 --> 00:05:53,333 Let us now use this function 131 00:05:53,334 --> 00:05:55,466 inside the postfix evaluation function. 132 00:05:55,600 --> 00:05:57,766 I will call this function with the two operands, 133 00:05:57,800 --> 00:05:59,800 and the corresponding operation, and we'll 134 00:05:59,801 --> 00:06:02,984 store its result value in the variable of result. 135 00:06:02,985 --> 00:06:07,900 [No Audio] 136 00:06:07,901 --> 00:06:09,733 Next, we need to push the result into 137 00:06:09,734 --> 00:06:12,300 the stack, the result needs to be to be 138 00:06:12,301 --> 00:06:13,939 converted into string type. 139 00:06:13,940 --> 00:06:16,988 [No Audio] 140 00:06:16,989 --> 00:06:17,900 Now let us look 141 00:06:17,901 --> 00:06:19,800 at the default arm which corresponds to an 142 00:06:19,801 --> 00:06:22,233 operand. In this case, we will just push it 143 00:06:22,234 --> 00:06:24,759 into the stack. Let me add the suitable code here. 144 00:06:24,760 --> 00:06:31,566 [No Audio] 145 00:06:31,567 --> 00:06:33,500 Now when all the symbols are being scanned, 146 00:06:33,501 --> 00:06:35,500 and the loop, and anything that is being 147 00:06:35,501 --> 00:06:38,000 left in the stack is the final result. So we 148 00:06:38,001 --> 00:06:40,200 will pop the final result from the stack, and 149 00:06:40,201 --> 00:06:42,700 we'll return that. Please note that, since this 150 00:06:42,701 --> 00:06:45,033 function returns an f32 value, so I will 151 00:06:45,034 --> 00:06:47,542 convert the final value to that of f32. 152 00:06:47,543 --> 00:06:50,566 [No Audio] 153 00:06:50,567 --> 00:06:54,100 Finally, I will delete the dummy return value that is 0.0. 154 00:06:54,666 --> 00:06:56,600 Okay, we are done. Now let us 155 00:06:56,601 --> 00:06:59,133 call this function from the main, I will call 156 00:06:59,134 --> 00:07:00,566 it inside the print statement. 157 00:07:00,589 --> 00:07:08,841 [No Audio] 158 00:07:08,842 --> 00:07:10,374 Let us cargo run this. 159 00:07:10,375 --> 00:07:13,866 [No Audio] 160 00:07:13,867 --> 00:07:16,933 The final result seems correct. The main objective in this 161 00:07:16,934 --> 00:07:18,866 example was to give you some practice, and 162 00:07:18,867 --> 00:07:20,766 make you a bit more familiar with the coding 163 00:07:20,767 --> 00:07:23,300 style and syntax of Rust. I hope you would 164 00:07:23,301 --> 00:07:25,433 have enjoyed doing coding with me. 165 00:07:25,600 --> 00:07:27,933 Of course, there are many optimizations that we 166 00:07:27,934 --> 00:07:29,766 can perform in this code to make it more 167 00:07:29,767 --> 00:07:32,833 efficient, but we will not cover that right now. 168 00:07:33,000 --> 00:07:35,000 We will rather come to the topic of code 169 00:07:35,001 --> 00:07:37,500 optimization, in possibly later on videos. 170 00:07:37,866 --> 00:07:40,200 For now our focus is to learn the basics and 171 00:07:40,201 --> 00:07:42,333 familiarize ourselves with programming in Rust. 172 00:07:42,566 --> 00:07:44,633 Once we have sufficient skills, then we 173 00:07:44,634 --> 00:07:46,500 will be in a better position to write and 174 00:07:46,501 --> 00:07:48,600 optimize the code. With this, we end this 175 00:07:48,601 --> 00:07:50,966 tutorial, do come back again for more, and until 176 00:07:50,967 --> 00:07:52,733 then happy Rust programming. 177 00:07:52,734 --> 00:07:56,833 [No Audio]