Not logged in. Log in with GitHub. About Websheets.

List of all available Websheets


Viewing cpp/vectors/rpn by redekopp@usc.edu. You have unsaved changes. Log in above to create, edit, preview and publish Websheets.
PropertyValue
Description
html markup
shown to student
 
<i>Postfix notation</i> transforms mathematical expressions by putting 
each operator <i>after</i> its arguments. So <tt>1 + 2</tt> becomes 
<tt>1 2 +</tt> and <tt>5 * 2</tt> becomes <tt>5 2 *</tt>. 
<p>An advantage
over normal (infix) expressions is that parentheses are not needed.
For example <tt>4 3 * 2 1 * +</tt> is the prefix version of <tt>4*3+2*1</tt>
while <tt>4 3 2 + * 1 *</tt> is the prefix version of <tt>4*(3+2)*1</tt>.
<p>Write an evaluator for prefix notation (just the <tt>*</tt> and <tt>+</tt>
operators) by using a vector as a stack.
Each time you read a number, place it at the end of the stack. Each time
you read an operator, combine the last two elements of the stack.
Public permissions remove
Engine
Template / Reference solution
 
#include <vector>
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
int main() {
   vector<int> memory;
   string input;
   // read each input
   while (cin >> input) {
      // if you read an operator, combine the last two elements of the stack
      if (input == "+" || input == "*") {
\[
         int v1 = memory.back();
         memory.pop_back();
         int v2 = memory.back();
         memory.pop_back();
]\
         if (input == "+"){
            \[memory.push_back(v1+v2);]\
         }
         else {
            \[memory.push_back(v1*v2);]\
         }
      }
      // if you read a number, place it at the end of the stack
      else {
         memory.push_back(stoi(input));
      }
   }
   cout << memory.back();
}
C++ test suite
json list of stdin/args tests
e.g. [{"stdin":"hi", "args":["4", "5"]},
{"stdin":"noargs"}]

to just run once with no input use [{}]
 
[
   {"stdin": "1 2 +"},
   {"stdin": "5 2 *"},
   {"stdin": "4 3 * 2 1 * +"},
   {"stdin": "4 3 2 + * 1 *"},
   {"stdin": "5 4 3 2 1 * * * *"},
   {"stdin": "5 4 3 2 1 + + + +"},
   {"stdin": "1 2 + 3 4 * 5 6 + 7 8 + 9 * + + *"}
]
Solution visibility remove


Optional properties:

Note: problems are open-source by default (see 'Public permissions'). Assumed license for open problems is Creative Commons 4.0 Attribution-ShareAlike unless specified in 'Remarks'.