C++ で書けば、オブジェクト指向でのプログラミングになるのかな?
Stack という名前のクラスを定義すれば、スタック計算になるのかな?
実際には、スタックも逆ポーランド記法も使っていない
インチキプログラムを書いてみました。
C++
1#include<iostream>// cin, cout2#include<sstream>// istringstream3#include<string>// getline4#include<cctype>// isdigit5#include<cmath>// pow6using namespace std;78class Stack {9 istringstream iss;10unsignedchar c;11public:12Stack(const string& s):iss(s){13double v =expr("+-*/^^");14 cout.precision(15);15 c ? cout <<" error\n": cout <<" "<< v <<"\n";16}17private:18intget(){return iss >> c ? c :(c =0);}19doubleexpr(constchar*b){20double v;21if(*b)// binary operators. power is right-associative.22for(v =expr(b +2); c == b[0]|| c == b[1];)23 c =='+'? v +=expr(b +2):24 c =='-'? v -=expr(b +2):25 c =='*'? v *=expr(b +2):26 c =='/'? v /=expr(b +2):(v =pow(v,expr(b)));27else// number, parentheses or unary operators28get()=='.'||isdigit(c)? iss.unget(), iss >> v ?get():(c =3):29 c =='('? v =expr("+-*/^^"), c ==')'?get():(c =2):30 c =='+'? v =expr(b):31 c =='-'? v =-expr(b):(v = c =1);32return v;33}34};3536intmain()37{38 string s;39while(cout <<">> ",getline(cin, s)&& s !=".") Stack t(s);40}