Question: Write a c++ program that accepts a string that is a space-delimited integer arithmetic expression in postfix notation. Use a stack to compute the value.

Write a c++ program that accepts a string that is a space-delimited integer arithmetic expression in postfix notation. Use a stack to compute the value. Example: The input string 5 4 + 3 10 * + is equivalent to the infix expression (5 + 4) + (3 * 10) The answer is 39. The algorithm is: Take the leftmost token from the string If it is numeric (which may include a unary operator), push that token onto the stack If it is a binary operation, pop the vector twice, apply the operation, and push the result When the input string is empty, the stack will contain one entry: the final answer Heres how the given string gets processed: String Stack 5 4 + 3 10 * + 4 + 3 10 * + 5 + 3 10 * + a 4 5 3 10 * + 9 10 * + 3 9 * + 10 3 9 + 30 9 39 The final answer, 39, is the only element in the stack after all tokens in the string have been consumed. This problem has an extra twist. The string contains characters, so 10 isnt a ten, but is the char 1 followed by the char 0. You will need to convert string representations of integers (signed and unsigned) into their integer values. Youll also need to handle unary - and +. We also have to worry about the three non-commuting operators / % . We will evaluate the postfix string 4 5 - as 4-5 and, likewise, will evaluate 4 5 / as 4/5 and 4 5 % as 4%5. The program should continue to accept and process input strings until the user enters a zero as input. Theres no need to check for validity; all input will be well-formed postfix expressions.

I do not need the program to ask for a users input, instead i need it modified to read a text file line at a time

Modify your postfix calculator to read entries from a file, one line at a time. The expressions in this file are guaranteed to be valid, so no error checking is necessary. Process each expression and keep a running total of the sum. Enter the sum to complete this quiz. Use int data type

The contents of the Postfix1.txt file is:

130 172 -82 -183 / - 117 - + 165 114 17 * / 154 % + 124 96 % + 25 71 * -131 192 - 57 9 - -137 -162 * % % % 174 25 99 41 187 - 177 + / * % 105 33 % 1 / -37 27 -58 + 4 % + + 149 / -59 32 136 140 - / * 192 % 63 93 + + 167 % 21 -16 36 * - / 99 196 / 167 -152 * 168 192 -129 + 176 57 38 80 65 % + % / - * % 57 172 / -168 - -133 173 + 9 107 - * 106 / 101 * -136 59 % * 159 -155 - 96 165 / 41 33 - 172 -148 - 148 38 -129 * / - 18 -137 -32 - % 67 126 103 % / 65 % -53 119 82 64 % 15 + / - % -136 144 - 176 51 75 68 / 46 123 57 % * * * + 122 109 74 102 116 182 - * * + % 15 110 164 43 -117 167 66 * 124 -155 + - - 64 155 % % + - + / 45 -57 * -81 * -41 - -14 -46 54 128 * * % 83 122 167 113 101 / -33 -26 + - * - - 147 80 169 185 + 151 20 135 8 198 * / + * / - - 2 -157 - 108 132 77 + 22 174 12 - 32 + % - * / 93 199 % 93 + 17 165 133 - 139 + * 93 - * 187 160 -177 5 - 117 + % 71 / 138 -153 % - * 32 1 64 72 + 108 * * 160 199 + * % -85 111 * + 149 149 * 15 171 123 % % -59 * - -184 178 % 22 170 - 60 * 191 31 92 + * 57 - 58 198 * -57 + + 179 % 7 -102 -174 65 66 * / + - 37 193 / -17 28 103 109 * - 23 79 92 - -91 + 100 * + % + - 126 -3 + 111 18 % 35 / 163 - 119 78 + 73 124 191 + 44 57 168 - 14 / + -180 140 - * * + % 48 -38 -108 + + 131 -134 27 / 175 * 175 -90 - + % + 120 104 % 13 - 193 - 141 122 + 156 123 - * 47 84 + -54 67 163 29 * % % + 159 13 * 2 100 56 47 / + * - 71 -177 % 96 144 116 133 -69 176 42 * + % + % + 158 22 + 99 / -49 / -84 * 181 95 15 137 39 / -65 + / * + / 40 178 92 85 * 77 - + + 133 -167 - 79 196 / 13 125 139 -59 -168 163 17 / + / - + + + + 147 2 + 10 / 45 * 191 % -188 % -93 163 - 4 * 176 80 147 - 89 + / 39 129 + 93 - * -175 164 * 83 / % 171 45 * -132 % 31 % 93 + -90 - -91 %

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!