Convert and Calculate by RPN for typescript.

Overview

Coaca.ts:
Convert and Calculate by RPN for typescript

注意点

まずそもそも、typescriptはサーバサイドで使用されるべき言語です。一応typescript4.5以降の機能を用いていますが、typescriptの本来の使い方を超越してます。ご注意ください。

詳しくはこちらの記事をご覧ください。

また、Coaca.jsとの互換性は一切ありません。好きな書き方を選んでください()

Coaca.js (English)

Todo

  • データ型のチェック
  • sin(...)やln(...)、!(...)のような数式を実装
  • オニオンアーキテクチャの実装 🧅

用語

  • RPN => 逆ポーランド記法の略称
  • RPN配列 => RPNへ変換した時に出力される配列。AB+の場合、['A', 'B', '+']となる

使い方

  1. distフォルダ内のjavascriptファイルをダウンロードします
  2. その中のcoaca.jsファイルを実行したいjavascriptファイル内にてimportします
  3. インスタンス化をします

機能一覧

インポート

import {Calc, ConvertRpn, Variable} from './coaca/coaca.js'

クラス名とそのクラスの役割です。必要に応じて変更してください。

クラス名 内容
Calc 計算
ConvertRpn RPNへの変換
Variable 計算式の変数に関する内容

RPN配列の生成

const convertRpn = new ConvertRpn()
convertRpn.setFormula('11+16')
const rpnArr = convertRpn.convertRpn()
console.log(rpnArr.join(' ')) // 11 16 +

RPN配列を生成します。

計算

const calc = new Calc()
calc.setRpnArr(rpnArr) // rpnArr = ['11', '16', '+']
const res = calc.calc()
console.log(res) // 31

RPN配列から計算を行います。

変数機能について

追加

const variable = new Variable()
variable.addVariable('x', 20)

変数の追加を行います。上の例では、変数x(初期値20)を設定してます。

値の変更

variable.changeVariable('x', 10)

変数の値を変更します。

変数の削除

variable.removeVariable('x')

変数を削除します。

変数の代入

let rpnArr = ['11', 'x', '+']

/* xに16を代入 */

rpnArr = variable.convertVariable(rpnArr) // ['11', '16', '+']

変数を代入します。

エラー処理について

coaca.tsはまともなエラー処理をしてません。特に数学的なエラー(無限大etc.)は全く対処してませんのでご注意ください。

演算子について

coaca.tsは以下の演算子によって計算を行います。

演算子 内容
+ 足し算
- 引き算
/ 割り算
* 掛け算
% 割り算
^ 冪乗
A_B Bを底数としたAの対数

また、特殊演算子として以下の演算子を提供しています。

演算子 内容
! 累乗
sin/cos/tan 三角関数
asin/acos/atan 逆三角関数
ln 底数10の対数
rad ラジアン角度への変換
abs 絶対値

この演算子は以下のように記述します。

const pattern1 = sin[rad[90]]

const pattern2 = abs[5-10]

変数について

条件

追加時

  1. 特殊演算子並びに演算子、デフォルト変数を変数名に使用しない
  2. 20字以内で命名

変更時

  1. デフォルト変数の値を変更しない

削除時

  1. デフォルト変数を削除しない

デフォルト変数について

coaca.tsでは、デフォルト変数を提供しています。なお、この変数を利用するにはVariableクラスを用いて代入する必要があります

デフォルト変数名 内容
pie 円周率
e ネイピア数
K 1,000
M 1,000,000
G 1,000,000,000
m 0.001
μ 0.000001
n 0.000000001

使用例

ヘロンの公式

import {Calc, ConvertRpn, Variable} from '../dist/coaca.js'

const variableList = {
  a: 15,
  b: 13,
  c: 14
}

const setVariableList = (variable, variableList) => {
  for (let key in variableList) {
    variable.addVariable(key, variableList[key])
  }
}

/* sを求める処理 */
const s = () => {
  const convert = new ConvertRpn()
  convert.setFormula('(a+b+c)/2')
  let rpnArr = convert.convertRpn()

  const variable = new Variable()
  setVariableList(variable, variableList)
  rpnArr = variable.convertVariable(rpnArr)

  const calc = new Calc()
  return calc.calc(rpnArr)
}

const res = s => {
  const convert = new ConvertRpn()
  convert.setFormula('(s*(s-a)*(s-b)*(s-c))^(1/2)')
  let rpnArr = convert.convertRpn()

  const variable = new Variable()
  setVariableList(variable, variableList)
  variable.addVariable('s', s)
  rpnArr = variable.convertVariable(rpnArr)

  const calc = new Calc()
  return calc.calc(rpnArr)
}

console.log(res(s())) // 84
You might also like...

Module to convert units for distances, weights and temperatures!

Unit Converter (Distance, Temperature, Weights) Every unit you can see in this file can be transformed into any of the other units. The Calculator wor

Jan 27, 2022

When pasting screenshots into obsidian notes, convert the images to jpeg and compress them

When pasting screenshots into obsidian notes, convert the images to jpeg and compress them

obsidian-paste-png-to-jpeg This plugin is inspired by obsidian-paste-image-rename, obsidian-paste-image-rename can be used when inserting images renam

Nov 15, 2022

Convert your SVG file directly to Flutter paths and prevent all the messing with bezier curves.

Convert your SVG file directly to Flutter paths and prevent all the messing with bezier curves.

svg-to-flutter-path-converter Convert your SVG file directly to Flutter paths and prevent all the messing with bezier curves. Flutter Clutter The tool

Jan 2, 2023

Yunisdev-table2csv - Lightweight library to convert HTML table to CSV file

Installation Add following HTML before body end: script src="https://yunisdev.github.io/table2csv/table2csv.min.js"/script !-- or -- script src

Oct 19, 2020

Convert JSON to human readable HTML

json.human.js: Json Formatting for Human Beings A small library to convert a JSON object into a human readable HTML representation that is easy to sty

Dec 3, 2022

convert markdown to html in under 5kb

convert markdown to html in under 5kb

convert markdown to HTML in under 5kb take a look at the to PHP translated version: https://github.com/SimonWaldherr/micromarkdown.php about License:

Dec 8, 2022

convert SWC to Babel AST

SWC-to-babel Convert SWC JavaScript AST to Babel AST. To use SWC parser with babel tools like: @babel/traverse @babel/types etc... The thing is @babel

Oct 28, 2022

MZ-Switchwire - Convert Anycubic Mega Zero v1 to Switchwire

MZ-Switchwire - Convert Anycubic Mega Zero v1 to Switchwire

Anycubic Mega Zero v1 Switchwire Mod 390mm X-Axis with MGN9H - Click for 3D prev

Dec 25, 2022

A command-line tool to convert Project Zomboid map data into Deep Zoom format

A command-line tool to convert Project Zomboid map data into Deep Zoom format

pzmap2dzi pzmap2dzi is a command-line tool running on Windows to convert Project Zomboid map data into Deep Zoom format. Features Supports both python

Dec 31, 2022
Releases(v1.0.0)
Owner
赤紫
自分で作ったwebアプリのコードを中心に、適当にアップしていく予定
赤紫
Hemsida för personer i Sverige som kan och vill erbjuda boende till människor på flykt

Getting Started with Create React App This project was bootstrapped with Create React App. Available Scripts In the project directory, you can run: np

null 4 May 3, 2022
Kurs-repo för kursen Webbserver och Databaser

Webbserver och databaser This repository is meant for CME students to access exercises and codealongs that happen throughout the course. I hope you wi

null 14 Jan 3, 2023
A concise collection of classes for PHP, Python, JavaScript and Ruby to calculate great circle distance, bearing, and destination from geographic coordinates

GreatCircle A set of three functions, useful in geographical calculations of different sorts. Available for PHP, Python, Javascript and Ruby. Live dem

null 72 Sep 30, 2022
Calculate the price range for property advertised on Domain and Real Estate.

Property Seeker Calculate the price range for property advertised on Domain and Real Estate. Install Chrome Firefox Edge Privacy All searches are perf

null 42 Dec 27, 2022
A Node.js library to calculate Uniswap V3 ratios (prices) from token pairs.

UniV3Prices A Node.js library to calculate Uniswap V3 ratios (prices) and liquidity (reserves). Features This library will allow you to: Calculate pri

Thanos Polychronakis 121 Dec 29, 2022
A tool to calculate discount available on SGB (Sovereign Gold Bonds) compared to current market price on NSE

A tool to calculate discount available on SGB (Sovereign Gold Bonds) compared to current market price on NSE

Amit Wani 9 Nov 20, 2022
Calculate the Mexican RFC as specified by the SAT (Servicio de Administración Tributaria) for Personas Físicas

About The Project This project calculates a "Persona Física"'s RFC based on SAT's specifications including homonymy and verification digit. Built With

null 4 Nov 6, 2022
Convert some JavaScript/TypeScript code string into a .d.ts TypeScript Declaration code string

convert-to-dts Converts the source code for any .js or .ts file into the equivalent .d.ts code TypeScript would generate. Usage import { convertToDecl

Lily Scott 11 Mar 3, 2022
Whatsapp bot with Typescript Convert to js Lmao 🤣

Rain-BotV2 Join Group ONLY RUN RAILWAY railway.app Deploy to Heroku ✍️ Editing the file Edit the required value in config.js Heroku Buildpack BuildPac

Rizky 15 Nov 1, 2022
Mercury: easily convert Python notebook to web app and share with others

Mercury Share your Python notebooks with others Easily convert your Python notebooks into interactive web apps by adding parameters in YAML. Simply ad

MLJAR 2.2k Dec 30, 2022