Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 

二分木を使った数式の逆ポーランド記法化と計算のC++での実装。

前提条件・依存関係

C++コンパイラが必要です。

Makefileを使用してビルド・実行する場合は、g++が必要です。

ビルドおよび実行

Makefileを使用する場合

コマンドmake runを実行し、式を入力することにより、入力された式に対して逆ポーランド記法化・計算を行うことができます。

実行例:

$ make run
input expression: 2 + 5 * 3 - 4
expression: 2+5*3-4
reverse polish notation: 2 5 3 * + 4 -
infix notation: ((2 + (5 * 3)) - 4)
polish notation: - + 2 * 5 3 4
calculated result: 13

その他、makeコマンドで以下の操作を行うことができます。

make       # ソースファイルをコンパイルする
make run   # ソースファイルをコンパイルして実行する
make clean # 成果物ファイルを削除する

デフォルトではg++を使用しますが、CXX=clang++を指定することでclang++を使用するように変更することもできます。

MSBuildを使用する場合

Visual C++がインストールされている環境では、コマンドmsbuildを実行することでビルドすることができます。 実行可能ファイルはDebug\polish.exeに生成されます。

> msbuild
    ︙
    (中略)
    ︙
ビルドに成功しました。
    0 個の警告
    0 エラー

経過時間 00:00:01.31

> Debug\polish.exe
input expression: x=1+2
expression: x=1+2
reverse polish notation: x 1 2 + =
infix notation: (x = (1 + 2))
polish notation: = x + 1 2
calculated expression: (x = 3)

その他、msbuildコマンドの/tオプションで以下の操作を行うことができます。

msbuild /t:Build # ソースファイルをコンパイルする(/t:Buildは省略できます)
msbuild /t:Clean # 成果物ファイルを削除する

g++でのコンパイル・実行方法

g++ -std=c++2a polish.cpp -o polish # ソースファイルをコンパイルする
./polish                            # コンパイルした実行可能ファイルを実行する

Clangでのコンパイル・実行方法

clang++ -std=c++2a polish.cpp -o polish # ソースファイルをコンパイルする
./polish                                # コンパイルした実行可能ファイルを実行する

Clangのインストール方法

Ubuntuの場合:

sudo apt install clang