PowerShell - 簡易GUI電卓
簡単なGUI電卓を作成。
少々悩んだ点として、関数内でグローバル変数にアクセスする場合は、$varを$global:varとすること。でも、本当は引数経由でアクセスしたほうが良いのですが……。
あとボタンオブジェクトは、配列で処理するとソースがもっとすっきりするはず。
ソース
Add-Type -AssemblyName System.Windows.Forms Add-Type -AssemblyName System.Drawing $number = "" # 被演算数値文字列 $op = "" # 演算記号文字列 function newButton { param( [Windows.Forms.Form]$form , [string]$loc, [string]$size, [string]$caption ) $btn = New-Object Windows.Forms.Button $btn.Location = $loc $btn.Size = $size $btn.Text = $caption $form.Controls.Add($btn) return $btn } function pushCLR{ $txt.text = "0" $global:number = "" $global:op = "" } function pushNumber{ param([string]$c) if($txt.text -eq "0"){ $txt.text = $c } else { $txt.text += $c } } function pushOp{ param([string]$c) if("+-*/".Contains($c)){ $global:op = $c $global:number = $txt.Text $txt.Text = "0" } } function pushEq{ if($global:op -eq ""){ $global:number = $txt.Text $txt.Text = "0" } else { switch($global:op){ "+" {$txt.Text = [double]$global:number + [double]$txt.Text; break} "-" {$txt.Text = [double]$global:number - [double]$txt.Text; break} "*" {$txt.Text = [double]$global:number * [double]$txt.Text; break} "/" {$txt.Text = [double]$global:number / [double]$txt.Text; break} } } } function pushDot{ if(!$txt.text.Contains(".")){ $txt.text += "." } } $font = New-Object Drawing.Font("MS ゴシック",12) # Form $form = New-Object Windows.Forms.Form $form.Text = "Calc" $form.Size = "185,250" $form.Font = $font # Text $txt = New-Object Windows.Forms.TextBox $txt.Location = "5,10" $txt.Size = "160,20" $txt.readonly = $true $txt.TextAlign = [Windows.Forms.HorizontalAlignment]::Right $form.Controls.Add($txt) # Button $btnC = newButton $form "125,40" "40,30" "C" $btnC.Add_Click( {pushCLR} ) $btnC.Focus() $btn7 = newButton $form "5,70" "40,30" "7" $btn7.Add_Click( {pushNumber "7"} ) $btn8 = newButton $form "45,70" "40,30" "8" $btn8.Add_Click( {pushNumber "8"} ) $btn9 = newButton $form "85,70" "40,30" "9" $btn9.Add_Click( {pushNumber "9"} ) $btnDiv = newButton $form "125,70" "40,30" "/" $btnDiv.Add_Click( {pushOp "/"} ) $btn4 = newButton $form "5,100" "40,30" "4" $btn4.Add_Click( {pushNumber "4"} ) $btn5 = newButton $form "45,100" "40,30" "5" $btn5.Add_Click( {pushNumber "5"} ) $btn6 = newButton $form "85,100" "40,30" "6" $btn6.Add_Click( {pushNumber "6"} ) $btnMul = newButton $form "125,100" "40,30" "*" $btnMul.Add_Click( {pushOp "*"} ) $btn1 = newButton $form "5,130" "40,30" "1" $btn1.Add_Click( {pushNumber "1"} ) $btn2 = newButton $form "45,130" "40,30" "2" $btn2.Add_Click( {pushNumber "2"} ) $btn3 = newButton $form "85,130" "40,30" "3" $btn3.Add_Click( {pushNumber "3"} ) $btnSub = newButton $form "125,130" "40,30" "-" $btnSub.Add_Click( {pushOp "-"} ) $btn0 = newButton $form "5,160" "40,30" "0" $btn0.Add_Click( {pushNumber "0"} ) $btnDot = newButton $form "45,160" "40,30" "." $btnDot.Add_Click( {pushDot} ) $btnEq = newButton $form "85,160" "40,30" "=" $btnEq.Add_Click( {pushEq} ) $btnAdd = newButton $form "125,160" "40,30" "+" $btnAdd.Add_Click( {pushOp "+"} ) # 表示 $form.ShowDialog()