about summary refs log tree commit diff
path: root/usth/MATH2.3/3/dc.cc
blob: b69742a5076ce044f23569684ff5437399e1b093 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <cmath>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>

using namespace std;

int
main ()
{
  double num, x, y;
  string op;
  vector<double> v;

  while (cin.peek () != '\n')
    {
      getline (cin, op, ';');
      stringstream s {op};
      s >> num;
      if (s)
        {
          v.push_back (num);
          continue;
        }

      y = v.back ();
      v.pop_back ();
      x = v.back ();
      v.pop_back ();
      if (op == "^")
        v.push_back (pow (x, y));
      else if (op == "+")
        v.push_back (x + y);
      else if (op == "-")
        v.push_back (x - y);
      else if (op == "*")
        v.push_back (x * y);
      else if (op == "/")
        v.push_back (x / y);
    }
  cout << v.back () << endl;
}