my電卓を作ろう
逆ポーランド記法電卓
Reverse Polish Notation Calculator
画面をデザインする
ツールボックスの中で、
- コモンコントロール「TextBox」
- TextBox –> (name): typeText; text: 0
- TextBox –> (name): lastText; text: 0
- コモンコントロール「Button」
- Button1 –> (name): plusButton; text: +
- Button2 –> (name): subButton; text: –
- Button3 –> (name): multiButton; text: x
- Button4 –> (name): divButton; text: /
- Button5 –> (name): clrButton; text: CL
- Button6 –> (name): enterButton; text: ER
Formにドラッグ&ドロップする
プログラミング
KeyPressイベントを作成
Form1を選択し、Form1のイベントの表示に切り替えて、「KeyPress」イベントに、FormKeyPressを入力
FormKeyPressメソッドを作成
六つボタンを選択した状態で、コントロール類の、「KeyPress」イベントに、FormKeyPressを選択。
private void FormKeyPress(object sender, KeyPressEventArgs e) { string key = e.KeyChar.ToString(); int n = 0; try { n = int.Parse(key); } catch { return; } string num = typeText.Text + n; try { typeText.Text = "" + int.Parse(num); } catch { return; } }
plusButtonClickメソッドを作成
private void plusButtonClick(object sender, EventArgs e) { try { int lastnum = int.Parse(lastText.Text); int typenum = int.Parse(typeText.Text); lastText.Text = "" + (lastnum + typenum); typeText.Text = "0"; } catch { return; } }
subButtonClickメソッドを作成
private void subButtonClick(object sender, EventArgs e) { try { int lastnum = int.Parse(lastText.Text); int typenum = int.Parse(typeText.Text); lastText.Text = "" + (lastnum - typenum); typeText.Text = "0"; } catch { return; } }
multiButtonClickメソッドを作成
private void multiButtonClick(object sender, EventArgs e) { try { int lastnum = int.Parse(lastText.Text); int typenum = int.Parse(typeText.Text); lastText.Text = "" + (lastnum * typenum); typeText.Text = "0"; } catch { return; } }
divButtonClickメソッドを作成
private void divButtonClick(object sender, EventArgs e) { try { int lastnum = int.Parse(lastText.Text); int typenum = int.Parse(typeText.Text); lastText.Text = "" + (lastnum / typenum); typeText.Text = "0"; } catch { return; } }
clrButtonClickメソッドを作成
private void clrButtonClick(object sender, EventArgs e) { if (typeText.Text == "0") { lastText.Text = "0"; } typeText.Text = "0"; }
enterButtonClickメソッドを作成
private void enterButtonClick(object sender, EventArgs e) { string s = typeText.Text; lastText.Text = s; typeText.Text = "0"; }
作成したコード
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace Calculator { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { } private void FormKeyPress(object sender, KeyPressEventArgs e) { string key = e.KeyChar.ToString(); int n = 0; try { n = int.Parse(key); } catch { return; } string num = typeText.Text + n; try { typeText.Text = "" + int.Parse(num); } catch { return; } } private void plusButtonClick(object sender, EventArgs e) { try { int lastnum = int.Parse(lastText.Text); int typenum = int.Parse(typeText.Text); lastText.Text = "" + (lastnum + typenum); typeText.Text = "0"; } catch { return; } } private void subButtonClick(object sender, EventArgs e) { try { int lastnum = int.Parse(lastText.Text); int typenum = int.Parse(typeText.Text); lastText.Text = "" + (lastnum - typenum); typeText.Text = "0"; } catch { return; } } private void multiButtonClick(object sender, EventArgs e) { try { int lastnum = int.Parse(lastText.Text); int typenum = int.Parse(typeText.Text); lastText.Text = "" + (lastnum * typenum); typeText.Text = "0"; } catch { return; } } private void divButtonClick(object sender, EventArgs e) { try { int lastnum = int.Parse(lastText.Text); int typenum = int.Parse(typeText.Text); lastText.Text = "" + (lastnum / typenum); typeText.Text = "0"; } catch { return; } } private void clrButtonClick(object sender, EventArgs e) { if (typeText.Text == "0") { lastText.Text = "0"; } typeText.Text = "0"; } private void enterButtonClick(object sender, EventArgs e) { string s = typeText.Text; lastText.Text = s; typeText.Text = "0"; } } }
My電卓使い方
計算例: 123+456=
まずキーボードから数字123入力
「ER」キーで数字を設定
次の数字456を入力
演算キーで計算