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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<?php
/* ---------------------------------------------------------------------------
* クラス・オブジェクト:-- ◆クラスの理解のための電卓機能◆ --の例
* 2015.10.14 作成 yoshi of CXMedia Inc.
* ---------------------------------------------------------------------------
*/
/* -------------------------[ クラスの定義 ]---------------------------- */
class MyCalc {
/* ----------------
* プロパティ
* ---------------- */
/* ---------------------------------------
* == フォームで利用パラメータと値 ==
* total : 合計計算値
* btn : 四則演算子
* memo : 計算結果の履歴
* --------------------------------------- */
public $inpAry;
private $inpdata; //電卓入力データ
private $mode; //入力した四則演算子の値
private $calc_tbl; //四則演算子ボタン配列
/* ----------------
* コンストラクタ
* ---------------- */
function __construct($intAry){
// 初期設定データの四則演算子ボタン配列のセット
$this->calc_tbl = $intAry;
}
/* ----------------
* メソッド
* ---------------- */
// POSTデータの入力処理
public function post_get($ary) {
$this->inpdata = isset($ary['data'])? $ary['data'] : '';
$this->inpAry['memo'] = isset($ary['memo'])? $ary['memo'] : '';
$this->inpAry['total'] = isset($ary['total'])? $ary['total'] : '';
$this->inpAry['btn'] = isset($ary['btn'])? $ary['btn'] : '';
// submitボタンのセット
$this->mode = '';
foreach ($this->calc_tbl as $idx => $value){
if(isset($ary[$idx])){
$this->mode = $value;
break;
}
}
}
// 四則データ計算とデータセット
public function calc_set() {
// データのみ入力
if( !$this->inpAry['btn'] ){
$this->inpAry['total'] = $this->inpdata;
}
// plusボタン
elseif($this->inpAry['btn'] == '+'){
$this->inpAry['total'] += $this->inpdata;
}
// minusボタン
elseif($this->inpAry['btn'] == 'ー'){
$this->inpAry['total'] -= $this->inpdata;
}
// multiボタン
elseif($this->inpAry['btn'] == '×'){
$this->inpAry['total'] *= $this->inpdata;
}
// divideボタン
elseif($this->inpAry['btn'] == '÷'){
$this->inpAry['total'] /= $this->inpdata;
}
// equalボタン
elseif($this->inpAry['btn'] == '='){
$this->inpAry['total'] = $this->inpdata;
}
// セッションデータに書き込みと計算履歴のセット
if(!$this->mode || $this->mode == 'AC'){
// ACボタンのとき保存データのクリア
$this->inpAry = array();
} else {
//計算結果履歴へ入力値の追加
$this->inpAry['memo'] .= ($this->mode == '=')?
$this->inpdata.$this->mode.$this->inpAry['total'].'<br>' :
$this->inpdata.$this->mode;
//submitボタンの計算実行演算子で更新
$this->inpAry['btn'] = $this->mode;
}
}
}
/* --------------------------[ クラスの定義END ]-------------------------- */
?>