Skip to content

Instantly share code, notes, and snippets.

@sdkfz181tiger
Last active March 23, 2023 13:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sdkfz181tiger/bf0925cea151b274d6020f9bffafac2c to your computer and use it in GitHub Desktop.
Save sdkfz181tiger/bf0925cea151b274d6020f9bffafac2c to your computer and use it in GitHub Desktop.
簡易電卓2_使いやすくまとめる
"use strict";
//==========
// JavaScript
// Window
window.onload = (e)=>{
// CalcManager
const cMng = new CalcManager();
// UI
const btns = document.getElementsByTagName("button");
for(let btn of btns){
// Buttons
btn.addEventListener("click", (e)=>{
const disp = document.getElementById("disp");
disp.textContent = cMng.put(e.target.textContent);
});
}
}
"use strict";
//==========
// Utility
const ST_VAL1 = 1;// 値1状態
const ST_VAL2 = 2;// 値2状態
class CalcManager{
constructor(){
console.log("CalcManager");
this._state = null;
this._val1 = null;
this._val2 = null;
this._op = null;
this.clear();
}
clear(){
this._state = ST_VAL1;
this._val1 = "0";
this._val2 = "";
this._op = "";
}
put(value){
// 数値1状態
if(this._state == ST_VAL1){
if(this.isNumeric(value)){// 数値の場合
this._val1 += value;
if(1 < this._val1.length && this._val1[0] == 0){
this._val1 = this._val1.substr(1);
}
}
if(this.isFlg(value)){// +-反転の場合
this._val1 = this.changeFlg(this._val1);// 符号反転
}
if(this.isOperator(value)){// 演算子の場合
this._op = value;
this._state = ST_VAL2;// 数値2状態へ
}
if(this.isClear(value)){// Cボタン
this.clear();
}
if(this._state == ST_VAL1) return this._val1;
if(this._state == ST_VAL2) return (this._val2.length<=0)?this._val1:this._val2;
return null;
}
// 数値2状態
if(this._state == ST_VAL2){
if(this.isNumeric(value)){// 数値の場合
this._val2 += value;
if(1 < this._val2.length && this._val2[0] == 0){
this._val2 = this._val2.substr(1);
}
}
if(this.isFlg(value)){// +-反転の場合
if(this._val2.length <= 0){
this._val1 = this.changeFlg(this._val1);
}else{
this._val2 = this.changeFlg(this._val2);
}
}
if(this.isOperator(value)){// 演算子の場合
if(this._val2.length <= 0){
this._op = value;
}else{
this._val1 = this.calc(this._val1, this._val2, this._op).toString();// 計算処理
this._val2 = "";
this._op = value;
}
}
if(this.isEqual(value)){// =の場合
this._val1 = this.calc(this._val1, this._val2, this._op).toString();// 計算処理
this._val2 = "";
this._op = "";
this._state = ST_VAL1;// 数値1状態へ
}
if(this.isClear(value)){// Cボタン
this.clear();
}
if(this._state == ST_VAL1) return this._val1;
if(this._state == ST_VAL2) return (this._val2.length<=0)?this._val1:this._val2;
return null;
}
return null;
}
calc(v1, v2, op){
const n1 = parseInt(v1);
const n2 = parseInt(v2);
if(op == "+") return n1 + n2;
if(op == "-") return n1 - n2;
if(op == "*") return n1 * n2;
if(op == "/" && n2 != 0) return n1 / n2;
return 0;
}
isNumeric(n){
return !isNaN(n);
}
isOperator(op){
if(op == "+") return true;
if(op == "-") return true;
if(op == "*") return true;
if(op == "/") return true;
return false;
}
isEqual(e){
return e == "=";
}
isClear(c){
return c == "C";
}
isFlg(op){
if(op == "+/-") return true;
return false;
}
changeFlg(n){
let num = "";
if(this.isNumeric(n[0])){
num = "-" + n;
}else{
num = n.substr(1);
}
return num;
}
}
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<link rel="shortcut icon" href="./images/favicon.ico">
<!-- Stylesheet -->
<link href="./css/custom.css" rel="stylesheet" type="text/css"/>
<!-- JavaScript -->
<script src="./js/custom_utility.js" defer></script>
<script src="./js/cutom_main.js" defer></script>
</head>
<body>
<table>
<tr>
<td><button>C</button></td>
<td colspan="3" id="disp"></td>
</tr>
<tr>
<td><button>7</button></td>
<td><button>8</button></td>
<td><button>9</button></td>
<td><button>+</button></td>
</tr>
<tr>
<td><button>4</button></td>
<td><button>5</button></td>
<td><button>6</button></td>
<td><button>-</button></td>
</tr>
<tr>
<td><button>1</button></td>
<td><button>2</button></td>
<td><button>3</button></td>
<td><button>*</button></td>
</tr>
<tr>
<td><button>0</button></td>
<td><button>+/-</button></td>
<td><button>=</button></td>
<td><button>/</button></td>
</tr>
</table>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment